I want to do something like this in Slick 2.0.x: select users with age <= maxAge or if maxAge is None, select all users (limited by a number). I tried this:
private def ageQuery(maxAge: Column[Option[Int]], limit: Column[Int]) = {
Users.filter(user => maxAge.map(_ >= user.age).getOrElse(true)).take(limit)
}
val compiledAgeQuery = Compiled(ageQuery _)
I am running into following issues:
limitis aColumn[_], butUsers.takeexpects anInt. If I change itlimittoIntthen I can't use theCompiledmacro anymore since that expects onlyColumn[_]This snippet itself does not compile:
user => maxAge.map(_ >= user.age).getOrElse(true)
I have found following solution to your problem:
This is what I did:
limitparameter toscala.slick.lifted.ConstColumn[Long]. Now it satisfies bothtakemethod andCompiledmacromaxAge, and whenmaxAgeisNonethen it returns all users younger than 1000 years. It is workaround but it works good (assuming that noone is older than 1000 years)