From f6b0beef7b0f3142d742bd747da4cc47bd4833c4 Mon Sep 17 00:00:00 2001 From: Tapac Date: Fri, 27 Dec 2019 12:43:39 +0300 Subject: [PATCH] Batch insert throws irrelevant exception #741 --- .../kotlin/org/jetbrains/exposed/sql/Queries.kt | 3 +++ .../exposed/sql/statements/BatchInsertStatement.kt | 14 ++++++++------ .../kotlin/org/jetbrains/exposed/DefaultsTest.kt | 13 ++++++++++++- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Queries.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Queries.kt index 1339fcf820..dc2b8da751 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Queries.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Queries.kt @@ -104,6 +104,9 @@ fun T.batchInsert(data: Iterable, ignore: Boolean = false, b if(removeLastData) validateLastBatch() } catch (e: BatchDataInconsistentException) { + if (this.data.size == 1) { + throw e + } val notTheFirstBatch = this.data.size > 1 if (notTheFirstBatch) { if (removeLastData) { diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/BatchInsertStatement.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/BatchInsertStatement.kt index ce084c3dcf..67fea8b649 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/BatchInsertStatement.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/BatchInsertStatement.kt @@ -44,26 +44,28 @@ open class BatchInsertStatement(table: Table, ignore: Boolean = false): InsertSt } internal open fun validateLastBatch() { + val tr = TransactionManager.current() val cantBeDefaulted = (allColumnsInDataSet - values.keys).filterNot { it.isDefaultable() } if (cantBeDefaulted.isNotEmpty()) { - val columnList = cantBeDefaulted.joinToString { TransactionManager.current().fullIdentity(it) } - throw BatchDataInconsistentException("Can't add new batch because columns: $columnList don't have client default values. DB defaults don't support in batch inserts") + val columnList = cantBeDefaulted.joinToString { tr.fullIdentity(it) } + throw BatchDataInconsistentException("Can't add a new batch because columns: $columnList don't have client default values. DB defaults don't support in batch inserts") } val requiredInTargets = (targets.flatMap { it.columns } - values.keys).filter { !it.isDefaultable() && !it.columnType.isAutoInc && it.dbDefaultValue == null && it.columnType !is EntityIDColumnType<*> } if (requiredInTargets.any()) { - throw BatchDataInconsistentException("Can't add new batch because columns: ${requiredInTargets.joinToString()} don't have default values. DB defaults don't support in batch inserts") + val columnList = requiredInTargets.joinToString { tr.fullIdentity(it) } + throw BatchDataInconsistentException("Can't add a new batch because columns: $columnList don't have default values. DB defaults don't support in batch inserts") } } private val allColumnsInDataSet = mutableSetOf>() - private fun allColumnsInDataSet() = allColumnsInDataSet + (data.lastOrNull()?.keys ?: error("No data provided for inserting into ${table.tableName}")) + private fun allColumnsInDataSet() = allColumnsInDataSet + (data.lastOrNull()?.keys ?: throw BatchDataInconsistentException("No data provided for inserting into ${table.tableName}")) override var arguments: List, Any?>>>? = null get() = field ?: run { - val nullableColumns = allColumnsInDataSet().filter { it.columnType.nullable } + val nullableColumns by lazy { allColumnsInDataSet().filter { it.columnType.nullable } } data.map { single -> val valuesAndDefaults = super.valuesAndDefaults(single) - (valuesAndDefaults + (nullableColumns - valuesAndDefaults.keys).associate { it to null }).toList().sortedBy { it.first } + (valuesAndDefaults + (nullableColumns - valuesAndDefaults.keys).associateWith { null }).toList().sortedBy { it.first } }.apply { field = this } } diff --git a/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt b/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt index d350885261..acb35c9869 100644 --- a/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt +++ b/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt @@ -118,7 +118,7 @@ class DefaultsTest : DatabaseTestsBase() { } @Test - fun testRawBatchInsertFails02() { + fun testBatchInsertNotFails01() { withTables(TableWithDBDefault) { TableWithDBDefault.batchInsert(initBatch) { foo -> foo(this) @@ -126,6 +126,17 @@ class DefaultsTest : DatabaseTestsBase() { } } + @Test + fun testBatchInsertFails01() { + withTables(TableWithDBDefault) { + expectException { + TableWithDBDefault.batchInsert(listOf(1)) { + this[TableWithDBDefault.t1] = LocalDateTime.now() + } + } + } + } + @Test fun testDefaults01() { val currentDT = CurrentDateTime()