on 1ab51d8 help sign in
rel8/src/Rel8/Query/Maybe.hs
81 lines 3.0 kB history raw
1module Rel8.Query.Maybe
2 ( optional
3 , catMaybeTable
4 , bindMaybeTable
5 , traverseMaybeTable
6 )
7where
9-- base
10import Prelude
12-- opaleye
13import qualified Opaleye.Internal.PackMap as Opaleye
14import qualified Opaleye.Internal.PrimQuery as Opaleye
15import qualified Opaleye.Internal.QueryArr as Opaleye
16import qualified Opaleye.Internal.Tag as Opaleye
17import qualified Opaleye.Internal.Unpackspec as Opaleye
19-- rel8
20import Rel8.Expr.Bool ( true )
21import Rel8.Expr.Eq ( (==.) )
22import Rel8.Expr.Opaleye ( toPrimExpr )
23import Rel8.Query ( Query )
24import Rel8.Query.Filter ( where_ )
25import Rel8.Query.Opaleye ( mapOpaleye )
26import Rel8.Table.Maybe ( MaybeTable( MaybeTable ), isJustTable )
27import Rel8.Table.Opaleye ( unpackspec )
28import Rel8.Table.Tag ( Tag(..), fromExpr )
31optional :: Query a -> Query (MaybeTable a)
32optional = mapOpaleye $ Opaleye.QueryArr . go
33 where
34 go query (i, left, tag) =
35 (MaybeTable (fromExpr t') a, join, Opaleye.next tag')
36 where
37 (MaybeTable Tag {expr = t} a, right, tag') =
38 Opaleye.runSimpleQueryArr (pure <$> query) (i, tag)
39 (t', bindings) = Opaleye.run $
40 Opaleye.runUnpackspec unpackspec (Opaleye.extractAttr "maybe" tag') t
41 join = Opaleye.Join Opaleye.LeftJoin condition [] bindings left right
42 condition = toPrimExpr true
45-- | Filter out 'MaybeTable's, returning only the tables that are not-null.
46--
47-- This operation can be used to "undo" the effect of 'optional', which
48-- operationally is like turning a @LEFT JOIN@ back into a full @JOIN@. You
49-- can think of this as analogous to 'Data.Maybe.catMaybes'.
50catMaybeTable :: MaybeTable a -> Query a
51catMaybeTable ma@(MaybeTable _ a) = do
52 where_ $ isJustTable ma
53 pure a
56-- | @bindMaybeTable f x@ is similar to the monadic bind (@>>=@) operation. It
57-- allows you to "extend" an optional query with another query. If either the
58-- input or output are 'noTable', then the result is 'noTable'.
59--
60-- This is similar to 'traverseMaybeTable', followed by a @join@ on the
61-- resulting @MaybeTable@s.
62bindMaybeTable :: Monad m
63 => (a -> m (MaybeTable b)) -> MaybeTable a -> m (MaybeTable b)
64bindMaybeTable query (MaybeTable input a) = do
65 MaybeTable output b <- query a
66 pure $ MaybeTable (input <> output) b
69-- | Extend an optional query with another query. This is useful if you want
70-- to step through multiple @LEFT JOINs@.
71--
72-- Note that @traverseMaybeTable@ takes a @a -> Query b@ function, which means
73-- you also have the ability to "expand" one row into multiple rows. If the
74-- @a -> Query b@ function returns no rows, then the resulting query will also
75-- have no rows. However, regardless of the given @a -> Query b@ function, if
76-- the input is @noTable@, you will always get exactly one @noTable@ back.
77traverseMaybeTable :: (a -> Query b) -> MaybeTable a -> Query (MaybeTable b)
78traverseMaybeTable query ma@(MaybeTable input _) = do
79 MaybeTable output b <- optional (query =<< catMaybeTable ma)
80 where_ $ expr output ==. expr input
81 pure $ MaybeTable input b