16

I'm trying to filter against an optional date column with Scala Slick 1.0.1.

It may be I just don't see it, but I've got a table that looks something like this:

case class UserRole(id:UUID, userID:UUID, role:String)
object UserRole extends Table[UserRole]("User_Role")  {

  //(id: Long = 0l, name: String, active: Boolean) extends KeyedEntity[Long] {
  def id = column[UUID]("ID", O.PrimaryKey)
  def userID = column[UUID]("user_id")
  def vendorID = column[UUID]("vendor_id")
  def role = column[String]("role")
  def user = foreignKey("user_FK", userID, User)(_.id)

  def start = column[java.sql.Date]("startDate")
  def endDate = column[Option[java.sql.Date]]("endDate")

  def * = id ~ userID ~ role  <> (UserRole.apply _, UserRole.unapply _)
}

You'll see there that the endDate is optional.

How do I construct a query where I filter so endDate can be NULL/None or greater than the current (db) date? FYI, I'm generally using the embedded api

thanks

Michael Kohout
  • 1,073
  • 1
  • 14
  • 26

1 Answers1

11

This is not pretty (the part about null.asInstanceOf), but I think it will work. I got that idea from an old Scala Query post, so I don't know if slick ever put something better in for that, but when I looked at the resulting selectStatement from the query, it looked correct:

val now = new java.sql.Date(System.currentTimeMillis())
val query = for {
  role <- UserRole
  if (role.endDate === null.asInstanceOf[Option[java.sql.Date]] || role.endDate > now)
} yield role

EDIT

Thanks to the comment by @MartinKolinek, this code will also work and is much cleaner and probably the better way to do things:

val now = new java.sql.Date(System.currentTimeMillis())
val query = for {
  role <- UserRole
  if (role.endDate.isNull || role.endDate > now)
} yield role
cmbaxter
  • 35,283
  • 4
  • 86
  • 95
  • 3
    I think we should add isEmpty to the Slick API so we conform to the Scala Option API. – cvogt Jul 10 '13 at 18:43
  • 1
    What's the Slick 3.0 way to check if an `Option[A]` column equals some `A`? – Kevin Meredith Aug 19 '15 at 20:25
  • 1
    @KevinMeredith, I'm not using slick 3.0 yet, but a quick look at the `OptionColumnExtensionMethods` api doc (http://slick.typesafe.com/doc/3.0.2/api/index.html#slick.lifted.OptionColumnExtensionMethods) reveals things like `===`, `>=`, etc... that seem like the operators to use in queries for comparisons – cmbaxter Aug 19 '15 at 21:01