Skip to content

Commit

Permalink
EXPOSED-353 dateLiteral does not work on OracleDB 12c or 19c #1338
Browse files Browse the repository at this point in the history
  • Loading branch information
obabichevjb committed Apr 25, 2024
1 parent d3c5dbc commit 552ddb8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,14 @@ class JavaLocalDateColumnType : ColumnType<LocalDate>(), 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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())
}
}


}

0 comments on commit 552ddb8

Please sign in to comment.