insert into tables with identity

This commit is contained in:
Jan Prochazka
2020-05-21 15:09:48 +02:00
parent 6df7743b4c
commit 68f5c88fb8
9 changed files with 87 additions and 36 deletions

View File

@@ -1,6 +1,6 @@
import _ from 'lodash'; import _ from 'lodash';
import { Command, Insert, Update, Delete, UpdateField, Condition } from '@dbgate/sqltree'; import { Command, Insert, Update, Delete, UpdateField, Condition, AllowIdentityInsert } from '@dbgate/sqltree';
import { NamedObjectInfo } from '@dbgate/types'; import { NamedObjectInfo, DatabaseInfo } from '@dbgate/types';
export interface ChangeSetItem { export interface ChangeSetItem {
pureName: string; pureName: string;
@@ -175,17 +175,48 @@ function extractFields(item: ChangeSetItem, allowNulls = true): UpdateField[] {
})); }));
} }
function insertToSql(item: ChangeSetItem): Insert { function insertToSql(
item: ChangeSetItem,
dbinfo: DatabaseInfo = null
): [AllowIdentityInsert, Insert, AllowIdentityInsert] {
const fields = extractFields(item, false); const fields = extractFields(item, false);
if (fields.length == 0) return null; if (fields.length == 0) return null;
return { let autoInc = false;
targetTable: { if (dbinfo) {
const table = dbinfo.tables.find((x) => x.schemaName == item.schemaName && x.pureName == item.pureName);
if (table) {
const autoIncCol = table.columns.find((x) => x.autoIncrement);
console.log('autoIncCol', autoIncCol);
if (autoIncCol && fields.find((x) => x.targetColumn == autoIncCol.columnName)) {
autoInc = true;
}
}
}
const targetTable = {
pureName: item.pureName, pureName: item.pureName,
schemaName: item.schemaName, schemaName: item.schemaName,
}, };
return [
autoInc
? {
targetTable,
commandType: 'allowIdentityInsert',
allow: true,
}
: null,
{
targetTable,
commandType: 'insert', commandType: 'insert',
fields, fields,
}; },
autoInc
? {
targetTable,
commandType: 'allowIdentityInsert',
allow: false,
}
: null,
];
} }
function extractCondition(item: ChangeSetItem): Condition { function extractCondition(item: ChangeSetItem): Condition {
@@ -239,12 +270,14 @@ function deleteToSql(item: ChangeSetItem): Delete {
}; };
} }
export function changeSetToSql(changeSet: ChangeSet): Command[] { export function changeSetToSql(changeSet: ChangeSet, dbinfo: DatabaseInfo): Command[] {
return _.compact([ return _.compact(
...changeSet.inserts.map(insertToSql), _.flatten([
...changeSet.inserts.map((item) => insertToSql(item, dbinfo)),
...changeSet.updates.map(updateToSql), ...changeSet.updates.map(updateToSql),
...changeSet.deletes.map(deleteToSql), ...changeSet.deletes.map(deleteToSql),
]); ])
);
} }
export function revertChangeSetRowChanges(changeSet: ChangeSet, definition: ChangeSetRowDefinition): ChangeSet { export function revertChangeSetRowChanges(changeSet: ChangeSet, definition: ChangeSetRowDefinition): ChangeSet {

View File

@@ -1,6 +1,6 @@
import _ from 'lodash'; import _ from 'lodash';
import { GridConfig, GridCache, GridConfigColumns, createGridCache } from './GridConfig'; import { GridConfig, GridCache, GridConfigColumns, createGridCache } from './GridConfig';
import { ForeignKeyInfo, TableInfo, ColumnInfo, DbType, EngineDriver, NamedObjectInfo } from '@dbgate/types'; import { ForeignKeyInfo, TableInfo, ColumnInfo, DbType, EngineDriver, NamedObjectInfo, DatabaseInfo } from '@dbgate/types';
import { parseFilter, getFilterType } from '@dbgate/filterparser'; import { parseFilter, getFilterType } from '@dbgate/filterparser';
import { filterName } from './filterName'; import { filterName } from './filterName';
import { ChangeSetFieldDefinition, ChangeSetRowDefinition } from './ChangeSet'; import { ChangeSetFieldDefinition, ChangeSetRowDefinition } from './ChangeSet';
@@ -47,7 +47,8 @@ export abstract class GridDisplay {
protected setConfig: ChangeConfigFunc, protected setConfig: ChangeConfigFunc,
public cache: GridCache, public cache: GridCache,
protected setCache: ChangeCacheFunc, protected setCache: ChangeCacheFunc,
public driver?: EngineDriver public driver?: EngineDriver,
public dbinfo: DatabaseInfo = null
) {} ) {}
columns: DisplayColumn[]; columns: DisplayColumn[];
baseTable?: TableInfo; baseTable?: TableInfo;

View File

@@ -15,9 +15,9 @@ export class TableGridDisplay extends GridDisplay {
setConfig: ChangeConfigFunc, setConfig: ChangeConfigFunc,
cache: GridCache, cache: GridCache,
setCache: ChangeCacheFunc, setCache: ChangeCacheFunc,
protected dbinfo: DatabaseInfo dbinfo: DatabaseInfo
) { ) {
super(config, setConfig, cache, setCache, driver); super(config, setConfig, cache, setCache, driver, dbinfo);
this.table = this.findTable(tableName); this.table = this.findTable(tableName);
if (!this.table) { if (!this.table) {

View File

@@ -198,6 +198,7 @@ class SqlDumper {
lambda(item); lambda(item);
} }
} }
/** @param table {import('@dbgate/types').TableInfo} */ /** @param table {import('@dbgate/types').TableInfo} */
createTable(table) { createTable(table) {
this.put('^create ^table %f ( &>&n', table); this.put('^create ^table %f ( &>&n', table);
@@ -253,6 +254,12 @@ class SqlDumper {
if (fk.deleteAction) this.put(' ^on ^delete %k', fk.deleteAction); if (fk.deleteAction) this.put(' ^on ^delete %k', fk.deleteAction);
if (fk.updateAction) this.put(' ^on ^update %k', fk.updateAction); if (fk.updateAction) this.put(' ^on ^update %k', fk.updateAction);
} }
/**
* @param table {import('@dbgate/types').NamedObjectInfo}
* @param allow {boolean}
*/
allowIdentityInsert(table, allow) {}
} }
module.exports = SqlDumper; module.exports = SqlDumper;

View File

@@ -11,6 +11,10 @@ class MsSqlDumper extends SqlDumper {
} }
super.putStringValue(value); super.putStringValue(value);
} }
allowIdentityInsert(table, allow) {
this.putCmd("^set ^identity_insert %f %k;&n", table, allow ? "on" : "off");
}
} }
module.exports = MsSqlDumper; module.exports = MsSqlDumper;

View File

@@ -18,7 +18,7 @@ export function dumpSqlSelect(dmp: SqlDumper, cmd: Select) {
if (cmd.columns) { if (cmd.columns) {
if (cmd.selectAll) dmp.put('&n,'); if (cmd.selectAll) dmp.put('&n,');
dmp.put('&>&n'); dmp.put('&>&n');
dmp.putCollection(',&n', cmd.columns, fld => { dmp.putCollection(',&n', cmd.columns, (fld) => {
dumpSqlExpression(dmp, fld); dumpSqlExpression(dmp, fld);
if (fld.alias) dmp.put(' ^as %i', fld.alias); if (fld.alias) dmp.put(' ^as %i', fld.alias);
}); });
@@ -33,12 +33,12 @@ export function dumpSqlSelect(dmp: SqlDumper, cmd: Select) {
} }
if (cmd.groupBy) { if (cmd.groupBy) {
dmp.put('&n^group ^by '); dmp.put('&n^group ^by ');
dmp.putCollection(', ', cmd.groupBy, expr => dumpSqlExpression(dmp, expr)); dmp.putCollection(', ', cmd.groupBy, (expr) => dumpSqlExpression(dmp, expr));
dmp.put('&n'); dmp.put('&n');
} }
if (cmd.orderBy) { if (cmd.orderBy) {
dmp.put('&n^order ^by '); dmp.put('&n^order ^by ');
dmp.putCollection(', ', cmd.orderBy, expr => { dmp.putCollection(', ', cmd.orderBy, (expr) => {
dumpSqlExpression(dmp, expr); dumpSqlExpression(dmp, expr);
dmp.put(' %k', expr.direction); dmp.put(' %k', expr.direction);
}); });
@@ -59,7 +59,7 @@ export function dumpSqlUpdate(dmp: SqlDumper, cmd: Update) {
dmp.put('&n^set '); dmp.put('&n^set ');
dmp.put('&>'); dmp.put('&>');
dmp.putCollection(', ', cmd.fields, col => { dmp.putCollection(', ', cmd.fields, (col) => {
dmp.put('%i=', col.targetColumn); dmp.put('%i=', col.targetColumn);
dumpSqlExpression(dmp, col); dumpSqlExpression(dmp, col);
}); });
@@ -73,7 +73,7 @@ export function dumpSqlUpdate(dmp: SqlDumper, cmd: Update) {
} }
export function dumpSqlDelete(dmp: SqlDumper, cmd: Delete) { export function dumpSqlDelete(dmp: SqlDumper, cmd: Delete) {
dmp.put('^delete '); dmp.put('^delete ^from ');
dumpSqlSourceRef(dmp, cmd.from); dumpSqlSourceRef(dmp, cmd.from);
if (cmd.where) { if (cmd.where) {
@@ -87,9 +87,9 @@ export function dumpSqlInsert(dmp: SqlDumper, cmd: Insert) {
dmp.put( dmp.put(
'^insert ^into %f (%,i) ^values (', '^insert ^into %f (%,i) ^values (',
cmd.targetTable, cmd.targetTable,
cmd.fields.map(x => x.targetColumn) cmd.fields.map((x) => x.targetColumn)
); );
dmp.putCollection(',', cmd.fields, x => dumpSqlExpression(dmp, x)); dmp.putCollection(',', cmd.fields, (x) => dumpSqlExpression(dmp, x));
dmp.put(')'); dmp.put(')');
} }
@@ -107,5 +107,8 @@ export function dumpSqlCommand(dmp: SqlDumper, cmd: Command) {
case 'insert': case 'insert':
dumpSqlInsert(dmp, cmd); dumpSqlInsert(dmp, cmd);
break; break;
case 'allowIdentityInsert':
dmp.allowIdentityInsert(cmd.targetTable, cmd.allow);
break;
} }
} }

View File

@@ -39,7 +39,13 @@ export interface Insert {
targetTable: NamedObjectInfo; targetTable: NamedObjectInfo;
} }
export type Command = Select | Update | Delete | Insert; export interface AllowIdentityInsert {
commandType: 'allowIdentityInsert';
targetTable: NamedObjectInfo;
allow: boolean;
}
export type Command = Select | Update | Delete | Insert | AllowIdentityInsert;
// export interface Condition { // export interface Condition {
// conditionType: "eq" | "not" | "binary"; // conditionType: "eq" | "not" | "binary";

View File

@@ -1,5 +1,5 @@
import { TableInfo } from "./dbinfo"; import { TableInfo } from './dbinfo';
import { SqlDialect } from "./dialect"; import { SqlDialect } from './dialect';
export interface SqlDumper { export interface SqlDumper {
s: string; s: string;
@@ -9,12 +9,9 @@ export interface SqlDumper {
put(format: string, ...args); put(format: string, ...args);
putCmd(format: string, ...args); putCmd(format: string, ...args);
putValue(value: string | number | Date); putValue(value: string | number | Date);
putCollection<T>( putCollection<T>(delimiter: string, collection: T[], lambda: (item: T) => void);
delimiter: string,
collection: T[],
lambda: (item: T) => void
);
endCommand(); endCommand();
createTable(table: TableInfo); createTable(table: TableInfo);
allowIdentityInsert(table: NamedObjectInfo, allow: boolean);
} }

View File

@@ -816,7 +816,7 @@ export default function DataGridCore(props) {
dispatchInsplaceEditor({ type: 'shouldSave' }); dispatchInsplaceEditor({ type: 'shouldSave' });
return; return;
} }
const script = changeSetToSql(changeSetRef.current); const script = changeSetToSql(changeSetRef.current, display.dbinfo);
const sql = scriptToSql(display.driver, script); const sql = scriptToSql(display.driver, script);
setConfirmSql(sql); setConfirmSql(sql);
confirmSqlModalState.open(); confirmSqlModalState.open();