Skip to content

Commit

Permalink
add setDateTime parameter for DriverJdbcType[DateTime]
Browse files Browse the repository at this point in the history
I think #30 is **NOT** right way in most cases.
But I have not change the default behavior directly for keep compatibility.
  • Loading branch information
xuwei-k committed Dec 10, 2020
1 parent 0ab6eaf commit d11f457
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
19 changes: 17 additions & 2 deletions src/main/scala/com/github/tototoshi/slick/JodaSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,22 @@
package com.github.tototoshi.slick

import org.joda.time.{ DateTime, DateTimeZone, Instant, LocalDate, LocalDateTime, LocalTime }
import java.util.Calendar
import org.joda.time.DateTime
import slick.jdbc._

class GenericJodaSupport(val driver: JdbcProfile) {
/**
* @param setTimeZone `Calendar` parameter for [[https://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html#setTimestamp-int-java.sql.Timestamp-java.util.Calendar-]]
*/
class GenericJodaSupport(val driver: JdbcProfile, setTimeZone: DateTime => Option[Calendar]) {

def this(driver: JdbcProfile) = {
this(driver, GenericJodaSupport.defaultSetTimeZoneFunction)
}

protected val dateTimeZoneMapperDelegate: JodaDateTimeZoneMapper = new JodaDateTimeZoneMapper(driver)
protected val localDateMapperDelegate: JodaLocalDateMapper = new JodaLocalDateMapper(driver)
protected val dateTimeMapperDelegate: JodaDateTimeMapper = new JodaDateTimeMapper(driver)
protected val dateTimeMapperDelegate: JodaDateTimeMapper = new JodaDateTimeMapper(driver, setTimeZone)
protected val instantMapperDelegate: JodaInstantMapper = new JodaInstantMapper(driver)
protected val localDateTimeMapperDelegate: JodaLocalDateTimeMapper = new JodaLocalDateTimeMapper(driver)
protected val localTimeMapperDelegate: JodaLocalTimeMapper = new JodaLocalTimeMapper(driver)
Expand Down Expand Up @@ -76,6 +86,11 @@ class GenericJodaSupport(val driver: JdbcProfile) {

}

object GenericJodaSupport {
val defaultSetTimeZoneFunction: DateTime => Option[Calendar] = datetime =>
Some(Calendar.getInstance(datetime.getZone.toTimeZone))
}

object H2JodaSupport extends GenericJodaSupport(H2Profile)
object PostgresJodaSupport extends GenericJodaSupport(PostgresProfile)
object MySQLJodaSupport extends GenericJodaSupport(MySQLProfile)
Expand Down
12 changes: 9 additions & 3 deletions src/main/scala/com/github/tototoshi/slick/JodaTypeMapper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,22 @@ class JodaLocalDateMapper(val driver: JdbcProfile) {

}

class JodaDateTimeMapper(val driver: JdbcProfile) {
class JodaDateTimeMapper(val driver: JdbcProfile, setTimeZone: DateTime => Option[Calendar]) {

object TypeMapper extends driver.DriverJdbcType[DateTime]
with JodaDateTimeSqlTimestampConverter {

def sqlType = java.sql.Types.TIMESTAMP
override def sqlTypeName(sym: scala.Option[slick.ast.FieldSymbol]): String =
driver.columnTypes.timestampJdbcType.sqlTypeName(sym)
override def setValue(v: DateTime, p: PreparedStatement, idx: Int): Unit =
p.setTimestamp(idx, toSqlType(v), Calendar.getInstance(v.getZone().toTimeZone()))
override def setValue(v: DateTime, p: PreparedStatement, idx: Int): Unit = {
setTimeZone(v) match {
case Some(calendar) =>
p.setTimestamp(idx, toSqlType(v), calendar)
case None =>
p.setTimestamp(idx, toSqlType(v))
}
}
override def getValue(r: ResultSet, idx: Int): DateTime =
fromSqlType(r.getTimestamp(idx))
override def updateValue(v: DateTime, r: ResultSet, idx: Int): Unit =
Expand Down

0 comments on commit d11f457

Please sign in to comment.