dbgate-tools
Version:
Auxiliary tools for other DbGate packages.
118 lines (117 loc) • 5.41 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DatabaseInfoAlterProcessor = void 0;
const lodash_1 = __importDefault(require("lodash"));
class DatabaseInfoAlterProcessor {
constructor(db) {
this.db = db;
}
createTable(table) {
this.db.tables.push(table);
}
dropTable(table) {
lodash_1.default.remove(this.db.tables, x => x.pureName == table.pureName && x.schemaName == table.schemaName);
}
createSqlObject(obj) {
this.db[obj.objectTypeField].push(obj);
}
dropSqlObject(obj) {
lodash_1.default.remove(this.db[obj.objectTypeField], x => x.pureName == obj.pureName && x.schemaName == obj.schemaName);
}
createColumn(column) {
const table = this.db.tables.find(x => x.pureName == column.pureName && x.schemaName == column.schemaName);
if (!table)
throw new Error(`createColumn error, cannot find table: ${column.schemaName}.${column.pureName}`);
table.columns.push(column);
}
changeColumn(oldColumn, newColumn) {
const table = this.db.tables.find(x => x.pureName == oldColumn.pureName && x.schemaName == oldColumn.schemaName);
if (!table)
throw new Error(`changeColumn error, cannot find table: ${oldColumn.schemaName}.${oldColumn.pureName}`);
table.columns = table.columns.map(x => (x.columnName == oldColumn.columnName ? newColumn : x));
}
dropColumn(column) {
const table = this.db.tables.find(x => x.pureName == column.pureName && x.schemaName == column.schemaName);
if (!table)
throw new Error(`dropColumn error, cannot find table: ${column.schemaName}.${column.pureName}`);
lodash_1.default.remove(table.columns, x => x.columnName == column.columnName);
}
createConstraint(constraint) {
const table = this.db.tables.find(x => x.pureName == constraint.pureName && x.schemaName == constraint.schemaName);
switch (constraint.constraintType) {
case 'primaryKey':
table.primaryKey = constraint;
break;
case 'sortingKey':
table.sortingKey = constraint;
break;
case 'foreignKey':
table.foreignKeys.push(constraint);
break;
case 'index':
table.indexes.push(constraint);
break;
case 'unique':
table.uniques.push(constraint);
break;
case 'check':
table.checks.push(constraint);
break;
}
}
changeConstraint(oldConstraint, newConstraint) {
const table = this.db.tables.find(x => x.pureName == oldConstraint.pureName && x.schemaName == oldConstraint.schemaName);
}
dropConstraint(constraint) {
const table = this.db.tables.find(x => x.pureName == constraint.pureName && x.schemaName == constraint.schemaName);
switch (constraint.constraintType) {
case 'primaryKey':
table.primaryKey = null;
break;
case 'sortingKey':
table.sortingKey = null;
break;
case 'foreignKey':
table.foreignKeys = table.foreignKeys.filter(x => x.constraintName != constraint.constraintName);
break;
case 'index':
table.indexes = table.indexes.filter(x => x.constraintName != constraint.constraintName);
break;
case 'unique':
table.uniques = table.uniques.filter(x => x.constraintName != constraint.constraintName);
break;
case 'check':
table.checks = table.checks.filter(x => x.constraintName != constraint.constraintName);
break;
}
}
renameTable(table, newName) {
this.db.tables.find(x => x.pureName == table.pureName && x.schemaName == table.schemaName).pureName = newName;
}
renameSqlObject(obj, newName) {
this.db[obj.objectTypeField].find(x => x.pureName == obj.pureName && x.schemaName == obj.schemaName).pureName =
newName;
}
renameColumn(column, newName) {
const table = this.db.tables.find(x => x.pureName == column.pureName && x.schemaName == column.schemaName);
table.columns.find(x => x.columnName == column.columnName).columnName = newName;
}
renameConstraint(constraint, newName) { }
recreateTable(oldTable, newTable) {
throw new Error('recreateTable not implemented for DatabaseInfoAlterProcessor');
}
fillPreloadedRows(table, oldRows, newRows, key, insertOnly, autoIncrementColumn) {
const tableInfo = this.db.tables.find(x => x.pureName == table.pureName && x.schemaName == table.schemaName);
tableInfo.preloadedRows = newRows;
tableInfo.preloadedRowsKey = key;
tableInfo.preloadedRowsInsertOnly = insertOnly;
}
setTableOption(table, optionName, optionValue) {
const tableInfo = this.db.tables.find(x => x.pureName == table.pureName && x.schemaName == table.schemaName);
tableInfo[optionName] = optionValue;
}
}
exports.DatabaseInfoAlterProcessor = DatabaseInfoAlterProcessor;