Skip to content

Commit

Permalink
ColumnWithTransform to support nullable columns
Browse files Browse the repository at this point in the history
  • Loading branch information
oharaandrew314 committed Feb 11, 2023
1 parent fccdb92 commit b225c41
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
12 changes: 8 additions & 4 deletions exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/Entity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.jetbrains.exposed.sql.transactions.TransactionManager
import kotlin.properties.Delegates
import kotlin.reflect.KProperty

open class ColumnWithTransform<TColumn, TReal>(
open class ColumnWithTransform<TColumn: Any?, TReal: Any?>(
val column: Column<TColumn>,
val toColumn: (TReal) -> TColumn,
toReal: (TColumn) -> TReal,
Expand Down Expand Up @@ -221,12 +221,16 @@ open class Entity<ID : Comparable<ID>>(val id: EntityID<ID>) {
}
}

operator fun <TColumn, TReal> ColumnWithTransform<TColumn, TReal>.getValue(o: Entity<ID>, desc: KProperty<*>): TReal =
toReal(column.getValue(o, desc))
operator fun <TColumn: Any?, TReal: Any?> ColumnWithTransform<TColumn, TReal>.getValue(o: Entity<ID>, desc: KProperty<*>): TReal? =
column.getValue(o, desc)?.let(toReal)

operator fun <TColumn, TReal> ColumnWithTransform<TColumn, TReal>.setValue(o: Entity<ID>, desc: KProperty<*>, value: TReal) {
operator fun <TColumn: Any?, TReal: Any?> ColumnWithTransform<TColumn, TReal>.setValue(o: Entity<ID>, desc: KProperty<*>, value: TReal) {
column.setValue(o, desc, toColumn(value))
}
@JvmName("setValueNullable")
operator fun <TColumn: Any?, TReal: Any> ColumnWithTransform<TColumn, TReal>.setValue(o: Entity<ID>, desc: KProperty<*>, value: TReal?) {
column.setValue(o, desc, value?.let(toColumn) as TColumn)
}

infix fun <TID : Comparable<TID>, Target : Entity<TID>> EntityClass<TID, Target>.via(table: Table): InnerTableLink<ID, Entity<ID>, TID, Target> =
InnerTableLink(table, this@Entity.id.table, this@via)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import org.junit.Test

object TransformTables {
object Transformations : IntIdTable() {
val value = varchar("name", 50)
val value = varchar("value", 50)
}
object NullableTransformations: IntIdTable() {
val value = varchar("nullable", 50).nullable()
}
class Transformation(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Transformation>(Transformations)
Expand All @@ -21,6 +24,13 @@ object TransformTables {
toReal = { it.replace("transformed-", "") }
)
}
class NullableTransformation(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<NullableTransformation>(NullableTransformations)
var value by NullableTransformations.value.transform(
toColumn = { "transformed-$it" },
toReal = { it?.replace("transformed-", "") }
)
}
}

class ColumnWithTransformTest: DatabaseTestsBase() {
Expand All @@ -39,4 +49,32 @@ class ColumnWithTransformTest: DatabaseTestsBase() {
assertEquals("transformed-stuff", row[TransformTables.Transformations.value])
}
}

@Test fun `set and get nullable value - while present`() {
withTables(TransformTables.NullableTransformations) {
val entity = TransformTables.NullableTransformation.new {
value = "stuff"
}

assertEquals("stuff", entity.value)

val row = TransformTables.NullableTransformations.select(Op.TRUE)
.first()

assertEquals("transformed-stuff", row[TransformTables.NullableTransformations.value])
}
}

@Test fun `set and get nullable value - while absent`() {
withTables(TransformTables.NullableTransformations) {
val entity = TransformTables.NullableTransformation.new {}

assertEquals(null, entity.value)

val row = TransformTables.NullableTransformations.select(Op.TRUE)
.first()

assertEquals(null, row[TransformTables.NullableTransformations.value])
}
}
}

0 comments on commit b225c41

Please sign in to comment.