Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: EXPOSED-391 Cannot map columns to different types anymore #2099

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}
Loading