mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-22 01:45:59 +00:00
of not exitsts fields
This commit is contained in:
@@ -20,6 +20,7 @@ export interface ChangeSetItem {
|
|||||||
document?: any;
|
document?: any;
|
||||||
condition?: { [column: string]: string };
|
condition?: { [column: string]: string };
|
||||||
fields?: { [column: string]: string };
|
fields?: { [column: string]: string };
|
||||||
|
insertIfNotExistsFields?: { [column: string]: string };
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ChangeSetItemFields {
|
export interface ChangeSetItemFields {
|
||||||
@@ -284,6 +285,9 @@ function changeSetInsertToSql(
|
|||||||
targetTable,
|
targetTable,
|
||||||
commandType: 'insert',
|
commandType: 'insert',
|
||||||
fields,
|
fields,
|
||||||
|
insertWhereNotExistsCondition: item.insertIfNotExistsFields
|
||||||
|
? compileSimpleChangeSetCondition(item.insertIfNotExistsFields)
|
||||||
|
: null,
|
||||||
},
|
},
|
||||||
autoInc
|
autoInc
|
||||||
? {
|
? {
|
||||||
@@ -332,6 +336,36 @@ export function extractChangeSetCondition(item: ChangeSetItem, alias?: string):
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function compileSimpleChangeSetCondition(fields: { [column: string]: string }): Condition {
|
||||||
|
function getColumnCondition(columnName: string): Condition {
|
||||||
|
const value = fields[columnName];
|
||||||
|
const expr: Expression = {
|
||||||
|
exprType: 'column',
|
||||||
|
columnName,
|
||||||
|
};
|
||||||
|
if (value == null) {
|
||||||
|
return {
|
||||||
|
conditionType: 'isNull',
|
||||||
|
expr,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
conditionType: 'binary',
|
||||||
|
operator: '=',
|
||||||
|
left: expr,
|
||||||
|
right: {
|
||||||
|
exprType: 'value',
|
||||||
|
value,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
conditionType: 'and',
|
||||||
|
conditions: _.keys(fields).map(columnName => getColumnCondition(columnName)),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function changeSetUpdateToSql(item: ChangeSetItem, dbinfo: DatabaseInfo = null): Update {
|
function changeSetUpdateToSql(item: ChangeSetItem, dbinfo: DatabaseInfo = null): Update {
|
||||||
const table = dbinfo?.tables?.find(x => x.schemaName == item.schemaName && x.pureName == item.pureName);
|
const table = dbinfo?.tables?.find(x => x.schemaName == item.schemaName && x.pureName == item.pureName);
|
||||||
|
|
||||||
|
|||||||
@@ -92,13 +92,28 @@ export function dumpSqlDelete(dmp: SqlDumper, cmd: Delete) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function dumpSqlInsert(dmp: SqlDumper, cmd: Insert) {
|
export function dumpSqlInsert(dmp: SqlDumper, cmd: Insert) {
|
||||||
dmp.put(
|
if (cmd.insertWhereNotExistsCondition) {
|
||||||
'^insert ^into %f (%,i) ^values (',
|
dmp.put(
|
||||||
cmd.targetTable,
|
'^insert ^into %f (%,i) ^select ',
|
||||||
cmd.fields.map(x => x.targetColumn)
|
cmd.targetTable,
|
||||||
);
|
cmd.fields.map(x => x.targetColumn)
|
||||||
dmp.putCollection(',', cmd.fields, x => dumpSqlExpression(dmp, x));
|
);
|
||||||
dmp.put(')');
|
dmp.putCollection(',', cmd.fields, x => dumpSqlExpression(dmp, x));
|
||||||
|
if (dmp.dialect.requireFromDual) {
|
||||||
|
dmp.put(' ^from ^dual ');
|
||||||
|
}
|
||||||
|
dmp.put(' ^where ^not ^exists (^select * ^from %f ^where ', cmd.targetTable);
|
||||||
|
dumpSqlCondition(dmp, cmd.insertWhereNotExistsCondition);
|
||||||
|
dmp.put(')');
|
||||||
|
} else {
|
||||||
|
dmp.put(
|
||||||
|
'^insert ^into %f (%,i) ^values (',
|
||||||
|
cmd.targetTable,
|
||||||
|
cmd.fields.map(x => x.targetColumn)
|
||||||
|
);
|
||||||
|
dmp.putCollection(',', cmd.fields, x => dumpSqlExpression(dmp, x));
|
||||||
|
dmp.put(')');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function dumpSqlCommand(dmp: SqlDumper, cmd: Command) {
|
export function dumpSqlCommand(dmp: SqlDumper, cmd: Command) {
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ export interface Insert {
|
|||||||
commandType: 'insert';
|
commandType: 'insert';
|
||||||
fields: UpdateField[];
|
fields: UpdateField[];
|
||||||
targetTable: NamedObjectInfo;
|
targetTable: NamedObjectInfo;
|
||||||
|
insertWhereNotExistsCondition?: Condition;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AllowIdentityInsert {
|
export interface AllowIdentityInsert {
|
||||||
|
|||||||
1
packages/types/dialect.d.ts
vendored
1
packages/types/dialect.d.ts
vendored
@@ -34,6 +34,7 @@ export interface SqlDialect {
|
|||||||
dropCheck?: boolean;
|
dropCheck?: boolean;
|
||||||
|
|
||||||
dropReferencesWhenDropTable?: boolean;
|
dropReferencesWhenDropTable?: boolean;
|
||||||
|
requireFromDual?: boolean;
|
||||||
|
|
||||||
predefinedDataTypes: string[];
|
predefinedDataTypes: string[];
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ const dialect = {
|
|||||||
dropCheck: true,
|
dropCheck: true,
|
||||||
|
|
||||||
dropReferencesWhenDropTable: true,
|
dropReferencesWhenDropTable: true,
|
||||||
|
requireFromDual: true,
|
||||||
|
|
||||||
predefinedDataTypes: [
|
predefinedDataTypes: [
|
||||||
'VARCHAR2',
|
'VARCHAR2',
|
||||||
|
|||||||
Reference in New Issue
Block a user