module Database.Relational.SqlSyntax.Join (
growProduct, restrictProduct,
) where
import Prelude hiding (and, product)
import Control.Applicative (pure)
import Data.Monoid ((<>), mempty)
import Data.DList (DList)
import Database.Relational.Internal.ContextType (Flat)
import Database.Relational.SqlSyntax.Types
(NodeAttr (..), ProductTree (..), Node (..), Qualified, SubQuery,
Predicate)
growRight :: Maybe (Node (DList (Predicate Flat)))
-> (NodeAttr, ProductTree (DList (Predicate Flat)))
-> Node (DList (Predicate Flat))
growRight :: Maybe (Node (DList (Predicate Flat)))
-> (NodeAttr, ProductTree (DList (Predicate Flat)))
-> Node (DList (Predicate Flat))
growRight = Maybe (Node (DList (Predicate Flat)))
-> (NodeAttr, ProductTree (DList (Predicate Flat)))
-> Node (DList (Predicate Flat))
forall rs.
Monoid rs =>
Maybe (Node rs) -> (NodeAttr, ProductTree rs) -> Node rs
d where
d :: Maybe (Node rs) -> (NodeAttr, ProductTree rs) -> Node rs
d Maybe (Node rs)
Nothing (NodeAttr
naR, ProductTree rs
q) = NodeAttr -> ProductTree rs -> Node rs
forall rs. NodeAttr -> ProductTree rs -> Node rs
Node NodeAttr
naR ProductTree rs
q
d (Just Node rs
l) (NodeAttr
naR, ProductTree rs
q) = NodeAttr -> ProductTree rs -> Node rs
forall rs. NodeAttr -> ProductTree rs -> Node rs
Node NodeAttr
Just' (ProductTree rs -> Node rs) -> ProductTree rs -> Node rs
forall a b. (a -> b) -> a -> b
$ Node rs -> Node rs -> rs -> ProductTree rs
forall rs. Node rs -> Node rs -> rs -> ProductTree rs
Join Node rs
l (NodeAttr -> ProductTree rs -> Node rs
forall rs. NodeAttr -> ProductTree rs -> Node rs
Node NodeAttr
naR ProductTree rs
q) rs
forall a. Monoid a => a
mempty
growProduct :: Maybe (Node (DList (Predicate Flat)))
-> (NodeAttr, (Bool, Qualified SubQuery))
-> Node (DList (Predicate Flat))
growProduct :: Maybe (Node (DList (Predicate Flat)))
-> (NodeAttr, (Bool, Qualified SubQuery))
-> Node (DList (Predicate Flat))
growProduct = Maybe (Node (DList (Predicate Flat)))
-> (NodeAttr, (Bool, Qualified SubQuery))
-> Node (DList (Predicate Flat))
match where
match :: Maybe (Node (DList (Predicate Flat)))
-> (NodeAttr, (Bool, Qualified SubQuery))
-> Node (DList (Predicate Flat))
match Maybe (Node (DList (Predicate Flat)))
t (NodeAttr
na, (Bool, Qualified SubQuery)
q) = Maybe (Node (DList (Predicate Flat)))
-> (NodeAttr, ProductTree (DList (Predicate Flat)))
-> Node (DList (Predicate Flat))
growRight Maybe (Node (DList (Predicate Flat)))
t (NodeAttr
na, (Bool, Qualified SubQuery) -> ProductTree (DList (Predicate Flat))
forall rs. (Bool, Qualified SubQuery) -> ProductTree rs
Leaf (Bool, Qualified SubQuery)
q)
restrictProduct' :: ProductTree (DList (Predicate Flat))
-> Predicate Flat
-> ProductTree (DList (Predicate Flat))
restrictProduct' :: ProductTree (DList (Predicate Flat))
-> Predicate Flat -> ProductTree (DList (Predicate Flat))
restrictProduct' = ProductTree (DList (Predicate Flat))
-> Predicate Flat -> ProductTree (DList (Predicate Flat))
forall (f :: * -> *) a.
(Semigroup (f a), Applicative f) =>
ProductTree (f a) -> a -> ProductTree (f a)
d where
d :: ProductTree (f a) -> a -> ProductTree (f a)
d (Join Node (f a)
lp Node (f a)
rp f a
rs) a
rs' = Node (f a) -> Node (f a) -> f a -> ProductTree (f a)
forall rs. Node rs -> Node rs -> rs -> ProductTree rs
Join Node (f a)
lp Node (f a)
rp (f a
rs f a -> f a -> f a
forall a. Semigroup a => a -> a -> a
<> a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
rs')
d leaf' :: ProductTree (f a)
leaf'@(Leaf (Bool, Qualified SubQuery)
_) a
_ = ProductTree (f a)
leaf'
restrictProduct :: Node (DList (Predicate Flat))
-> Predicate Flat
-> Node (DList (Predicate Flat))
restrictProduct :: Node (DList (Predicate Flat))
-> Predicate Flat -> Node (DList (Predicate Flat))
restrictProduct (Node NodeAttr
a ProductTree (DList (Predicate Flat))
t) Predicate Flat
e = NodeAttr
-> ProductTree (DList (Predicate Flat))
-> Node (DList (Predicate Flat))
forall rs. NodeAttr -> ProductTree rs -> Node rs
Node NodeAttr
a (ProductTree (DList (Predicate Flat))
-> Predicate Flat -> ProductTree (DList (Predicate Flat))
restrictProduct' ProductTree (DList (Predicate Flat))
t Predicate Flat
e)