@@ -13,13 +13,12 @@
1313module Rel8.Schema.Context.Nullify
1414 ( Nullifiable( ConstrainTag, encodeTag, decodeTag, nullifier, unnullifier )
1515 , HNullifiable( HConstrainTag, hencodeTag, hdecodeTag, hnullifier, hunnullifier )
16− , runTag, runTagExpr, unnull, unnullExpr
16+ , runTag, unnull
1717 )
1818where
1919
2020-- base
2121import Control.Monad ( guard )
22−import Data.Functor.Identity ( Identity )
2322import Data.Kind ( Constraint, Type )
2423import GHC.TypeLits ( KnownSymbol )
2524import Prelude hiding ( null )
@@ -38,7 +37,7 @@ import Rel8.Expr.Bool ( boolExpr )
3837import Rel8.Expr.Null ( nullify, unsafeUnnullify )
3938import Rel8.Expr.Opaleye ( fromPrimExpr, toPrimExpr )
4039import Rel8.Kind.Necessity ( Necessity( Required ) )
41−import Rel8.Schema.Context ( Interpretation, Col(..) )
40+import Rel8.Schema.Context ( Interpretation )
4241import qualified Rel8.Schema.Kind as K
4342import Rel8.Schema.Name ( Name( Name ), Col(..) )
4443import Rel8.Schema.Nullability
@@ -52,10 +51,7 @@ import Rel8.Schema.Spec.ConstrainDBType
5251 ( ConstrainDBType
5352 , dbTypeDict, dbTypeNullability, fromNullabilityDict
5453 )
55−import Rel8.Table.Tag
56− ( Tag(..), Taggable
57− , fromAggregate, fromExpr, fromIdentity, fromName
58− )
54+import Rel8.Table.Tag ( Tag(..), Taggable, fromAggregate, fromExpr, fromName )
5955
6056
6157type Nullifiable :: K.Context -> Constraint
@@ -81,7 +77,6 @@ class Interpretation context => Nullifiable context where
8177
8278 nullifier :: ()
8379 => Tag label a
84− -> (a -> Bool)
8580 -> (Expr a -> Expr Bool)
8681 -> SSpec ('Spec labels necessity x)
8782 -> Col context ('Spec labels necessity x)
@@ -99,15 +94,15 @@ instance Nullifiable Aggregate where
9994
10095 decodeTag (Aggregation aggregate) = fromAggregate aggregate
10196
102− nullifier Tag {expr} _ test SSpec {nullability} (Aggregation aggregate) =
97+ nullifier Tag {expr} test SSpec {nullability} (Aggregation aggregate) =
10398 Aggregation $
104− mapInputs (toPrimExpr . runTagExpr nullability condition . fromPrimExpr) $
105− runTagExpr nullability condition <$> aggregate
99+ mapInputs (toPrimExpr . runTag nullability condition . fromPrimExpr) $
100+ runTag nullability condition <$> aggregate
106101 where
107102 condition = test expr
108103
109104 unnullifier SSpec {nullability} (Aggregation aggregate) =
110− Aggregation $ unnullExpr nullability <$> aggregate
105+ Aggregation $ unnull nullability <$> aggregate
111106
112107 {-# INLINABLE encodeTag #-}
113108 {-# INLINABLE decodeTag #-}
@@ -118,22 +113,9 @@ instance Nullifiable Aggregate where
118113instance Nullifiable Expr where
119114 encodeTag Tag {expr} = DB expr
120115 decodeTag (DB a) = fromExpr a
121− 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
124−
125− {-# INLINABLE encodeTag #-}
126− {-# INLINABLE decodeTag #-}
127− {-# INLINABLE nullifier #-}
128− {-# INLINABLE unnullifier #-}
129−
130−
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
136− unnullifier SSpec {nullability} (Result a) = Result $ unnull nullability a
116+ nullifier Tag {expr} test SSpec {nullability} (DB a) =
117+ DB $ runTag nullability (test expr) a
118+ unnullifier SSpec {nullability} (DB a) = DB $ unnull nullability a
137119
138120 {-# INLINABLE encodeTag #-}
139121 {-# INLINABLE decodeTag #-}
@@ -144,7 +126,7 @@ instance Nullifiable Identity where
144126instance Nullifiable Name where
145127 encodeTag Tag {name = Name name} = NameCol name
146128 decodeTag (NameCol name) = fromName (Name name)
147− nullifier _ _ _ _ (NameCol name) = NameCol name
129+ nullifier _ _ _ (NameCol name) = NameCol name
148130 unnullifier _ (NameCol name) = NameCol name
149131
150132 {-# INLINABLE encodeTag #-}
@@ -153,35 +135,20 @@ instance Nullifiable Name where
153135 {-# INLINABLE unnullifier #-}
154136
155137
156−runTagExpr :: Nullability a -> Expr Bool -> Expr a -> Expr (Nullify a)
157−runTagExpr nullability tag a = case nullability of
138+runTag :: Nullability a -> Expr Bool -> Expr a -> Expr (Nullify a)
139+runTag nullability tag a = case nullability of
158140 Nullable -> boolExpr null a tag
159141 NonNullable -> boolExpr null (nullify a) tag
160142 where
161143 null = fromPrimExpr $ Opaleye.ConstExpr Opaleye.NullLit
162144
163145
164−unnullExpr :: Nullability a -> Expr (Nullify a) -> Expr a
165−unnullExpr nullability a = case nullability of
146+unnull :: Nullability a -> Expr (Nullify a) -> Expr a
147+unnull nullability a = case nullability of
166148 Nullable -> a
167149 NonNullable -> unsafeUnnullify a
168150
169151
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
174−
175−
176−unnull :: Nullability a -> Nullify a -> a
177−unnull nullability ma = case nullability of
178− Nullable -> ma
179− NonNullable -> case ma of
180− Just a -> a
181− Nothing ->
182− error "Rel8.Schema.Context.Nullify.unnull: mismatch between tag and data"
183−
184−
185152type HNullifiable :: K.HContext -> Constraint
186153class HNullifiable context where
187154 type HConstrainTag context :: Type -> Constraint
@@ -197,7 +164,6 @@ class HNullifiable context where
197164
198165 hnullifier :: ()
199166 => Tag label a
200− -> (a -> Bool)
201167 -> (Expr a -> Expr Bool)
202168 -> SSpec ('Spec labels necessity x)
203169 -> context ('Spec labels necessity x)
@@ -223,7 +189,7 @@ instance HNullifiable (Dict (ConstrainDBType constraint)) where
223189 hencodeTag _ = Dict
224190 hdecodeTag = mempty
225191
226− hnullifier _ _ _ SSpec {} dict = case dbTypeDict dict of
192+ hnullifier _ _ SSpec {} dict = case dbTypeDict dict of
227193 Dict -> case dbTypeNullability dict of
228194 Nullable -> Dict
229195 NonNullable -> Dict