|
| 1 | +/* |
| 2 | + * Copyright 2018-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.ktorm.support.postgresql |
| 18 | + |
| 19 | +import org.ktorm.database.Database |
| 20 | +import org.ktorm.dsl.AssignmentsBuilder |
| 21 | +import org.ktorm.dsl.KtormDsl |
| 22 | +import org.ktorm.dsl.batchInsert |
| 23 | +import org.ktorm.expression.ColumnAssignmentExpression |
| 24 | +import org.ktorm.expression.ColumnExpression |
| 25 | +import org.ktorm.expression.SqlExpression |
| 26 | +import org.ktorm.expression.TableExpression |
| 27 | +import org.ktorm.schema.BaseTable |
| 28 | +import org.ktorm.schema.Column |
| 29 | + |
| 30 | +/** |
| 31 | + * Bulk insert expression, represents a bulk insert statement in PostgreSQL. |
| 32 | + * |
| 33 | + * For example: `insert into table (column1, column2) values (?, ?), (?, ?), (?, ?)... ON |
| 34 | + * CONFLICT (...) DO NOTHING/UPDATE SET ...`. |
| 35 | + * |
| 36 | + * @property table the table to be inserted. |
| 37 | + * @property assignments column assignments of the bulk insert statement. |
| 38 | + * @property conflictTarget the index columns on which the conflict may happens. |
| 39 | + * @property updateAssignments the updated column assignments while key conflict exists. |
| 40 | + */ |
| 41 | +public data class BulkInsertExpression( |
| 42 | + val table: TableExpression, |
| 43 | + val assignments: List<List<ColumnAssignmentExpression<*>>>, |
| 44 | + val conflictTarget: List<ColumnExpression<*>>, |
| 45 | + val updateAssignments: List<ColumnAssignmentExpression<*>> = emptyList(), |
| 46 | + override val isLeafNode: Boolean = false, |
| 47 | + override val extraProperties: Map<String, Any> = emptyMap() |
| 48 | +) : SqlExpression() |
| 49 | + |
| 50 | +/** |
| 51 | + * Construct a bulk insert expression in the given closure, then execute it and return the |
| 52 | + * effected row count. |
| 53 | + * |
| 54 | + * The usage is almost the same as [batchInsert], but this function is implemented by generating a |
| 55 | + * special SQL using PostgreSQL's bulk insert syntax, instead of based on JDBC batch operations. |
| 56 | + * For this reason, its performance is much better than [batchInsert]. |
| 57 | + * |
| 58 | + * The generated SQL is like: `insert into table (column1, column2) values (?, ?), (?, ?), (?, ?)... ON |
| 59 | + * CONFLICT (...) DO NOTHING/UPDATE SET ...`. |
| 60 | + * |
| 61 | + * Usage: |
| 62 | + * |
| 63 | + * ```kotlin |
| 64 | + * database.bulkInsert(Employees) { |
| 65 | + * item { |
| 66 | + * set(it.id, 1) |
| 67 | + * set(it.name, "vince") |
| 68 | + * set(it.job, "engineer") |
| 69 | + * set(it.salary, 1000) |
| 70 | + * set(it.hireDate, LocalDate.now()) |
| 71 | + * set(it.departmentId, 1) |
| 72 | + * } |
| 73 | + * item { |
| 74 | + * set(it.id, 5) |
| 75 | + * set(it.name, "vince") |
| 76 | + * set(it.job, "engineer") |
| 77 | + * set(it.salary, 1000) |
| 78 | + * set(it.hireDate, LocalDate.now()) |
| 79 | + * set(it.departmentId, 1) |
| 80 | + * } |
| 81 | + * |
| 82 | + * onDuplicateKey(Employees.id) { |
| 83 | + * // Or leave this empty to simply ignore without updating (do nothing) |
| 84 | + * set(it.salary, it.salary + 900) |
| 85 | + * } |
| 86 | + * } |
| 87 | + * ``` |
| 88 | + * |
| 89 | + * @since 3.3.0 |
| 90 | + * @param table the table to be inserted. |
| 91 | + * @param block the DSL block, extension function of [BulkInsertStatementBuilder], |
| 92 | + * used to construct the expression. |
| 93 | + * @return the effected row count. |
| 94 | + * @see batchInsert |
| 95 | + */ |
| 96 | +public fun <T : BaseTable<*>> Database.bulkInsert( |
| 97 | + table: T, |
| 98 | + block: BulkInsertStatementBuilder<T>.() -> Unit |
| 99 | +): Int { |
| 100 | + val builder = BulkInsertStatementBuilder(table).apply(block) |
| 101 | + |
| 102 | + val expression = BulkInsertExpression( |
| 103 | + table = table.asExpression(), |
| 104 | + assignments = builder.assignments, |
| 105 | + conflictTarget = builder.conflictColumns.map { it.asExpression() }, |
| 106 | + updateAssignments = builder.updateAssignments |
| 107 | + ) |
| 108 | + |
| 109 | + return executeUpdate(expression) |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * DSL builder for bulk insert statements. |
| 114 | + */ |
| 115 | +@KtormDsl |
| 116 | +public class BulkInsertStatementBuilder<T : BaseTable<*>>(internal val table: T) { |
| 117 | + internal val assignments = ArrayList<List<ColumnAssignmentExpression<*>>>() |
| 118 | + internal val conflictColumns = ArrayList<Column<*>>() |
| 119 | + internal val updateAssignments = ArrayList<ColumnAssignmentExpression<*>>() |
| 120 | + |
| 121 | + /** |
| 122 | + * Add the assignments of a new row to the bulk insert. |
| 123 | + */ |
| 124 | + public fun item(block: AssignmentsBuilder.(T) -> Unit) { |
| 125 | + val builder = PostgreSqlAssignmentsBuilder() |
| 126 | + builder.block(table) |
| 127 | + |
| 128 | + if (assignments.isEmpty() |
| 129 | + || assignments[0].map { it.column.name } == builder.assignments.map { it.column.name } |
| 130 | + ) { |
| 131 | + assignments += builder.assignments |
| 132 | + } else { |
| 133 | + throw IllegalArgumentException("Every item in a batch operation must be the same.") |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Specify the update assignments while any key conflict exists. |
| 139 | + */ |
| 140 | + public fun onDuplicateKey(vararg columns: Column<*>, block: AssignmentsBuilder.(T) -> Unit) { |
| 141 | + val builder = PostgreSqlAssignmentsBuilder() |
| 142 | + builder.block(table) |
| 143 | + |
| 144 | + updateAssignments += builder.assignments |
| 145 | + conflictColumns += columns |
| 146 | + } |
| 147 | +} |
0 commit comments