Skip to content

Commit 2b53149

Browse files
support table alias for postgresql bulk insert
1 parent 658ab52 commit 2b53149

File tree

6 files changed

+31
-32
lines changed

6 files changed

+31
-32
lines changed

ktorm-core/src/main/kotlin/org/ktorm/expression/SqlFormatter.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,9 @@ public abstract class SqlFormatter(
526526
removeLastBlank()
527527
write(", ")
528528
}
529-
visitColumn(assignment.column)
529+
530+
checkColumnName(assignment.column.name)
531+
write("${assignment.column.name.quoted} ")
530532
write("= ")
531533
visit(assignment.expression)
532534
}
@@ -536,7 +538,7 @@ public abstract class SqlFormatter(
536538

537539
override fun visitInsert(expr: InsertExpression): InsertExpression {
538540
writeKeyword("insert into ")
539-
visitTable(expr.table.copy(tableAlias = null))
541+
visitTable(expr.table)
540542
write("(")
541543

542544
for ((i, assignment) in expr.assignments.withIndex()) {
@@ -554,7 +556,7 @@ public abstract class SqlFormatter(
554556

555557
override fun visitInsertFromQuery(expr: InsertFromQueryExpression): InsertFromQueryExpression {
556558
writeKeyword("insert into ")
557-
visitTable(expr.table.copy(tableAlias = null))
559+
visitTable(expr.table)
558560
write("(")
559561

560562
for ((i, column) in expr.columns.withIndex()) {

ktorm-support-mysql/src/main/kotlin/org/ktorm/support/mysql/MySqlDialect.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public open class MySqlFormatter(
7676

7777
protected open fun visitInsertOrUpdate(expr: InsertOrUpdateExpression): InsertOrUpdateExpression {
7878
writeKeyword("insert into ")
79-
visitTable(expr.table.copy(tableAlias = null))
79+
visitTable(expr.table)
8080
write("(")
8181

8282
for ((i, assignment) in expr.assignments.withIndex()) {
@@ -98,7 +98,7 @@ public open class MySqlFormatter(
9898

9999
protected open fun visitBulkInsert(expr: BulkInsertExpression): BulkInsertExpression {
100100
writeKeyword("insert into ")
101-
visitTable(expr.table.copy(tableAlias = null))
101+
visitTable(expr.table)
102102
write("(")
103103

104104
for ((i, assignment) in expr.assignments[0].withIndex()) {

ktorm-support-postgresql/src/main/kotlin/org/ktorm/support/postgresql/BulkInsert.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public fun <T : BaseTable<*>> Database.bulkInsert(
129129
* ```sql
130130
* insert into t_employee (id, name, job, salary, hire_date, department_id)
131131
* values (?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?)
132-
* on conflict (id) do update set salary = salary + ?
132+
* on conflict (id) do update set salary = t_employee.salary + ?
133133
* ```
134134
*
135135
* @since 3.3.0

ktorm-support-postgresql/src/main/kotlin/org/ktorm/support/postgresql/InsertOrUpdate.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public data class InsertOrUpdateExpression(
6868
*
6969
* ```sql
7070
* insert into t_employee (id, name, job, salary, hire_date, department_id) values (?, ?, ?, ?, ?, ?)
71-
* on conflict (id) do update set salary = salary + ?
71+
* on conflict (id) do update set salary = t_employee.salary + ?
7272
* ```
7373
*
7474
* @since 2.7

ktorm-support-postgresql/src/main/kotlin/org/ktorm/support/postgresql/PostgreSqlDialect.kt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ public open class PostgreSqlFormatter(
6767
return result as ScalarExpression<T>
6868
}
6969

70+
override fun visitTable(expr: TableExpression): TableExpression {
71+
if (expr.catalog != null && expr.catalog!!.isNotBlank()) {
72+
write("${expr.catalog!!.quoted}.")
73+
}
74+
if (expr.schema != null && expr.schema!!.isNotBlank()) {
75+
write("${expr.schema!!.quoted}.")
76+
}
77+
78+
write("${expr.name.quoted} ")
79+
80+
if (expr.tableAlias != null && expr.tableAlias!!.isNotBlank()) {
81+
writeKeyword("as ") // The 'as' keyword is required for update statements in PostgreSQL.
82+
write("${expr.tableAlias!!.quoted} ")
83+
}
84+
85+
return expr
86+
}
87+
7088
override fun writePagination(expr: QueryExpression) {
7189
newLine(Indentation.SAME)
7290

@@ -179,24 +197,6 @@ public open class PostgreSqlFormatter(
179197
return expr
180198
}
181199

182-
override fun visitColumnAssignments(
183-
original: List<ColumnAssignmentExpression<*>>
184-
): List<ColumnAssignmentExpression<*>> {
185-
for ((i, assignment) in original.withIndex()) {
186-
if (i > 0) {
187-
removeLastBlank()
188-
write(", ")
189-
}
190-
191-
checkColumnName(assignment.column.name)
192-
write("${assignment.column.name.quoted} ")
193-
write("= ")
194-
visit(assignment.expression)
195-
}
196-
197-
return original
198-
}
199-
200200
private fun writeColumnNames(columns: List<ColumnExpression<*>>) {
201201
write("(")
202202

ktorm-support-postgresql/src/test/kotlin/org/ktorm/support/postgresql/PostgreSqlTest.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import org.ktorm.schema.ColumnDeclaring
1616
import org.ktorm.schema.Table
1717
import org.ktorm.schema.int
1818
import org.ktorm.schema.varchar
19-
import org.postgresql.util.PSQLException
2019
import org.testcontainers.containers.PostgreSQLContainer
2120
import java.time.LocalDate
2221
import java.util.concurrent.ExecutionException
@@ -120,20 +119,18 @@ class PostgreSqlTest : BaseTest() {
120119
set(it.salary, 1000)
121120
set(it.hireDate, LocalDate.now())
122121
set(it.departmentId, 1)
123-
124-
onDuplicateKey {
122+
onConflict {
125123
set(it.salary, it.salary + 1000)
126124
}
127125
}
128-
database.insertOrUpdate(Employees) {
126+
database.insertOrUpdate(Employees.aliased("t")) {
129127
set(it.id, 5)
130128
set(it.name, "vince")
131129
set(it.job, "engineer")
132130
set(it.salary, 1000)
133131
set(it.hireDate, LocalDate.now())
134132
set(it.departmentId, 1)
135-
136-
onDuplicateKey(it.id) {
133+
onConflict {
137134
set(it.salary, it.salary + 1000)
138135
}
139136
}
@@ -166,7 +163,7 @@ class PostgreSqlTest : BaseTest() {
166163

167164
@Test
168165
fun testBulkInsertOrUpdate() {
169-
database.bulkInsertOrUpdate(Employees.aliased("t")) {
166+
database.bulkInsertOrUpdate(Employees) {
170167
item {
171168
set(it.id, 1)
172169
set(it.name, "vince")

0 commit comments

Comments
 (0)