UNPKG

@devbro/sql-generator

Version:
138 lines 5.05 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var SchemaGrammar_exports = {}; __export(SchemaGrammar_exports, { SchemaGrammar: () => SchemaGrammar }); module.exports = __toCommonJS(SchemaGrammar_exports); var import_Expression = require("./Expression"); class SchemaGrammar { toSql(blueprint) { let sql = "create table " + blueprint.tableName + " ("; const columns = blueprint.columns.map((v) => { return this.compileColumn(v); }).join(", "); const primaryKeys = this.compilePrimaryKeys(blueprint.primaryKeys); sql += [columns, primaryKeys].join(",") + ")"; return sql; } compileColumn(column) { const rc = [`${column.columnName}`]; if (column.properties.type === "string") { rc.push("varchar(" + column.properties.length + ")"); } else if (column.properties.type === "char") { rc.push("char"); } else if (column.properties.type === "boolean") { rc.push("boolean"); } else if (column.properties.type === "integer") { rc.push("integer"); } else if (column.properties.type === "text") { rc.push("text"); } else if (column.properties.type === "timestamp") { rc.push("timestamp"); } else if (column.properties.type === "serial") { rc.push("serial"); } else if (column.properties.type === "float") { rc.push("float"); } else if (column.properties.type === "double") { rc.push("double precision"); } else if (column.properties.type === "date") { rc.push("date"); } else { throw new Error("Unknown column type: " + column.properties.type); } if (column.properties.nullable) { rc.push("null"); } else { rc.push("not null"); } if (column.properties.unique) { rc.push("unique"); } if (column.properties.default !== null) { rc.push("default " + this.escape(column.properties.default)); } return rc.join(" "); } escape(value) { if (value === null || value === void 0) { return "null"; } if (typeof value === "number") { return value.toString(); } if (typeof value === "boolean") { return value ? "true" : "false"; } if (value instanceof Date) { return "'" + value.toISOString() + "'"; } if (value instanceof import_Expression.Expression) { return value.toCompiledSql().sql; } return "'" + value.replace("'", "\\'") + "'"; } compilePrimaryKeys(primaryKeys) { if (!primaryKeys.length) { return ""; } return "primary key (" + primaryKeys.join(", ") + ")"; } compileTables(schema = void 0) { return { sql: "select c.relname as name, n.nspname as schema, pg_total_relation_size(c.oid) as size, obj_description(c.oid, 'pg_class') as comment from pg_class c, pg_namespace n where c.relkind in ('r', 'p') and n.oid = c.relnamespace and " + this.compileSchemaWhereClause(schema, "n.nspname") + " order by n.nspname, c.relname", bindings: [] }; } compileTableExists(tableName, schema = "") { return { sql: "select exists (select 1 from pg_class c, pg_namespace n where n.nspname = " + (schema ? this.escape(schema) : "current_schema()") + " and c.relname = $1 and c.relkind in ('r', 'p') and n.oid = c.relnamespace)", bindings: [tableName] }; } compileDropTable(tableName) { return { sql: `drop table ${this.doubleQuoteString(tableName)}`, bindings: [] }; } compileSchemaWhereClause(schema, column) { if (Array.isArray(schema) && schema.length > 0) { return `${column} in (${this.quoteString(schema)})`; } else if (schema && typeof schema === "string") { return `${column} = ${this.quoteString(schema)}`; } else { return `${column} <> 'information_schema' and ${column} not like 'pg\\_%'`; } } quoteString(value) { if (Array.isArray(value)) { return value.map((v) => `'${v.replace(/'/g, "\\'")}'`).join(", "); } return `'${value.replace(/'/g, "\\'")}'`; } doubleQuoteString(value) { if (Array.isArray(value)) { return value.map((v) => this.doubleQuoteString(v)).join(", "); } return `"${value.replace(/"/g, '\\"')}"`; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { SchemaGrammar }); //# sourceMappingURL=SchemaGrammar.js.map