Customer Type Mapper for Slick SQL

前端 未结 2 1790
自闭症患者
自闭症患者 2020-12-15 10:22

I\'ve found this example from slick testing:
https://github.com/slick/slick/blob/master/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/MapperTest.scala

相关标签:
2条回答
  • 2020-12-15 10:57

    I use the following in my code, which might also work for you:

    import java.sql.Timestamp
    import org.joda.time.DateTime
    import org.joda.time.DateTimeZone.UTC
    import scala.slick.lifted.MappedTypeMapper.base
    import scala.slick.lifted.TypeMapper
    
    implicit val DateTimeMapper: TypeMapper[DateTime] = 
      base[DateTime, Timestamp](
        d => new Timestamp(d millis), 
        t => new DateTime(t getTime, UTC))
    

    Edit (after your edit =^.~= ): (a bit late but I hope it still helps)

    Ah, OK, since you're not using lifted embedding, you'll have to define different implicit values (as indicated by the error message from the compiler). Something like the following should work (though I haven't tried myself):

    implicit val SetDateTime: SetParameter[DateTime] = new SetParameter { 
      def apply(d: DateTime, p: PositionedParameters): Unit =
        p setTimestamp (new Timestamp(d millis))
    }
    

    For the other way round (retrieving the results of the SELECT), it looks like you'd need to define a GetResult:

    implicit val GetDateTime: GetResult[DateTime] = new GetResult {
      def apply(r: PositionedResult) = new DateTime(r.nextTimestamp getTime, UTC))
    }
    

    So, basically this is just the same as with the lifted embedding, just encoded with different types.

    0 讨论(0)
  • 2020-12-15 11:11

    Why not to digg into something that works great? Look at

    https://gist.github.com/dragisak/4756344

    and

    https://github.com/tototoshi/slick-joda-mapper

    First you could copy-paste to your project, and the second is available from Maven central.

    0 讨论(0)
提交回复
热议问题