Skip to content

Commit cf6b84c

Browse files
mysql: check empty insert & update #457
1 parent 9745d05 commit cf6b84c

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

ktorm-core/src/main/kotlin/org/ktorm/dsl/Dml.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,17 @@ public fun <T : BaseTable<*>> Database.batchUpdate(
9090
block: BatchUpdateStatementBuilder<T>.() -> Unit
9191
): IntArray {
9292
val builder = BatchUpdateStatementBuilder(table).apply(block)
93-
94-
val expressions = builder.expressions.map { expr ->
93+
if (builder.expressions.isEmpty()) {
94+
throw IllegalArgumentException("There are no items in the batch operation.")
95+
}
96+
for (expr in builder.expressions) {
9597
if (expr.assignments.isEmpty()) {
9698
throw IllegalArgumentException("There are no columns to update in the statement.")
97-
} else {
98-
dialect.createExpressionVisitor(AliasRemover).visit(expr)
9999
}
100100
}
101101

102+
val expressions = builder.expressions.map { dialect.createExpressionVisitor(AliasRemover).visit(it) }
103+
102104
if (expressions.isEmpty()) {
103105
return IntArray(0)
104106
} else {
@@ -247,15 +249,17 @@ public fun <T : BaseTable<*>> Database.batchInsert(
247249
block: BatchInsertStatementBuilder<T>.() -> Unit
248250
): IntArray {
249251
val builder = BatchInsertStatementBuilder(table).apply(block)
250-
251-
val expressions = builder.expressions.map { expr ->
252+
if (builder.expressions.isEmpty()) {
253+
throw IllegalArgumentException("There are no items in the batch operation.")
254+
}
255+
for (expr in builder.expressions) {
252256
if (expr.assignments.isEmpty()) {
253257
throw IllegalArgumentException("There are no columns to insert in the statement.")
254-
} else {
255-
dialect.createExpressionVisitor(AliasRemover).visit(expr)
256258
}
257259
}
258260

261+
val expressions = builder.expressions.map { dialect.createExpressionVisitor(AliasRemover).visit(it) }
262+
259263
if (expressions.isEmpty()) {
260264
return IntArray(0)
261265
} else {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ public fun <T : BaseTable<*>> Database.bulkInsert(
9292
table: T, block: BulkInsertStatementBuilder<T>.() -> Unit
9393
): Int {
9494
val builder = BulkInsertStatementBuilder(table).apply(block)
95+
if (builder.assignments.isEmpty()) {
96+
throw IllegalArgumentException("There are no items in the bulk operation.")
97+
}
98+
for (assignments in builder.assignments) {
99+
if (assignments.isEmpty()) {
100+
throw IllegalArgumentException("There are no columns to insert in the statement.")
101+
}
102+
}
95103

96104
val expression = dialect.createExpressionVisitor(AliasRemover).visit(
97105
BulkInsertExpression(table.asExpression(), builder.assignments)
@@ -148,6 +156,14 @@ public fun <T : BaseTable<*>> Database.bulkInsertOrUpdate(
148156
table: T, block: BulkInsertOrUpdateStatementBuilder<T>.() -> Unit
149157
): Int {
150158
val builder = BulkInsertOrUpdateStatementBuilder(table).apply(block)
159+
if (builder.assignments.isEmpty()) {
160+
throw IllegalArgumentException("There are no items in the bulk operation.")
161+
}
162+
for (assignments in builder.assignments) {
163+
if (assignments.isEmpty()) {
164+
throw IllegalArgumentException("There are no columns to insert in the statement.")
165+
}
166+
}
151167

152168
val expression = dialect.createExpressionVisitor(AliasRemover).visit(
153169
BulkInsertExpression(table.asExpression(), builder.assignments, builder.updateAssignments)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public fun <T : BaseTable<*>> Database.insertOrUpdate(
7777
table: T, block: InsertOrUpdateStatementBuilder.(T) -> Unit
7878
): Int {
7979
val builder = InsertOrUpdateStatementBuilder().apply { block(table) }
80+
if (builder.assignments.isEmpty()) {
81+
throw IllegalArgumentException("There are no columns to insert in the statement.")
82+
}
8083

8184
val expression = dialect.createExpressionVisitor(AliasRemover).visit(
8285
InsertOrUpdateExpression(table.asExpression(), builder.assignments, builder.updateAssignments)

0 commit comments

Comments
 (0)