1{-# language DataKinds #-}
2{-# language FlexibleContexts #-}
3{-# language TypeFamilies #-}
5{-# options -fno-warn-redundant-constraints #-}
8 ( null, snull, nullable, nullableOf
10 , nullify, unsafeUnnullify
11 , mapNullable, liftOpNullable
12 , unsafeMapNullable, unsafeLiftOpNullable
17import Prelude hiding ( null )
20import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
23import {-# SOURCE #-} Rel8.Expr ( Expr( Expr ) )
24import Rel8.Expr.Bool ( (||.), boolExpr )
25import Rel8.Expr.Opaleye ( scastExpr, mapPrimExpr )
26import Rel8.Schema.Nullability ( Nullability( Nullable ), IsMaybe )
27import Rel8.Type ( DBType, typeInformation )
28import Rel8.Type.Information ( TypeInformation )
31-- | Lift an expression that can't be @null@ to a type that might be @null@.
32-- This is an identity operation in terms of any generated query, and just
33-- modifies the query's type.
34nullify :: IsMaybe a ~ 'False => Expr a -> Expr (Maybe a)
35nullify (Expr a) = Expr a
38unsafeUnnullify :: Expr (Maybe a) -> Expr a
39unsafeUnnullify (Expr a) = Expr a
42-- | Like 'maybe', but to eliminate @null@.
44-- >>> select c $ pure $ null 0 id (nullExpr :: Expr (Maybe Int32))
47-- >>> select c $ pure $ null 0 id (lit (Just 42) :: Expr (Maybe Int32))
49nullable :: Expr b -> (Expr a -> Expr b) -> Expr (Maybe a) -> Expr b
50nullable b f ma = boolExpr (f (unsafeUnnullify ma)) b (isNull ma)
53nullableOf :: (IsMaybe a ~ 'False, DBType a)
54 => Maybe (Expr a) -> Expr (Maybe a)
55nullableOf = maybe null nullify
58-- | Like 'isNothing', but for @null@.
60-- >>> select c $ pure $ isNull (nullExpr :: Expr (Maybe Int32))
63-- >>> select c $ pure $ isNull (lit (Just 42) :: Expr (Maybe Int32))
65isNull :: Expr (Maybe a) -> Expr Bool
66isNull = mapPrimExpr (Opaleye.UnExpr Opaleye.OpIsNull)
69-- | Like 'isJust', but for @null@.
70isNonNull :: Expr (Maybe a) -> Expr Bool
71isNonNull = mapPrimExpr (Opaleye.UnExpr Opaleye.OpIsNotNull)
74-- | Lift an operation on non-@null@ values to an operation on possibly @null@
75-- values. When given @null@, @mapNullable f@ returns @null@.
77-- This is like 'fmap' for 'Maybe'.
78mapNullable :: (IsMaybe b ~ 'False, DBType b)
79 => (Expr a -> Expr b) -> Expr (Maybe a) -> Expr (Maybe b)
80mapNullable f ma = boolExpr (unsafeMapNullable f ma) null (isNull ma)
83-- | Lift a binary operation on non-@null@ expressions to an equivalent binary
84-- operator on possibly @null@ expressions. If either of the final arguments
85-- are @null@, @liftOpNullable@ returns @null@.
87-- This is like 'liftA2' for 'Maybe'.
88liftOpNullable :: (IsMaybe c ~ 'False, DBType c)
89 => (Expr a -> Expr b -> Expr c)
90 -> Expr (Maybe a) -> Expr (Maybe b) -> Expr (Maybe c)
91liftOpNullable f ma mb =
92 boolExpr (unsafeLiftOpNullable f ma mb) null
93 (isNull ma ||. isNull mb)
96snull :: Nullability a (Maybe a) -> TypeInformation a -> Expr (Maybe a)
97snull nullability info =
98 scastExpr nullability info $ Expr $ Opaleye.ConstExpr Opaleye.NullLit
101-- | Corresponds to SQL @null@.
102null :: (IsMaybe a ~ 'False, DBType a) => Expr (Maybe a)
103null = snull Nullable typeInformation
106unsafeMapNullable :: IsMaybe b ~ 'False
107 => (Expr a -> Expr b) -> Expr (Maybe a) -> Expr (Maybe b)
108unsafeMapNullable f ma = nullify (f (unsafeUnnullify ma))
111unsafeLiftOpNullable :: IsMaybe c ~ 'False
112 => (Expr a -> Expr b -> Expr c)
113 -> Expr (Maybe a) -> Expr (Maybe b) -> Expr (Maybe c)
114unsafeLiftOpNullable f ma mb =
115 nullify (f (unsafeUnnullify ma) (unsafeUnnullify mb))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list