Skip to content

Commit

Permalink
fix null json bug
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlauvlwj committed May 9, 2021
1 parent a7c287c commit 35113b3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion ktorm-jackson/src/main/kotlin/org/ktorm/jackson/JsonSqlType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,24 @@ public inline fun <reified C : Any> BaseTable<*>.json(
public class JsonSqlType<T : Any>(
public val objectMapper: ObjectMapper,
public val javaType: JavaType
) : SqlType<T>(Types.VARCHAR, "json") {
) : SqlType<T>(Types.OTHER, "json") {

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

override fun setParameter(ps: PreparedStatement, index: Int, parameter: T?) {
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: T) {
if (hasPostgresqlDriver && ps is PGStatement) {
val obj = PGobject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,11 @@ class MySqlTest : BaseTest() {
set(it.arr, listOf(1, 2, 3))
}

database.insert(t) {
set(it.obj, null)
set(it.arr, null)
}

database
.from(t)
.select(t.obj, t.arr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,11 @@ class PostgreSqlTest : BaseTest() {
set(it.arr, listOf(1, 2, 3))
}

database.insert(t) {
set(it.obj, null)
set(it.arr, null)
}

database
.from(t)
.select(t.obj, t.arr)
Expand Down

0 comments on commit 35113b3

Please sign in to comment.