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