From 2e3591acb583b4769d12796418b3dd8e850daf78 Mon Sep 17 00:00:00 2001 From: Jocelyne Date: Mon, 27 May 2024 18:11:25 +0200 Subject: [PATCH] fix: EXPOSED-391 Cannot map columns to different types anymore Allow mapping a databade type to a different type in Kotlin code --- .../exposed/sql/SQLExpressionBuilder.kt | 17 +----- .../shared/types/BooleanColumnTypeTests.kt | 56 +++++++++++++++++++ 2 files changed, 58 insertions(+), 15 deletions(-) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SQLExpressionBuilder.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SQLExpressionBuilder.kt index 7d0e6702a0..4a5a382431 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SQLExpressionBuilder.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SQLExpressionBuilder.kt @@ -921,21 +921,8 @@ interface ISqlExpressionBuilder { /** Returns the specified [value] as a query parameter of type [T]. */ @Suppress("UNCHECKED_CAST") - fun ExpressionWithColumnType.wrap(value: T): QueryParameter = 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) // String value should inherit from column - else -> QueryParameter(value, columnType as IColumnType) - } as QueryParameter + fun ExpressionWithColumnType.wrap(value: T): QueryParameter = + QueryParameter(value, columnType as IColumnType) /** Returns the specified [value] as a literal of type [T]. */ @Suppress("UNCHECKED_CAST", "ComplexMethod") diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/BooleanColumnTypeTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/BooleanColumnTypeTests.kt index ef46ddac73..582f6bcdba 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/BooleanColumnTypeTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/BooleanColumnTypeTests.kt @@ -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() { + 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 = + registerColumn(name, CharBooleanColumnType()) }