From 552ddb8959922a480e5f2661b198011426017b39 Mon Sep 17 00:00:00 2001 From: Oleg Babichev Date: Thu, 25 Apr 2024 14:20:07 +0200 Subject: [PATCH] EXPOSED-353 dateLiteral does not work on OracleDB 12c or 19c #1338 --- .../sql/javatime/JavaDateColumnType.kt | 9 +++- .../org/jetbrains/exposed/DateLiteralTest.kt | 41 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DateLiteralTest.kt diff --git a/exposed-java-time/src/main/kotlin/org/jetbrains/exposed/sql/javatime/JavaDateColumnType.kt b/exposed-java-time/src/main/kotlin/org/jetbrains/exposed/sql/javatime/JavaDateColumnType.kt index 7649c653ca..135e7fda22 100644 --- a/exposed-java-time/src/main/kotlin/org/jetbrains/exposed/sql/javatime/JavaDateColumnType.kt +++ b/exposed-java-time/src/main/kotlin/org/jetbrains/exposed/sql/javatime/JavaDateColumnType.kt @@ -127,7 +127,14 @@ class JavaLocalDateColumnType : ColumnType(), IDateColumnType { override fun nonNullValueToString(value: LocalDate): String { val instant = Instant.from(value.atStartOfDay(ZoneId.systemDefault())) - return "'${DEFAULT_DATE_STRING_FORMATTER.format(instant)}'" + val formatted = DEFAULT_DATE_STRING_FORMATTER.format(instant) + if (currentDialect is OracleDialect) { + // Date literal in Oracle DB must match NLS_DATE_FORMAT parameter. + // That parameter can be changed on DB level. + // But format can be also specified per literal with TO_DATE function + return "TO_DATE('$formatted', 'YYYY-MM-DD')" + } + return "'$formatted'" } override fun valueFromDB(value: Any): LocalDate? = when (value) { diff --git a/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DateLiteralTest.kt b/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DateLiteralTest.kt new file mode 100644 index 0000000000..6f1e96136c --- /dev/null +++ b/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DateLiteralTest.kt @@ -0,0 +1,41 @@ +package org.jetbrains.exposed + +import junit.framework.TestCase.assertNull +import org.jetbrains.exposed.DateLiteralTest.TableWithDate.date +import org.jetbrains.exposed.dao.id.IntIdTable +import org.jetbrains.exposed.sql.insert +import org.jetbrains.exposed.sql.javatime.date +import org.jetbrains.exposed.sql.javatime.dateLiteral +import org.jetbrains.exposed.sql.selectAll +import org.jetbrains.exposed.sql.tests.DatabaseTestsBase +import org.jetbrains.exposed.sql.tests.shared.assertEquals +import org.junit.Test +import java.time.LocalDate + +class DateLiteralTest : DatabaseTestsBase() { + object TableWithDate : IntIdTable() { + val date = date("date") + } + + @Test + fun testDateLiteralInQuery() { + withTables(TableWithDate) { + val future = LocalDate.of(3000, 1, 1) + val past = LocalDate.of(1500, 1, 1) + + TableWithDate.insert { + it[date] = future + } + + assertEquals(future, TableWithDate.selectAll().first()[date]) + + assertEquals(future, TableWithDate.selectAll().where { date greater past }.first()[date]) + + assertEquals(future, TableWithDate.selectAll().where { date greater dateLiteral(past) }.first()[date]) + + assertNull(TableWithDate.selectAll().where { date less dateLiteral(past) }.firstOrNull()) + } + } + + +}