UNPKG

dbgate-tools

Version:

Auxiliary tools for other DbGate packages.

201 lines (200 loc) 8.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.databaseInfoFromYamlModel = exports.tableInfoFromYaml = exports.tableInfoToYaml = void 0; const cloneDeep_1 = __importDefault(require("lodash/cloneDeep")); const compact_1 = __importDefault(require("lodash/compact")); const DatabaseAnalyser_1 = require("./DatabaseAnalyser"); // function foreignKeyInfoToYaml() {} function columnInfoToYaml(column, table) { const res = { name: column.columnName, type: column.dataType, default: column.defaultValue, }; if (column.autoIncrement) res.autoIncrement = true; if (column.notNull) res.notNull = true; const fk = table.foreignKeys && table.foreignKeys.find(fk => fk.columns.length == 1 && fk.columns[0].columnName == column.columnName); if (fk && // fk.refSchemaName == table.schemaName && (!fk.deleteAction || fk.deleteAction == 'NO ACTION') && (!fk.updateAction || fk.updateAction == 'NO ACTION')) { res.references = fk.refTableName; fk['_dumped'] = true; } // if (table.primaryKey && table.primaryKey.columns.length == 1) { // table.primaryKey['_dumped'] = true; // res.primaryKey = true; // } return res; } function columnInfoFromYaml(column, table) { const res = { pureName: table.name, columnName: column.name, dataType: column.length ? `${column.type}(${column.length < 0 ? 'max' : column.length})` : column.type, autoIncrement: column.autoIncrement, notNull: column.notNull || (table.primaryKey && table.primaryKey.includes(column.name)), defaultValue: column.default, defaultConstraint: column.default != null ? `DF_${table.name}_${column.name}` : undefined, }; return res; } function tableInfoToYaml(table) { var _a, _b; const tableCopy = (0, cloneDeep_1.default)(table); const res = { name: tableCopy.pureName, // schema: tableCopy.schemaName, columns: tableCopy.columns.map(c => columnInfoToYaml(c, tableCopy)), }; if (tableCopy.primaryKey && !tableCopy.primaryKey['_dumped']) { res.primaryKey = tableCopy.primaryKey.columns.map(x => x.columnName); } if (tableCopy.sortingKey && !tableCopy.sortingKey['_dumped']) { res.sortingKey = tableCopy.sortingKey.columns.map(x => x.columnName); } // const foreignKeys = (tableCopy.foreignKeys || []).filter(x => !x['_dumped']).map(foreignKeyInfoToYaml); if (((_a = tableCopy.indexes) === null || _a === void 0 ? void 0 : _a.length) > 0) { res.indexes = tableCopy.indexes.map(index => { const idx = { name: index.constraintName, unique: index.isUnique, filter: index.filterDefinition, columns: index.columns.filter(x => !x.isIncludedColumn).map(x => x.columnName), }; if (index.columns.some(x => x.isIncludedColumn)) { idx.included = index.columns.filter(x => x.isIncludedColumn).map(x => x.columnName); } return idx; }); } if (((_b = tableCopy.uniques) === null || _b === void 0 ? void 0 : _b.length) > 0) { res.uniques = tableCopy.uniques.map(unique => ({ name: unique.constraintName, columns: unique.columns.map(x => x.columnName), })); } return res; } exports.tableInfoToYaml = tableInfoToYaml; function convertForeignKeyFromYaml(col, table, allTables) { const refTable = allTables.find(x => x.name == col.references); if (!refTable || !refTable.primaryKey) return null; return { constraintType: 'foreignKey', constraintName: `FK_${table.name}_${col.name}`, pureName: table.name, refTableName: col.references, deleteAction: col.refDeleteAction, updateAction: col.refUpdateAction, columns: [ { columnName: col.name, refColumnName: refTable.primaryKey[0], }, ], }; } function tableInfoFromYaml(table, allTables) { var _a, _b; const res = { pureName: table.name, columns: table.columns.map(c => columnInfoFromYaml(c, table)), foreignKeys: (0, compact_1.default)(table.columns.filter(x => x.references).map(col => convertForeignKeyFromYaml(col, table, allTables))), indexes: (_a = table.indexes) === null || _a === void 0 ? void 0 : _a.map(index => ({ constraintName: index.name, pureName: table.name, isUnique: index.unique, constraintType: 'index', filterDefinition: index.filter, columns: [ ...index.columns.map(columnName => ({ columnName })), ...(index.included || []).map(columnName => ({ columnName, isIncludedColumn: true })), ], })), uniques: (_b = table.uniques) === null || _b === void 0 ? void 0 : _b.map(unique => ({ constraintName: unique.name, pureName: table.name, constraintType: 'unique', columns: unique.columns.map(columnName => ({ columnName })), })), }; if (table.primaryKey) { res.primaryKey = { pureName: table.name, constraintType: 'primaryKey', constraintName: `PK_${table.name}`, columns: table.primaryKey.map(columnName => ({ columnName })), }; } if (table.sortingKey) { res.sortingKey = { pureName: table.name, constraintType: 'sortingKey', constraintName: `SK_${table.name}`, columns: table.sortingKey.map(columnName => ({ columnName })), }; } res.preloadedRows = table.data; res.preloadedRowsKey = table.insertKey; res.preloadedRowsInsertOnly = table.insertOnly; return res; } exports.tableInfoFromYaml = tableInfoFromYaml; function databaseInfoFromYamlModel(filesOrDbInfo) { if (!Array.isArray(filesOrDbInfo)) { return filesOrDbInfo; } const model = DatabaseAnalyser_1.DatabaseAnalyser.createEmptyStructure(); const tablesYaml = []; for (const file of filesOrDbInfo) { if (file.name.endsWith('.table.yaml') || file.name.endsWith('.sql')) { if (file.name.endsWith('.table.yaml')) { tablesYaml.push(file.json); } if (file.name.endsWith('.view.sql')) { model.views.push({ pureName: file.name.slice(0, -'.view.sql'.length), createSql: file.text, columns: [], }); } if (file.name.endsWith('.matview.sql')) { model.matviews.push({ pureName: file.name.slice(0, -'.matview.sql'.length), createSql: file.text, columns: [], }); } if (file.name.endsWith('.proc.sql')) { model.procedures.push({ pureName: file.name.slice(0, -'.proc.sql'.length), createSql: file.text, }); } if (file.name.endsWith('.func.sql')) { model.functions.push({ pureName: file.name.slice(0, -'.func.sql'.length), createSql: file.text, }); } if (file.name.endsWith('.trigger.sql')) { model.triggers.push({ objectId: `triggers:${file.name.slice(0, -'.trigger.sql'.length)}`, pureName: file.name.slice(0, -'.trigger.sql'.length), createSql: file.text, }); } } } model.tables = tablesYaml.map(table => tableInfoFromYaml(table, tablesYaml)); return model; } exports.databaseInfoFromYamlModel = databaseInfoFromYamlModel;