Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(DoubleColumnType): correctly handle precision when casting Float to DoubleColumnType for a real column #2115

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ class DoubleColumnType : ColumnType<Double>() {
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}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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.shared.assertEquals
import org.junit.Test

class DoubleColumnTypeTests : DatabaseTestsBase() {
object TestTable : IntIdTable("double_table") {
val amount = double("amount")
}

@Test
fun testInsertAndSelectFromDoubleColumn() {
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])
}
}
}

@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)
}
}
}
Loading