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 all commits
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,38 +921,13 @@ 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")
fun <T, S : T?> ExpressionWithColumnType<S>.asLiteral(value: T): LiteralOp<T> = when (value) {
is Boolean -> booleanLiteral(value)
is Byte -> byteLiteral(value)
is UByte -> ubyteLiteral(value)
is Short -> shortLiteral(value)
is UShort -> ushortLiteral(value)
is Int -> intLiteral(value)
is UInt -> uintLiteral(value)
is Long -> longLiteral(value)
is ULong -> ulongLiteral(value)
is Float -> floatLiteral(value)
is Double -> doubleLiteral(value)
is String -> stringLiteral(value)
is ByteArray -> stringLiteral(value.toString(Charsets.UTF_8))
fun <T, S : T?> ExpressionWithColumnType<S>.asLiteral(value: T): LiteralOp<T> = when {
value is ByteArray && columnType is BasicBinaryColumnType -> stringLiteral(value.toString(Charsets.UTF_8))
else -> LiteralOp(columnType as IColumnType<T & Any>, value)
} as LiteralOp<T>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package org.jetbrains.exposed.sql.json

import org.jetbrains.exposed.sql.ComplexExpression
import org.jetbrains.exposed.sql.Expression
import org.jetbrains.exposed.sql.ExpressionWithColumnType
import org.jetbrains.exposed.sql.IColumnType
import org.jetbrains.exposed.sql.Op
import org.jetbrains.exposed.sql.QueryBuilder
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.asLiteral
import org.jetbrains.exposed.sql.stringLiteral
import org.jetbrains.exposed.sql.vendors.currentDialect

// Operator Classes
Expand Down Expand Up @@ -69,6 +63,7 @@ fun ExpressionWithColumnType<*>.contains(candidate: Expression<*>, path: String?
*/
fun <T> ExpressionWithColumnType<*>.contains(candidate: T, path: String? = null): Contains = when (candidate) {
is Iterable<*>, is Array<*> -> Contains(this, stringLiteral(asLiteral(candidate).toString()), path, columnType)
is String -> Contains(this, stringLiteral(candidate), path, columnType)
else -> Contains(this, asLiteral(candidate), path, columnType)
}

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 =
characterColumnType.nonNullValueToString(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