mirror of
https://github.com/jlengrand/Exposed.git
synced 2026-03-10 08:11:20 +00:00
Batch insert throws irrelevant exception #741
This commit is contained in:
@@ -104,6 +104,9 @@ fun <T:Table, E:Any> T.batchInsert(data: Iterable<E>, 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) {
|
||||
|
||||
@@ -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<Column<*>>()
|
||||
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<List<Pair<Column<*>, 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 }
|
||||
}
|
||||
|
||||
|
||||
@@ -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<BatchDataInconsistentException> {
|
||||
TableWithDBDefault.batchInsert(listOf(1)) {
|
||||
this[TableWithDBDefault.t1] = LocalDateTime.now()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDefaults01() {
|
||||
val currentDT = CurrentDateTime()
|
||||
|
||||
Reference in New Issue
Block a user