From b9d03fe28bd60238277e1f2396f31745f9af1242 Mon Sep 17 00:00:00 2001 From: Thiago Cordeiro Date: Mon, 9 Sep 2024 16:06:13 +0200 Subject: [PATCH] Fixed nullable plain params --- src/main/kotlin/io/tcds/orm/Param.kt | 2 +- src/main/kotlin/io/tcds/orm/param/BooleanParam.kt | 1 - src/main/kotlin/io/tcds/orm/param/ColumnParam.kt | 1 - src/main/kotlin/io/tcds/orm/param/DoubleParam.kt | 1 - src/main/kotlin/io/tcds/orm/param/FloatParam.kt | 1 - src/main/kotlin/io/tcds/orm/param/IntegerParam.kt | 1 - src/main/kotlin/io/tcds/orm/param/LongParam.kt | 1 - src/main/kotlin/io/tcds/orm/param/StringParam.kt | 1 - src/main/kotlin/io/tcds/orm/param/nullable/NullableEnumParam.kt | 1 + .../kotlin/io/tcds/orm/param/nullable/NullableInstantParam.kt | 1 + src/main/kotlin/io/tcds/orm/param/nullable/NullableJsonParam.kt | 1 + 11 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/io/tcds/orm/Param.kt b/src/main/kotlin/io/tcds/orm/Param.kt index 5c20c6e..57bc4cc 100644 --- a/src/main/kotlin/io/tcds/orm/Param.kt +++ b/src/main/kotlin/io/tcds/orm/Param.kt @@ -7,6 +7,6 @@ interface Param { val value: T fun bind(stmt: PreparedStatement, index: Int) - fun plain(): Any = value as Any + fun plain(): Any? = value fun describe(): String = "$name=$value" } diff --git a/src/main/kotlin/io/tcds/orm/param/BooleanParam.kt b/src/main/kotlin/io/tcds/orm/param/BooleanParam.kt index 215f975..7cb52a4 100644 --- a/src/main/kotlin/io/tcds/orm/param/BooleanParam.kt +++ b/src/main/kotlin/io/tcds/orm/param/BooleanParam.kt @@ -7,6 +7,5 @@ data class BooleanParam( override val name: String, override val value: Boolean, ) : Param { - override fun plain(): Boolean = value override fun bind(stmt: PreparedStatement, index: Int) = stmt.setBoolean(index, value) } diff --git a/src/main/kotlin/io/tcds/orm/param/ColumnParam.kt b/src/main/kotlin/io/tcds/orm/param/ColumnParam.kt index 172e650..8c63f89 100644 --- a/src/main/kotlin/io/tcds/orm/param/ColumnParam.kt +++ b/src/main/kotlin/io/tcds/orm/param/ColumnParam.kt @@ -9,6 +9,5 @@ data class ColumnParam( override val value: Type, ) : Param { override val name: String = column.name - override fun plain(): Any = value as Any override fun bind(stmt: PreparedStatement, index: Int) = column.bind(stmt, index, value) } diff --git a/src/main/kotlin/io/tcds/orm/param/DoubleParam.kt b/src/main/kotlin/io/tcds/orm/param/DoubleParam.kt index 83b68a0..2a803bf 100644 --- a/src/main/kotlin/io/tcds/orm/param/DoubleParam.kt +++ b/src/main/kotlin/io/tcds/orm/param/DoubleParam.kt @@ -7,6 +7,5 @@ data class DoubleParam( override val name: String, override val value: Double, ) : Param { - override fun plain(): Double = value override fun bind(stmt: PreparedStatement, index: Int) = stmt.setDouble(index, value) } diff --git a/src/main/kotlin/io/tcds/orm/param/FloatParam.kt b/src/main/kotlin/io/tcds/orm/param/FloatParam.kt index 592ff6f..4af2798 100644 --- a/src/main/kotlin/io/tcds/orm/param/FloatParam.kt +++ b/src/main/kotlin/io/tcds/orm/param/FloatParam.kt @@ -7,6 +7,5 @@ data class FloatParam( override val name: String, override val value: Float, ) : Param { - override fun plain(): Float = value override fun bind(stmt: PreparedStatement, index: Int) = stmt.setFloat(index, value) } diff --git a/src/main/kotlin/io/tcds/orm/param/IntegerParam.kt b/src/main/kotlin/io/tcds/orm/param/IntegerParam.kt index 4cabc25..a464273 100644 --- a/src/main/kotlin/io/tcds/orm/param/IntegerParam.kt +++ b/src/main/kotlin/io/tcds/orm/param/IntegerParam.kt @@ -7,6 +7,5 @@ data class IntegerParam( override val name: String, override val value: Int, ) : Param { - override fun plain(): Int = value override fun bind(stmt: PreparedStatement, index: Int) = stmt.setInt(index, value) } diff --git a/src/main/kotlin/io/tcds/orm/param/LongParam.kt b/src/main/kotlin/io/tcds/orm/param/LongParam.kt index bca5a47..fdfde37 100644 --- a/src/main/kotlin/io/tcds/orm/param/LongParam.kt +++ b/src/main/kotlin/io/tcds/orm/param/LongParam.kt @@ -7,6 +7,5 @@ data class LongParam( override val name: String, override val value: Long, ) : Param { - override fun plain(): Long = value override fun bind(stmt: PreparedStatement, index: Int) = stmt.setLong(index, value) } diff --git a/src/main/kotlin/io/tcds/orm/param/StringParam.kt b/src/main/kotlin/io/tcds/orm/param/StringParam.kt index 3733385..b887929 100644 --- a/src/main/kotlin/io/tcds/orm/param/StringParam.kt +++ b/src/main/kotlin/io/tcds/orm/param/StringParam.kt @@ -7,6 +7,5 @@ data class StringParam( override val name: String, override val value: String, ) : Param { - override fun plain(): String = value override fun bind(stmt: PreparedStatement, index: Int) = stmt.setString(index, value) } diff --git a/src/main/kotlin/io/tcds/orm/param/nullable/NullableEnumParam.kt b/src/main/kotlin/io/tcds/orm/param/nullable/NullableEnumParam.kt index 900a2c3..103e862 100644 --- a/src/main/kotlin/io/tcds/orm/param/nullable/NullableEnumParam.kt +++ b/src/main/kotlin/io/tcds/orm/param/nullable/NullableEnumParam.kt @@ -8,6 +8,7 @@ data class NullableEnumParam>( override val name: String, override val value: T?, ) : Param { + override fun plain(): Any? = value?.name override fun bind(stmt: PreparedStatement, index: Int) = when (value) { null -> stmt.setNull(index, Types.VARCHAR) else -> stmt.setString(index, value.name) diff --git a/src/main/kotlin/io/tcds/orm/param/nullable/NullableInstantParam.kt b/src/main/kotlin/io/tcds/orm/param/nullable/NullableInstantParam.kt index c7aeea0..dc16e36 100644 --- a/src/main/kotlin/io/tcds/orm/param/nullable/NullableInstantParam.kt +++ b/src/main/kotlin/io/tcds/orm/param/nullable/NullableInstantParam.kt @@ -10,6 +10,7 @@ data class NullableInstantParam( override val name: String, override val value: Instant?, ) : Param { + override fun plain(): String? = value?.toString() override fun bind(stmt: PreparedStatement, index: Int) = when (value) { null -> stmt.setNull(index, Types.TIMESTAMP) else -> stmt.setTimestamp(index, Timestamp.from(value)) diff --git a/src/main/kotlin/io/tcds/orm/param/nullable/NullableJsonParam.kt b/src/main/kotlin/io/tcds/orm/param/nullable/NullableJsonParam.kt index bf08e5f..240667d 100644 --- a/src/main/kotlin/io/tcds/orm/param/nullable/NullableJsonParam.kt +++ b/src/main/kotlin/io/tcds/orm/param/nullable/NullableJsonParam.kt @@ -9,6 +9,7 @@ data class NullableJsonParam( override val name: String, override val value: T?, ) : Param { + override fun plain(): String? = value?.let { JsonParam.mapper.writeValueAsString(it) } override fun bind(stmt: PreparedStatement, index: Int) = when (value) { null -> stmt.setNull(index, Types.VARCHAR) else -> stmt.setString(index, JsonParam.mapper.writeValueAsString(value))