2014-10-20 79 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