Skip to content

Commit

Permalink
Batch insert throws irrelevant exception #741
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapac committed Dec 27, 2019
1 parent 142a84e commit f6b0bee
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,25 @@ class DefaultsTest : DatabaseTestsBase() {
}

@Test
fun testRawBatchInsertFails02() {
fun testBatchInsertNotFails01() {
withTables(TableWithDBDefault) {
TableWithDBDefault.batchInsert(initBatch) { foo ->
foo(this)
}
}
}

@Test
fun testBatchInsertFails01() {
withTables(TableWithDBDefault) {
expectException<BatchDataInconsistentException> {
TableWithDBDefault.batchInsert(listOf(1)) {
this[TableWithDBDefault.t1] = LocalDateTime.now()
}
}
}
}

@Test
fun testDefaults01() {
val currentDT = CurrentDateTime()
Expand Down

0 comments on commit f6b0bee

Please sign in to comment.