From 88f33047c1a4635d4eeefd1b41b81a96a9a68e12 Mon Sep 17 00:00:00 2001 From: Jack Gisel Date: Tue, 4 Jun 2024 14:06:20 -0700 Subject: [PATCH 1/4] bug fix for when casting a Float from a ResultRow into a DoubleColumnType --- .../org/jetbrains/exposed/sql/ColumnType.kt | 2 ++ .../shared/types/DoubleColumnTypeTests.kt | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt 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 702ffaab6d..31b3de778d 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 @@ -440,6 +440,8 @@ class DoubleColumnType : ColumnType() { override fun sqlType(): String = currentDialect.dataTypeProvider.doubleType() override fun valueFromDB(value: Any): Double = when (value) { is Double -> value + // Cast as string to prevent precision loss + is Float -> value.toString().toDouble() is Number -> value.toDouble() is String -> value.toDouble() else -> error("Unexpected value of type Double: $value of ${value::class.qualifiedName}") diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt new file mode 100644 index 0000000000..cfc93522a1 --- /dev/null +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt @@ -0,0 +1,28 @@ +package org.jetbrains.exposed.sql.tests.shared.types + +import org.jetbrains.exposed.dao.id.IntIdTable +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.tests.DatabaseTestsBase +import org.jetbrains.exposed.sql.tests.TestDB +import org.jetbrains.exposed.sql.tests.shared.assertEqualLists +import org.jetbrains.exposed.sql.tests.shared.assertEquals +import org.junit.Test + +class DoubleColumnTypeTests : DatabaseTestsBase() { + object TestTable : IntIdTable("double_table") { + val amount = double("amount") + } + + @Test + fun `test correctly gets data from the DB`() { + withTables(TestTable) { + val id = TestTable.insertAndGetId { + it[amount] = 9.23 + } + + TestTable.selectAll().where { TestTable.id eq id }.singleOrNull()?.let { + assertEquals(9.23, it[TestTable.amount]) + } + } + } +} From 71106ada77f755e3e42fd80218bd55d184164dd1 Mon Sep 17 00:00:00 2001 From: Jack Gisel Date: Wed, 5 Jun 2024 15:06:05 -0700 Subject: [PATCH 2/4] fix imports --- .../exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt index cfc93522a1..66b20338e1 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt @@ -3,8 +3,6 @@ package org.jetbrains.exposed.sql.tests.shared.types import org.jetbrains.exposed.dao.id.IntIdTable import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.tests.DatabaseTestsBase -import org.jetbrains.exposed.sql.tests.TestDB -import org.jetbrains.exposed.sql.tests.shared.assertEqualLists import org.jetbrains.exposed.sql.tests.shared.assertEquals import org.junit.Test From 18771f5daa6048ee10fc20449887f15a358c7707 Mon Sep 17 00:00:00 2001 From: Jack Gisel Date: Thu, 6 Jun 2024 12:27:09 -0700 Subject: [PATCH 3/4] adding another test case --- .../shared/types/DoubleColumnTypeTests.kt | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt index 66b20338e1..899806ddae 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt @@ -12,7 +12,7 @@ class DoubleColumnTypeTests : DatabaseTestsBase() { } @Test - fun `test correctly gets data from the DB`() { + fun testInsertAndSelectFromDoubleColumn() { withTables(TestTable) { val id = TestTable.insertAndGetId { it[amount] = 9.23 @@ -23,4 +23,28 @@ class DoubleColumnTypeTests : DatabaseTestsBase() { } } } + + @Test + fun testInsertAndSelectFromRealColumn() { + withDb { + val originalColumnDDL = TestTable.amount.descriptionDdl() + val realColumnDDL = originalColumnDDL.replace(" DOUBLE PRECISION ", " REAL ") + + // create table with double() column that uses SQL type REAL + TestTable.ddl + .map { it.replace(originalColumnDDL, realColumnDDL) } + .forEach { exec(it) } + + val id = TestTable.insertAndGetId { + it[amount] = 9.23 + } + + TestTable.selectAll().where { TestTable.id eq id }.singleOrNull()?.let { + assertEquals(9.23, it[TestTable.amount]) + } + + SchemaUtils.drop(TestTable) + } + } + } From 6dc1679868cbf19feb11769b66a637298449debc Mon Sep 17 00:00:00 2001 From: Jack Gisel Date: Thu, 6 Jun 2024 12:28:06 -0700 Subject: [PATCH 4/4] detekt --- .../exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt index 899806ddae..16f2b141b5 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/types/DoubleColumnTypeTests.kt @@ -46,5 +46,4 @@ class DoubleColumnTypeTests : DatabaseTestsBase() { SchemaUtils.drop(TestTable) } } - }