@@ -13,13 +13,12 @@
13module Rel8.Schema.Context.Nullify
13module Rel8.Schema.Context.Nullify
14 ( Nullifiable( ConstrainTag, encodeTag, decodeTag, nullifier, unnullifier )
14 ( Nullifiable( ConstrainTag, encodeTag, decodeTag, nullifier, unnullifier )
15 , HNullifiable( HConstrainTag, hencodeTag, hdecodeTag, hnullifier, hunnullifier )
15 , HNullifiable( HConstrainTag, hencodeTag, hdecodeTag, hnullifier, hunnullifier )
16− , runTag, runTagExpr, unnull, unnullExpr
16+ , runTag, unnull
21import Control.Monad ( guard )
21import Control.Monad ( guard )
22−import Data.Functor.Identity ( Identity )
23import Data.Kind ( Constraint, Type )
22import Data.Kind ( Constraint, Type )
24import GHC.TypeLits ( KnownSymbol )
23import GHC.TypeLits ( KnownSymbol )
25import Prelude hiding ( null )
24import Prelude hiding ( null )
@@ -38,7 +37,7 @@ import Rel8.Expr.Bool ( boolExpr )
38import Rel8.Expr.Null ( nullify, unsafeUnnullify )
37import Rel8.Expr.Null ( nullify, unsafeUnnullify )
39import Rel8.Expr.Opaleye ( fromPrimExpr, toPrimExpr )
38import Rel8.Expr.Opaleye ( fromPrimExpr, toPrimExpr )
40import Rel8.Kind.Necessity ( Necessity( Required ) )
39import Rel8.Kind.Necessity ( Necessity( Required ) )
41−import Rel8.Schema.Context ( Interpretation, Col(..) )
40+import Rel8.Schema.Context ( Interpretation )
42import qualified Rel8.Schema.Kind as K
41import qualified Rel8.Schema.Kind as K
43import Rel8.Schema.Name ( Name( Name ), Col(..) )
42import Rel8.Schema.Name ( Name( Name ), Col(..) )
44import Rel8.Schema.Nullability
43import Rel8.Schema.Nullability
@@ -52,10 +51,7 @@ import Rel8.Schema.Spec.ConstrainDBType
52 ( ConstrainDBType
51 ( ConstrainDBType
53 , dbTypeDict, dbTypeNullability, fromNullabilityDict
52 , dbTypeDict, dbTypeNullability, fromNullabilityDict
57− , fromAggregate, fromExpr, fromIdentity, fromName
54+import Rel8.Table.Tag ( Tag(..), Taggable, fromAggregate, fromExpr, fromName )
61type Nullifiable :: K.Context -> Constraint
57type Nullifiable :: K.Context -> Constraint
@@ -81,7 +77,6 @@ class Interpretation context => Nullifiable context where
82 nullifier :: ()
78 nullifier :: ()
83 => Tag label a
79 => Tag label a
85 -> (Expr a -> Expr Bool)
80 -> (Expr a -> Expr Bool)
86 -> SSpec ('Spec labels necessity x)
81 -> SSpec ('Spec labels necessity x)
87 -> Col context ('Spec labels necessity x)
82 -> Col context ('Spec labels necessity x)
@@ -99,15 +94,15 @@ instance Nullifiable Aggregate where
100 decodeTag (Aggregation aggregate) = fromAggregate aggregate
95 decodeTag (Aggregation aggregate) = fromAggregate aggregate
102− nullifier Tag {expr} _ test SSpec {nullability} (Aggregation aggregate) =
97+ nullifier Tag {expr} test SSpec {nullability} (Aggregation aggregate) =
103 Aggregation $
98 Aggregation $
104− mapInputs (toPrimExpr . runTagExpr nullability condition . fromPrimExpr) $
99+ mapInputs (toPrimExpr . runTag nullability condition . fromPrimExpr) $
105− runTagExpr nullability condition <$> aggregate
100+ runTag nullability condition <$> aggregate
107 condition = test expr
102 condition = test expr
109 unnullifier SSpec {nullability} (Aggregation aggregate) =
104 unnullifier SSpec {nullability} (Aggregation aggregate) =
110− Aggregation $ unnullExpr nullability <$> aggregate
105+ Aggregation $ unnull nullability <$> aggregate
112 {-# INLINABLE encodeTag #-}
107 {-# INLINABLE encodeTag #-}
113 {-# INLINABLE decodeTag #-}
108 {-# INLINABLE decodeTag #-}
@@ -118,22 +113,9 @@ instance Nullifiable Aggregate where
118instance Nullifiable Expr where
113instance Nullifiable Expr where
119 encodeTag Tag {expr} = DB expr
114 encodeTag Tag {expr} = DB expr
120 decodeTag (DB a) = fromExpr a
115 decodeTag (DB a) = fromExpr a
121− nullifier Tag {expr} _ test SSpec {nullability} (DB a) =
116+ nullifier Tag {expr} test SSpec {nullability} (DB a) =
122− DB $ runTagExpr nullability (test expr) a
123− unnullifier SSpec {nullability} (DB a) = DB $ unnullExpr nullability a
125− {-# INLINABLE encodeTag #-}
126− {-# INLINABLE decodeTag #-}
127− {-# INLINABLE nullifier #-}
128− {-# INLINABLE unnullifier #-}
131−instance Nullifiable Identity where
132− encodeTag Tag {identity} = Result identity
133− decodeTag (Result a) = fromIdentity a
134− nullifier Tag {identity} test _ SSpec {nullability} (Result a) =
135− Result $ runTag nullability (test identity) a
117+ DB $ runTag nullability (test expr) a
136− unnullifier SSpec {nullability} (Result a) = Result $ unnull nullability a
118+ unnullifier SSpec {nullability} (DB a) = DB $ unnull nullability a
138 {-# INLINABLE encodeTag #-}
120 {-# INLINABLE encodeTag #-}
139 {-# INLINABLE decodeTag #-}
121 {-# INLINABLE decodeTag #-}
@@ -144,7 +126,7 @@ instance Nullifiable Identity where
144instance Nullifiable Name where
126instance Nullifiable Name where
145 encodeTag Tag {name = Name name} = NameCol name
127 encodeTag Tag {name = Name name} = NameCol name
146 decodeTag (NameCol name) = fromName (Name name)
128 decodeTag (NameCol name) = fromName (Name name)
147− nullifier _ _ _ _ (NameCol name) = NameCol name
129+ nullifier _ _ _ (NameCol name) = NameCol name
148 unnullifier _ (NameCol name) = NameCol name
130 unnullifier _ (NameCol name) = NameCol name
150 {-# INLINABLE encodeTag #-}
132 {-# INLINABLE encodeTag #-}
@@ -153,35 +135,20 @@ instance Nullifiable Name where
153 {-# INLINABLE unnullifier #-}
135 {-# INLINABLE unnullifier #-}
156−runTagExpr :: Nullability a -> Expr Bool -> Expr a -> Expr (Nullify a)
138+runTag :: Nullability a -> Expr Bool -> Expr a -> Expr (Nullify a)
157−runTagExpr nullability tag a = case nullability of
139+runTag nullability tag a = case nullability of
158 Nullable -> boolExpr null a tag
140 Nullable -> boolExpr null a tag
159 NonNullable -> boolExpr null (nullify a) tag
141 NonNullable -> boolExpr null (nullify a) tag
161 null = fromPrimExpr $ Opaleye.ConstExpr Opaleye.NullLit
143 null = fromPrimExpr $ Opaleye.ConstExpr Opaleye.NullLit
164−unnullExpr :: Nullability a -> Expr (Nullify a) -> Expr a
146+unnull :: Nullability a -> Expr (Nullify a) -> Expr a
165−unnullExpr nullability a = case nullability of
147+unnull nullability a = case nullability of
166 Nullable -> a
148 Nullable -> a
167 NonNullable -> unsafeUnnullify a
149 NonNullable -> unsafeUnnullify a
170−runTag :: Nullability a -> Bool -> a -> Nullify a
171−runTag nullability tag a = case nullability of
172− Nullable -> a <* guard tag
173− NonNullable -> a <$ guard tag
176−unnull :: Nullability a -> Nullify a -> a
177−unnull nullability ma = case nullability of
179− NonNullable -> case ma of
182− error "Rel8.Schema.Context.Nullify.unnull: mismatch between tag and data"
185type HNullifiable :: K.HContext -> Constraint
152type HNullifiable :: K.HContext -> Constraint
186class HNullifiable context where
153class HNullifiable context where
187 type HConstrainTag context :: Type -> Constraint
154 type HConstrainTag context :: Type -> Constraint
@@ -197,7 +164,6 @@ class HNullifiable context where
198 hnullifier :: ()
165 hnullifier :: ()
199 => Tag label a
166 => Tag label a
201 -> (Expr a -> Expr Bool)
167 -> (Expr a -> Expr Bool)
202 -> SSpec ('Spec labels necessity x)
168 -> SSpec ('Spec labels necessity x)
203 -> context ('Spec labels necessity x)
169 -> context ('Spec labels necessity x)
@@ -223,7 +189,7 @@ instance HNullifiable (Dict (ConstrainDBType constraint)) where
223 hencodeTag _ = Dict
189 hencodeTag _ = Dict
224 hdecodeTag = mempty
190 hdecodeTag = mempty
226− hnullifier _ _ _ SSpec {} dict = case dbTypeDict dict of
192+ hnullifier _ _ SSpec {} dict = case dbTypeDict dict of
227 Dict -> case dbTypeNullability dict of
193 Dict -> case dbTypeNullability dict of
228 Nullable -> Dict
194 Nullable -> Dict
229 NonNullable -> Dict
195 NonNullable -> Dict