Reputation: 1081
I have the following Slick function that has a filter (d.sk < sk)
:
def func1(sk: Int) = {
val date2 = TableQuery[DatesDB]
val action = date2.filter( d => (d.sk < sk)).result
val future = db.run(action.asTry)
future.map {
case Success(s) =>
if (s.length > 0)
Some(s(0))
else
None
case Failure(e) => throw new Exception("Failure")
}
}
What I need is to add a feature to this function that depending on a condition X the filter should be either the existent one or (d.sk <= sk)
. Since filters are not variables I don't know how to achieve this, and I prefer not to rewrite the filters or actions. Any ideas?
Upvotes: 0
Views: 1808
Reputation: 23798
Cyrille's link seems to be about a bit different case but that approach might be extended to cover your case as well. Still if this is just one place, what's wrong with good old if
:
val date2 = TableQuery[DatesDB]
val query = if (condition) date2.filter(d => (d.sk < sk)) else date2.filter(d => (d.sk <= sk))
val action = query.result
Another idea
val date2 = TableQuery[DatesDB]
val action = date2.filter(d => ((LiteralColumn(condition) && (d.sk < sk)) || (LiteralColumn(!condition) && (d.sk <= sk))).result
Upvotes: 1