动态查询条件 Slick 3.0
Dynamic query conditions Slick 3.0
我正在尝试使用可选参数过滤 table。使用 slick 2.1 这可行,但是当我转到 3.0 版时不再有效,错误是: "can not resolve symbol &&" 当我调用函数 "reduce".
如果有任何帮助,我将不胜感激..非常感谢
这是代码:
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)
这是来自文档的信息 http://slick.typesafe.com/doc/3.0.0/queries.html#sorting-and-filtering
我想你可以在这里找到你想要的。来自文档的示例:
//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])
}
我正在尝试使用可选参数过滤 table。使用 slick 2.1 这可行,但是当我转到 3.0 版时不再有效,错误是: "can not resolve symbol &&" 当我调用函数 "reduce".
如果有任何帮助,我将不胜感激..非常感谢
这是代码:
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)
这是来自文档的信息 http://slick.typesafe.com/doc/3.0.0/queries.html#sorting-and-filtering
我想你可以在这里找到你想要的。来自文档的示例:
//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])
}