{-# LANGUAGE
DataKinds
, OverloadedStrings
, TypeOperators
#-}
module Squeal.PostgreSQL.Expression.Logic
(
Condition
, true
, false
, not_
, (.&&)
, (.||)
, caseWhenThenElse
, ifThenElse
) where
import Squeal.PostgreSQL.Expression
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Type.Schema
type Condition grp lat with db params from =
Expression grp lat with db params from ('Null 'PGbool)
true :: Expr (null 'PGbool)
true :: forall (null :: PGType -> NullType). Expr (null 'PGbool)
true = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression ByteString
"TRUE"
false :: Expr (null 'PGbool)
false :: forall (null :: PGType -> NullType). Expr (null 'PGbool)
false = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression ByteString
"FALSE"
not_ :: null 'PGbool --> null 'PGbool
not_ :: forall (null :: PGType -> NullType). null 'PGbool --> null 'PGbool
not_ = forall (x :: NullType) (y :: NullType). ByteString -> x --> y
unsafeLeftOp ByteString
"NOT"
(.&&) :: Operator (null 'PGbool) (null 'PGbool) (null 'PGbool)
infixr 3 .&&
.&& :: forall (null :: PGType -> NullType).
Operator (null 'PGbool) (null 'PGbool) (null 'PGbool)
(.&&) = forall (ty0 :: NullType) (ty1 :: NullType) (ty2 :: NullType).
ByteString -> Operator ty0 ty1 ty2
unsafeBinaryOp ByteString
"AND"
(.||) :: Operator (null 'PGbool) (null 'PGbool) (null 'PGbool)
infixr 2 .||
.|| :: forall (null :: PGType -> NullType).
Operator (null 'PGbool) (null 'PGbool) (null 'PGbool)
(.||) = forall (ty0 :: NullType) (ty1 :: NullType) (ty2 :: NullType).
ByteString -> Operator ty0 ty1 ty2
unsafeBinaryOp ByteString
"OR"
caseWhenThenElse
:: [ ( Condition grp lat with db params from
, Expression grp lat with db params from ty
) ]
-> Expression grp lat with db params from ty
-> Expression grp lat with db params from ty
caseWhenThenElse :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
[(Condition grp lat with db params from,
Expression grp lat with db params from ty)]
-> Expression grp lat with db params from ty
-> Expression grp lat with db params from ty
caseWhenThenElse [(Condition grp lat with db params from,
Expression grp lat with db params from ty)]
whenThens Expression grp lat with db params from ty
else_ = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> Expression grp lat with db params from ty
UnsafeExpression forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat
[ ByteString
"CASE"
, forall a. Monoid a => [a] -> a
mconcat
[ forall a. Monoid a => [a] -> a
mconcat
[ ByteString
" WHEN ", forall sql. RenderSQL sql => sql -> ByteString
renderSQL Condition grp lat with db params from
when_
, ByteString
" THEN ", forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ty
then_
]
| (Condition grp lat with db params from
when_,Expression grp lat with db params from ty
then_) <- [(Condition grp lat with db params from,
Expression grp lat with db params from ty)]
whenThens
]
, ByteString
" ELSE ", forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ty
else_
, ByteString
" END"
]
ifThenElse
:: Condition grp lat with db params from
-> Expression grp lat with db params from ty
-> Expression grp lat with db params from ty
-> Expression grp lat with db params from ty
ifThenElse :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
Condition grp lat with db params from
-> Expression grp lat with db params from ty
-> Expression grp lat with db params from ty
-> Expression grp lat with db params from ty
ifThenElse Condition grp lat with db params from
if_ Expression grp lat with db params from ty
then_ Expression grp lat with db params from ty
else_ = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
[(Condition grp lat with db params from,
Expression grp lat with db params from ty)]
-> Expression grp lat with db params from ty
-> Expression grp lat with db params from ty
caseWhenThenElse [(Condition grp lat with db params from
if_,Expression grp lat with db params from ty
then_)] Expression grp lat with db params from ty
else_