dbgate-tools
Version:
Auxiliary tools for other DbGate packages.
255 lines (254 loc) • 9.14 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.editorDeleteConstraint = exports.editorModifyConstraint = exports.editorAddConstraint = exports.editorDeleteColumn = exports.editorModifyColumn = exports.editorAddColumn = exports.processJsonDataUpdateCommands = exports.fillEditorColumnInfo = void 0;
const v1_1 = __importDefault(require("uuid/v1"));
const omit_1 = __importDefault(require("lodash/omit"));
const lodash_1 = __importDefault(require("lodash"));
const stringTools_1 = require("./stringTools");
function fillEditorColumnInfo(column, table) {
var _a, _b;
return {
isPrimaryKey: !!((_b = (_a = table === null || table === void 0 ? void 0 : table.primaryKey) === null || _a === void 0 ? void 0 : _a.columns) === null || _b === void 0 ? void 0 : _b.find(x => x.columnName == column.columnName)),
dataType: lodash_1.default.isEmpty(column) ? 'int' : undefined,
...column,
};
}
exports.fillEditorColumnInfo = fillEditorColumnInfo;
function processJsonDataUpdateCommands(obj, commands = []) {
for (const cmd of commands) {
switch (cmd.type) {
case 'deleteField':
obj = {
...obj,
};
delete obj[cmd.oldField];
break;
case 'renameField':
obj = {
...obj,
};
obj[cmd.newField] = obj[cmd.oldField];
delete obj[cmd.oldField];
break;
case 'setField':
obj = {
...obj,
};
obj[cmd.newField] = cmd.value;
break;
case 'setFieldIfNull':
obj = {
...obj,
};
if (obj[cmd.newField] == null) {
obj[cmd.newField] = cmd.value;
}
break;
}
}
return obj;
}
exports.processJsonDataUpdateCommands = processJsonDataUpdateCommands;
function processPrimaryKey(table, oldColumn, newColumn) {
if (!(oldColumn === null || oldColumn === void 0 ? void 0 : oldColumn.isPrimaryKey) && (newColumn === null || newColumn === void 0 ? void 0 : newColumn.isPrimaryKey)) {
let primaryKey = table === null || table === void 0 ? void 0 : table.primaryKey;
if (!primaryKey) {
primaryKey = {
constraintType: 'primaryKey',
pureName: table.pureName,
schemaName: table.schemaName,
columns: [],
};
}
return {
...table,
primaryKey: {
...primaryKey,
columns: [
...primaryKey.columns,
{
columnName: newColumn.columnName,
},
],
},
};
}
if ((oldColumn === null || oldColumn === void 0 ? void 0 : oldColumn.isPrimaryKey) && !(newColumn === null || newColumn === void 0 ? void 0 : newColumn.isPrimaryKey)) {
let primaryKey = table === null || table === void 0 ? void 0 : table.primaryKey;
if (primaryKey) {
primaryKey = {
...primaryKey,
columns: table.primaryKey.columns.filter(x => x.columnName != oldColumn.columnName),
};
if (primaryKey.columns.length == 0) {
return {
...table,
primaryKey: null,
};
}
return {
...table,
primaryKey,
};
}
}
return table;
}
function defineDataCommand(table, cmd) {
table['__addDataCommands'] = [...(table['__addDataCommands'] || []), cmd()];
}
function editorAddColumn(table, column, addDataCommand) {
let res = {
...table,
columns: [...((table === null || table === void 0 ? void 0 : table.columns) || []), { ...column, pairingId: (0, v1_1.default)() }],
};
res = processPrimaryKey(res, null, column);
if (addDataCommand && column.defaultValue) {
defineDataCommand(res, () => ({
type: 'setField',
newField: column.columnName,
value: (0, stringTools_1.parseSqlDefaultValue)(column.defaultValue),
}));
}
return res;
}
exports.editorAddColumn = editorAddColumn;
function editorModifyColumn(table, column, addDataCommand) {
var _a;
const oldColumn = (_a = table === null || table === void 0 ? void 0 : table.columns) === null || _a === void 0 ? void 0 : _a.find(x => x.pairingId == column.pairingId);
let res = {
...table,
columns: table.columns.map(col => (col.pairingId == column.pairingId ? (0, omit_1.default)(column, ['isPrimaryKey']) : col)),
};
res = processPrimaryKey(res, fillEditorColumnInfo(oldColumn, table), column);
if (addDataCommand && oldColumn.columnName != column.columnName) {
defineDataCommand(res, () => ({
type: 'renameField',
oldField: oldColumn.columnName,
newField: column.columnName,
}));
}
if (addDataCommand && !oldColumn.defaultValue && column.defaultValue) {
defineDataCommand(res, () => ({
type: 'setFieldIfNull',
newField: column.columnName,
value: (0, stringTools_1.parseSqlDefaultValue)(column.defaultValue),
}));
}
return res;
}
exports.editorModifyColumn = editorModifyColumn;
function editorDeleteColumn(table, column, addDataCommand) {
let res = {
...table,
columns: table.columns.filter(col => col.pairingId != column.pairingId),
};
res = processPrimaryKey(res, column, null);
if (addDataCommand) {
defineDataCommand(res, () => ({
type: 'deleteField',
oldField: column.columnName,
}));
}
return res;
}
exports.editorDeleteColumn = editorDeleteColumn;
function editorAddConstraint(table, constraint) {
const res = {
...table,
};
if (constraint.constraintType == 'primaryKey') {
res.primaryKey = {
pairingId: (0, v1_1.default)(),
...constraint,
};
}
if (constraint.constraintType == 'sortingKey') {
res.sortingKey = {
pairingId: (0, v1_1.default)(),
...constraint,
};
}
if (constraint.constraintType == 'foreignKey') {
res.foreignKeys = [
...(res.foreignKeys || []),
{
pairingId: (0, v1_1.default)(),
...constraint,
},
];
}
if (constraint.constraintType == 'index') {
res.indexes = [
...(res.indexes || []),
{
pairingId: (0, v1_1.default)(),
...constraint,
},
];
}
if (constraint.constraintType == 'unique') {
res.uniques = [
...(res.uniques || []),
{
pairingId: (0, v1_1.default)(),
...constraint,
},
];
}
return res;
}
exports.editorAddConstraint = editorAddConstraint;
function editorModifyConstraint(table, constraint) {
const res = {
...table,
};
if (constraint.constraintType == 'primaryKey') {
res.primaryKey = {
...res.primaryKey,
...constraint,
};
}
if (constraint.constraintType == 'sortingKey') {
res.sortingKey = {
...res.sortingKey,
...constraint,
};
}
if (constraint.constraintType == 'foreignKey') {
res.foreignKeys = table.foreignKeys.map(fk => fk.pairingId == constraint.pairingId ? { ...fk, ...constraint } : fk);
}
if (constraint.constraintType == 'index') {
res.indexes = table.indexes.map(fk => (fk.pairingId == constraint.pairingId ? { ...fk, ...constraint } : fk));
}
if (constraint.constraintType == 'unique') {
res.uniques = table.uniques.map(fk => (fk.pairingId == constraint.pairingId ? { ...fk, ...constraint } : fk));
}
return res;
}
exports.editorModifyConstraint = editorModifyConstraint;
function editorDeleteConstraint(table, constraint) {
const res = {
...table,
};
if (constraint.constraintType == 'primaryKey') {
res.primaryKey = null;
}
if (constraint.constraintType == 'sortingKey') {
res.sortingKey = null;
}
if (constraint.constraintType == 'foreignKey') {
res.foreignKeys = table.foreignKeys.filter(x => x.pairingId != constraint.pairingId);
}
if (constraint.constraintType == 'index') {
res.indexes = table.indexes.filter(x => x.pairingId != constraint.pairingId);
}
if (constraint.constraintType == 'unique') {
res.uniques = table.uniques.filter(x => x.pairingId != constraint.pairingId);
}
return res;
}
exports.editorDeleteConstraint = editorDeleteConstraint;