{-# language FlexibleContexts #-}

module Rel8.Query.Either
  ( keepLeftTable
  , keepRightTable
  , bitraverseEitherTable
  )
where

-- base
import Prelude

-- rel8
import Rel8.Expr.Eq ( (==.) )
import Rel8.Query ( Query )
import Rel8.Query.Filter ( where_ )
import Rel8.Query.Maybe ( optional )
import Rel8.Table.Either
  ( EitherTable( EitherTable )
  , isLeftTable, isRightTable
  )
import Rel8.Table.Maybe ( MaybeTable( MaybeTable ), isJustTable )


-- | Filter 'EitherTable's, keeping only 'leftTable's.
keepLeftTable :: EitherTable a b -> Query a
keepLeftTable :: EitherTable a b -> Query a
keepLeftTable e :: EitherTable a b
e@(EitherTable Tag "isRight" EitherTag
_ a
a b
_) = do
  Expr Bool -> Query ()
where_ (Expr Bool -> Query ()) -> Expr Bool -> Query ()
forall a b. (a -> b) -> a -> b
$ EitherTable a b -> Expr Bool
forall a b. EitherTable a b -> Expr Bool
isLeftTable EitherTable a b
e
  a -> Query a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a


-- | Filter 'EitherTable's, keeping only 'rightTable's.
keepRightTable :: EitherTable a b -> Query b
keepRightTable :: EitherTable a b -> Query b
keepRightTable e :: EitherTable a b
e@(EitherTable Tag "isRight" EitherTag
_ a
_ b
b) = do
  Expr Bool -> Query ()
where_ (Expr Bool -> Query ()) -> Expr Bool -> Query ()
forall a b. (a -> b) -> a -> b
$ EitherTable a b -> Expr Bool
forall a b. EitherTable a b -> Expr Bool
isRightTable EitherTable a b
e
  b -> Query b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
b


-- | @bitraverseEitherTable f g x@ will pass all @leftTable@s through @f@ and
-- all @rightTable@s through @g@. The results are then lifted back into
-- @leftTable@ and @rightTable@, respectively. This is similar to 'bitraverse'
-- for 'Either'.
--
-- For example,
--
-- >>> :{
-- select do
--   x <- values (map lit [ Left True, Right (42 :: Int32) ])
--   bitraverseEitherTable (\y -> values [y, not_ y]) (\y -> pure (y * 100)) x
-- :}
-- [ Left True
-- , Left False
-- , Right 4200
-- ]
bitraverseEitherTable :: ()
  => (a -> Query c)
  -> (b -> Query d)
  -> EitherTable a b
  -> Query (EitherTable c d)
bitraverseEitherTable :: (a -> Query c)
-> (b -> Query d) -> EitherTable a b -> Query (EitherTable c d)
bitraverseEitherTable a -> Query c
f b -> Query d
g e :: EitherTable a b
e@(EitherTable Tag "isRight" EitherTag
tag a
_ b
_) = do
  mc :: MaybeTable c
mc@(MaybeTable Tag "isJust" (Maybe MaybeTag)
_ c
c) <- Query c -> Query (MaybeTable c)
forall a. Query a -> Query (MaybeTable a)
optional (a -> Query c
f (a -> Query c) -> Query a -> Query c
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< EitherTable a b -> Query a
forall a b. EitherTable a b -> Query a
keepLeftTable EitherTable a b
e)
  md :: MaybeTable d
md@(MaybeTable Tag "isJust" (Maybe MaybeTag)
_ d
d) <- Query d -> Query (MaybeTable d)
forall a. Query a -> Query (MaybeTable a)
optional (b -> Query d
g (b -> Query d) -> Query b -> Query d
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< EitherTable a b -> Query b
forall a b. EitherTable a b -> Query b
keepRightTable EitherTable a b
e)
  Expr Bool -> Query ()
where_ (Expr Bool -> Query ()) -> Expr Bool -> Query ()
forall a b. (a -> b) -> a -> b
$ MaybeTable c -> Expr Bool
forall a. MaybeTable a -> Expr Bool
isJustTable MaybeTable c
mc Expr Bool -> Expr Bool -> Expr Bool
forall a. Sql DBEq a => Expr a -> Expr a -> Expr Bool
==. EitherTable a b -> Expr Bool
forall a b. EitherTable a b -> Expr Bool
isLeftTable EitherTable a b
e
  Expr Bool -> Query ()
where_ (Expr Bool -> Query ()) -> Expr Bool -> Query ()
forall a b. (a -> b) -> a -> b
$ MaybeTable d -> Expr Bool
forall a. MaybeTable a -> Expr Bool
isJustTable MaybeTable d
md Expr Bool -> Expr Bool -> Expr Bool
forall a. Sql DBEq a => Expr a -> Expr a -> Expr Bool
==. EitherTable a b -> Expr Bool
forall a b. EitherTable a b -> Expr Bool
isRightTable EitherTable a b
e
  EitherTable c d -> Query (EitherTable c d)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (EitherTable c d -> Query (EitherTable c d))
-> EitherTable c d -> Query (EitherTable c d)
forall a b. (a -> b) -> a -> b
$ Tag "isRight" EitherTag -> c -> d -> EitherTable c d
forall a b. Tag "isRight" EitherTag -> a -> b -> EitherTable a b
EitherTable Tag "isRight" EitherTag
tag c
c d
d