Skip to content

Commit db38825

Browse files
add global bulk isnert or update
1 parent a4890fa commit db38825

File tree

1 file changed

+77
-36
lines changed
  • ktorm-support-postgresql/src/main/kotlin/org/ktorm/support/postgresql

1 file changed

+77
-36
lines changed

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

Lines changed: 77 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ internal val Database.Companion.global: Database get() {
5252
* set(it.salary, 1000)
5353
* set(it.hireDate, LocalDate.now())
5454
* set(it.departmentId, 1)
55-
* onDuplicateKey {
55+
* onConflict {
5656
* set(it.salary, it.salary + 900)
5757
* }
5858
* }
@@ -62,7 +62,7 @@ internal val Database.Companion.global: Database get() {
6262
*
6363
* ```sql
6464
* insert into t_employee (id, name, job, salary, hire_date, department_id) values (?, ?, ?, ?, ?, ?)
65-
* on conflict (id) do update set salary = t_employee.salary + ?
65+
* on conflict (id) do update set salary = salary + ?
6666
* ```
6767
*
6868
* @param block the DSL block used to construct the expression.
@@ -73,49 +73,90 @@ public fun <T : BaseTable<*>> T.insertOrUpdate(block: InsertOrUpdateStatementBui
7373
}
7474

7575
/**
76-
* Construct a bulk insert-or-update expression in the given closure, then execute it and return the effected
77-
* row count.
76+
* Construct a bulk insert expression in the given closure, then execute it and return the
77+
* effected row count.
7878
*
79-
* The usage is almost the same as [batchInsert], but this function is implemented by generating a special SQL
80-
* using PostgreSQL's bulk insert (with on conflict) syntax, instead of based on JDBC batch operations.
79+
* The usage is almost the same as [batchInsert], but this function is implemented by generating a
80+
* special SQL using PostgreSQL's bulk insert syntax, instead of based on JDBC batch operations.
8181
* For this reason, its performance is much better than [batchInsert].
8282
*
83-
* The generated SQL is like: `insert into table (column1, column2) values (?, ?), (?, ?), (?, ?)... ON
84-
* CONFLICT (...) DO NOTHING/UPDATE SET ...`.
83+
* The generated SQL is like: `insert into table (column1, column2) values (?, ?), (?, ?), (?, ?)...`.
8584
*
8685
* Usage:
8786
*
8887
* ```kotlin
89-
* database.bulkInsert(Employees) {
90-
* item {
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)
97-
* }
98-
* item {
99-
* set(it.id, 5)
100-
* set(it.name, "vince")
101-
* set(it.job, "engineer")
102-
* set(it.salary, 1000)
103-
* set(it.hireDate, LocalDate.now())
104-
* set(it.departmentId, 1)
105-
* }
106-
*
107-
* onDuplicateKey(Employees.id) {
108-
* // Or leave this empty to simply ignore without updating (do nothing)
109-
* set(it.salary, it.salary + 900)
110-
* }
111-
* }
88+
* Employees.bulkInsert {
89+
* item {
90+
* set(it.id, 1)
91+
* set(it.name, "vince")
92+
* set(it.job, "engineer")
93+
* set(it.salary, 1000)
94+
* set(it.hireDate, LocalDate.now())
95+
* set(it.departmentId, 1)
96+
* }
97+
* item {
98+
* set(it.id, 5)
99+
* set(it.name, "vince")
100+
* set(it.job, "engineer")
101+
* set(it.salary, 1000)
102+
* set(it.hireDate, LocalDate.now())
103+
* set(it.departmentId, 1)
104+
* }
105+
* }
112106
* ```
113107
*
114-
* @param block the DSL block, extension function of [BulkInsertStatementBuilder],
115-
* used to construct the expression.
108+
* @since 3.3.0
109+
* @param block the DSL block, extension function of [BulkInsertStatementBuilder], used to construct the expression.
116110
* @return the effected row count.
117111
* @see batchInsert
118112
*/
119-
//public fun <T : BaseTable<*>> T.bulkInsert(block: BulkInsertStatementBuilder<T>.() -> Unit): Int {
120-
// return Database.global.bulkInsert(this, block)
121-
//}
113+
public fun <T : BaseTable<*>> T.bulkInsert(block: BulkInsertStatementBuilder<T>.() -> Unit): Int {
114+
return Database.global.bulkInsert(this, block)
115+
}
116+
117+
/**
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.
120+
*
121+
* Usage:
122+
*
123+
* ```kotlin
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+
* }
141+
* onConflict {
142+
* set(it.salary, it.salary + 900)
143+
* }
144+
* }
145+
* ```
146+
*
147+
* Generated SQL:
148+
*
149+
* ```sql
150+
* insert into t_employee (id, name, job, salary, hire_date, department_id)
151+
* values (?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?)
152+
* on conflict (id) do update set salary = salary + ?
153+
* ```
154+
*
155+
* @since 3.3.0
156+
* @param block the DSL block used to construct the expression.
157+
* @return the effected row count.
158+
* @see bulkInsert
159+
*/
160+
public fun <T : BaseTable<*>> T.bulkInsertOrUpdate(block: BulkInsertOrUpdateStatementBuilder<T>.() -> Unit): Int {
161+
return Database.global.bulkInsertOrUpdate(this, block)
162+
}

0 commit comments

Comments
 (0)