Skip to content

Commit 140c900

Browse files
support excluded table for bulk insert
1 parent 179f27d commit 140c900

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public data class ColumnExpression<T : Any>(
332332
val table: TableExpression?,
333333
val name: String,
334334
override val sqlType: SqlType<T>,
335-
override val isLeafNode: Boolean = true,
335+
override val isLeafNode: Boolean = false,
336336
override val extraProperties: Map<String, Any> = emptyMap()
337337
) : ScalarExpression<T>()
338338

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public abstract class SqlFormatter(
176176

177177
protected val SqlExpression.removeBrackets: Boolean get() {
178178
return isLeafNode
179+
|| this is ColumnExpression<*>
179180
|| this is FunctionExpression<*>
180181
|| this is AggregateExpression<*>
181182
|| this is ExistsExpression

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.ktorm.expression.FunctionExpression
2525
import org.ktorm.expression.SqlExpression
2626
import org.ktorm.expression.TableExpression
2727
import org.ktorm.schema.BaseTable
28-
import org.ktorm.schema.ColumnDeclaring
28+
import org.ktorm.schema.Column
2929

3030
/**
3131
* Bulk insert expression, represents a bulk insert statement in MySQL.
@@ -127,20 +127,20 @@ public class BulkInsertStatementBuilder<T : BaseTable<*>>(internal val table: T)
127127
}
128128

129129
/**
130-
* DSL builder for bulk insert assignments.
130+
* DSL builder for bulk insert on duplicate key clause.
131131
*/
132132
@KtormDsl
133133
public class BulkInsertOnDuplicateKeyClauseBuilder : MySqlAssignmentsBuilder() {
134134

135135
/**
136136
* Use VALUES() function in a ON DUPLICATE KEY UPDATE clause.
137137
*/
138-
public fun <T : Any> values(expr: ColumnDeclaring<T>): FunctionExpression<T> {
139-
// values(expr)
138+
public fun <T : Any> values(column: Column<T>): FunctionExpression<T> {
139+
// values(column)
140140
return FunctionExpression(
141141
functionName = "values",
142-
arguments = listOf(expr.asExpression()),
143-
sqlType = expr.sqlType
142+
arguments = listOf(column.asExpression()),
143+
sqlType = column.sqlType
144144
)
145145
}
146146
}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,28 @@ public class BulkInsertOrUpdateStatementBuilder<T : BaseTable<*>>(table: T) : Bu
195195
/**
196196
* Specify the update assignments while any key conflict exists.
197197
*/
198-
public fun onConflict(vararg columns: Column<*>, block: AssignmentsBuilder.() -> Unit) {
199-
val builder = PostgreSqlAssignmentsBuilder().apply(block)
198+
public fun onConflict(vararg columns: Column<*>, block: BulkInsertOrUpdateOnConflictClauseBuilder.() -> Unit) {
199+
val builder = BulkInsertOrUpdateOnConflictClauseBuilder().apply(block)
200200
updateAssignments += builder.assignments
201201
conflictColumns += columns
202202
}
203203
}
204+
205+
/**
206+
* DSL builder for bulk insert or update on conflict clause.
207+
*/
208+
@KtormDsl
209+
public class BulkInsertOrUpdateOnConflictClauseBuilder : PostgreSqlAssignmentsBuilder() {
210+
211+
/**
212+
* Reference the 'EXCLUDED' table in a ON CONFLICT clause.
213+
*/
214+
public fun <T : Any> excluded(column: Column<T>): ColumnExpression<T> {
215+
// excluded.name
216+
return ColumnExpression(
217+
table = TableExpression(name = "excluded"),
218+
name = column.name,
219+
sqlType = column.sqlType
220+
)
221+
}
222+
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class PostgreSqlTest : BaseTest() {
167167
item {
168168
set(it.id, 1)
169169
set(it.name, "vince")
170-
set(it.job, "engineer")
170+
set(it.job, "trainee")
171171
set(it.salary, 1000)
172172
set(it.hireDate, LocalDate.now())
173173
set(it.departmentId, 1)
@@ -181,12 +181,23 @@ class PostgreSqlTest : BaseTest() {
181181
set(it.departmentId, 1)
182182
}
183183
onConflict(it.id) {
184+
set(it.job, it.job)
185+
set(it.hireDate, excluded(it.hireDate))
184186
set(it.salary, it.salary + 1000)
185187
}
186188
}
187189

188-
assert(database.employees.find { it.id eq 1 }!!.salary == 1100L)
189-
assert(database.employees.find { it.id eq 5 }!!.salary == 1000L)
190+
database.employees.find { it.id eq 1 }!!.let {
191+
assert(it.job == "engineer")
192+
assert(it.hireDate == LocalDate.now())
193+
assert(it.salary == 1100L)
194+
}
195+
196+
database.employees.find { it.id eq 5 }!!.let {
197+
assert(it.job == "engineer")
198+
assert(it.hireDate == LocalDate.now())
199+
assert(it.salary == 1000L)
200+
}
190201
}
191202

192203
@Test

0 commit comments

Comments
 (0)