mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 00:56:02 +00:00
restore script - update
This commit is contained in:
@@ -2,6 +2,7 @@ import _ from 'lodash';
|
|||||||
import type { SqlDumper } from 'dbgate-types';
|
import type { SqlDumper } from 'dbgate-types';
|
||||||
import { Expression, ColumnRefExpression } from './types';
|
import { Expression, ColumnRefExpression } from './types';
|
||||||
import { dumpSqlSourceRef } from './dumpSqlSource';
|
import { dumpSqlSourceRef } from './dumpSqlSource';
|
||||||
|
import { dumpSqlSelect } from './dumpSqlCommand';
|
||||||
|
|
||||||
export function dumpSqlExpression(dmp: SqlDumper, expr: Expression) {
|
export function dumpSqlExpression(dmp: SqlDumper, expr: Expression) {
|
||||||
switch (expr.exprType) {
|
switch (expr.exprType) {
|
||||||
@@ -67,5 +68,11 @@ export function dumpSqlExpression(dmp: SqlDumper, expr: Expression) {
|
|||||||
});
|
});
|
||||||
dmp.put(')');
|
dmp.put(')');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'select':
|
||||||
|
dmp.put('(');
|
||||||
|
dumpSqlSelect(dmp, expr.select);
|
||||||
|
dmp.put(')');
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -227,6 +227,11 @@ export interface RowNumberExpression {
|
|||||||
orderBy: OrderByExpression[];
|
orderBy: OrderByExpression[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface SelectExpression {
|
||||||
|
exprType: 'select';
|
||||||
|
select: Select;
|
||||||
|
}
|
||||||
|
|
||||||
export type Expression =
|
export type Expression =
|
||||||
| ColumnRefExpression
|
| ColumnRefExpression
|
||||||
| ValueExpression
|
| ValueExpression
|
||||||
@@ -236,7 +241,8 @@ export type Expression =
|
|||||||
| CallExpression
|
| CallExpression
|
||||||
| MethodCallExpression
|
| MethodCallExpression
|
||||||
| TranformExpression
|
| TranformExpression
|
||||||
| RowNumberExpression;
|
| RowNumberExpression
|
||||||
|
| SelectExpression;
|
||||||
export type OrderByExpression = Expression & { direction: 'ASC' | 'DESC' };
|
export type OrderByExpression = Expression & { direction: 'ASC' | 'DESC' };
|
||||||
|
|
||||||
export type ResultField = Expression & { alias?: string };
|
export type ResultField = Expression & { alias?: string };
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import _ from 'lodash';
|
import _, { cond } from 'lodash';
|
||||||
import { dumpSqlInsert, Insert } from 'dbgate-sqltree';
|
import { Condition, dumpSqlInsert, dumpSqlUpdate, Insert, Update } from 'dbgate-sqltree';
|
||||||
import { TableInfo, SqlDumper } from 'dbgate-types';
|
import { TableInfo, SqlDumper } from 'dbgate-types';
|
||||||
|
|
||||||
export function createTableRestoreScript(backupTable: TableInfo, originalTable: TableInfo, dmp: SqlDumper) {
|
export function createTableRestoreScript(backupTable: TableInfo, originalTable: TableInfo, dmp: SqlDumper) {
|
||||||
@@ -11,6 +11,69 @@ export function createTableRestoreScript(backupTable: TableInfo, originalTable:
|
|||||||
originalTable.primaryKey?.columns?.map(x => x.columnName) || [],
|
originalTable.primaryKey?.columns?.map(x => x.columnName) || [],
|
||||||
backupTable.columns.map(x => x.columnName)
|
backupTable.columns.map(x => x.columnName)
|
||||||
);
|
);
|
||||||
|
const valueColumns = _.difference(bothColumns, keyColumns);
|
||||||
|
|
||||||
|
function makeColumnCond(colName: string, operator: '=' | '<>' | '<' | '>' | '<=' | '>=' = '='): Condition {
|
||||||
|
return {
|
||||||
|
conditionType: 'binary',
|
||||||
|
operator,
|
||||||
|
left: {
|
||||||
|
exprType: 'column',
|
||||||
|
columnName: colName,
|
||||||
|
source: { name: originalTable },
|
||||||
|
},
|
||||||
|
right: {
|
||||||
|
exprType: 'column',
|
||||||
|
columnName: colName,
|
||||||
|
source: { alias: 'bak' },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const update: Update = {
|
||||||
|
commandType: 'update',
|
||||||
|
from: { name: originalTable },
|
||||||
|
fields: valueColumns.map(colName => ({
|
||||||
|
exprType: 'select',
|
||||||
|
select: {
|
||||||
|
commandType: 'select',
|
||||||
|
from: { name: backupTable, alias: 'bak' },
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
exprType: 'column',
|
||||||
|
columnName: colName,
|
||||||
|
source: { alias: 'bak' },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
where: {
|
||||||
|
conditionType: 'and',
|
||||||
|
conditions: keyColumns.map(colName => makeColumnCond(colName)),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
targetColumn: colName,
|
||||||
|
})),
|
||||||
|
where: {
|
||||||
|
conditionType: 'exists',
|
||||||
|
subQuery: {
|
||||||
|
commandType: 'select',
|
||||||
|
from: { name: backupTable, alias: 'bak' },
|
||||||
|
selectAll: true,
|
||||||
|
where: {
|
||||||
|
conditionType: 'and',
|
||||||
|
conditions: [
|
||||||
|
...keyColumns.map(keyColName => makeColumnCond(keyColName)),
|
||||||
|
{
|
||||||
|
conditionType: 'or',
|
||||||
|
conditions: valueColumns.map(colName => makeColumnCond(colName, '<>')),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
dumpSqlUpdate(dmp, update);
|
||||||
|
dmp.endCommand();
|
||||||
|
|
||||||
const insert: Insert = {
|
const insert: Insert = {
|
||||||
commandType: 'insert',
|
commandType: 'insert',
|
||||||
targetTable: originalTable,
|
targetTable: originalTable,
|
||||||
@@ -23,20 +86,7 @@ export function createTableRestoreScript(backupTable: TableInfo, originalTable:
|
|||||||
whereNotExistsSource: { name: backupTable, alias: 'bak' },
|
whereNotExistsSource: { name: backupTable, alias: 'bak' },
|
||||||
insertWhereNotExistsCondition: {
|
insertWhereNotExistsCondition: {
|
||||||
conditionType: 'and',
|
conditionType: 'and',
|
||||||
conditions: keyColumns.map(colName => ({
|
conditions: keyColumns.map(colName => makeColumnCond(colName)),
|
||||||
conditionType: 'binary',
|
|
||||||
operator: '=',
|
|
||||||
left: {
|
|
||||||
exprType: 'column',
|
|
||||||
columnName: colName,
|
|
||||||
source: { name: originalTable },
|
|
||||||
},
|
|
||||||
right: {
|
|
||||||
exprType: 'column',
|
|
||||||
columnName: colName,
|
|
||||||
source: { alias: 'bak' },
|
|
||||||
},
|
|
||||||
})),
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user