mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 16:36:00 +00:00
generate update script
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import _ from 'lodash';
|
||||
import { Command, Insert, Update, Delete, UpdateField, Condition } from '@dbgate/sqltree';
|
||||
|
||||
export interface ChangeSetItem {
|
||||
pureName: string;
|
||||
@@ -100,3 +101,81 @@ export function setChangeSetValue(
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
function extractFields(item: ChangeSetItem): UpdateField[] {
|
||||
return _.keys(item.fields).map(targetColumn => ({
|
||||
targetColumn,
|
||||
exprType: 'value',
|
||||
value: item.fields[targetColumn],
|
||||
}));
|
||||
}
|
||||
|
||||
function insertToSql(item: ChangeSetItem): Insert {
|
||||
return {
|
||||
targetTable: {
|
||||
pureName: item.pureName,
|
||||
schemaName: item.schemaName,
|
||||
},
|
||||
commandType: 'insert',
|
||||
fields: extractFields(item),
|
||||
};
|
||||
}
|
||||
|
||||
function extractCondition(item: ChangeSetItem): Condition {
|
||||
return {
|
||||
conditionType: 'and',
|
||||
conditions: _.keys(item.condition).map(columnName => ({
|
||||
conditionType: 'binary',
|
||||
operator: '=',
|
||||
left: {
|
||||
exprType: 'column',
|
||||
columnName,
|
||||
source: {
|
||||
name: {
|
||||
pureName: item.pureName,
|
||||
schemaName: item.schemaName,
|
||||
},
|
||||
},
|
||||
},
|
||||
right: {
|
||||
exprType: 'value',
|
||||
value: item.condition[columnName],
|
||||
},
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
function updateToSql(item: ChangeSetItem): Update {
|
||||
return {
|
||||
from: {
|
||||
name: {
|
||||
pureName: item.pureName,
|
||||
schemaName: item.schemaName,
|
||||
},
|
||||
},
|
||||
commandType: 'update',
|
||||
fields: extractFields(item),
|
||||
where: extractCondition(item),
|
||||
};
|
||||
}
|
||||
|
||||
function deleteToSql(item: ChangeSetItem): Delete {
|
||||
return {
|
||||
from: {
|
||||
name: {
|
||||
pureName: item.pureName,
|
||||
schemaName: item.schemaName,
|
||||
},
|
||||
},
|
||||
commandType: 'delete',
|
||||
where: extractCondition(item),
|
||||
};
|
||||
}
|
||||
|
||||
export function changeSetToSql(changeSet: ChangeSet): Command[] {
|
||||
return [
|
||||
...changeSet.inserts.map(insertToSql),
|
||||
...changeSet.updates.map(updateToSql),
|
||||
...changeSet.deletes.map(deleteToSql),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import _ from 'lodash';
|
||||
import { GridConfig, GridCache, GridConfigColumns } from './GridConfig';
|
||||
import { ForeignKeyInfo, TableInfo, ColumnInfo, DbType } from '@dbgate/types';
|
||||
import { ForeignKeyInfo, TableInfo, ColumnInfo, DbType, EngineDriver } from '@dbgate/types';
|
||||
import { parseFilter, getFilterType } from '@dbgate/filterparser';
|
||||
import { filterName } from './filterName';
|
||||
import { Select, Expression } from '@dbgate/sqltree';
|
||||
@@ -44,12 +44,14 @@ export abstract class GridDisplay {
|
||||
protected setConfig: (config: GridConfig) => void,
|
||||
public cache: GridCache,
|
||||
protected setCache: (config: GridCache) => void,
|
||||
protected getTableInfo: ({ schemaName, pureName }) => Promise<TableInfo>
|
||||
protected getTableInfo: ({ schemaName, pureName }) => Promise<TableInfo>,
|
||||
public driver: EngineDriver
|
||||
) {}
|
||||
abstract getPageQuery(offset: number, count: number): string;
|
||||
columns: DisplayColumn[];
|
||||
baseTable?: TableInfo;
|
||||
changeSetKeyFields: string[] = null;
|
||||
|
||||
setColumnVisibility(uniquePath: string[], isVisible: boolean) {
|
||||
const uniqueName = uniquePath.join('.');
|
||||
if (uniquePath.length == 1) {
|
||||
@@ -60,6 +62,10 @@ export abstract class GridDisplay {
|
||||
}
|
||||
}
|
||||
|
||||
get engine() {
|
||||
return this.driver.engine;
|
||||
}
|
||||
|
||||
reload() {
|
||||
this.setCache({
|
||||
...this.cache,
|
||||
|
||||
@@ -7,19 +7,21 @@ import { GridConfig, GridCache } from './GridConfig';
|
||||
export class TableGridDisplay extends GridDisplay {
|
||||
constructor(
|
||||
public table: TableInfo,
|
||||
public driver: EngineDriver,
|
||||
driver: EngineDriver,
|
||||
config: GridConfig,
|
||||
setConfig: (config: GridConfig) => void,
|
||||
cache: GridCache,
|
||||
setCache: (config: GridCache) => void,
|
||||
getTableInfo: ({ schemaName, pureName }) => Promise<TableInfo>
|
||||
) {
|
||||
super(config, setConfig, cache, setCache, getTableInfo);
|
||||
super(config, setConfig, cache, setCache, getTableInfo, driver);
|
||||
this.columns = this.getDisplayColumns(table, []);
|
||||
this.baseTable = table;
|
||||
this.changeSetKeyFields = table.primaryKey
|
||||
? table.primaryKey.columns.map(x => x.columnName)
|
||||
: table.columns.map(x => x.columnName);
|
||||
if (table && table.columns) {
|
||||
this.changeSetKeyFields = table.primaryKey
|
||||
? table.primaryKey.columns.map(x => x.columnName)
|
||||
: table.columns.map(x => x.columnName);
|
||||
}
|
||||
}
|
||||
|
||||
createSelect() {
|
||||
|
||||
Reference in New Issue
Block a user