2
votes

I am trying to filter a table with parameters that can be optional. With slick 2.1 this worked, but when I go to version 3.0 no longer works, the error is: "can not resolve symbol &&" when I call the function "reduce".

I would appreciate any help .. thank you very much

This is the code:

def getAll(params : ClienteSearchParameters) : DBIOAction[Iterable[Cliente], NoStream, Effect.Read] = {

      val q = for {
        (x, (y, z)) <- tabla join (tablaPersonas joinLeft tablaContactos on (_.id === _.idPersona)) on (_.idPersona === _._1.id)
          if {
            List(
                params.nombre.map(y.nombre === _),
                params.apellido.map(y.apellido === _),
                params.fechaAlta.map(x.fechaAlta === _),
                params.fechaRegistracion.map(x.fechaRegistracion === _)
            ).flatten match {
              case Nil => LiteralColumn[Boolean](true)
              case seq => seq.reduce(_ && _)
            }
          }
      } yield (x, y, z)
1

1 Answers

0
votes

Here is info from doc http://slick.typesafe.com/doc/3.0.0/queries.html#sorting-and-filtering

I guess you can find here what you want. The example from doc:

//building criteria using a "dynamic filter" e.g. from a webform. 
val criteriaColombian = Option("Colombian")
val criteriaEspresso = Option("Espresso")
val criteriaRoast:Option[String] = None

val q4 = coffees.filter { coffee => 
  List(
      criteriaColombian.map(coffee.name === _),
      criteriaEspresso.map(coffee.name === _),
      criteriaRoast.map(coffee.name === _) // not a condition as `criteriaRoast` evaluates to `None` 
  ).collect({case Some(criteria)  => criteria}).reduceLeftOption(_ || _).getOrElse(true:Column[Boolean])
}