on db148ff help sign in
rel8/src/Rel8/Table/Maybe.hs
264 lines 8.0 kB history raw
1{-# language DataKinds #-}
2{-# language DeriveFunctor #-}
3{-# language DerivingStrategies #-}
4{-# language FlexibleContexts #-}
5{-# language FlexibleInstances #-}
6{-# language LambdaCase #-}
7{-# language MultiParamTypeClasses #-}
8{-# language NamedFieldPuns #-}
9{-# language ScopedTypeVariables #-}
10{-# language StandaloneKindSignatures #-}
11{-# language TypeApplications #-}
12{-# language TypeFamilies #-}
13{-# language UndecidableInstances #-}
15module Rel8.Table.Maybe
16 ( MaybeTable(..)
17 , maybeTable, nothingTable, justTable
18 , isNothingTable, isJustTable
19 , ($?)
20 , aggregateMaybeTable, insertMaybeTable, nameMaybeTable
21 )
22where
24-- base
25import Data.Functor.Identity ( runIdentity )
26import Data.Kind ( Type )
27import Prelude hiding ( null, undefined )
29-- rel8
30import Rel8.Aggregate ( Aggregate, unsafeMakeAggregate )
31import Rel8.Expr ( Expr )
32import Rel8.Expr.Bool ( boolExpr )
33import Rel8.Expr.Null ( isNull, isNonNull, null, nullify )
34import Rel8.Expr.Opaleye ( fromPrimExpr, toPrimExpr )
35import Rel8.Schema.Context.Label
36 ( Labelable, HLabelable, hlabeler, hunlabeler
37 )
38import Rel8.Schema.Context.Nullify
39 ( Nullifiable, ConstrainTag
40 , HNullifiable, HConstrainTag
41 , hencodeTag, hdecodeTag
42 , hnullifier, hunnullifier
43 )
44import Rel8.Schema.HTable ( HTable )
45import Rel8.Schema.HTable.Identity ( HIdentity(..) )
46import Rel8.Schema.HTable.Label ( hlabel, hunlabel )
47import Rel8.Schema.HTable.Maybe ( HMaybeTable(..) )
48import Rel8.Schema.HTable.Nullify ( hnullify, hunnullify )
49import Rel8.Schema.Insert ( Insert )
50import Rel8.Schema.Name ( Name )
51import Rel8.Schema.Null ( Nullify, Nullity( Null, NotNull ), Sql, nullable )
52import Rel8.Table
53 ( Table, Columns, Context, fromColumns, toColumns
54 , reify, unreify
55 )
56import Rel8.Table.Alternative
57 ( AltTable, (<|>:)
58 , AlternativeTable, emptyTable
59 )
60import Rel8.Table.Bool ( bool )
61import Rel8.Table.Eq ( EqTable, eqTable )
62import Rel8.Table.Insert ( toInsert )
63import Rel8.Table.Ord ( OrdTable, ordTable )
64import Rel8.Table.Recontextualize ( Recontextualize )
65import Rel8.Table.Serialize ( FromExprs, ToExprs, fromResult, toResult )
66import Rel8.Table.Tag ( Tag(..), fromExpr, fromName )
67import Rel8.Table.Undefined ( undefined )
68import Rel8.Type ( DBType )
69import Rel8.Type.Tag ( MaybeTag )
71-- semigroupoids
72import Data.Functor.Apply ( Apply, (<.>), liftF2 )
73import Data.Functor.Bind ( Bind, (>>-) )
76-- | @MaybeTable t@ is the table @t@, but as the result of an outer join. If
77-- the outer join fails to match any rows, this is essentialy @Nothing@, and if
78-- the outer join does match rows, this is like @Just@. Unfortunately, SQL
79-- makes it impossible to distinguish whether or not an outer join matched any
80-- rows based generally on the row contents - if you were to join a row
81-- entirely of nulls, you can't distinguish if you matched an all null row, or
82-- if the match failed. For this reason @MaybeTable@ contains an extra field -
83-- a "nullTag" - to track whether or not the outer join produced any rows.
84type MaybeTable :: Type -> Type
85data MaybeTable a = MaybeTable
86 { tag :: Tag "isJust" (Maybe MaybeTag)
87 , just :: a
88 }
89 deriving stock Functor
92instance Apply MaybeTable where
93 MaybeTable tag f <.> MaybeTable tag' a = MaybeTable (tag <> tag') (f a)
96-- | Has the same behavior as the @Applicative@ instance for @Maybe@. See also:
97-- 'Rel8.traverseMaybeTable'.
98instance Applicative MaybeTable where
99 (<*>) = (<.>)
100 pure = justTable
103instance Bind MaybeTable where
104 MaybeTable tag a >>- f = case f a of
105 MaybeTable tag' b -> MaybeTable (tag <> tag') b
108-- | Has the same behavior as the @Monad@ instance for @Maybe@. See also:
109-- 'Rel8.bindMaybeTable'.
110instance Monad MaybeTable where
111 (>>=) = (>>-)
114instance AltTable MaybeTable where
115 ma@(MaybeTable tag a) <|>: MaybeTable tag' b = MaybeTable
116 { tag = (tag <> tag')
117 { expr = boolExpr (expr tag) (expr tag') condition
118 }
119 , just = bool a b condition
120 }
121 where
122 condition = isNothingTable ma
125instance AlternativeTable MaybeTable where
126 emptyTable = nothingTable
129instance (Table Expr a, Semigroup a) => Semigroup (MaybeTable a) where
130 ma <> mb = maybeTable mb (\a -> maybeTable ma (justTable . (a <>)) mb) ma
133instance (Table Expr a, Semigroup a) => Monoid (MaybeTable a) where
134 mempty = nothingTable
137instance
138 ( Table context a
139 , Labelable context, Nullifiable context
140 , ConstrainTag context MaybeTag
141 ) => Table context (MaybeTable a)
142 where
143 type Columns (MaybeTable a) = HMaybeTable (Columns a)
144 type Context (MaybeTable a) = Context a
146 toColumns = toColumns1 toColumns
147 fromColumns = fromColumns1 fromColumns
148 reify = fmap fmap reify
149 unreify = fmap fmap unreify
152instance
153 ( Labelable from, Nullifiable from, ConstrainTag from MaybeTag
154 , Labelable to, Nullifiable to, ConstrainTag to MaybeTag
155 , Recontextualize from to a b
156 )
157 => Recontextualize from to (MaybeTable a) (MaybeTable b)
160instance EqTable a => EqTable (MaybeTable a) where
161 eqTable = toColumns1 id (justTable (eqTable @a))
164instance OrdTable a => OrdTable (MaybeTable a) where
165 ordTable = toColumns1 id (justTable (ordTable @a))
168type instance FromExprs (MaybeTable a) = Maybe (FromExprs a)
171instance ToExprs exprs a => ToExprs (MaybeTable exprs) (Maybe a) where
172 fromResult = fmap (fromResult @exprs) . fromColumns
173 toResult = toColumns . fmap (toResult @exprs)
176-- | Check if a @MaybeTable@ is absent of any row. Like 'Data.Maybe.isNothing'.
177isNothingTable :: MaybeTable a -> Expr Bool
178isNothingTable (MaybeTable tag _) = isNull (expr tag)
181-- | Check if a @MaybeTable@ contains a row. Like 'Data.Maybe.isJust'.
182isJustTable :: MaybeTable a -> Expr Bool
183isJustTable (MaybeTable tag _) = isNonNull (expr tag)
186-- | Perform case analysis on a 'MaybeTable'. Like 'maybe'.
187maybeTable :: Table Expr b => b -> (a -> b) -> MaybeTable a -> b
188maybeTable b f ma@(MaybeTable _ a) = bool (f a) b (isNothingTable ma)
189{-# INLINABLE maybeTable #-}
192-- | The null table. Like 'Nothing'.
193nothingTable :: Table Expr a => MaybeTable a
194nothingTable = MaybeTable (fromExpr null) undefined
197-- | Lift any table into 'MaybeTable'. Like 'Just'. Note you can also use
198-- 'pure'.
199justTable :: a -> MaybeTable a
200justTable = MaybeTable (fromExpr mempty)
203-- | Project a single expression out of a 'MaybeTable'. You can think of this
204-- operator like the '$' operator, but it also has the ability to return
205-- @null@.
206($?) :: forall a b. Sql DBType b
207 => (a -> Expr b) -> MaybeTable a -> Expr (Nullify b)
208f $? ma@(MaybeTable _ a) = case nullable @b of
209 Null -> boolExpr (f a) null (isNothingTable ma)
210 NotNull -> boolExpr (nullify (f a)) null (isNothingTable ma)
211infixl 4 $?
214aggregateMaybeTable :: ()
215 => (a -> Aggregate b) -> MaybeTable a -> Aggregate (MaybeTable b)
216aggregateMaybeTable f MaybeTable {tag = tag@Tag {aggregator, expr}, just} =
217 liftF2 MaybeTable (tag <$ aggregate) (f just)
218 where
219 aggregate = unsafeMakeAggregate toPrimExpr fromPrimExpr aggregator expr
222insertMaybeTable :: Table Insert a => Maybe a -> MaybeTable a
223insertMaybeTable = \case
224 Nothing -> MaybeTable (fromExpr null) (fromColumns (toInsert undefined))
225 Just a -> justTable a
228nameMaybeTable :: Name (Maybe MaybeTag) -> a -> MaybeTable a
229nameMaybeTable = MaybeTable . fromName
232toColumns1 ::
233 ( HTable t
234 , HConstrainTag context MaybeTag
235 , HLabelable context
236 , HNullifiable context
237 )
238 => (a -> t context)
239 -> MaybeTable a
240 -> HMaybeTable t context
241toColumns1 f MaybeTable {tag, just} = HMaybeTable
242 { htag
243 , hjust = hlabel hlabeler $ hnullify (hnullifier tag isNonNull) $ f just
244 }
245 where
246 htag = HIdentity (hencodeTag tag)
249fromColumns1 ::
250 ( HTable t
251 , HConstrainTag context MaybeTag
252 , HLabelable context
253 , HNullifiable context
254 )
255 => (t context -> a)
256 -> HMaybeTable t context
257 -> MaybeTable a
258fromColumns1 f HMaybeTable {htag = HIdentity htag, hjust} = MaybeTable
259 { tag
260 , just = f $ runIdentity $
261 hunnullify (\a -> pure . hunnullifier a) (hunlabel hunlabeler hjust)
262 }
263 where
264 tag = hdecodeTag htag