{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
module Opaleye.Field (
Field_,
Field,
FieldNullable,
Nullability(..),
unsafeCoerceField,
Opaleye.Field.null,
isNull,
matchNullable,
fromNullable,
toNullable,
maybeToNullable,
) where
import Prelude hiding (null)
import Opaleye.Internal.Column
(Field_(Column), FieldNullable, Field, Nullability(NonNullable, Nullable))
import qualified Opaleye.Internal.Column as C
import qualified Opaleye.Internal.PGTypesExternal as T
import qualified Opaleye.Internal.HaskellDB.PrimQuery as HPQ
null :: FieldNullable a
null :: FieldNullable a
null = PrimExpr -> FieldNullable a
forall (n :: Nullability) sqlType. PrimExpr -> Field_ n sqlType
Column (Literal -> PrimExpr
HPQ.ConstExpr Literal
HPQ.NullLit)
isNull :: FieldNullable a -> Field T.PGBool
isNull :: FieldNullable a -> Field PGBool
isNull = UnOp -> FieldNullable a -> Field PGBool
forall (n :: Nullability) a (n' :: Nullability) b.
UnOp -> Field_ n a -> Field_ n' b
C.unOp UnOp
HPQ.OpIsNull
matchNullable :: Field b
-> (Field a -> Field b)
-> FieldNullable a
-> Field b
matchNullable :: Field b -> (Field a -> Field b) -> FieldNullable a -> Field b
matchNullable Field b
replacement Field a -> Field b
f FieldNullable a
x = Field PGBool -> Field b -> Field b -> Field b
forall (n' :: Nullability) pgBool (n :: Nullability) a.
Field_ n' pgBool -> Field_ n a -> Field_ n a -> Field_ n a
C.unsafeIfThenElse (FieldNullable a -> Field PGBool
forall a. FieldNullable a -> Field PGBool
isNull FieldNullable a
x) Field b
replacement
(Field a -> Field b
f (FieldNullable a -> Field a
forall (n :: Nullability) a (n' :: Nullability) b.
Field_ n a -> Field_ n' b
unsafeCoerceField FieldNullable a
x))
fromNullable :: Field a
-> FieldNullable a
-> Field a
fromNullable :: Field a -> FieldNullable a -> Field a
fromNullable = (Field a -> (Field a -> Field a) -> FieldNullable a -> Field a)
-> (Field a -> Field a) -> Field a -> FieldNullable a -> Field a
forall a b c. (a -> b -> c) -> b -> a -> c
flip Field a -> (Field a -> Field a) -> FieldNullable a -> Field a
forall b a.
Field b -> (Field a -> Field b) -> FieldNullable a -> Field b
matchNullable Field a -> Field a
forall a. a -> a
id
toNullable :: Field a -> FieldNullable a
toNullable :: Field a -> FieldNullable a
toNullable = Field a -> FieldNullable a
forall (n :: Nullability) a (n' :: Nullability) b.
Field_ n a -> Field_ n' b
C.unsafeCoerceColumn
maybeToNullable :: Maybe (Field a)
-> FieldNullable a
maybeToNullable :: Maybe (Field a) -> FieldNullable a
maybeToNullable = FieldNullable a
-> (Field a -> FieldNullable a)
-> Maybe (Field a)
-> FieldNullable a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe FieldNullable a
forall a. FieldNullable a
null Field a -> FieldNullable a
forall a. Field a -> FieldNullable a
toNullable
unsafeCoerceField :: Field_ n a -> Field_ n' b
unsafeCoerceField :: Field_ n a -> Field_ n' b
unsafeCoerceField = Field_ n a -> Field_ n' b
forall (n :: Nullability) a (n' :: Nullability) b.
Field_ n a -> Field_ n' b
C.unsafeCoerceColumn