{-# language DataKinds #-}
{-# language FlexibleContexts #-}
{-# language TypeFamilies #-}
{-# options -fno-warn-redundant-constraints #-}
module Rel8.Expr.Null
( null, snull, nullableExpr, nullableOf
, isNull, isNonNull
, nullify, unsafeUnnullify
, mapNull, liftOpNull
, unsafeMapNull, unsafeLiftOpNull
)
where
import Prelude hiding ( null )
import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
import {-# SOURCE #-} Rel8.Expr ( Expr( Expr ) )
import Rel8.Expr.Bool ( (||.), boolExpr )
import Rel8.Expr.Opaleye ( scastExpr, mapPrimExpr )
import Rel8.Schema.Null ( NotNull )
import Rel8.Type ( DBType, typeInformation )
import Rel8.Type.Information ( TypeInformation )
nullify :: NotNull a => Expr a -> Expr (Maybe a)
nullify :: Expr a -> Expr (Maybe a)
nullify (Expr PrimExpr
a) = PrimExpr -> Expr (Maybe a)
forall k (a :: k). (k ~ *) => PrimExpr -> Expr a
Expr PrimExpr
a
unsafeUnnullify :: Expr (Maybe a) -> Expr a
unsafeUnnullify :: Expr (Maybe a) -> Expr a
unsafeUnnullify (Expr PrimExpr
a) = PrimExpr -> Expr a
forall k (a :: k). (k ~ *) => PrimExpr -> Expr a
Expr PrimExpr
a
nullableExpr :: Expr b -> (Expr a -> Expr b) -> Expr (Maybe a) -> Expr b
nullableExpr :: Expr b -> (Expr a -> Expr b) -> Expr (Maybe a) -> Expr b
nullableExpr Expr b
b Expr a -> Expr b
f Expr (Maybe a)
ma = Expr b -> Expr b -> Expr Bool -> Expr b
forall a. Expr a -> Expr a -> Expr Bool -> Expr a
boolExpr (Expr a -> Expr b
f (Expr (Maybe a) -> Expr a
forall a. Expr (Maybe a) -> Expr a
unsafeUnnullify Expr (Maybe a)
ma)) Expr b
b (Expr (Maybe a) -> Expr Bool
forall a. Expr (Maybe a) -> Expr Bool
isNull Expr (Maybe a)
ma)
nullableOf :: DBType a => Maybe (Expr a) -> Expr (Maybe a)
nullableOf :: Maybe (Expr a) -> Expr (Maybe a)
nullableOf = Expr (Maybe a)
-> (Expr a -> Expr (Maybe a)) -> Maybe (Expr a) -> Expr (Maybe a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Expr (Maybe a)
forall a. DBType a => Expr (Maybe a)
null Expr a -> Expr (Maybe a)
forall a. NotNull a => Expr a -> Expr (Maybe a)
nullify
isNull :: Expr (Maybe a) -> Expr Bool
isNull :: Expr (Maybe a) -> Expr Bool
isNull = (PrimExpr -> PrimExpr) -> Expr (Maybe a) -> Expr Bool
forall a b. (PrimExpr -> PrimExpr) -> Expr a -> Expr b
mapPrimExpr (UnOp -> PrimExpr -> PrimExpr
Opaleye.UnExpr UnOp
Opaleye.OpIsNull)
isNonNull :: Expr (Maybe a) -> Expr Bool
isNonNull :: Expr (Maybe a) -> Expr Bool
isNonNull = (PrimExpr -> PrimExpr) -> Expr (Maybe a) -> Expr Bool
forall a b. (PrimExpr -> PrimExpr) -> Expr a -> Expr b
mapPrimExpr (UnOp -> PrimExpr -> PrimExpr
Opaleye.UnExpr UnOp
Opaleye.OpIsNotNull)
mapNull :: DBType b
=> (Expr a -> Expr b) -> Expr (Maybe a) -> Expr (Maybe b)
mapNull :: (Expr a -> Expr b) -> Expr (Maybe a) -> Expr (Maybe b)
mapNull Expr a -> Expr b
f Expr (Maybe a)
ma = Expr (Maybe b) -> Expr (Maybe b) -> Expr Bool -> Expr (Maybe b)
forall a. Expr a -> Expr a -> Expr Bool -> Expr a
boolExpr ((Expr a -> Expr b) -> Expr (Maybe a) -> Expr (Maybe b)
forall b a.
NotNull b =>
(Expr a -> Expr b) -> Expr (Maybe a) -> Expr (Maybe b)
unsafeMapNull Expr a -> Expr b
f Expr (Maybe a)
ma) Expr (Maybe b)
forall a. DBType a => Expr (Maybe a)
null (Expr (Maybe a) -> Expr Bool
forall a. Expr (Maybe a) -> Expr Bool
isNull Expr (Maybe a)
ma)
liftOpNull :: DBType c
=> (Expr a -> Expr b -> Expr c)
-> Expr (Maybe a) -> Expr (Maybe b) -> Expr (Maybe c)
liftOpNull :: (Expr a -> Expr b -> Expr c)
-> Expr (Maybe a) -> Expr (Maybe b) -> Expr (Maybe c)
liftOpNull Expr a -> Expr b -> Expr c
f Expr (Maybe a)
ma Expr (Maybe b)
mb =
Expr (Maybe c) -> Expr (Maybe c) -> Expr Bool -> Expr (Maybe c)
forall a. Expr a -> Expr a -> Expr Bool -> Expr a
boolExpr ((Expr a -> Expr b -> Expr c)
-> Expr (Maybe a) -> Expr (Maybe b) -> Expr (Maybe c)
forall c a b.
NotNull c =>
(Expr a -> Expr b -> Expr c)
-> Expr (Maybe a) -> Expr (Maybe b) -> Expr (Maybe c)
unsafeLiftOpNull Expr a -> Expr b -> Expr c
f Expr (Maybe a)
ma Expr (Maybe b)
mb) Expr (Maybe c)
forall a. DBType a => Expr (Maybe a)
null
(Expr (Maybe a) -> Expr Bool
forall a. Expr (Maybe a) -> Expr Bool
isNull Expr (Maybe a)
ma Expr Bool -> Expr Bool -> Expr Bool
||. Expr (Maybe b) -> Expr Bool
forall a. Expr (Maybe a) -> Expr Bool
isNull Expr (Maybe b)
mb)
{-# INLINABLE liftOpNull #-}
snull :: TypeInformation a -> Expr (Maybe a)
snull :: TypeInformation a -> Expr (Maybe a)
snull TypeInformation a
info = TypeInformation (Unnullify (Maybe a))
-> Expr (Maybe a) -> Expr (Maybe a)
forall a. TypeInformation (Unnullify a) -> Expr a -> Expr a
scastExpr TypeInformation a
TypeInformation (Unnullify (Maybe a))
info (Expr (Maybe a) -> Expr (Maybe a))
-> Expr (Maybe a) -> Expr (Maybe a)
forall a b. (a -> b) -> a -> b
$ PrimExpr -> Expr (Maybe a)
forall k (a :: k). (k ~ *) => PrimExpr -> Expr a
Expr (PrimExpr -> Expr (Maybe a)) -> PrimExpr -> Expr (Maybe a)
forall a b. (a -> b) -> a -> b
$ Literal -> PrimExpr
Opaleye.ConstExpr Literal
Opaleye.NullLit
null :: DBType a => Expr (Maybe a)
null :: Expr (Maybe a)
null = TypeInformation a -> Expr (Maybe a)
forall a. TypeInformation a -> Expr (Maybe a)
snull TypeInformation a
forall a. DBType a => TypeInformation a
typeInformation
unsafeMapNull :: NotNull b
=> (Expr a -> Expr b) -> Expr (Maybe a) -> Expr (Maybe b)
unsafeMapNull :: (Expr a -> Expr b) -> Expr (Maybe a) -> Expr (Maybe b)
unsafeMapNull Expr a -> Expr b
f Expr (Maybe a)
ma = Expr b -> Expr (Maybe b)
forall a. NotNull a => Expr a -> Expr (Maybe a)
nullify (Expr a -> Expr b
f (Expr (Maybe a) -> Expr a
forall a. Expr (Maybe a) -> Expr a
unsafeUnnullify Expr (Maybe a)
ma))
unsafeLiftOpNull :: NotNull c
=> (Expr a -> Expr b -> Expr c)
-> Expr (Maybe a) -> Expr (Maybe b) -> Expr (Maybe c)
unsafeLiftOpNull :: (Expr a -> Expr b -> Expr c)
-> Expr (Maybe a) -> Expr (Maybe b) -> Expr (Maybe c)
unsafeLiftOpNull Expr a -> Expr b -> Expr c
f Expr (Maybe a)
ma Expr (Maybe b)
mb =
Expr c -> Expr (Maybe c)
forall a. NotNull a => Expr a -> Expr (Maybe a)
nullify (Expr a -> Expr b -> Expr c
f (Expr (Maybe a) -> Expr a
forall a. Expr (Maybe a) -> Expr a
unsafeUnnullify Expr (Maybe a)
ma) (Expr (Maybe b) -> Expr b
forall a. Expr (Maybe a) -> Expr a
unsafeUnnullify Expr (Maybe b)
mb))