mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 03:45:59 +00:00
change structure generates data commands
This commit is contained in:
@@ -10,6 +10,14 @@ import type {
|
||||
UniqueInfo,
|
||||
} from 'dbgate-types';
|
||||
import _ from 'lodash';
|
||||
import { parseSqlDefaultValue } from './stringTools';
|
||||
|
||||
export interface JsonDataObjectUpdateCommand {
|
||||
type: 'renameField' | 'deleteField' | 'setField' | 'setFieldIfNull';
|
||||
oldField?: string;
|
||||
newField?: string;
|
||||
value?: any;
|
||||
}
|
||||
|
||||
export interface EditorColumnInfo extends ColumnInfo {
|
||||
isPrimaryKey?: boolean;
|
||||
@@ -23,6 +31,41 @@ export function fillEditorColumnInfo(column: ColumnInfo, table: TableInfo): Edit
|
||||
};
|
||||
}
|
||||
|
||||
export function processJsonDataUpdateCommands(obj: any, commands: JsonDataObjectUpdateCommand[] = []) {
|
||||
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;
|
||||
}
|
||||
|
||||
function processPrimaryKey(table: TableInfo, oldColumn: EditorColumnInfo, newColumn: EditorColumnInfo): TableInfo {
|
||||
if (!oldColumn?.isPrimaryKey && newColumn?.isPrimaryKey) {
|
||||
let primaryKey = table?.primaryKey;
|
||||
@@ -71,7 +114,11 @@ function processPrimaryKey(table: TableInfo, oldColumn: EditorColumnInfo, newCol
|
||||
return table;
|
||||
}
|
||||
|
||||
export function editorAddColumn(table: TableInfo, column: EditorColumnInfo): TableInfo {
|
||||
function defineDataCommand(table: TableInfo, cmd: () => JsonDataObjectUpdateCommand) {
|
||||
table['__addDataCommands'] = [...(table['__addDataCommands'] || []), cmd()];
|
||||
}
|
||||
|
||||
export function editorAddColumn(table: TableInfo, column: EditorColumnInfo, addDataCommand?: boolean): TableInfo {
|
||||
let res = {
|
||||
...table,
|
||||
columns: [...(table?.columns || []), { ...column, pairingId: uuidv1() }],
|
||||
@@ -79,10 +126,18 @@ export function editorAddColumn(table: TableInfo, column: EditorColumnInfo): Tab
|
||||
|
||||
res = processPrimaryKey(res, null, column);
|
||||
|
||||
if (addDataCommand && column.defaultValue) {
|
||||
defineDataCommand(res, () => ({
|
||||
type: 'setField',
|
||||
field: column.columnName,
|
||||
value: parseSqlDefaultValue(column.defaultValue),
|
||||
}));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
export function editorModifyColumn(table: TableInfo, column: EditorColumnInfo): TableInfo {
|
||||
export function editorModifyColumn(table: TableInfo, column: EditorColumnInfo, addDataCommand?: boolean): TableInfo {
|
||||
const oldColumn = table?.columns?.find(x => x.pairingId == column.pairingId);
|
||||
|
||||
let res = {
|
||||
@@ -91,10 +146,26 @@ export function editorModifyColumn(table: TableInfo, column: EditorColumnInfo):
|
||||
};
|
||||
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: parseSqlDefaultValue(column.defaultValue),
|
||||
}));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
export function editorDeleteColumn(table: TableInfo, column: EditorColumnInfo): TableInfo {
|
||||
export function editorDeleteColumn(table: TableInfo, column: EditorColumnInfo, addDataCommand?: boolean): TableInfo {
|
||||
let res = {
|
||||
...table,
|
||||
columns: table.columns.filter(col => col.pairingId != column.pairingId),
|
||||
@@ -102,6 +173,13 @@ export function editorDeleteColumn(table: TableInfo, column: EditorColumnInfo):
|
||||
|
||||
res = processPrimaryKey(res, column, null);
|
||||
|
||||
if (addDataCommand) {
|
||||
defineDataCommand(res, () => ({
|
||||
type: 'deleteField',
|
||||
oldField: column.columnName,
|
||||
}));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user