mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-28 01:26:01 +00:00
feat: dumper data type handling
This commit is contained in:
committed by
Nybkox
parent
bcf89b1f09
commit
09fa3ce438
@@ -230,7 +230,12 @@ export function batchUpdateChangeSet(
|
||||
return changeSet;
|
||||
}
|
||||
|
||||
function extractFields(item: ChangeSetItem, allowNulls = true, allowedDocumentColumns: string[] = []): UpdateField[] {
|
||||
function extractFields(
|
||||
item: ChangeSetItem,
|
||||
allowNulls = true,
|
||||
allowedDocumentColumns: string[] = [],
|
||||
table?: TableInfo
|
||||
): UpdateField[] {
|
||||
const allFields = {
|
||||
...item.fields,
|
||||
};
|
||||
@@ -247,6 +252,7 @@ function extractFields(item: ChangeSetItem, allowNulls = true, allowedDocumentCo
|
||||
targetColumn,
|
||||
exprType: 'value',
|
||||
value: allFields[targetColumn],
|
||||
dataType: table?.columns?.find(x => x.columnName == targetColumn)?.dataType,
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -259,7 +265,8 @@ function changeSetInsertToSql(
|
||||
const fields = extractFields(
|
||||
item,
|
||||
false,
|
||||
table?.columns?.map(x => x.columnName)
|
||||
table?.columns?.map(x => x.columnName),
|
||||
table
|
||||
);
|
||||
if (fields.length == 0) return null;
|
||||
let autoInc = false;
|
||||
@@ -319,6 +326,7 @@ export function extractChangeSetCondition(
|
||||
|
||||
function getColumnCondition(columnName: string): Condition {
|
||||
const shouldUseRawRightValue = getShouldUseRawRightValue(columnName);
|
||||
const dataType = table?.columns?.find(x => x.columnName == columnName)?.dataType;
|
||||
|
||||
const value = item.condition[columnName];
|
||||
const expr: Expression = {
|
||||
@@ -351,6 +359,7 @@ export function extractChangeSetCondition(
|
||||
}
|
||||
: {
|
||||
exprType: 'value',
|
||||
dataType,
|
||||
value,
|
||||
},
|
||||
};
|
||||
@@ -408,7 +417,8 @@ function changeSetUpdateToSql(item: ChangeSetItem, dbinfo: DatabaseInfo = null,
|
||||
fields: extractFields(
|
||||
item,
|
||||
true,
|
||||
table?.columns?.map(x => x.columnName).filter(x => x != autoIncCol?.columnName)
|
||||
table?.columns?.map(x => x.columnName).filter(x => x != autoIncCol?.columnName),
|
||||
table
|
||||
),
|
||||
where: extractChangeSetCondition(item, undefined, table, dialect),
|
||||
};
|
||||
|
||||
@@ -21,7 +21,14 @@ export function dumpSqlExpression(dmp: SqlDumper, expr: Expression) {
|
||||
break;
|
||||
|
||||
case 'value':
|
||||
dmp.put('%v', expr.value);
|
||||
if (expr.dataType) {
|
||||
dmp.put('%V', {
|
||||
value: expr.value,
|
||||
dataType: expr.dataType,
|
||||
});
|
||||
} else {
|
||||
dmp.put('%v', expr.value);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'raw':
|
||||
|
||||
@@ -182,6 +182,7 @@ export interface ColumnRefExpression {
|
||||
export interface ValueExpression {
|
||||
exprType: 'value';
|
||||
value: any;
|
||||
dataType?: string;
|
||||
}
|
||||
|
||||
export interface PlaceholderExpression {
|
||||
|
||||
@@ -70,7 +70,7 @@ export class SqlDumper implements AlterProcessor {
|
||||
putByteArrayValue(value) {
|
||||
this.put('^null');
|
||||
}
|
||||
putValue(value) {
|
||||
putValue(value, dataType = null) {
|
||||
if (value === null) this.put('^null');
|
||||
else if (value === true) this.putRaw('1');
|
||||
else if (value === false) this.putRaw('0');
|
||||
@@ -117,6 +117,9 @@ export class SqlDumper implements AlterProcessor {
|
||||
case 'v':
|
||||
this.putValue(value);
|
||||
break;
|
||||
case 'V':
|
||||
this.putValue(value.value, value.dataType);
|
||||
break;
|
||||
case 'c':
|
||||
value(this);
|
||||
break;
|
||||
|
||||
2
packages/types/dumper.d.ts
vendored
2
packages/types/dumper.d.ts
vendored
@@ -11,7 +11,7 @@ export interface SqlDumper extends AlterProcessor {
|
||||
putRaw(s: string);
|
||||
put(format: string, ...args);
|
||||
putCmd(format: string, ...args);
|
||||
putValue(value: string | number | Date);
|
||||
putValue(value: string | number | Date, dataType?: string);
|
||||
putCollection<T>(delimiter: string, collection: T[], lambda: (item: T) => void);
|
||||
transform(type: TransformType, dumpExpr: () => void);
|
||||
createDatabase(name: string);
|
||||
|
||||
@@ -22,6 +22,18 @@ class Dumper extends SqlDumper {
|
||||
dropColumn(column) {
|
||||
this.putCmd('^alter ^table %f ^drop %i', column, column.columnName);
|
||||
}
|
||||
|
||||
putValue(value, dataType) {
|
||||
if (
|
||||
dataType?.toLowerCase() === 'uuid' &&
|
||||
value.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/)
|
||||
) {
|
||||
this.putRaw(value);
|
||||
return;
|
||||
}
|
||||
|
||||
super.putValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Dumper;
|
||||
|
||||
Reference in New Issue
Block a user