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.
37-- mapM_ print =<< select c do
38-- x <- values [lit True, lit False]
39-- y <- values [lit True, lit False]
40-- return (x, y, x &&. y)
46(&&.) :: Expr Bool -> Expr Bool -> Expr Bool
47(&&.) = zipPrimExprsWith (Opaleye.BinExpr Opaleye.OpAnd)
51-- | The SQL @OR@ operator.
54-- mapM_ print =<< select c do
55-- x <- values [lit True, lit False]
56-- y <- values [lit True, lit False]
57-- return (x, y, x ||. y)
63(||.) :: Expr Bool -> Expr Bool -> Expr Bool
64(||.) = zipPrimExprsWith (Opaleye.BinExpr Opaleye.OpOr)
68-- | The SQL @NOT@ operator.
70-- >>> select c $ pure $ not_ $ lit True
73-- >>> select c $ pure $ not_ $ lit False
75not_ :: Expr Bool -> Expr Bool
76not_ = mapPrimExpr (Opaleye.UnExpr Opaleye.OpNot)
79-- | Fold @AND@ over a collection of expressions.
81-- >>> select c $ pure $ and_ [ lit True ==. lit False, lit False, lit True ]
84-- >>> select c $ pure $ and_ []
86and_ :: Foldable f => f (Expr Bool) -> Expr Bool
87and_ = foldl' (&&.) true
90-- | Fold @OR@ over a collection of expressions.
92-- >>> select c $ pure $ or_ [ lit True ==. lit False, lit False, lit True ]
95-- >>> select c $ pure $ or_ []
97or_ :: Foldable f => f (Expr Bool) -> Expr Bool
98or_ = foldl' (||.) false
101-- | Eliminate a boolean-valued expression.
103-- Corresponds to 'Data.Bool.bool'.
104boolExpr :: Expr a -> Expr a -> Expr Bool -> Expr a
105boolExpr ifFalse ifTrue condition = caseExpr [(condition, ifTrue)] ifFalse
108-- | A multi-way if/then/else statement. The first argument to @caseExpr@ is a
109-- list of alternatives. The first alternative that is of the form @(true, x)@
110-- will be returned. If no such alternative is found, a fallback expression is
113-- Corresponds to a @CASE@ expression in SQL.
114caseExpr :: [(Expr Bool, Expr a)] -> Expr a -> Expr a
115caseExpr branches (Expr fallback) =
116 Expr $ Opaleye.CaseExpr (map go branches) fallback
118 go (Expr condition, Expr value) = (condition, value)
121coalesce :: Expr (Maybe Bool) -> Expr Bool
122coalesce (Expr a) = Expr a &&. Expr (Opaleye.FunExpr "COALESCE" [a, untrue])
124 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