scalaslickslick-2.0

Slick: Option column filtering


I want to do something like this (this is a made-up example to simplify my actual problem):

def findByGender(isMale: Option[Boolean]) = {
  People.filter(row => row.name.isNotNull && isMale match {
    case Some(true) => row.wife.isNotNull      // find rows where wife column is not null
    case Some(false) => row.wife.isNull        // find rows where wife column is null
    case None => true                          // select everything
  })    
}

This does not compile because of the last "true". Any better way to do this?


Solution

  • You have to make it a Column[Boolean]:

    def findByGender(isMale: Option[Boolean]) = {
      People.filter(row => row.name.isNotNull && isMale match {
        case Some(true) => row.wife.isNotNull      // find rows where wife column is not null
        case Some(false) => row.wife.isNull        // find rows where wife column is null
        case None => LiteralColumn(true)           // select everything
      })    
    }