{-# language GADTs #-}
module Rel8.Expr.Bool
( false, true
, (&&.), (||.), not_
, and_, or_
, boolExpr
, caseExpr
, coalesce
)
where
import Data.Foldable ( foldl' )
import Prelude hiding ( null )
import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
import {-# SOURCE #-} Rel8.Expr ( Expr( Expr ) )
import Rel8.Expr.Opaleye ( mapPrimExpr, toPrimExpr, zipPrimExprsWith )
import Rel8.Expr.Serialize ( litExpr )
false :: Expr Bool
false :: Expr Bool
false = Bool -> Expr Bool
forall a. Sql DBType a => a -> Expr a
litExpr Bool
False
true :: Expr Bool
true :: Expr Bool
true = Bool -> Expr Bool
forall a. Sql DBType a => a -> Expr a
litExpr Bool
True
(&&.) :: Expr Bool -> Expr Bool -> Expr Bool
&&. :: Expr Bool -> Expr Bool -> Expr Bool
(&&.) = (PrimExpr -> PrimExpr -> PrimExpr)
-> Expr Bool -> Expr Bool -> Expr Bool
forall a b c.
(PrimExpr -> PrimExpr -> PrimExpr) -> Expr a -> Expr b -> Expr c
zipPrimExprsWith (BinOp -> PrimExpr -> PrimExpr -> PrimExpr
Opaleye.BinExpr BinOp
Opaleye.OpAnd)
infixr 3 &&.
(||.) :: Expr Bool -> Expr Bool -> Expr Bool
||. :: Expr Bool -> Expr Bool -> Expr Bool
(||.) = (PrimExpr -> PrimExpr -> PrimExpr)
-> Expr Bool -> Expr Bool -> Expr Bool
forall a b c.
(PrimExpr -> PrimExpr -> PrimExpr) -> Expr a -> Expr b -> Expr c
zipPrimExprsWith (BinOp -> PrimExpr -> PrimExpr -> PrimExpr
Opaleye.BinExpr BinOp
Opaleye.OpOr)
infixr 2 ||.
not_ :: Expr Bool -> Expr Bool
not_ :: Expr Bool -> Expr Bool
not_ = (PrimExpr -> PrimExpr) -> Expr Bool -> Expr Bool
forall a b. (PrimExpr -> PrimExpr) -> Expr a -> Expr b
mapPrimExpr (UnOp -> PrimExpr -> PrimExpr
Opaleye.UnExpr UnOp
Opaleye.OpNot)
and_ :: Foldable f => f (Expr Bool) -> Expr Bool
and_ :: f (Expr Bool) -> Expr Bool
and_ = (Expr Bool -> Expr Bool -> Expr Bool)
-> Expr Bool -> f (Expr Bool) -> Expr Bool
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Expr Bool -> Expr Bool -> Expr Bool
(&&.) Expr Bool
true
or_ :: Foldable f => f (Expr Bool) -> Expr Bool
or_ :: f (Expr Bool) -> Expr Bool
or_ = (Expr Bool -> Expr Bool -> Expr Bool)
-> Expr Bool -> f (Expr Bool) -> Expr Bool
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Expr Bool -> Expr Bool -> Expr Bool
(||.) Expr Bool
false
boolExpr :: Expr a -> Expr a -> Expr Bool -> Expr a
boolExpr :: Expr a -> Expr a -> Expr Bool -> Expr a
boolExpr Expr a
ifFalse Expr a
ifTrue Expr Bool
condition = [(Expr Bool, Expr a)] -> Expr a -> Expr a
forall a. [(Expr Bool, Expr a)] -> Expr a -> Expr a
caseExpr [(Expr Bool
condition, Expr a
ifTrue)] Expr a
ifFalse
caseExpr :: [(Expr Bool, Expr a)] -> Expr a -> Expr a
caseExpr :: [(Expr Bool, Expr a)] -> Expr a -> Expr a
caseExpr [(Expr Bool, Expr a)]
branches (Expr PrimExpr
fallback) =
PrimExpr -> Expr a
forall k (a :: k). (k ~ *) => PrimExpr -> Expr a
Expr (PrimExpr -> Expr a) -> PrimExpr -> Expr a
forall a b. (a -> b) -> a -> b
$ [(PrimExpr, PrimExpr)] -> PrimExpr -> PrimExpr
Opaleye.CaseExpr (((Expr Bool, Expr a) -> (PrimExpr, PrimExpr))
-> [(Expr Bool, Expr a)] -> [(PrimExpr, PrimExpr)]
forall a b. (a -> b) -> [a] -> [b]
map (Expr Bool, Expr a) -> (PrimExpr, PrimExpr)
forall a a. (Expr a, Expr a) -> (PrimExpr, PrimExpr)
go [(Expr Bool, Expr a)]
branches) PrimExpr
fallback
where
go :: (Expr a, Expr a) -> (PrimExpr, PrimExpr)
go (Expr a
condition, Expr a
value) = (Expr a -> PrimExpr
forall a. Expr a -> PrimExpr
toPrimExpr Expr a
condition, Expr a -> PrimExpr
forall a. Expr a -> PrimExpr
toPrimExpr Expr a
value)
coalesce :: Expr (Maybe Bool) -> Expr Bool
coalesce :: Expr (Maybe Bool) -> Expr Bool
coalesce (Expr PrimExpr
a) = PrimExpr -> Expr Bool
forall k (a :: k). (k ~ *) => PrimExpr -> Expr a
Expr PrimExpr
a Expr Bool -> Expr Bool -> Expr Bool
&&. PrimExpr -> Expr Bool
forall k (a :: k). (k ~ *) => PrimExpr -> Expr a
Expr (Name -> [PrimExpr] -> PrimExpr
Opaleye.FunExpr Name
"COALESCE" [PrimExpr
a, PrimExpr
untrue])
where
untrue :: PrimExpr
untrue = Literal -> PrimExpr
Opaleye.ConstExpr (Bool -> Literal
Opaleye.BoolLit Bool
False)