on master help sign in
Document Rel8.Expr.Null
ab3a193Ollie Charles
src/Rel8/Expr/Null.hs+28
@@ -28,6 +28,9 @@ import Rel8.Type ( DBType, typeInformation )
28import Rel8.Type.Information ( TypeInformation )
28import Rel8.Type.Information ( TypeInformation )
29
29
30
30
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.
31nullify :: IsMaybe a ~ 'False => Expr a -> Expr (Maybe a)
34nullify :: IsMaybe a ~ 'False => Expr a -> Expr (Maybe a)
32nullify (Expr a) = Expr a
35nullify (Expr a) = Expr a
33
36
@@ -36,6 +39,13 @@ unsafeUnnullify :: Expr (Maybe a) -> Expr a
36unsafeUnnullify (Expr a) = Expr a
39unsafeUnnullify (Expr a) = Expr a
37
40
38
41
42+-- | Like 'maybe', but to eliminate @null@.
43+--
44+-- >>> select c $ pure $ null 0 id (nullExpr :: Expr (Maybe Int32))
45+-- [0]
46+--
47+-- >>> select c $ pure $ null 0 id (lit (Just 42) :: Expr (Maybe Int32))
48+-- [42]
39nullable :: Expr b -> (Expr a -> Expr b) -> Expr (Maybe a) -> Expr b
49nullable :: Expr b -> (Expr a -> Expr b) -> Expr (Maybe a) -> Expr b
40nullable b f ma = boolExpr (f (unsafeUnnullify ma)) b (isNull ma)
50nullable b f ma = boolExpr (f (unsafeUnnullify ma)) b (isNull ma)
41
51
@@ -45,19 +55,36 @@ nullableOf :: (IsMaybe a ~ 'False, DBType a)
45nullableOf = maybe null nullify
55nullableOf = maybe null nullify
46
56
47
57
58+-- | Like 'isNothing', but for @null@.
59+--
60+-- >>> select c $ pure $ isNull (nullExpr :: Expr (Maybe Int32))
61+-- [True]
62+--
63+-- >>> select c $ pure $ isNull (lit (Just 42) :: Expr (Maybe Int32))
64+-- [False]
48isNull :: Expr (Maybe a) -> Expr Bool
65isNull :: Expr (Maybe a) -> Expr Bool
49isNull = mapPrimExpr (Opaleye.UnExpr Opaleye.OpIsNull)
66isNull = mapPrimExpr (Opaleye.UnExpr Opaleye.OpIsNull)
50
67
51
68
69+-- | Like 'isJust', but for @null@.
52isNonNull :: Expr (Maybe a) -> Expr Bool
70isNonNull :: Expr (Maybe a) -> Expr Bool
53isNonNull = mapPrimExpr (Opaleye.UnExpr Opaleye.OpIsNotNull)
71isNonNull = mapPrimExpr (Opaleye.UnExpr Opaleye.OpIsNotNull)
54
72
55
73
74+-- | Lift an operation on non-@null@ values to an operation on possibly @null@
75+-- values. When given @null@, @mapNullable f@ returns @null@.
76+--
77+-- This is like 'fmap' for 'Maybe'.
56mapNullable :: (IsMaybe b ~ 'False, DBType b)
78mapNullable :: (IsMaybe b ~ 'False, DBType b)
57 => (Expr a -> Expr b) -> Expr (Maybe a) -> Expr (Maybe b)
79 => (Expr a -> Expr b) -> Expr (Maybe a) -> Expr (Maybe b)
58mapNullable f ma = boolExpr (unsafeMapNullable f ma) null (isNull ma)
80mapNullable f ma = boolExpr (unsafeMapNullable f ma) null (isNull ma)
59
81
60
82
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@.
86+--
87+-- This is like 'liftA2' for 'Maybe'.
61liftOpNullable :: (IsMaybe c ~ 'False, DBType c)
88liftOpNullable :: (IsMaybe c ~ 'False, DBType c)
62 => (Expr a -> Expr b -> Expr c)
89 => (Expr a -> Expr b -> Expr c)
63 -> Expr (Maybe a) -> Expr (Maybe b) -> Expr (Maybe c)
90 -> Expr (Maybe a) -> Expr (Maybe b) -> Expr (Maybe c)
@@ -71,6 +98,7 @@ snull nullability info =
71 scastExpr nullability info $ Expr $ Opaleye.ConstExpr Opaleye.NullLit
98 scastExpr nullability info $ Expr $ Opaleye.ConstExpr Opaleye.NullLit
72
99
73
100
101+-- | Corresponds to SQL @null@.
74null :: (IsMaybe a ~ 'False, DBType a) => Expr (Maybe a)
102null :: (IsMaybe a ~ 'False, DBType a) => Expr (Maybe a)
75null = snull Nullable typeInformation
103null = snull Nullable typeInformation
76
104