Dynamic OR filtering - Slick

∥☆過路亽.° 提交于 2019-12-01 11:33:36

Something like this should work. I had to use a similiar fragment in my own code and it is also close to what cvogt proposes in above comment (I think).

val username = Option("")
val petname = Option("")
val ff:Option[String] = None

val default = LiteralColumn(1) === LiteralColumn(1) 

yourTable.filter { it => 
  List(
      username.map(it.username === _),
      petname.map(it.petname === _),
      ff.map(it.ff === _)
  ).collect({case Some(it)  => it}).reduceLeftOption(_ || _).getOrElse(default)
}
Sebastien Lorber

The thoefer is nice for simple use cases but has some limits. Like if all your options are None's the list is empty and you can't reduce an empty list :)

If you need something more composable, based on predicate, conjunctions and disjunctions (a bit like Hibernate/JPA Criteria API), you can check my answer in Slick: create query conjunctions/disjunctions dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!