on 4e90af3 help sign in
rel8/src/Rel8/Query/Filter.hs
35 lines 954 B history raw
1module Rel8.Query.Filter
2 ( filter
3 , where_
4 )
5where
7-- base
8import Prelude hiding ( filter )
10-- opaleye
11import qualified Opaleye.Operators as Opaleye
13-- profunctors
14import Data.Profunctor ( lmap )
16-- rel8
17import Rel8.Expr ( Expr )
18import Rel8.Expr.Opaleye ( toColumn, toPrimExpr )
19import Rel8.Query ( Query )
20import Rel8.Query.Opaleye ( fromOpaleye )
23-- | @filter f x@ will be a zero-row query when @f x@ is @False@, and will
24-- return @x@ unchanged when @f x@ is @True@. This is similar to
25-- 'Control.Monad.guard', but as the predicate is separate from the argument,
26-- it is easy to use in a pipeline of 'Query' transformations.
27filter :: (a -> Expr Bool) -> a -> Query a
28filter f a = a <$ where_ (f a)
31-- | Drop any rows that don't match a predicate. @where_ expr@ is equivalent
32-- to the SQL @WHERE expr@.
33where_ :: Expr Bool -> Query ()
34where_ condition =
35 fromOpaleye $ lmap (\_ -> toColumn $ toPrimExpr condition) Opaleye.restrict