module Database.Ferry.Algebra.Data.Create where
import Database.Ferry.Algebra.Data.Algebra
import Database.Ferry.Algebra.Data.GraphBuilder
int :: Integer -> AVal
int = VInt
string :: String -> AVal
string = VStr
bool :: Bool -> AVal
bool = VBool
double :: Double -> AVal
double = VDouble
dec :: Float -> AVal
dec = VDec
nat :: Integer -> AVal
nat = VNat
intT, stringT, boolT, decT, doubleT, natT, surT :: ATy
intT = AInt
stringT = AStr
boolT = ABool
decT = ADec
doubleT = ADouble
natT = ANat
surT = ASur
emptyTable :: SchemaInfos -> GraphM a AlgNode
emptyTable = insertNode . EmptyTable
dbTable :: String -> Columns -> KeyInfos -> GraphM a AlgNode
dbTable n cs ks = insertNode $ TableRef (n, attr, ks)
where
attr = map (\c -> case c of
(NCol n' [Col i t]) -> (n', "item" ++ show i, t)
_ -> error "Not a named column") cs
litTable :: AVal -> String -> ATy -> GraphM a AlgNode
litTable v s t = insertNode $ LitTable [[v]] [(s, t)]
attach :: ResAttrName -> ATy -> AVal -> AlgNode -> GraphM a AlgNode
attach n t v c = insertNode $ Attach (n, (t, v)) c
cast :: AttrName -> ResAttrName -> ATy -> AlgNode -> GraphM a AlgNode
cast n r t c = insertNode $ Cast (r, n, t) c
eqJoin :: String -> String -> AlgNode -> AlgNode -> GraphM a AlgNode
eqJoin n1 n2 c1 c2 = insertNode $ EqJoin (n1, n2) c1 c2
eqTJoin :: [(String, String)] -> ProjInf -> AlgNode -> AlgNode -> GraphM a AlgNode
eqTJoin eqs projI q1 q2 = let (a, b) = head eqs
in foldr filterEqs (eqJoin a b q1 q2) $ tail eqs
where resCol = "item99999002"
filterEqs :: (String, String) -> GraphM a AlgNode -> GraphM a AlgNode
filterEqs (l, r) res = proj projI =<< select resCol =<< oper "==" resCol l r =<< res
rank :: ResAttrName -> SortInf -> AlgNode -> GraphM a AlgNode
rank res sort c1 = insertNode $ Rank (res, sort) c1
difference :: AlgNode -> AlgNode -> GraphM a AlgNode
difference q1 q2 = insertNode $ Difference q1 q2
rowrank :: ResAttrName -> SortInf -> AlgNode -> GraphM a AlgNode
rowrank res sort c1 = insertNode $ RowRank (res, sort) c1
posSelect :: Int -> SortInf -> Maybe AttrName -> AlgNode -> GraphM a AlgNode
posSelect n sort part c1 = insertNode $ PosSel (n, sort, part) c1
select :: SelAttrName -> AlgNode -> GraphM a AlgNode
select sel c1 = insertNode $ Sel sel c1
distinct :: AlgNode -> GraphM a AlgNode
distinct c1 = insertNode $ Distinct c1
cross :: AlgNode -> AlgNode -> GraphM a AlgNode
cross c1 c2 = insertNode $ Cross c1 c2
notC :: AttrName -> AttrName -> AlgNode -> GraphM a AlgNode
notC r n c1 = insertNode $ FunBoolNot (r, n) c1
union :: AlgNode -> AlgNode -> GraphM a AlgNode
union c1 c2 = insertNode $ DisjUnion c1 c2
proj :: ProjInf -> AlgNode -> GraphM a AlgNode
proj cols c = insertNode $ Proj cols c
aggr :: [(AggrType, ResAttrName, Maybe AttrName)] -> Maybe PartAttrName -> AlgNode -> GraphM a AlgNode
aggr aggrs part c1 = insertNode $ Aggr (aggrs, part) c1
rownum :: AttrName -> [AttrName] -> Maybe AttrName -> AlgNode -> GraphM a AlgNode
rownum res sort part c1 = insertNode $ RowNum (res, zip sort $ repeat Asc, part) c1
rownum' :: AttrName -> [(AttrName, SortDir)] -> Maybe AttrName -> AlgNode -> GraphM a AlgNode
rownum' res sort part c1 = insertNode $ RowNum (res, sort, part) c1
oper :: String -> ResAttrName -> LeftAttrName -> RightAttrName -> AlgNode -> GraphM a AlgNode
oper o r la ra c = insertNode $ FunBinOp (o, r, la, ra) c
tag :: String -> AlgNode -> GraphM a AlgNode
tag s c = insertNode $ Dummy s c
initLoop :: Algebra
initLoop = LitTable [[(nat 1)]] [("iter", natT)]