From d1cefc477a704c835a3c9e602a5be2330bad6f3d Mon Sep 17 00:00:00 2001 From: Ilya Muromtsev Date: Fri, 26 May 2023 22:54:40 +0300 Subject: [PATCH 1/3] support Short type for BigDecimal conversion functions - in case of avg aggregate functions for h2 db - it returns Short type and fail convert it to BigDecimal. --- .../src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt index d5efe947e5..4982d7c9d7 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt @@ -490,6 +490,7 @@ class DecimalColumnType( } is Long -> value.toBigDecimal() is Int -> value.toBigDecimal() + is Short -> value.toBigDecimal() else -> error("Unexpected value of type Decimal: $value of ${value::class.qualifiedName}") }.setScale(scale, RoundingMode.HALF_EVEN) From 45c31362e1c9d49431741bfb042a5f88bf595890 Mon Sep 17 00:00:00 2001 From: Ilya Muromtsev Date: Fri, 26 May 2023 23:05:21 +0300 Subject: [PATCH 2/3] short doesn't have toBigDecimal extension function --- .../src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt index 4982d7c9d7..9eefc0cee1 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt @@ -490,7 +490,7 @@ class DecimalColumnType( } is Long -> value.toBigDecimal() is Int -> value.toBigDecimal() - is Short -> value.toBigDecimal() + is Short -> value.toLong().toBigDecimal() else -> error("Unexpected value of type Decimal: $value of ${value::class.qualifiedName}") }.setScale(scale, RoundingMode.HALF_EVEN) From f39b31f4ebfc4361fdd1e51f7642bbbec4dd988a Mon Sep 17 00:00:00 2001 From: Chantal Loncle <82039410+bog-walk@users.noreply.github.com> Date: Wed, 17 Jan 2024 11:43:06 -0500 Subject: [PATCH 3/3] Add unit test for H2 (both v1 and v2) --- .../jetbrains/exposed/sql/tests/h2/H2Tests.kt | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/h2/H2Tests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/h2/H2Tests.kt index bbfeb4dbaa..dfeb56f798 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/h2/H2Tests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/h2/H2Tests.kt @@ -91,6 +91,29 @@ class H2Tests : DatabaseTestsBase() { } } + @Test + fun testH2V1WithBigDecimalFunctionThatReturnsShort() { + val testTable = object : Table("test_table") { + val number = short("number") + } + + withDb(TestDB.allH2TestDB) { + try { + SchemaUtils.create(testTable) + + testTable.batchInsert(listOf(2, 4, 6, 8, 10)) { n -> + this[testTable.number] = n + } + + val average = testTable.number.avg() + val result = testTable.select(average).single()[average] + assertEquals("6.00".toBigDecimal(), result) + } finally { + SchemaUtils.drop(testTable) + } + } + } + class WrappedTransactionManager(val transactionManager: TransactionManager) : TransactionManager by transactionManager