Skip to content

Commit dc5fb4d

Browse files
postgresql: check empty insert & update #457
1 parent cf6b84c commit dc5fb4d

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.ktorm.support.postgresql
1818

19-
import org.ktorm.database.CachedRowSet
2019
import org.ktorm.database.Database
2120
import org.ktorm.database.asIterable
2221
import org.ktorm.dsl.AssignmentsBuilder
@@ -97,8 +96,7 @@ public data class BulkInsertExpression(
9796
public 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(
145143
public 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()) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ private fun <T : BaseTable<*>> buildInsertOrUpdateExpression(
258258
table: T, returning: List<Column<*>>, block: InsertOrUpdateStatementBuilder.(T) -> Unit
259259
): InsertOrUpdateExpression {
260260
val builder = InsertOrUpdateStatementBuilder().apply { block(table) }
261+
if (builder.assignments.isEmpty()) {
262+
throw IllegalArgumentException("There are no columns to insert in the statement.")
263+
}
261264

262265
val conflictColumns = builder.conflictColumns.ifEmpty { table.primaryKeys }
263266
if (conflictColumns.isEmpty()) {
@@ -404,6 +407,9 @@ private fun <T : BaseTable<*>> Database.insertReturningRow(
404407
table: T, returning: List<Column<*>>, block: AssignmentsBuilder.(T) -> Unit
405408
): CachedRowSet {
406409
val builder = PostgreSqlAssignmentsBuilder().apply { block(table) }
410+
if (builder.assignments.isEmpty()) {
411+
throw IllegalArgumentException("There are no columns to insert in the statement.")
412+
}
407413

408414
val expression = InsertOrUpdateExpression(
409415
table = table.asExpression(),

0 commit comments

Comments
 (0)