Skip to content

Commit

Permalink
fix: EXPOSED-29 Cannot set nullable composite column in InsertStatement
Browse files Browse the repository at this point in the history
Remove the upper bound generic constraint for the function `set(column: CompositeColumn<S>, value: S)`

https://kotlinlang.org/docs/generics.html#upper-bounds:~:text=The%20default%20upper%20bound%20(if%20there%20was%20none%20specified)%20is%20Any%3F
  • Loading branch information
joc-a committed Apr 26, 2023
1 parent c32e147 commit 950627e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class UpdateBuilder<out T>(type: StatementType, targets: List<Table>) :
}

@LowPriorityInOverloadResolution
open operator fun < S> set(column: Column<S>, value: S) {
open operator fun <S> set(column: Column<S>, value: S) {
require(column.columnType.nullable || (value != null && value !is Op.NULL)) {
"Trying to set null to not nullable column $column"
}
Expand Down Expand Up @@ -66,7 +66,7 @@ abstract class UpdateBuilder<out T>(type: StatementType, targets: List<Table>) :

open operator fun <S> set(column: Column<S>, value: Query) = update(column, wrapAsExpression(value))

open operator fun <S : Any> set(column: CompositeColumn<S>, value: S) {
open operator fun <S> set(column: CompositeColumn<S>, value: S) {
column.getRealColumnsWithValues(value).forEach { (realColumn, itsValue) -> set(realColumn as Column<Any?>, itsValue) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ open class MoneyBaseTest : DatabaseTestsBase() {
}
}

@Test
fun testNullableCompositeColumnInsertAndSelect() {
val table = object : IntIdTable("Table") {
val composite_money = compositeMoney(8, AMOUNT_SCALE, "composite_money").nullable()
}

val usdAmount = Money.of(BigDecimal.TEN, "USD")

withTables(table) {
val id = table.insertAndGetId {
it[composite_money] = usdAmount
}

val resultRow = table.select { table.id.eq(id) }.single()
val result = resultRow[table.composite_money]

assertEquals(usdAmount, result)
}
}

private fun testInsertedAndSelect(toInsert: Money?) {
withTables(Account) {
val accountID = Account.insertAndGetId {
Expand Down

0 comments on commit 950627e

Please sign in to comment.