module Database.Relational.Query.Internal.Product (
node, nodeAttr, nodeTree,
growProduct, restrictProduct,
) where
import Prelude hiding (and, product)
import Control.Applicative (pure, empty)
import Data.Monoid ((<>))
import Database.Relational.Query.Context (Flat)
import Database.Relational.Query.Internal.Sub (NodeAttr (..), ProductTree (..), Node (..), Projection)
nodeAttr :: Node q -> NodeAttr
nodeAttr (Node a _) = a where
nodeTree :: Node q -> ProductTree q
nodeTree (Node _ t) = t
node :: NodeAttr
-> ProductTree q
-> Node q
node = Node
growRight :: Maybe (Node q)
-> (NodeAttr, ProductTree q)
-> Node q
growRight = d where
d Nothing (naR, q) = node naR q
d (Just l) (naR, q) = node Just' $ Join l (node naR q) empty
growProduct :: Maybe (Node q)
-> (NodeAttr, q)
-> Node q
growProduct = match where
match t (na, q) = growRight t (na, Leaf q)
restrictProduct' :: ProductTree q
-> Projection Flat (Maybe Bool)
-> ProductTree q
restrictProduct' = d where
d (Join lp rp rs) rs' = Join lp rp (rs <> pure rs')
d leaf'@(Leaf _) _ = leaf'
restrictProduct :: Node q
-> Projection Flat (Maybe Bool)
-> Node q
restrictProduct (Node a t) e = node a (restrictProduct' t e)