Skip to content

Commit dff1628

Browse files
mysql global bulk insert or update
1 parent 8df51a9 commit dff1628

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ public fun <T : BaseTable<*>> Database.bulkInsertOrUpdate(
158158
/**
159159
* DSL builder for bulk insert statements.
160160
*/
161+
@KtormDsl
161162
public class BulkInsertStatementBuilder<T : BaseTable<*>>(table: T) : BulkInsertOrUpdateStatementBuilder<T>(table) {
162163

163164
@Deprecated("This function will be removed in the future, please use bulkInsertOrUpdate instead of bulkInsert.")

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

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,40 @@ internal val Database.Companion.global: Database get() {
4040
}
4141
}
4242

43+
/**
44+
* Insert a record to the table, determining if there is a key conflict while it's being inserted, and automatically
45+
* performs an update if any conflict exists.
46+
*
47+
* Usage:
48+
*
49+
* ```kotlin
50+
* Employees.insertOrUpdate {
51+
* set(it.id, 1)
52+
* set(it.name, "vince")
53+
* set(it.job, "engineer")
54+
* set(it.salary, 1000)
55+
* set(it.hireDate, LocalDate.now())
56+
* set(it.departmentId, 1)
57+
* onDuplicateKey {
58+
* set(it.salary, it.salary + 900)
59+
* }
60+
* }
61+
* ```
62+
*
63+
* Generated SQL:
64+
*
65+
* ```sql
66+
* insert into t_employee (id, name, job, salary, hire_date, department_id) values (?, ?, ?, ?, ?, ?)
67+
* on duplicate key update salary = salary + ?
68+
* ```
69+
*
70+
* @param block the DSL block used to construct the expression.
71+
* @return the effected row count.
72+
*/
73+
public fun <T : BaseTable<*>> T.insertOrUpdate(block: InsertOrUpdateStatementBuilder.(T) -> Unit): Int {
74+
return Database.global.insertOrUpdate(this, block)
75+
}
76+
4377
/**
4478
* Construct a bulk insert expression in the given closure, then execute it and return the effected row count.
4579
*
@@ -81,19 +115,29 @@ public fun <T : BaseTable<*>> T.bulkInsert(block: BulkInsertStatementBuilder<T>.
81115
}
82116

83117
/**
84-
* Insert a record to the table, determining if there is a key conflict while it's being inserted, and automatically
85-
* performs an update if any conflict exists.
118+
* Bulk insert records to the table, determining if there is a key conflict while inserting each of them,
119+
* and automatically performs updates if any conflict exists.
86120
*
87121
* Usage:
88122
*
89123
* ```kotlin
90-
* Employees.insertOrUpdate {
91-
* set(it.id, 1)
92-
* set(it.name, "vince")
93-
* set(it.job, "engineer")
94-
* set(it.salary, 1000)
95-
* set(it.hireDate, LocalDate.now())
96-
* set(it.departmentId, 1)
124+
* Employees.bulkInsertOrUpdate {
125+
* item {
126+
* set(it.id, 1)
127+
* set(it.name, "vince")
128+
* set(it.job, "engineer")
129+
* set(it.salary, 1000)
130+
* set(it.hireDate, LocalDate.now())
131+
* set(it.departmentId, 1)
132+
* }
133+
* item {
134+
* set(it.id, 5)
135+
* set(it.name, "vince")
136+
* set(it.job, "engineer")
137+
* set(it.salary, 1000)
138+
* set(it.hireDate, LocalDate.now())
139+
* set(it.departmentId, 1)
140+
* }
97141
* onDuplicateKey {
98142
* set(it.salary, it.salary + 900)
99143
* }
@@ -103,15 +147,18 @@ public fun <T : BaseTable<*>> T.bulkInsert(block: BulkInsertStatementBuilder<T>.
103147
* Generated SQL:
104148
*
105149
* ```sql
106-
* insert into t_employee (id, name, job, salary, hire_date, department_id) values (?, ?, ?, ?, ?, ?)
150+
* insert into t_employee (id, name, job, salary, hire_date, department_id)
151+
* values (?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?)
107152
* on duplicate key update salary = salary + ?
108153
* ```
109154
*
155+
* @since 3.3.0
110156
* @param block the DSL block used to construct the expression.
111157
* @return the effected row count.
158+
* @see bulkInsert
112159
*/
113-
public fun <T : BaseTable<*>> T.insertOrUpdate(block: InsertOrUpdateStatementBuilder.(T) -> Unit): Int {
114-
return Database.global.insertOrUpdate(this, block)
160+
public fun <T : BaseTable<*>> T.bulkInsertOrUpdate(block: BulkInsertOrUpdateStatementBuilder<T>.() -> Unit): Int {
161+
return Database.global.bulkInsertOrUpdate(this, block)
115162
}
116163

117164
/**

0 commit comments

Comments
 (0)