Skip to content

Commit

Permalink
optReference column should allow update { it[column] = nullableValue }
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapac committed Dec 4, 2021
1 parent 621dc5b commit bd0bdab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,13 @@ abstract class UpdateBuilder<out T>(type: StatementType, targets: List<Table>) :
}

@JvmName("setWithEntityIdValue")
operator fun <S : Comparable<S>, ID : EntityID<S>, E : S?> set(column: Column<ID>, value: E) {
require(column.columnType.nullable || (value != null && !value.equals(Op.NULL))) {
"Trying to set null to not nullable column $column"
}
operator fun <S : Comparable<S>, E : S, ID : EntityID<E>?> set(column: Column<ID>, value: S) {
column.columnType.validateValueBeforeUpdate(value)
values[column] = value
}

@JvmName("setWithEntityIdExpression")
operator fun <S, ID : EntityID<S>, E : Expression<S>> set(column: Column<ID>, value: E) {
operator fun <S : Any?, ID : EntityID<S>, E : Expression<S>> set(column: Column<ID>, value: E) {
require(column.columnType.nullable || value != Op.NULL) {
"Trying to set null to not nullable column $column"
}
Expand All @@ -58,7 +55,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> set(column: CompositeColumn<S>, value: S) {
open operator fun <S : Any> 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 @@ -11,6 +11,7 @@ import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.shared.*
import org.jetbrains.exposed.sql.tests.shared.entities.EntityTests
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.vendors.MysqlDialect
import org.junit.Test
Expand All @@ -19,6 +20,7 @@ import java.sql.SQLException
import java.util.*
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.fail

class InsertTests : DatabaseTestsBase() {
Expand Down Expand Up @@ -520,4 +522,27 @@ class InsertTests : DatabaseTestsBase() {
}
}
}

@Test fun `test optReference allows null values`() {
withTables(EntityTests.Posts) {
val id1 = EntityTests.Posts.insertAndGetId {
it[board] = null
it[category] = null
}

val inserted1 = EntityTests.Posts.select { EntityTests.Posts.id eq id1 }.single()
assertNull(inserted1[EntityTests.Posts.board])
assertNull(inserted1[EntityTests.Posts.category])

val boardId = EntityTests.Boards.insertAndGetId {
it[name] = UUID.randomUUID().toString()
}

val id2 = EntityTests.Posts.insertAndGetId {
it[board] = Op.nullOp()
it[category] = null
it[board] = boardId.value
}
}
}
}

0 comments on commit bd0bdab

Please sign in to comment.