on 410af83 help sign in
rel8/src/Rel8/Query/These.hs
129 lines 3.7 kB history raw
1{-# language FlexibleContexts #-}
3module Rel8.Query.These
4 ( alignBy
5 , keepHereTable, loseHereTable
6 , keepThereTable, loseThereTable
7 , keepThisTable, loseThisTable
8 , keepThatTable, loseThatTable
9 , keepThoseTable, loseThoseTable
10 , bindTheseTable
11 , bitraverseTheseTable
12 )
13where
15-- base
16import Prelude
18-- opaleye
19import qualified Opaleye.Internal.Join as Opaleye
20import qualified Opaleye.Internal.PrimQuery as Opaleye
22-- rel8
23import Rel8.Expr ( Expr )
24import Rel8.Expr.Bool ( boolExpr, not_ )
25import Rel8.Expr.Eq ( (==.) )
26import Rel8.Expr.Opaleye ( toColumn, toPrimExpr )
27import Rel8.Expr.Serialize ( litExpr )
28import Rel8.Query ( Query )
29import Rel8.Query.Filter ( where_ )
30import Rel8.Query.Maybe ( optional )
31import Rel8.Query.Opaleye ( zipOpaleyeWith )
32import Rel8.Table ( Table )
33import Rel8.Table.Either ( EitherTable( EitherTable ) )
34import Rel8.Table.Maybe ( MaybeTable( MaybeTable ), isJustTable )
35import Rel8.Table.Opaleye ( unpackspec )
36import Rel8.Table.These
37 ( TheseTable( TheseTable )
38 , hasHereTable, hasThereTable
39 , isThisTable, isThatTable, isThoseTable
40 )
41import Rel8.Type.Tag ( EitherTag( IsLeft, IsRight ) )
44-- | Corresponds to a @FULL OUTER JOIN@ between two queries.
45alignBy :: (Table Expr a, Table Expr b)
46 => (a -> b -> Expr Bool)
47 -> Query a -> Query b -> Query (TheseTable a b)
48alignBy condition as bs =
49 uncurry TheseTable <$> zipOpaleyeWith fullOuterJoin as bs
50 where
51 fullOuterJoin a b =
52 Opaleye.joinExplicit unpackspec unpackspec pure pure full a b on
53 where
54 full = Opaleye.FullJoin
55 on = toColumn . toPrimExpr . uncurry condition
58keepHereTable :: TheseTable a b -> Query (a, MaybeTable b)
59keepHereTable = loseThatTable
62loseHereTable :: TheseTable a b -> Query b
63loseHereTable = keepThatTable
66keepThereTable :: TheseTable a b -> Query (MaybeTable a, b)
67keepThereTable = loseThisTable
70loseThereTable :: TheseTable a b -> Query a
71loseThereTable = keepThisTable
74keepThisTable :: TheseTable a b -> Query a
75keepThisTable t@(TheseTable (MaybeTable _ a) _) = do
76 where_ $ isThisTable t
77 pure a
80loseThisTable :: TheseTable a b -> Query (MaybeTable a, b)
81loseThisTable t@(TheseTable ma (MaybeTable _ b)) = do
82 where_ $ not_ $ isThisTable t
83 pure (ma, b)
86keepThatTable :: TheseTable a b -> Query b
87keepThatTable t@(TheseTable _ (MaybeTable _ b)) = do
88 where_ $ isThatTable t
89 pure b
92loseThatTable :: TheseTable a b -> Query (a, MaybeTable b)
93loseThatTable t@(TheseTable (MaybeTable _ a) mb) = do
94 where_ $ not_ $ isThatTable t
95 pure (a, mb)
98keepThoseTable :: TheseTable a b -> Query (a, b)
99keepThoseTable t@(TheseTable (MaybeTable _ a) (MaybeTable _ b)) = do
100 where_ $ isThoseTable t
101 pure (a, b)
104loseThoseTable :: TheseTable a b -> Query (EitherTable a b)
105loseThoseTable t@(TheseTable (MaybeTable _ a) (MaybeTable _ b)) = do
106 where_ $ not_ $ isThoseTable t
107 pure $ EitherTable tag a b
108 where
109 tag = boolExpr (litExpr IsLeft) (litExpr IsRight) (isThatTable t)
112bindTheseTable :: (Table Expr a, Semigroup a, Monad m)
113 => (i -> m (TheseTable a b)) -> TheseTable a i -> m (TheseTable a b)
114bindTheseTable query (TheseTable here (MaybeTable input i)) = do
115 TheseTable here' (MaybeTable output b) <- query i
116 pure $ TheseTable (here <> here') (MaybeTable (input <> output) b)
119bitraverseTheseTable :: ()
120 => (a -> Query c)
121 -> (b -> Query d)
122 -> TheseTable a b
123 -> Query (TheseTable c d)
124bitraverseTheseTable f g t = do
125 mc <- optional (f . fst =<< keepHereTable t)
126 md <- optional (g . snd =<< keepThereTable t)
127 where_ $ isJustTable mc ==. hasHereTable t
128 where_ $ isJustTable md ==. hasThereTable t
129 pure $ TheseTable mc md