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: Add ArrayColumnType default override for datetime module types #1995

Merged
merged 2 commits into from
Feb 20, 2024
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
1 change: 1 addition & 0 deletions exposed-core/api/exposed-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public final class org/jetbrains/exposed/sql/ArrayColumnType : org/jetbrains/exp
public final fun getDelegate ()Lorg/jetbrains/exposed/sql/ColumnType;
public final fun getDelegateType ()Ljava/lang/String;
public final fun getMaximumCardinality ()Ljava/lang/Integer;
public fun nonNullValueAsDefaultString (Ljava/lang/Object;)Ljava/lang/String;
public fun nonNullValueToString (Ljava/lang/Object;)Ljava/lang/String;
public fun notNullValueToDB (Ljava/lang/Object;)Ljava/lang/Object;
public fun readObject (Ljava/sql/ResultSet;I)Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,15 @@ class ArrayColumnType(
else -> super.nonNullValueToString(value)
}

override fun nonNullValueAsDefaultString(value: Any): String = when (value) {
is List<*> -> {
val prefix = if (currentDialect is H2Dialect) "ARRAY [" else "ARRAY["
value.joinToString(",", prefix, "]") { delegate.valueAsDefaultString(it) }
}
is Array<*> -> nonNullValueAsDefaultString(value.toList())
else -> super.nonNullValueAsDefaultString(value)
}

override fun readObject(rs: ResultSet, index: Int): Any? = rs.getArray(index)

override fun setParameter(stmt: PreparedStatementApi, index: Int, value: Any?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.jetbrains.exposed.sql.json.jsonb
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.shared.assertEqualLists
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertTrue
import org.jetbrains.exposed.sql.tests.shared.expectException
Expand Down Expand Up @@ -474,6 +475,38 @@ open class JavaTimeBaseTest : DatabaseTestsBase() {
currentDbDateTime()
}
}

@Test
fun testDateTimeAsArray() {
val defaultDates = listOf(today)
val defaultDateTimes = listOf(LocalDateTime.now())
val tester = object : Table("array_tester") {
val dates = array<LocalDate>("dates", JavaLocalDateColumnType()).default(defaultDates)
val datetimes = array<LocalDateTime>("datetimes", JavaLocalDateTimeColumnType()).default(defaultDateTimes)
}

withTables(excludeSettings = TestDB.entries - TestDB.POSTGRESQL - TestDB.H2, tester) {
tester.insert { }
val result1 = tester.selectAll().single()
assertEqualLists(result1[tester.dates], defaultDates)
assertEqualLists(result1[tester.datetimes], defaultDateTimes)

val datesInput = List(3) { LocalDate.of(2020 + it, 5, 4) }
val datetimeInput = List(3) { LocalDateTime.of(2020 + it, 5, 4, 9, 9, 9) }
tester.insert {
it[dates] = datesInput
it[datetimes] = datetimeInput
}

val lastDate = tester.dates[3]
val firstTwoDatetimes = tester.datetimes.slice(1, 2)
val result2 = tester.select(lastDate, firstTwoDatetimes).where {
tester.dates[1].year() eq 2020
}.single()
assertEqualDateTime(datesInput.last(), result2[lastDate])
assertEqualLists(result2[firstTwoDatetimes], datetimeInput.take(2))
}
}
}

fun <T : Temporal> assertEqualDateTime(d1: T?, d2: T?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.jetbrains.exposed.sql.json.jsonb
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.shared.assertEqualLists
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertTrue
import org.jetbrains.exposed.sql.tests.shared.expectException
Expand Down Expand Up @@ -372,6 +373,38 @@ open class JodaTimeBaseTest : DatabaseTestsBase() {
currentDbDateTime()
}
}

@Test
fun testDateTimeAsArray() {
val defaultDates = listOf(today)
val defaultDateTimes = listOf(DateTime.now())
val tester = object : Table("array_tester") {
val dates = array<DateTime>("dates", DateColumnType(false)).default(defaultDates)
val datetimes = array<DateTime>("datetimes", DateColumnType(true)).default(defaultDateTimes)
}

withTables(excludeSettings = TestDB.entries - TestDB.POSTGRESQL - TestDB.H2, tester) {
tester.insert { }
val result1 = tester.selectAll().single()
assertEqualLists(result1[tester.dates], defaultDates)
assertEqualLists(result1[tester.datetimes], defaultDateTimes)

val datesInput = List(3) { DateTime.parse("${2020 + it}-5-4") }
val datetimeInput = List(3) { DateTime(2020 + it, 5, 4, 9, 9, 9) }
tester.insert {
it[dates] = datesInput
it[datetimes] = datetimeInput
}

val lastDate = tester.dates[3]
val firstTwoDatetimes = tester.datetimes.slice(1, 2)
val result2 = tester.select(lastDate, firstTwoDatetimes).where {
tester.dates[1].year() eq 2020
}.single()
assertEqualDateTime(datesInput.last(), result2[lastDate])
assertEqualLists(result2[firstTwoDatetimes], datetimeInput.take(2))
}
}
}

fun assertEqualDateTime(d1: DateTime?, d2: DateTime?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.jetbrains.exposed.sql.json.jsonb
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.shared.assertEqualLists
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertTrue
import org.jetbrains.exposed.sql.tests.shared.expectException
Expand Down Expand Up @@ -481,6 +482,38 @@ open class KotlinTimeBaseTest : DatabaseTestsBase() {
assertEquals(Duration.INFINITE, row[tester.duration])
}
}

@Test
fun testDateTimeAsArray() {
val defaultDates = listOf(now().date)
val defaultDateTimes = listOf(now())
val tester = object : Table("array_tester") {
val dates = array<LocalDate>("dates", KotlinLocalDateColumnType()).default(defaultDates)
val datetimes = array<LocalDateTime>("datetimes", KotlinLocalDateTimeColumnType()).default(defaultDateTimes)
}

withTables(excludeSettings = TestDB.entries - TestDB.POSTGRESQL - TestDB.H2, tester) {
tester.insert { }
val result1 = tester.selectAll().single()
assertEqualLists(result1[tester.dates], defaultDates)
assertEqualLists(result1[tester.datetimes], defaultDateTimes)

val datesInput = List(3) { LocalDate(2020 + it, 5, 4) }
val datetimeInput = List(3) { LocalDateTime(2020 + it, 5, 4, 9, 9, 9) }
tester.insert {
it[dates] = datesInput
it[datetimes] = datetimeInput
}

val lastDate = tester.dates[3]
val firstTwoDatetimes = tester.datetimes.slice(1, 2)
val result2 = tester.select(lastDate, firstTwoDatetimes).where {
tester.dates[1].year() eq 2020
}.single()
assertEqualDateTime(datesInput.last(), result2[lastDate])
assertEqualLists(result2[firstTwoDatetimes], datetimeInput.take(2))
}
}
}

fun <T> assertEqualDateTime(d1: T?, d2: T?) {
Expand Down
Loading