Created
September 25, 2015 13:36
-
-
Save leandrob13/09c6bfb333e106cf5931 to your computer and use it in GitHub Desktop.
Based on MaybeFilter (https://gist.github.com/cvogt/9193220), it uses implicit class to add the methods to the Query objects. FilteredBy works for filtering lists and it returns the query if no filters were applied. FoundBy works for finding a single register, if no filter defined it returns None.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit class OptionFilter[ X, Y ]( query: Query[ X, Y, Seq ] ) { | |
def filteredBy[ T ]( op: Option[ T ] )( f: ( X, T ) => Column[ Option[ Boolean ] ] ): Query[ X, Y, Seq ] = { | |
op map { o => query.filter( f( _, o ) ) } getOrElse { query } | |
} | |
def foundBy[ T ]( op: Option[ T ] )( f: ( X, T ) => Column[ Option[ Boolean ] ] ): Query[ X, Y, Seq ] = { | |
op map { o => query.filter( f( _, o ) ) } getOrElse { query.take( 0 ) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here you go, a full example in slick 3.1: https://medium.com/pragmatic-scala/filter-ops-with-slick-3-1-1b0c4ec59bb1#.vb2kipa2a