Skip to content

Commit 6470536

Browse files
committed
perf(query-builder): 优化 merge-into 查询的生成逻辑
-调整了 mergeSourceExpression 的生成逻辑,使其支持参数化查询 - 优化了对空值和默认值的处理,提高了查询效率 - 改进了对不同数据库类型(尤其是 MSSQL)的兼容性
1 parent f5b6ee5 commit 6470536

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

src/query-builder/InsertQueryBuilder.ts

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -973,14 +973,9 @@ export class InsertQueryBuilder<
973973

974974
const mergeSourceAlias = this.escape("mergeIntoSource")
975975

976-
const mergeSourceExpression = this.createMergeIntoSourceExpression()
976+
const mergeSourceExpression = this.createMergeIntoSourceExpression(mergeSourceAlias)
977977

978-
query += ` USING (${mergeSourceExpression})`
979-
query += ` ${mergeSourceAlias}`
980-
if (this.connection.driver.options.type === "mssql")
981-
query += ` (${this.getInsertedColumns()
982-
.map((c) => this.escape(c.databaseName))
983-
.join(", ")})`
978+
query += ` ${mergeSourceExpression}`
984979

985980
// build on condition
986981
if (this.expressionMap.onIgnore) {
@@ -1135,11 +1130,13 @@ export class InsertQueryBuilder<
11351130
/**
11361131
* Creates list of values needs to be inserted in the VALUES expression.
11371132
*/
1138-
protected createMergeIntoSourceExpression(): string {
1133+
protected createMergeIntoSourceExpression(mergeSourceAlias: string): string {
11391134
const valueSets = this.getValueSets()
11401135
const columns = this.getInsertedColumns()
11411136

1142-
let expression = ""
1137+
const columnNames: string[] = []
1138+
1139+
let expression = "USING ("
11431140
// if column metadatas are given then apply all necessary operations with values
11441141
if (columns.length > 0) {
11451142
if (this.connection.driver.options.type === "mssql") {
@@ -1155,11 +1152,34 @@ export class InsertQueryBuilder<
11551152
}
11561153
}
11571154

1158-
expression += this.createColumnValueExpression(
1159-
valueSets,
1160-
valueSetIndex,
1161-
column,
1162-
)
1155+
let value = column.getEntityValue(valueSet)
1156+
1157+
if (
1158+
!(
1159+
value === null ||
1160+
(value === undefined &&
1161+
column.default !== undefined &&
1162+
column.default !== null)
1163+
)
1164+
) {
1165+
if (columnIndex > 0) {
1166+
expression += ", "
1167+
}
1168+
expression += this.createColumnValueExpression(
1169+
valueSets,
1170+
valueSetIndex,
1171+
column,
1172+
)
1173+
1174+
if (this.connection.driver.options.type !== "mssql") {
1175+
expression += ` AS ${this.escape(
1176+
column.databaseName,
1177+
)}`
1178+
}
1179+
else {
1180+
columnNames.push(this.escape(column.databaseName))
1181+
}
1182+
}
11631183

11641184
if (this.connection.driver.options.type !== "mssql")
11651185
expression += ` AS ${this.escape(column.databaseName)}`
@@ -1198,8 +1218,6 @@ export class InsertQueryBuilder<
11981218
expression += " UNION ALL "
11991219
}
12001220
}
1201-
} else {
1202-
expression += ", "
12031221
}
12041222
})
12051223
})
@@ -1209,7 +1227,9 @@ export class InsertQueryBuilder<
12091227
'Upsert type "merge-into" is not supported without metadata tables',
12101228
)
12111229
}
1212-
if (expression === "()") return ""
1230+
expression += `) ${mergeSourceAlias}`
1231+
if (this.connection.driver.options.type === "mssql")
1232+
expression += ` (${columnNames.join(', ')})`
12131233
return expression
12141234
}
12151235

@@ -1230,6 +1250,7 @@ export class InsertQueryBuilder<
12301250
}
12311251

12321252
if (
1253+
column.default != null ||
12331254
(column.isGenerated &&
12341255
column.generationStrategy === "uuid" &&
12351256
this.connection.driver.isUUIDGenerationSupported()) ||

0 commit comments

Comments
 (0)