Skip to content

Commit

Permalink
fix: EXPOSED-19 Max timestamp in SQLite not working
Browse files Browse the repository at this point in the history
-Fix tests that fail because of precision loss in DecimalColumnType
-Remove WithColumnType interface because it's now redundant
  • Loading branch information
joc-a committed Apr 20, 2023
1 parent 8866f5a commit 81fb9e5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Column<T>(
val name: String,
/** Data type of the column. */
override val columnType: IColumnType
) : ExpressionWithColumnType<T>(), DdlAware, Comparable<Column<*>>, WithColumnType {
) : ExpressionWithColumnType<T>(), DdlAware, Comparable<Column<*>> {
var foreignKey: ForeignKeyConstraint? = null

/** Returns the column that this column references. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,29 +425,32 @@ class DecimalColumnType(
override fun sqlType(): String = "DECIMAL($precision, $scale)"

override fun readObject(rs: ResultSet, index: Int): Any? {
return rs.getBigDecimal(index)
return rs.getObject(index)
}

override fun valueFromDB(value: Any): BigDecimal = when (value) {
is BigDecimal -> value
is Double -> {
if (value.isNaN()) {
error("Unexpected value of type Double: NaN of ${value::class.qualifiedName}")
} else {
value.toBigDecimal()
override fun valueFromDB(value: Any): BigDecimal {
if (value is BigDecimal) return value

return when (value) {
is Double -> {
if (value.isNaN()) {
error("Unexpected value of type Double: NaN of ${value::class.qualifiedName}")
} else {
value.toBigDecimal()
}
}
}
is Float -> {
if (value.isNaN()) {
error("Unexpected value of type Float: NaN of ${value::class.qualifiedName}")
} else {
value.toBigDecimal()
is Float -> {
if (value.isNaN()) {
error("Unexpected value of type Float: NaN of ${value::class.qualifiedName}")
} else {
value.toBigDecimal()
}
}
}
is Long -> value.toBigDecimal()
is Int -> value.toBigDecimal()
else -> error("Unexpected value of type Decimal: $value of ${value::class.qualifiedName}")
}.setScale(scale, RoundingMode.HALF_EVEN)
is Long -> value.toBigDecimal()
is Int -> value.toBigDecimal()
else -> error("Unexpected value of type Decimal: $value of ${value::class.qualifiedName}")
}.setScale(scale, RoundingMode.HALF_EVEN)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down Expand Up @@ -916,10 +919,3 @@ class EnumerationNameColumnType<T : Enum<T>>(
interface IDateColumnType {
val hasTimePart: Boolean
}

/**
* Marker interface for columns/expressions with a column type.
*/
interface WithColumnType {
val columnType: IColumnType
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.math.BigDecimal
/**
* Represents an SQL function.
*/
abstract class Function<T>(override val columnType: IColumnType) : ExpressionWithColumnType<T>(), WithColumnType
abstract class Function<T>(override val columnType: IColumnType) : ExpressionWithColumnType<T>()

/**
* Represents a custom SQL function.
Expand Down Expand Up @@ -158,7 +158,6 @@ class Max<T : Comparable<T>, in S : T?>(
val expr: Expression<in S>,
columnType: IColumnType
) : Function<T?>(columnType) {

override fun toQueryBuilder(queryBuilder: QueryBuilder): Unit = queryBuilder { append("MAX(", expr, ")") }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class ResultRow(
fun create(rs: ResultSet, fieldsIndex: Map<Expression<*>, Int>): ResultRow {
return ResultRow(fieldsIndex).apply {
fieldsIndex.forEach { (field, index) ->
val columnType = (field as? WithColumnType)?.columnType
val columnType = (field as? ExpressionWithColumnType)?.columnType
val value = if (columnType != null)
columnType.readObject(rs, index + 1)
else rs.getObject(index + 1)
Expand Down

0 comments on commit 81fb9e5

Please sign in to comment.