I want join an arbitrary-length list of filters with or
. If the list would be fixed-length, it would look like this:
query.filter(filters(0) || filters(1) || … || filter(n))
Joining filters with and
would be easy:
for (filter ← filters)
query = query.filter(filter)
Joining things that evaluate to Boolean
s with or
is easy, too:
val any = evaluateToBools.foldLeft(true)(
(left: Boolean, right: Eval2Bool) =>
left || right.evaluate
)
Update:
as i wrote it, it would be easy, if scalaquery’s filter
was a standard one. unfortunately, scalaquery only allows these filters to be executed by the sql engine.
so my specific question would be: if i have a set of string tuples:
val tms = Set( ("A","a"), ("B", "b"), ... )
and a query with the two columns “t” and “m”,
how can i generate a filter that represents the following SQL:
... WHERE/AND ( (t="A" and m="a") or (t="B" and m="b") or ... )
…or can sql in
operators be used with tuples like this?
... WHERE (t,m) IN (("A","a"), ("B","b"), ...)
and if so, how to do it in scalaquery
Hack:
currently, i do the following:
val tms = markers map { tm ⇒ tm._1 +"||"+ tm._2 }
query.filter(d ⇒ d._4 ++"||"++ d._5 inSet tms)
…but that’s unbearably hacky.
Solution
I implemented Stefan’s solution like this:
rq = rq filter { d ⇒
markers map { tm ⇒
(d._4 is tm._1) && (d._5 is tm._2)
} reduceLeft { _||_ }
}