I'm trying to implement a method to return filtered results, based on a set of parameters which may or may not be set. It doesn't seem like chaining multiple filters is possible conditionally, i.e. starting off with one filter...
val slickFlights = TableQuery[Flights]
val query = slickFlights.filter(_.departureLocation === params("departureLocation").toString)
Conditionally adding another filter to the query (if it exists in the Map of params) doesn't seem to work...
if (params.contains("arrivalLocation")) {
query.filter(_.arrivalLocation === params("arrivalLocation").toString)
}
Can this sort of conditional filtering be done using Slick through other means?
I've come across the MaybeFilter: https://gist.github.com/cvogt/9193220, which seems to be a decent approach for handling exactly this. However it doesn't seem to work with Slick 3.x
Following Hüseyin's suggestions below, I have also tried the following:
def search(departureLocation: Option[String], arrivalLocation: Option[String]) = {
val query = slickFlights.filter(flight =>
departureLocation.map {
param => param === flight.departureLocation
})
Where slickFlights
is a TableQuery object val slickFlights = TableQuery[Flights]
. However this produces the following compilation error:
value === is not a member of String
Intellij also complains about the === being an unknown symbol. Doesn't work with == either.