Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: EXPOSED-292 Explicit nulls in insert with databaseGenerated() #1993

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class Column<T>(
it.foreignKey = this.foreignKey
it.defaultValueFun = this.defaultValueFun
it.dbDefaultValue = this.dbDefaultValue
it.isDatabaseGenerated = this.isDatabaseGenerated
}

override fun compareTo(other: Column<*>): Int = comparator.compare(this, other)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
newColumn.defaultValueFun = defaultValueFun
@Suppress("UNCHECKED_CAST")
newColumn.dbDefaultValue = dbDefaultValue as Expression<T?>?
newColumn.isDatabaseGenerated = isDatabaseGenerated
newColumn.columnType.nullable = true
return replaceColumn(this, newColumn)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ abstract class BaseBatchInsertStatement(

override var arguments: List<List<Pair<Column<*>, Any?>>>? = null
get() = field ?: run {
val nullableColumns by lazy { allColumnsInDataSet().filter { it.columnType.nullable } }
val nullableColumns by lazy {
allColumnsInDataSet().filter { it.columnType.nullable && !it.isDatabaseGenerated }
}
data.map { single ->
val valuesAndDefaults = super.valuesAndDefaults(single) as MutableMap
val nullableMap = (nullableColumns - valuesAndDefaults.keys).associateWith { null }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ open class InsertStatement<Key : Any>(

protected open var arguments: List<List<Pair<Column<*>, Any?>>>? = null
get() = field ?: run {
val nullableColumns = table.columns.filter { it.columnType.nullable }
val nullableColumns = table.columns.filter { it.columnType.nullable && !it.isDatabaseGenerated }
val valuesAndDefaults = valuesAndDefaults() as MutableMap
valuesAndDefaults.putAll((nullableColumns - valuesAndDefaults.keys).associateWith { null })
val result = valuesAndDefaults.toList().sortedBy { it.first }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.statements.BatchInsertStatement
import org.jetbrains.exposed.sql.statements.StatementType
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentTestDB
import org.jetbrains.exposed.sql.tests.inProperCase
import org.jetbrains.exposed.sql.tests.shared.assertEqualLists
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertFailAndRollback
Expand Down Expand Up @@ -612,4 +614,64 @@ class InsertTests : DatabaseTestsBase() {
assertEquals(1, numInserted)
}
}

@Test
fun testInsertIntoNullableGeneratedColumn() {
val generatedTable = object : IntIdTable("generated_table") {
val amount = integer("amount").nullable()
val computedAmount = integer("computed_amount").nullable().databaseGenerated()
}

withDb { testDb ->
try {
if (testDb == TestDB.ORACLE || testDb == TestDB.H2_ORACLE) {
// create sequence for primary key
exec(generatedTable.ddl.first(), explicitStatementType = StatementType.CREATE)
}

// Exposed does not currently support creation of generated columns
val computedName = generatedTable.computedAmount.name.inProperCase()
val computedType = generatedTable.computedAmount.columnType.sqlType()
val computation = "${generatedTable.amount.name.inProperCase()} + 1"
val generatedColumnDescription = when (testDb) {
TestDB.SQLSERVER -> "$computedName AS ($computation)"
TestDB.ORACLE, in TestDB.allH2TestDB -> "$computedName $computedType GENERATED ALWAYS AS ($computation)"
else -> "$computedName $computedType GENERATED ALWAYS AS ($computation) STORED"
}
exec(
"""CREATE TABLE ${addIfNotExistsIfSupported()}${generatedTable.tableName.inProperCase()} (
${generatedTable.id.descriptionDdl()},
${generatedTable.amount.descriptionDdl()},
$generatedColumnDescription
)
""".trimIndent(),
explicitStatementType = StatementType.CREATE
)

assertFailAndRollback("Generated columns are auto-derived and read-only") {
generatedTable.insert {
it[amount] = 99
it[computedAmount] = 100
}
}

generatedTable.insert {
it[amount] = 99
}

val result1 = generatedTable.selectAll().single()
assertEquals(result1[generatedTable.amount]?.plus(1), result1[generatedTable.computedAmount])

generatedTable.insert {
it[amount] = null
}

val result2 = generatedTable.selectAll().where { generatedTable.amount.isNull() }.single()
assertNull(result2[generatedTable.amount])
assertNull(result2[generatedTable.computedAmount])
} finally {
SchemaUtils.drop(generatedTable)
}
}
}
}
Loading