Skip to content

Commit

Permalink
fix: EXPOSED-391 Cannot map columns to different types anymore
Browse files Browse the repository at this point in the history
Allow mapping a databade type to a different type in Kotlin code
  • Loading branch information
joc-a committed May 29, 2024
1 parent 4658bb7 commit 2e3591a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -921,21 +921,8 @@ interface ISqlExpressionBuilder {

/** Returns the specified [value] as a query parameter of type [T]. */
@Suppress("UNCHECKED_CAST")
fun <T, S : T?> ExpressionWithColumnType<in S>.wrap(value: T): QueryParameter<T> = when (value) {
is Boolean -> booleanParam(value)
is Byte -> byteParam(value)
is UByte -> ubyteParam(value)
is Short -> shortParam(value)
is UShort -> ushortParam(value)
is Int -> intParam(value)
is UInt -> uintParam(value)
is Long -> longParam(value)
is ULong -> ulongParam(value)
is Float -> floatParam(value)
is Double -> doubleParam(value)
is String -> QueryParameter(value, columnType as IColumnType<T & Any>) // String value should inherit from column
else -> QueryParameter(value, columnType as IColumnType<T & Any>)
} as QueryParameter<T>
fun <T, S : T?> ExpressionWithColumnType<in S>.wrap(value: T): QueryParameter<T> =
QueryParameter(value, columnType as IColumnType<T & Any>)

/** Returns the specified [value] as a literal of type [T]. */
@Suppress("UNCHECKED_CAST", "ComplexMethod")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,60 @@ class BooleanColumnTypeTests : DatabaseTestsBase() {
assertEquals(idTrue, resultTrue?.get(BooleanTable.id))
}
}

@Test
fun testCustomCharBooleanColumnType() {
val tester = object : Table("tester") {
val charBooleanColumn = charBoolean("charBooleanColumn")
val charBooleanColumnWithDefault = charBoolean("charBooleanColumnWithDefault")
.default(false)
}

withDb {
try {
SchemaUtils.create(tester)

tester.insert {
it[charBooleanColumn] = true
}

assertEquals(
1,
tester.select(tester.charBooleanColumn)
.where { tester.charBooleanColumn eq true }
.andWhere { tester.charBooleanColumnWithDefault eq false }
.count()
)
} finally {
SchemaUtils.drop(tester)
}
}
}

class CharBooleanColumnType(
private val characterColumnType: VarCharColumnType = VarCharColumnType(1),
) : ColumnType<Boolean>() {
override fun sqlType(): String = characterColumnType.preciseType()

override fun valueFromDB(value: Any): Boolean =
when (characterColumnType.valueFromDB(value)) {
"Y" -> true
else -> false
}

override fun valueToDB(value: Boolean?): Any? =
characterColumnType.valueToDB(value.toChar().toString())

override fun nonNullValueToString(value: Boolean): String =
value.toChar().toString()

private fun Boolean?.toChar() = when (this) {
true -> 'Y'
false -> 'N'
else -> ' '
}
}

fun Table.charBoolean(name: String): Column<Boolean> =
registerColumn(name, CharBooleanColumnType())
}

0 comments on commit 2e3591a

Please sign in to comment.