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..eca8ec1330 --- /dev/null +++ b/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DateLiteralTest.kt @@ -0,0 +1,39 @@ +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()) + } + } +}