on ba684c0 help sign in
rel8/src/Rel8/Table/Maybe.hs
241 lines 7.3 kB history raw
1{-# language DataKinds #-}
2{-# language DeriveFunctor #-}
3{-# language DerivingStrategies #-}
4{-# language FlexibleContexts #-}
5{-# language FlexibleInstances #-}
6{-# language MultiParamTypeClasses #-}
7{-# language NamedFieldPuns #-}
8{-# language ScopedTypeVariables #-}
9{-# language StandaloneKindSignatures #-}
10{-# language TypeApplications #-}
11{-# language TypeFamilies #-}
12{-# language UndecidableInstances #-}
14module Rel8.Table.Maybe
15 ( MaybeTable(..)
16 , maybeTable, nothingTable, justTable
17 , isNothingTable, isJustTable
18 , ($?)
19 , aggregateMaybeTable
20 , 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.Name ( Name )
50import Rel8.Schema.Null ( Nullify, Nullity( Null, NotNull ), Sql, nullable )
51import Rel8.Table ( Table, Columns, Context, fromColumns, toColumns )
52import Rel8.Table.Alternative
53 ( AltTable, (<|>:)
54 , AlternativeTable, emptyTable
55 )
56import Rel8.Table.Bool ( bool )
57import Rel8.Table.Eq ( EqTable, eqTable )
58import Rel8.Table.Ord ( OrdTable, ordTable )
59import Rel8.Table.Recontextualize ( Recontextualize )
60import Rel8.Table.Tag ( Tag(..), fromExpr, fromName )
61import Rel8.Table.Undefined ( undefined )
62import Rel8.Type ( DBType )
63import Rel8.Type.Tag ( MaybeTag )
65-- semigroupoids
66import Data.Functor.Apply ( Apply, (<.>), liftF2 )
67import Data.Functor.Bind ( Bind, (>>-) )
70-- | @MaybeTable t@ is the table @t@, but as the result of an outer join. If
71-- the outer join fails to match any rows, this is essentialy @Nothing@, and if
72-- the outer join does match rows, this is like @Just@. Unfortunately, SQL
73-- makes it impossible to distinguish whether or not an outer join matched any
74-- rows based generally on the row contents - if you were to join a row
75-- entirely of nulls, you can't distinguish if you matched an all null row, or
76-- if the match failed. For this reason @MaybeTable@ contains an extra field -
77-- a "nullTag" - to track whether or not the outer join produced any rows.
78type MaybeTable :: Type -> Type
79data MaybeTable a = MaybeTable
80 { tag :: Tag "isJust" (Maybe MaybeTag)
81 , just :: a
82 }
83 deriving stock Functor
86instance Apply MaybeTable where
87 MaybeTable tag f <.> MaybeTable tag' a = MaybeTable (tag <> tag') (f a)
90-- | Has the same behavior as the @Applicative@ instance for @Maybe@. See also:
91-- 'Rel8.traverseMaybeTable'.
92instance Applicative MaybeTable where
93 (<*>) = (<.>)
94 pure = justTable
97instance Bind MaybeTable where
98 MaybeTable tag a >>- f = case f a of
99 MaybeTable tag' b -> MaybeTable (tag <> tag') b
102-- | Has the same behavior as the @Monad@ instance for @Maybe@. See also:
103-- 'Rel8.bindMaybeTable'.
104instance Monad MaybeTable where
105 (>>=) = (>>-)
108instance AltTable MaybeTable where
109 ma@(MaybeTable tag a) <|>: MaybeTable tag' b = MaybeTable
110 { tag = (tag <> tag')
111 { expr = boolExpr (expr tag) (expr tag') condition
112 }
113 , just = bool a b condition
114 }
115 where
116 condition = isNothingTable ma
119instance AlternativeTable MaybeTable where
120 emptyTable = nothingTable
123instance (Table Expr a, Semigroup a) => Semigroup (MaybeTable a) where
124 ma <> mb = maybeTable mb (\a -> maybeTable ma (justTable . (a <>)) mb) ma
127instance (Table Expr a, Semigroup a) => Monoid (MaybeTable a) where
128 mempty = nothingTable
131instance
132 ( Table context a
133 , Labelable context, Nullifiable context
134 , ConstrainTag context MaybeTag
135 ) => Table context (MaybeTable a)
136 where
137 type Columns (MaybeTable a) = HMaybeTable (Columns a)
138 type Context (MaybeTable a) = Context a
140 toColumns = toColumns1 toColumns
141 fromColumns = fromColumns1 fromColumns
144instance
145 ( Labelable from, Nullifiable from, ConstrainTag from MaybeTag
146 , Labelable to, Nullifiable to, ConstrainTag to MaybeTag
147 , Recontextualize from to a b
148 ) => Recontextualize from to (MaybeTable a) (MaybeTable b)
151instance EqTable a => EqTable (MaybeTable a) where
152 eqTable = toColumns1 id (justTable (eqTable @a))
155instance OrdTable a => OrdTable (MaybeTable a) where
156 ordTable = toColumns1 id (justTable (ordTable @a))
159-- | Check if a @MaybeTable@ is absent of any row. Like 'Data.Maybe.isNothing'.
160isNothingTable :: MaybeTable a -> Expr Bool
161isNothingTable (MaybeTable tag _) = isNull (expr tag)
164-- | Check if a @MaybeTable@ contains a row. Like 'Data.Maybe.isJust'.
165isJustTable :: MaybeTable a -> Expr Bool
166isJustTable (MaybeTable tag _) = isNonNull (expr tag)
169-- | Perform case analysis on a 'MaybeTable'. Like 'maybe'.
170maybeTable :: Table Expr b => b -> (a -> b) -> MaybeTable a -> b
171maybeTable b f ma@(MaybeTable _ a) = bool (f a) b (isNothingTable ma)
172{-# INLINABLE maybeTable #-}
175-- | The null table. Like 'Nothing'.
176nothingTable :: Table Expr a => MaybeTable a
177nothingTable = MaybeTable (fromExpr null) undefined
180-- | Lift any table into 'MaybeTable'. Like 'Just'. Note you can also use
181-- 'pure'.
182justTable :: a -> MaybeTable a
183justTable = MaybeTable (fromExpr mempty)
186-- | Project a single expression out of a 'MaybeTable'. You can think of this
187-- operator like the '$' operator, but it also has the ability to return
188-- @null@.
189($?) :: forall a b. Sql DBType b
190 => (a -> Expr b) -> MaybeTable a -> Expr (Nullify b)
191f $? ma@(MaybeTable _ a) = case nullable @b of
192 Null -> boolExpr (f a) null (isNothingTable ma)
193 NotNull -> boolExpr (nullify (f a)) null (isNothingTable ma)
194infixl 4 $?
197aggregateMaybeTable :: ()
198 => (a -> Aggregate b) -> MaybeTable a -> Aggregate (MaybeTable b)
199aggregateMaybeTable f MaybeTable {tag = tag@Tag {aggregator, expr}, just} =
200 liftF2 MaybeTable (tag <$ aggregate) (f just)
201 where
202 aggregate = unsafeMakeAggregate toPrimExpr fromPrimExpr aggregator expr
205nameMaybeTable :: Name (Maybe MaybeTag) -> a -> MaybeTable a
206nameMaybeTable = MaybeTable . fromName
209toColumns1 ::
210 ( HTable t
211 , HConstrainTag context MaybeTag
212 , HLabelable context
213 , HNullifiable context
214 )
215 => (a -> t context)
216 -> MaybeTable a
217 -> HMaybeTable t context
218toColumns1 f MaybeTable {tag, just} = HMaybeTable
219 { htag
220 , hjust = hlabel hlabeler $ hnullify (hnullifier tag isNonNull) $ f just
221 }
222 where
223 htag = HIdentity (hencodeTag tag)
226fromColumns1 ::
227 ( HTable t
228 , HConstrainTag context MaybeTag
229 , HLabelable context
230 , HNullifiable context
231 )
232 => (t context -> a)
233 -> HMaybeTable t context
234 -> MaybeTable a
235fromColumns1 f HMaybeTable {htag = HIdentity htag, hjust} = MaybeTable
236 { tag
237 , just = f $ runIdentity $
238 hunnullify (\a -> pure . hunnullifier a) (hunlabel hunlabeler hjust)
239 }
240 where
241 tag = hdecodeTag htag