{-# language FlexibleContexts #-}
module Rel8.Internal.Query.Either
( keepLeftTable
, keepRightTable
, bitraverseEitherTable
)
where
import Prelude
import Control.Comonad ( extract )
import Rel8.Internal.Expr ( Expr )
import Rel8.Internal.Expr.Eq ( (==.) )
import Rel8.Internal.Query ( Query )
import Rel8.Internal.Query.Filter ( where_ )
import Rel8.Internal.Query.Maybe ( optional )
import Rel8.Internal.Table.Either
( EitherTable( EitherTable )
, isLeftTable, isRightTable
)
import Rel8.Internal.Table.Maybe ( MaybeTable( MaybeTable ), isJustTable )
keepLeftTable :: EitherTable Expr a b -> Query a
keepLeftTable :: forall a b. EitherTable Expr a b -> Query a
keepLeftTable e :: EitherTable Expr a b
e@(EitherTable Expr EitherTag
_ Nullify Expr a
a Nullify Expr b
_) = do
Expr Bool -> Query ()
where_ (Expr Bool -> Query ()) -> Expr Bool -> Query ()
forall a b. (a -> b) -> a -> b
$ EitherTable Expr a b -> Expr Bool
forall a b. EitherTable Expr a b -> Expr Bool
isLeftTable EitherTable Expr a b
e
a -> Query a
forall a. a -> Query a
forall (f :: Context) a. Applicative f => a -> f a
pure (Nullify Expr a -> a
forall a. Nullify Expr a -> a
forall (w :: Context) a. Comonad w => w a -> a
extract Nullify Expr a
a)
keepRightTable :: EitherTable Expr a b -> Query b
keepRightTable :: forall a b. EitherTable Expr a b -> Query b
keepRightTable e :: EitherTable Expr a b
e@(EitherTable Expr EitherTag
_ Nullify Expr a
_ Nullify Expr b
b) = do
Expr Bool -> Query ()
where_ (Expr Bool -> Query ()) -> Expr Bool -> Query ()
forall a b. (a -> b) -> a -> b
$ EitherTable Expr a b -> Expr Bool
forall a b. EitherTable Expr a b -> Expr Bool
isRightTable EitherTable Expr a b
e
b -> Query b
forall a. a -> Query a
forall (f :: Context) a. Applicative f => a -> f a
pure (Nullify Expr b -> b
forall a. Nullify Expr a -> a
forall (w :: Context) a. Comonad w => w a -> a
extract Nullify Expr b
b)
bitraverseEitherTable :: ()
=> (a -> Query c)
-> (b -> Query d)
-> EitherTable Expr a b
-> Query (EitherTable Expr c d)
bitraverseEitherTable :: forall a c b d.
(a -> Query c)
-> (b -> Query d)
-> EitherTable Expr a b
-> Query (EitherTable Expr c d)
bitraverseEitherTable a -> Query c
f b -> Query d
g e :: EitherTable Expr a b
e@(EitherTable Expr EitherTag
tag Nullify Expr a
_ Nullify Expr b
_) = do
mc@(MaybeTable _ c) <- Query c -> Query (MaybeTable Expr c)
forall a. Query a -> Query (MaybeTable Expr a)
optional (a -> Query c
f (a -> Query c) -> Query a -> Query c
forall (m :: Context) a b. Monad m => (a -> m b) -> m a -> m b
=<< EitherTable Expr a b -> Query a
forall a b. EitherTable Expr a b -> Query a
keepLeftTable EitherTable Expr a b
e)
md@(MaybeTable _ d) <- optional (g =<< keepRightTable e)
where_ $ isJustTable mc ==. isLeftTable e
where_ $ isJustTable md ==. isRightTable e
pure $ EitherTable tag c d