Skip to content

Commit

Permalink
fix null enum bug
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlauvlwj committed May 9, 2021
1 parent 306c8bc commit a7c287c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
14 changes: 13 additions & 1 deletion ktorm-core/src/main/kotlin/org/ktorm/schema/SqlTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,24 @@ public inline fun <reified C : Enum<C>> BaseTable<*>.enum(name: String): Column<
*
* @property enumClass the enum class.
*/
public class EnumSqlType<C : Enum<C>>(public val enumClass: Class<C>) : SqlType<C>(Types.VARCHAR, "enum") {
public class EnumSqlType<C : Enum<C>>(public val enumClass: Class<C>) : SqlType<C>(Types.OTHER, "enum") {

private val hasPostgresqlDriver by lazy {
runCatching { Class.forName("org.postgresql.Driver") }.isSuccess
}

override fun setParameter(ps: PreparedStatement, index: Int, parameter: C?) {
if (parameter != null) {
doSetParameter(ps, index, parameter)
} else {
if (hasPostgresqlDriver && ps is PGStatement) {
ps.setNull(index, Types.OTHER)
} else {
ps.setNull(index, Types.VARCHAR)
}
}
}

override fun doSetParameter(ps: PreparedStatement, index: Int, parameter: C) {
if (hasPostgresqlDriver && ps is PGStatement) {
ps.setObject(index, parameter.name, Types.OTHER)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.ktorm.support.mysql

import org.hamcrest.CoreMatchers
import org.hamcrest.MatcherAssert
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.ClassRule
import org.junit.Test
import org.ktorm.BaseTest
Expand Down Expand Up @@ -524,12 +524,16 @@ class MySqlTest : BaseTest() {
}

val count = database.sequenceOf(TableWithEnum).count { it.current_mood eq Mood.SAD }
assertThat(count, equalTo(1))

MatcherAssert.assertThat(count, CoreMatchers.equalTo(1))
val mood = database.sequenceOf(TableWithEnum).filter { it.id eq 1 }.mapColumns { it.current_mood }.first()
assertThat(mood, equalTo(Mood.SAD))

val currentMood =
database.sequenceOf(TableWithEnum).filter { it.id eq 1 }.mapColumns { it.current_mood }.first()
database.insert(TableWithEnum) {
set(it.current_mood, null)
}

MatcherAssert.assertThat(currentMood, CoreMatchers.equalTo(Mood.SAD))
val mood1 = database.sequenceOf(TableWithEnum).filter { it.id eq 2 }.mapColumns { it.current_mood }.first()
assertThat(mood1, equalTo(null))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -783,13 +783,17 @@ class PostgreSqlTest : BaseTest() {
}

val count = database.sequenceOf(TableWithEnum).count { it.current_mood eq Mood.SAD }

assertThat(count, equalTo(1))

val currentMood =
database.sequenceOf(TableWithEnum).filter { it.id eq 1 }.mapColumns { it.current_mood }.first()
val mood = database.sequenceOf(TableWithEnum).filter { it.id eq 1 }.mapColumns { it.current_mood }.first()
assertThat(mood, equalTo(Mood.HAPPY))

database.insert(TableWithEnum) {
set(it.current_mood, null)
}

assertThat(currentMood, equalTo(Mood.HAPPY))
val mood1 = database.sequenceOf(TableWithEnum).filter { it.id eq 3 }.mapColumns { it.current_mood }.first()
assertThat(mood1, equalTo(null))
}

@Test
Expand Down

0 comments on commit a7c287c

Please sign in to comment.