@@ -47,6 +47,9 @@ import java.sql.Statement
4747 */
4848public fun <T : BaseTable <* >> Database.update (table : T , block : UpdateStatementBuilder .(T ) -> Unit ): Int {
4949 val builder = UpdateStatementBuilder ().apply { block(table) }
50+ if (builder.assignments.isEmpty()) {
51+ throw IllegalArgumentException (" There are no columns to update in the statement." )
52+ }
5053
5154 val expression = dialect.createExpressionVisitor(AliasRemover ).visit(
5255 UpdateExpression (table.asExpression(), builder.assignments, builder.where?.asExpression())
@@ -87,7 +90,14 @@ public fun <T : BaseTable<*>> Database.batchUpdate(
8790 block : BatchUpdateStatementBuilder <T >.() -> Unit
8891): IntArray {
8992 val builder = BatchUpdateStatementBuilder (table).apply (block)
90- val expressions = builder.expressions.map { dialect.createExpressionVisitor(AliasRemover ).visit(it) }
93+
94+ val expressions = builder.expressions.map { expr ->
95+ if (expr.assignments.isEmpty()) {
96+ throw IllegalArgumentException (" There are no columns to update in the statement." )
97+ } else {
98+ dialect.createExpressionVisitor(AliasRemover ).visit(expr)
99+ }
100+ }
91101
92102 if (expressions.isEmpty()) {
93103 return IntArray (0 )
@@ -119,6 +129,10 @@ public fun <T : BaseTable<*>> Database.batchUpdate(
119129 */
120130public fun <T : BaseTable <* >> Database.insert (table : T , block : AssignmentsBuilder .(T ) -> Unit ): Int {
121131 val builder = AssignmentsBuilder ().apply { block(table) }
132+ if (builder.assignments.isEmpty()) {
133+ throw IllegalArgumentException (" There are no columns to insert in the statement." )
134+ }
135+
122136 val expression = dialect.createExpressionVisitor(AliasRemover ).visit(
123137 InsertExpression (table.asExpression(), builder.assignments)
124138 )
@@ -152,6 +166,10 @@ public fun <T : BaseTable<*>> Database.insert(table: T, block: AssignmentsBuilde
152166 */
153167public fun <T : BaseTable <* >> Database.insertAndGenerateKey (table : T , block : AssignmentsBuilder .(T ) -> Unit ): Any {
154168 val builder = AssignmentsBuilder ().apply { block(table) }
169+ if (builder.assignments.isEmpty()) {
170+ throw IllegalArgumentException (" There are no columns to insert in the statement." )
171+ }
172+
155173 val expression = dialect.createExpressionVisitor(AliasRemover ).visit(
156174 InsertExpression (table.asExpression(), builder.assignments)
157175 )
@@ -229,7 +247,14 @@ public fun <T : BaseTable<*>> Database.batchInsert(
229247 block : BatchInsertStatementBuilder <T >.() -> Unit
230248): IntArray {
231249 val builder = BatchInsertStatementBuilder (table).apply (block)
232- val expressions = builder.expressions.map { dialect.createExpressionVisitor(AliasRemover ).visit(it) }
250+
251+ val expressions = builder.expressions.map { expr ->
252+ if (expr.assignments.isEmpty()) {
253+ throw IllegalArgumentException (" There are no columns to insert in the statement." )
254+ } else {
255+ dialect.createExpressionVisitor(AliasRemover ).visit(expr)
256+ }
257+ }
233258
234259 if (expressions.isEmpty()) {
235260 return IntArray (0 )
@@ -242,6 +267,10 @@ public fun <T : BaseTable<*>> Database.batchInsert(
242267 * Insert the current [Query]'s results into the given table, useful when transfer data from a table to another table.
243268 */
244269public fun Query.insertTo (table : BaseTable <* >, vararg columns : Column <* >): Int {
270+ if (columns.isEmpty()) {
271+ throw IllegalArgumentException (" There are no columns to insert in the statement." )
272+ }
273+
245274 val expression = InsertFromQueryExpression (
246275 table = table.asExpression(),
247276 columns = columns.map { it.asExpression() },
@@ -335,7 +364,7 @@ public class UpdateStatementBuilder : AssignmentsBuilder() {
335364 */
336365@KtormDsl
337366public class BatchUpdateStatementBuilder <T : BaseTable <* >>(internal val table : T ) {
338- internal val expressions = ArrayList <SqlExpression >()
367+ internal val expressions = ArrayList <UpdateExpression >()
339368
340369 /* *
341370 * Add an update statement to the current batch operation.
@@ -353,7 +382,7 @@ public class BatchUpdateStatementBuilder<T : BaseTable<*>>(internal val table: T
353382 */
354383@KtormDsl
355384public class BatchInsertStatementBuilder <T : BaseTable <* >>(internal val table : T ) {
356- internal val expressions = ArrayList <SqlExpression >()
385+ internal val expressions = ArrayList <InsertExpression >()
357386
358387 /* *
359388 * Add an insert statement to the current batch operation.
0 commit comments