2014-10-20 78 views
2

我正在尝试在过滤器中使用日期时间的Column [DateTime],但无法弄清楚如何使其工作。比较过滤器中的joda.DateTime

import play.api.Play.current 
import play.api.db.slick.Config.driver.simple._ 
import play.api.db.slick._ 
import org.joda.time.{DateTime, DateTimeComparator, DateTimeZone} 

    private val dateComparator = DateTimeComparator.getInstance() 

    implicit def dateTimeToScalaWrapper(dt: DateTime): DateTimeWrapper = new DateTimeWrapper(dt) 

    class DateTimeWrapper(dt: DateTime) extends Ordered[DateTime] with Ordering[DateTime] { 
    def compare(that: DateTime): Int = dateComparator.compare(dt, that) 
    def compare(a: DateTime, b: DateTime): Int = dateComparator.compare(a, b) 
    } 

    implicit def jodaType = MappedColumnType.base[DateTime, Timestamp](
    {d => new Timestamp(d.getMillis)} , 
    {d => new DateTime(d.getTime, UTC)} 
) 

    def removeTimeSlots(start: DateTime, end: DateTime, reservationId: Long) : Int = DB withTransaction { implicit session => 
    timeSlots.filter(x => x.reservationId == reservationId && x.startTime >= start && x.starTime <= end).delete 
    } 

在这里我比较x.startTime开始我得到以下错误点:

Error:(90, -1) Play 2 Compiler: 
Reservations.scala:90: polymorphic expression cannot be instantiated to expected type; 
    found : [R]scala.slick.lifted.Column[R] 
    required: Boolean 

任何机会,我可以把这些比较在我的代码?

+2

帮助其他读者!在SO和邮件列表之间交叉链接。 https://groups.google.com/d/msgid/scalaquery/66b7a3c6-bd74-4269-a990-f46971e2af4b%40googlegroups.com?utm_medium=email&utm_source=footer – cvogt 2014-10-20 14:21:13

+0

将来会做。感谢您的链接 – Anton 2014-10-20 16:43:20

回答

1

里面filter,map等在Slick中你只能使用可以转换成SQL的代码;显然它不能使用像DateTimeComparatorDateTimeWrapper这样的任意Java/Scala类。油滑doesn't seem to support datetime comparison currently;你可能想看这个问题。另请参阅Slick: Filtering all records which have a joda DateTime date equal to today

+2

考虑到实际列类型是Timestamp(通过MappedColumnType)以及数据库引擎确实支持对其的操作,这是令人惊讶的。缺乏日期比较支持基本上使得Slick在这一点上对我毫无用处 – Anton 2014-10-20 16:45:19