1
Fork 0

add with expressions, all tpch appear to parse correct, some of the later ones pretty print badly though

This commit is contained in:
Jake Wheat 2013-12-14 00:58:12 +02:00
parent 15c83555ff
commit 931272d1db
5 changed files with 60 additions and 28 deletions
Language/SQL/SimpleSQL

View file

@ -539,34 +539,44 @@ attempt to fix the precedence and associativity. Doesn't work
> offset :: P (Maybe ScalarExpr)
> offset = optionalScalarExpr "offset"
> with :: P QueryExpr
> with = try (keyword_ "with") >>
> With <$> commaSep1 withQuery
> <*> queryExpr
> where
> withQuery = (,) <$> (identifierString
> <* optional (try $ keyword_ "as"))
> <*> parens queryExpr
> queryExpr :: P QueryExpr
> queryExpr =
> (try (keyword_ "select") >>
> Select
> <$> (fromMaybe All <$> duplicates)
> <*> selectList
> <*> from
> <*> swhere
> <*> sgroupBy
> <*> having
> <*> option [] orderBy
> <*> limit
> <*> offset)
> >>= queryExprSuffix
> choice [select >>= queryExprSuffix, with]
> where
> select = try (keyword_ "select") >>
> Select
> <$> (fromMaybe All <$> duplicates)
> <*> selectList
> <*> from
> <*> swhere
> <*> sgroupBy
> <*> having
> <*> option [] orderBy
> <*> limit
> <*> offset
> queryExprSuffix :: QueryExpr -> P QueryExpr
> queryExprSuffix qe =
> choice [CombineQueryExpr qe
> <$> try (choice
> [Union <$ keyword_ "union"
> ,Intersect <$ keyword_ "intersect"
> ,Except <$ keyword_ "except"])
> <*> (fromMaybe All <$> duplicates)
> <*> (option Respectively
> $ try (Corresponding
> <$ keyword_ "corresponding"))
> <*> queryExpr
> choice [(CombineQueryExpr qe
> <$> try (choice
> [Union <$ keyword_ "union"
> ,Intersect <$ keyword_ "intersect"
> ,Except <$ keyword_ "except"])
> <*> (fromMaybe All <$> duplicates)
> <*> (option Respectively
> $ try (Corresponding
> <$ keyword_ "corresponding"))
> <*> queryExpr)
> >>= queryExprSuffix
> ,return qe]
> queryExprs :: P [QueryExpr]

View file

@ -143,6 +143,12 @@ back into SQL source text. It attempts to format the output nicely.
> Corresponding -> text "corresponding"
> Respectively -> empty
> ,queryExpr q2]
> queryExpr (With withs qe) =
> text "with"
> <+> vcat [nest 4
> (vcat $ punctuate comma $ flip map withs $ \(n,q) ->
> text n <+> text "as" <+> parens (queryExpr q))
> ,queryExpr qe]
> selectList :: [(Maybe String, ScalarExpr)] -> Doc
> selectList is = commaSep $ map si is

View file

@ -77,6 +77,7 @@
> ,qeCorresponding :: Corresponding
> ,qe2 :: QueryExpr
> }
> | With [(String,QueryExpr)] QueryExpr
> deriving (Eq,Show)
> data Duplicates = Distinct | All deriving (Eq,Show)