12import Data.Foldable ( foldl' )
13import Prelude hiding ( null )
16import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
19import {-# SOURCE #-} Rel8.Expr ( Expr( Expr ) )
20import Rel8.Expr.Opaleye ( mapPrimExpr, zipPrimExprsWith )
21import Rel8.Expr.Serialize ( litExpr )
24-- | The SQL @false@ literal.
29-- | The SQL @true@ literal.
34-- | The SQL @AND@ operator.
35(&&.) :: Expr Bool -> Expr Bool -> Expr Bool
36(&&.) = zipPrimExprsWith (Opaleye.BinExpr Opaleye.OpAnd)
40-- | The SQL @OR@ operator.
41(||.) :: Expr Bool -> Expr Bool -> Expr Bool
42(||.) = zipPrimExprsWith (Opaleye.BinExpr Opaleye.OpOr)
46-- | The SQL @NOT@ operator.
47not_ :: Expr Bool -> Expr Bool
48not_ = mapPrimExpr (Opaleye.UnExpr Opaleye.OpNot)
51-- | Fold @AND@ over a collection of expressions.
52and_ :: Foldable f => f (Expr Bool) -> Expr Bool
53and_ = foldl' (&&.) true
56-- | Fold @OR@ over a collection of expressions.
57or_ :: Foldable f => f (Expr Bool) -> Expr Bool
58or_ = foldl' (||.) false
61-- | Eliminate a boolean-valued expression.
63-- Corresponds to 'Data.Bool.bool'.
64boolExpr :: Expr a -> Expr a -> Expr Bool -> Expr a
65boolExpr ifFalse ifTrue condition = caseExpr [(condition, ifTrue)] ifFalse
68-- | A multi-way if/then/else statement. The first argument to @caseExpr@ is a
69-- list of alternatives. The first alternative that is of the form @(true, x)@
70-- will be returned. If no such alternative is found, a fallback expression is
73-- Corresponds to a @CASE@ expression in SQL.
74caseExpr :: [(Expr Bool, Expr a)] -> Expr a -> Expr a
75caseExpr branches (Expr fallback) =
76 Expr $ Opaleye.CaseExpr (map go branches) fallback
78 go (Expr condition, Expr value) = (condition, value)
81-- | Convert a @Expr (Maybe Bool)@ to a @Expr Bool@ by treating @Nothing@ as
82-- @False@. This can be useful when combined with 'Rel8.where_', which expects
83-- a @Bool@, and produces expressions that optimize better than general case
85coalesce :: Expr (Maybe Bool) -> Expr Bool
86coalesce (Expr a) = Expr a &&. Expr (Opaleye.FunExpr "COALESCE" [a, untrue])
88 untrue = Opaleye.ConstExpr (Opaleye.BoolLit False)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list