{-# LANGUAGE
DataKinds
, OverloadedStrings
, RankNTypes
, TypeOperators
#-}
module Squeal.PostgreSQL.Expression.Null
(
null_
, notNull
, unsafeNotNull
, monoNotNull
, coalesce
, fromNull
, isNull
, isNotNull
, matchNull
, nullIf
) where
import Squeal.PostgreSQL.Expression
import Squeal.PostgreSQL.Expression.Logic
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Type.Schema
null_ :: Expr ('Null ty)
null_ = UnsafeExpression "NULL"
notNull :: 'NotNull ty --> 'Null ty
notNull = UnsafeExpression . renderSQL
unsafeNotNull :: 'Null ty --> 'NotNull ty
unsafeNotNull = UnsafeExpression . renderSQL
monoNotNull
:: (forall null. Expression grp lat with db params from (null ty))
-> Expression grp lat with db params from ('NotNull ty)
monoNotNull = id
coalesce :: FunctionVar ('Null ty) (null ty) (null ty)
coalesce nullxs notNullx = UnsafeExpression $
"COALESCE" <> parenthesized (commaSeparated
((renderSQL <$> nullxs) <> [renderSQL notNullx]))
fromNull
:: Expression grp lat with db params from ('NotNull ty)
-> Expression grp lat with db params from ('Null ty)
-> Expression grp lat with db params from ('NotNull ty)
fromNull notNullx nullx = coalesce [nullx] notNullx
isNull :: 'Null ty --> null 'PGbool
isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
isNotNull :: 'Null ty --> null 'PGbool
isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
matchNull
:: Expression grp lat with db params from (nullty)
-> ( Expression grp lat with db params from ('NotNull ty)
-> Expression grp lat with db params from (nullty) )
-> Expression grp lat with db params from ('Null ty)
-> Expression grp lat with db params from (nullty)
matchNull y f x = ifThenElse (isNull x) y
(f (UnsafeExpression (renderSQL x)))
nullIf :: '[ 'NotNull ty, 'NotNull ty] ---> 'Null ty
nullIf = unsafeFunctionN "NULLIF"