Customer Type Mapper for Slick SQL

送分小仙女□ 提交于 2019-11-28 23:39:49

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.

Alex Povar

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!