@@ -28,6 +28,9 @@ import Rel8.Type ( DBType, typeInformation )
28import Rel8.Type.Information ( 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.
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
@@ -36,6 +39,13 @@ unsafeUnnullify :: Expr (Maybe a) -> Expr a
36unsafeUnnullify (Expr 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))
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)
@@ -45,19 +55,36 @@ nullableOf :: (IsMaybe a ~ 'False, DBType a)
45nullableOf = maybe null nullify
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))
48isNull :: Expr (Maybe a) -> Expr Bool
65isNull :: Expr (Maybe a) -> Expr Bool
49isNull = mapPrimExpr (Opaleye.UnExpr Opaleye.OpIsNull)
66isNull = mapPrimExpr (Opaleye.UnExpr Opaleye.OpIsNull)
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)
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'.
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)
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'.
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
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