1
Fork 0

add support for union,intersect,except

This commit is contained in:
Jake Wheat 2013-12-13 23:41:12 +02:00
parent 64eb5a5c9d
commit 1199342477
4 changed files with 51 additions and 17 deletions

View file

@ -130,7 +130,8 @@ digitse[+-]digits
> blacklist = ["as", "from", "where", "having", "group", "order" > blacklist = ["as", "from", "where", "having", "group", "order"
> ,"inner", "left", "right", "full", "natural", "join" > ,"inner", "left", "right", "full", "natural", "join"
> ,"on", "using", "when", "then", "case", "end", "order" > ,"on", "using", "when", "then", "case", "end", "order"
> ,"limit", "offset", "in"] > ,"limit", "offset", "in"
> ,"except", "intersect", "union"]
TODO: talk about what must be in the blacklist, and what doesn't need TODO: talk about what must be in the blacklist, and what doesn't need
to be. to be.
@ -512,7 +513,7 @@ attempt to fix the precedence and associativity. Doesn't work
> queryExpr :: P QueryExpr > queryExpr :: P QueryExpr
> queryExpr = > queryExpr =
> try (keyword_ "select") >> > (try (keyword_ "select") >>
> Select > Select
> <$> (fromMaybe All <$> duplicates) > <$> (fromMaybe All <$> duplicates)
> <*> selectList > <*> selectList
@ -522,7 +523,18 @@ attempt to fix the precedence and associativity. Doesn't work
> <*> having > <*> having
> <*> option [] orderBy > <*> option [] orderBy
> <*> limit > <*> limit
> <*> offset > <*> offset)
> >>= queryExprSuffix
> queryExprSuffix :: QueryExpr -> P QueryExpr
> queryExprSuffix qe =
> choice [CombineQueryExpr qe
> <$> try (choice
> [Union <$ keyword "union"
> ,Intersect <$ keyword "intersect"
> ,Except <$ keyword "except"])
> <*> queryExpr
> ,return qe]
------------------------------------------------ ------------------------------------------------

View file

@ -119,6 +119,13 @@ back into SQL source text. It attempts to format the output nicely.
> ,maybeScalarExpr "limit" lm > ,maybeScalarExpr "limit" lm
> ,maybeScalarExpr "offset" off > ,maybeScalarExpr "offset" off
> ] > ]
> queryExpr (CombineQueryExpr q1 ct q2) =
> sep [queryExpr q1
> ,text $ case ct of
> Union -> "union"
> Intersect -> "intersect"
> Except -> "except"
> ,queryExpr q2]
> selectList :: [(Maybe String, ScalarExpr)] -> Doc > selectList :: [(Maybe String, ScalarExpr)] -> Doc
> selectList is = commaSep $ map si is > selectList is = commaSep $ map si is

View file

@ -8,6 +8,7 @@
> ,makeSelect > ,makeSelect
> ,Duplicates(..) > ,Duplicates(..)
> ,Direction(..) > ,Direction(..)
> ,CombineOp(..)
> ,TableRef(..) > ,TableRef(..)
> ,JoinType(..) > ,JoinType(..)
> ,JoinCondition(..) > ,JoinCondition(..)
@ -64,10 +65,17 @@
> ,qeOrderBy :: [(ScalarExpr,Direction)] > ,qeOrderBy :: [(ScalarExpr,Direction)]
> ,qeLimit :: Maybe ScalarExpr > ,qeLimit :: Maybe ScalarExpr
> ,qeOffset :: Maybe ScalarExpr > ,qeOffset :: Maybe ScalarExpr
> } deriving (Eq,Show) > }
> | CombineQueryExpr
> {qe1 :: QueryExpr
> ,qeCombOp :: CombineOp
> ,qe2 :: QueryExpr
> }
> deriving (Eq,Show)
> data Duplicates = Distinct | All deriving (Eq,Show) > data Duplicates = Distinct | All deriving (Eq,Show)
> data Direction = Asc | Desc deriving (Eq,Show) > data Direction = Asc | Desc deriving (Eq,Show)
> data CombineOp = Union | Except | Intersect deriving (Eq,Show)
> makeSelect :: QueryExpr > makeSelect :: QueryExpr
> makeSelect = Select {qeDuplicates = All > makeSelect = Select {qeDuplicates = All

View file

@ -364,13 +364,20 @@
> combos :: TestItem > combos :: TestItem
> combos = Group "combos" $ map (uncurry TestQueryExpr) > combos = Group "combos" $ map (uncurry TestQueryExpr)
> [{-("select a from t union select b from u" > [("select a from t union select b from u"
> ,makeSelect) > ,CombineQueryExpr ms1 Union ms2)
> ,("select a from t intersect select b from u" > ,("select a from t intersect select b from u"
> ,makeSelect) > ,CombineQueryExpr ms1 Intersect ms2)
> ,("select a from t except select b from u" > ,("select a from t except select b from u"
> ,makeSelect)-} > ,CombineQueryExpr ms1 Except ms2)
> ] > ]
> where
> ms1 = makeSelect
> {qeSelectList = [(Nothing,Iden "a")]
> ,qeFrom = [SimpleTableRef "t"]}
> ms2 = makeSelect
> {qeSelectList = [(Nothing,Iden "b")]
> ,qeFrom = [SimpleTableRef "u"]}
> fullQueries :: TestItem > fullQueries :: TestItem
> fullQueries = Group "queries" $ map (uncurry TestQueryExpr) > fullQueries = Group "queries" $ map (uncurry TestQueryExpr)