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

EXPOSED-19 Max timestamp in SQLite not working #1725

Merged
merged 5 commits into from
Apr 21, 2023
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 @@ -161,9 +161,11 @@ class AutoIncColumnType(

/** Returns `true` if this is an auto-increment column, `false` otherwise. */
val IColumnType.isAutoInc: Boolean get() = this is AutoIncColumnType || (this is EntityIDColumnType<*> && idColumn.columnType.isAutoInc)

/** Returns the name of the auto-increment sequence of this column. */
val Column<*>.autoIncColumnType: AutoIncColumnType?
get() = (columnType as? AutoIncColumnType) ?: (columnType as? EntityIDColumnType<*>)?.idColumn?.columnType as? AutoIncColumnType

@Deprecated(
message = "Will be removed in upcoming releases. Please use [autoIncColumnType.autoincSeq] instead",
replaceWith = ReplaceWith("this.autoIncColumnType.autoincSeq"),
Expand Down Expand Up @@ -423,7 +425,7 @@ 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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix for failing tests

}

override fun valueFromDB(value: Any): BigDecimal = when (value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ class ResultRow(
fun create(rs: ResultSet, fieldsIndex: Map<Expression<*>, Int>): ResultRow {
return ResultRow(fieldsIndex).apply {
fieldsIndex.forEach { (field, index) ->
val columnType = (field as? Column<*>)?.columnType
val value = if (columnType != null) columnType.readObject(rs, index + 1) else rs.getObject(index + 1)
val columnType = (field as? ExpressionWithColumnType)?.columnType
Copy link
Collaborator Author

@joc-a joc-a Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix for bug in title of PR

val value = if (columnType != null)
columnType.readObject(rs, index + 1)
else rs.getObject(index + 1)
data[index] = value
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import org.junit.Assert.fail
import org.junit.Test
import java.math.BigDecimal
import java.math.RoundingMode
import java.time.*
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneOffset
import java.time.temporal.Temporal
import kotlin.test.assertEquals

Expand Down Expand Up @@ -90,6 +94,39 @@ open class JavaTimeBaseTest : DatabaseTestsBase() {
assertEqualDateTime(dateTimeWithNanos, dateTimeFromDB)
}
}

@Test
fun `test selecting Instant using expressions`() {
val TestTable = object : Table() {
val ts = timestamp("ts")
val tsn = timestamp("tsn").nullable()
}

val now = Instant.now()

withTables(TestTable) {
TestTable.insert {
it[ts] = now
it[tsn] = now
}

val maxTsExpr = TestTable.ts.max()
val maxTimestamp = TestTable.slice(maxTsExpr).selectAll().single()[maxTsExpr]
assertEqualDateTime(now, maxTimestamp)

val minTsExpr = TestTable.ts.min()
val minTimestamp = TestTable.slice(minTsExpr).selectAll().single()[minTsExpr]
assertEqualDateTime(now, minTimestamp)

val maxTsnExpr = TestTable.tsn.max()
val maxNullableTimestamp = TestTable.slice(maxTsnExpr).selectAll().single()[maxTsnExpr]
assertEqualDateTime(now, maxNullableTimestamp)

val minTsnExpr = TestTable.tsn.min()
val minNullableTimestamp = TestTable.slice(minTsnExpr).selectAll().single()[minTsnExpr]
assertEqualDateTime(now, minNullableTimestamp)
}
}
}

fun <T : Temporal> assertEqualDateTime(d1: T?, d2: T?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,39 @@ open class KotlinTimeBaseTest : DatabaseTestsBase() {
assertEqualDateTime(dateTimeWithNanos, dateTimeFromDB)
}
}

@Test
fun `test selecting Instant using expressions`() {
val TestTable = object : Table() {
val ts = timestamp("ts")
val tsn = timestamp("tsn").nullable()
}

val now = Clock.System.now()

withTables(TestTable) {
TestTable.insert {
it[ts] = now
it[tsn] = now
}

val maxTsExpr = TestTable.ts.max()
val maxTimestamp = TestTable.slice(maxTsExpr).selectAll().single()[maxTsExpr]
assertEqualDateTime(now, maxTimestamp)

val minTsExpr = TestTable.ts.min()
val minTimestamp = TestTable.slice(minTsExpr).selectAll().single()[minTsExpr]
assertEqualDateTime(now, minTimestamp)

val maxTsnExpr = TestTable.tsn.max()
val maxNullableTimestamp = TestTable.slice(maxTsnExpr).selectAll().single()[maxTsnExpr]
assertEqualDateTime(now, maxNullableTimestamp)

val minTsnExpr = TestTable.tsn.min()
val minNullableTimestamp = TestTable.slice(minTsnExpr).selectAll().single()[minTsnExpr]
assertEqualDateTime(now, minNullableTimestamp)
}
}
}

fun <T> assertEqualDateTime(d1: T?, d2: T?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.shared.MiscTable
import org.jetbrains.exposed.sql.tests.shared.checkInsert
import org.jetbrains.exposed.sql.tests.shared.checkRow
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.junit.Test
import java.math.BigDecimal
import kotlin.test.assertEquals
Expand Down