1616
1717package org.ktorm.support.postgresql
1818
19- import org.ktorm.database.CachedRowSet
2019import org.ktorm.database.Database
2120import org.ktorm.database.asIterable
2221import org.ktorm.dsl.AssignmentsBuilder
@@ -97,8 +96,7 @@ public data class BulkInsertExpression(
9796public fun <T : BaseTable <* >> Database.bulkInsert (
9897 table : T , block : BulkInsertStatementBuilder <T >.(T ) -> Unit
9998): Int {
100- val builder = BulkInsertStatementBuilder (table).apply { block(table) }
101- val expression = BulkInsertExpression (table.asExpression(), builder.assignments)
99+ val expression = buildBulkInsertExpression(table, returning = emptyList(), block = block)
102100 return executeUpdate(expression)
103101}
104102
@@ -145,7 +143,8 @@ public fun <T : BaseTable<*>> Database.bulkInsert(
145143public fun <T : BaseTable <* >, C : Any > Database.bulkInsertReturning (
146144 table : T , returning : Column <C >, block : BulkInsertStatementBuilder <T >.(T ) -> Unit
147145): List <C ?> {
148- val rowSet = bulkInsertReturningRowSet(table, listOf (returning), block)
146+ val expression = buildBulkInsertExpression(table, listOf (returning), block)
147+ val (_, rowSet) = executeUpdateAndRetrieveKeys(expression)
149148 return rowSet.asIterable().map { row -> returning.sqlType.getResult(row, 1 ) }
150149}
151150
@@ -193,7 +192,8 @@ public fun <T : BaseTable<*>, C1 : Any, C2 : Any> Database.bulkInsertReturning(
193192 table : T , returning : Pair <Column <C1 >, Column <C2 >>, block : BulkInsertStatementBuilder <T >.(T ) -> Unit
194193): List <Pair <C1 ?, C2 ?>> {
195194 val (c1, c2) = returning
196- val rowSet = bulkInsertReturningRowSet(table, listOf (c1, c2), block)
195+ val expression = buildBulkInsertExpression(table, listOf (c1, c2), block)
196+ val (_, rowSet) = executeUpdateAndRetrieveKeys(expression)
197197 return rowSet.asIterable().map { row -> Pair (c1.sqlType.getResult(row, 1 ), c2.sqlType.getResult(row, 2 )) }
198198}
199199
@@ -241,7 +241,8 @@ public fun <T : BaseTable<*>, C1 : Any, C2 : Any, C3 : Any> Database.bulkInsertR
241241 table : T , returning : Triple <Column <C1 >, Column <C2 >, Column <C3 >>, block : BulkInsertStatementBuilder <T >.(T ) -> Unit
242242): List <Triple <C1 ?, C2 ?, C3 ?>> {
243243 val (c1, c2, c3) = returning
244- val rowSet = bulkInsertReturningRowSet(table, listOf (c1, c2, c3), block)
244+ val expression = buildBulkInsertExpression(table, listOf (c1, c2, c3), block)
245+ val (_, rowSet) = executeUpdateAndRetrieveKeys(expression)
245246 return rowSet.asIterable().map { row ->
246247 Triple (c1.sqlType.getResult(row, 1 ), c2.sqlType.getResult(row, 2 ), c3.sqlType.getResult(row, 3 ))
247248 }
@@ -250,19 +251,24 @@ public fun <T : BaseTable<*>, C1 : Any, C2 : Any, C3 : Any> Database.bulkInsertR
250251/* *
251252 * Bulk insert records to the table, returning row set.
252253 */
253- private fun <T : BaseTable <* >> Database. bulkInsertReturningRowSet (
254+ private fun <T : BaseTable <* >> buildBulkInsertExpression (
254255 table : T , returning : List <Column <* >>, block : BulkInsertStatementBuilder <T >.(T ) -> Unit
255- ): CachedRowSet {
256+ ): BulkInsertExpression {
256257 val builder = BulkInsertStatementBuilder (table).apply { block(table) }
258+ if (builder.assignments.isEmpty()) {
259+ throw IllegalArgumentException (" There are no items in the bulk operation." )
260+ }
261+ for (assignments in builder.assignments) {
262+ if (assignments.isEmpty()) {
263+ throw IllegalArgumentException (" There are no columns to insert in the statement." )
264+ }
265+ }
257266
258- val expression = BulkInsertExpression (
267+ return BulkInsertExpression (
259268 table = table.asExpression(),
260269 assignments = builder.assignments,
261270 returningColumns = returning.map { it.asExpression() }
262271 )
263-
264- val (_, rowSet) = executeUpdateAndRetrieveKeys(expression)
265- return rowSet
266272}
267273
268274/* *
@@ -487,6 +493,14 @@ private fun <T : BaseTable<*>> buildBulkInsertOrUpdateExpression(
487493 table : T , returning : List <Column <* >>, block : BulkInsertOrUpdateStatementBuilder <T >.(T ) -> Unit
488494): BulkInsertExpression {
489495 val builder = BulkInsertOrUpdateStatementBuilder (table).apply { block(table) }
496+ if (builder.assignments.isEmpty()) {
497+ throw IllegalArgumentException (" There are no items in the bulk operation." )
498+ }
499+ for (assignments in builder.assignments) {
500+ if (assignments.isEmpty()) {
501+ throw IllegalArgumentException (" There are no columns to insert in the statement." )
502+ }
503+ }
490504
491505 val conflictColumns = builder.conflictColumns.ifEmpty { table.primaryKeys }
492506 if (conflictColumns.isEmpty()) {
0 commit comments