Anton
Anton

Reputation: 4403

Compare joda.DateTime in filter

I'm trying to a Column[DateTime] with a date time in filter, but just can't figure out how to make it work.

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
  }

At the point where I compare x.startTime and start I get the following error:

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

Any chance I can make those comparisons in my code?

Upvotes: 2

Views: 3200

Answers (2)

R&#233;gis
R&#233;gis

Reputation: 8969

This jodatime mapper is really usefull with Plya! : https://github.com/tototoshi/slick-joda-mapper

Upvotes: 0

Alexey Romanov
Alexey Romanov

Reputation: 170815

Inside filter, map, etc. in Slick you can only use code which can be translated into SQL; obviously it can't use arbitrary Java/Scala classes like DateTimeComparator or DateTimeWrapper. Slick doesn't seem to support datetime comparison currently; you may want to watch this issue. See also Slick: Filtering all records which have a joda DateTime date equal to today.

Upvotes: 1

Related Questions