mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-27 18:26:00 +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;
|
return changeSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractFields(item: ChangeSetItem, allowNulls = true, allowedDocumentColumns: string[] = []): UpdateField[] {
|
function extractFields(
|
||||||
|
item: ChangeSetItem,
|
||||||
|
allowNulls = true,
|
||||||
|
allowedDocumentColumns: string[] = [],
|
||||||
|
table?: TableInfo
|
||||||
|
): UpdateField[] {
|
||||||
const allFields = {
|
const allFields = {
|
||||||
...item.fields,
|
...item.fields,
|
||||||
};
|
};
|
||||||
@@ -247,6 +252,7 @@ function extractFields(item: ChangeSetItem, allowNulls = true, allowedDocumentCo
|
|||||||
targetColumn,
|
targetColumn,
|
||||||
exprType: 'value',
|
exprType: 'value',
|
||||||
value: allFields[targetColumn],
|
value: allFields[targetColumn],
|
||||||
|
dataType: table?.columns?.find(x => x.columnName == targetColumn)?.dataType,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,7 +265,8 @@ function changeSetInsertToSql(
|
|||||||
const fields = extractFields(
|
const fields = extractFields(
|
||||||
item,
|
item,
|
||||||
false,
|
false,
|
||||||
table?.columns?.map(x => x.columnName)
|
table?.columns?.map(x => x.columnName),
|
||||||
|
table
|
||||||
);
|
);
|
||||||
if (fields.length == 0) return null;
|
if (fields.length == 0) return null;
|
||||||
let autoInc = false;
|
let autoInc = false;
|
||||||
@@ -319,6 +326,7 @@ export function extractChangeSetCondition(
|
|||||||
|
|
||||||
function getColumnCondition(columnName: string): Condition {
|
function getColumnCondition(columnName: string): Condition {
|
||||||
const shouldUseRawRightValue = getShouldUseRawRightValue(columnName);
|
const shouldUseRawRightValue = getShouldUseRawRightValue(columnName);
|
||||||
|
const dataType = table?.columns?.find(x => x.columnName == columnName)?.dataType;
|
||||||
|
|
||||||
const value = item.condition[columnName];
|
const value = item.condition[columnName];
|
||||||
const expr: Expression = {
|
const expr: Expression = {
|
||||||
@@ -351,6 +359,7 @@ export function extractChangeSetCondition(
|
|||||||
}
|
}
|
||||||
: {
|
: {
|
||||||
exprType: 'value',
|
exprType: 'value',
|
||||||
|
dataType,
|
||||||
value,
|
value,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -408,7 +417,8 @@ function changeSetUpdateToSql(item: ChangeSetItem, dbinfo: DatabaseInfo = null,
|
|||||||
fields: extractFields(
|
fields: extractFields(
|
||||||
item,
|
item,
|
||||||
true,
|
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),
|
where: extractChangeSetCondition(item, undefined, table, dialect),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -21,7 +21,14 @@ export function dumpSqlExpression(dmp: SqlDumper, expr: Expression) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'value':
|
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;
|
break;
|
||||||
|
|
||||||
case 'raw':
|
case 'raw':
|
||||||
|
|||||||
@@ -182,6 +182,7 @@ export interface ColumnRefExpression {
|
|||||||
export interface ValueExpression {
|
export interface ValueExpression {
|
||||||
exprType: 'value';
|
exprType: 'value';
|
||||||
value: any;
|
value: any;
|
||||||
|
dataType?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PlaceholderExpression {
|
export interface PlaceholderExpression {
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ export class SqlDumper implements AlterProcessor {
|
|||||||
putByteArrayValue(value) {
|
putByteArrayValue(value) {
|
||||||
this.put('^null');
|
this.put('^null');
|
||||||
}
|
}
|
||||||
putValue(value) {
|
putValue(value, dataType = null) {
|
||||||
if (value === null) this.put('^null');
|
if (value === null) this.put('^null');
|
||||||
else if (value === true) this.putRaw('1');
|
else if (value === true) this.putRaw('1');
|
||||||
else if (value === false) this.putRaw('0');
|
else if (value === false) this.putRaw('0');
|
||||||
@@ -117,6 +117,9 @@ export class SqlDumper implements AlterProcessor {
|
|||||||
case 'v':
|
case 'v':
|
||||||
this.putValue(value);
|
this.putValue(value);
|
||||||
break;
|
break;
|
||||||
|
case 'V':
|
||||||
|
this.putValue(value.value, value.dataType);
|
||||||
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
value(this);
|
value(this);
|
||||||
break;
|
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);
|
putRaw(s: string);
|
||||||
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, dataType?: string);
|
||||||
putCollection<T>(delimiter: string, collection: T[], lambda: (item: T) => void);
|
putCollection<T>(delimiter: string, collection: T[], lambda: (item: T) => void);
|
||||||
transform(type: TransformType, dumpExpr: () => void);
|
transform(type: TransformType, dumpExpr: () => void);
|
||||||
createDatabase(name: string);
|
createDatabase(name: string);
|
||||||
|
|||||||
@@ -22,6 +22,18 @@ class Dumper extends SqlDumper {
|
|||||||
dropColumn(column) {
|
dropColumn(column) {
|
||||||
this.putCmd('^alter ^table %f ^drop %i', column, column.columnName);
|
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;
|
module.exports = Dumper;
|
||||||
|
|||||||
Reference in New Issue
Block a user