1
Fork 0

add support for with recursive and column aliases in cte

This commit is contained in:
Jake Wheat 2013-12-17 13:41:06 +02:00
parent c52334943f
commit 4308acb982
7 changed files with 57 additions and 38 deletions
Language/SQL/SimpleSQL

View file

@ -491,8 +491,8 @@ expression tree (for efficiency and code clarity).
== select lists
> selectItem :: P (Maybe Name, ScalarExpr)
> selectItem = flip (,) <$> scalarExpr <*> optionMaybe (try alias)
> where alias = optional (try (keyword_ "as")) *> name
> selectItem = flip (,) <$> scalarExpr <*> optionMaybe (try als)
> where als = optional (try (keyword_ "as")) *> name
> selectList :: P [(Maybe Name,ScalarExpr)]
> selectList = commaSep1 selectItem
@ -517,11 +517,7 @@ tref
> <*> parens (commaSep scalarExpr))
> ,TRSimple <$> name]
> >>= optionSuffix aliasSuffix
> aliasSuffix j =
> let tableAlias = optional (try $ keyword_ "as") *> name
> columnAliases = optionMaybe $ try $ parens
> $ commaSep1 name
> in option j (TRAlias j <$> try tableAlias <*> try columnAliases)
> aliasSuffix j = option j (TRAlias j <$> alias)
> joinTrefSuffix t = (do
> nat <- option False $ try (True <$ try (keyword_ "natural"))
> TRJoin t <$> joinType
@ -546,6 +542,12 @@ tref
> JoinUsing <$> parens (commaSep1 name)
> ]
> alias :: P Alias
> alias = Alias <$> try tableAlias <*> try columnAliases
> where
> tableAlias = optional (try $ keyword_ "as") *> name
> columnAliases = optionMaybe $ try $ parens $ commaSep1 name
== simple other parts
Parsers for where, group by, having, order by and limit, which are
@ -585,10 +587,11 @@ where, having, limit, offset).
> with :: P QueryExpr
> with = try (keyword_ "with") >>
> With <$> commaSep1 withQuery <*> queryExpr
> With <$> option False (try (True <$ keyword_ "recursive"))
> <*> commaSep1 withQuery <*> queryExpr
> where
> withQuery =
> (,) <$> (name <* optional (try $ keyword_ "as"))
> (,) <$> (alias <* optional (try $ keyword_ "as"))
> <*> parens queryExpr
== query expression

View file

@ -171,21 +171,26 @@
> Corresponding -> text "corresponding"
> Respectively -> empty
> ,queryExpr q2]
> queryExpr (With withs qe) =
> text "with"
> queryExpr (With rc withs qe) =
> text "with" <+> (if rc then text "recursive" else empty)
> <+> vcat [nest 5
> (vcat $ punctuate comma $ flip map withs $ \(n,q) ->
> name n <+> text "as" <+> parens (queryExpr q))
> alias n <+> text "as" <+> parens (queryExpr q))
> ,queryExpr qe]
> queryExpr (Values vs) =
> text "values"
> <+> nest 7 (commaSep (map (parens . commaSep . map scalarExpr) vs))
> alias :: Alias -> Doc
> alias (Alias nm cols) =
> text "as" <+> name nm
> <+> maybe empty (parens . commaSep . map name) cols
> selectList :: [(Maybe Name, ScalarExpr)] -> Doc
> selectList is = commaSep $ map si is
> where
> si (al,e) = scalarExpr e <+> maybe empty alias al
> alias al = text "as" <+> name al
> si (al,e) = scalarExpr e <+> maybe empty als al
> als al = text "as" <+> name al
> from :: [TableRef] -> Doc
> from [] = empty
@ -197,10 +202,7 @@
> tr (TRLateral t) = text "lateral" <+> tr t
> tr (TRFunction f as) =
> name f <> parens (commaSep $ map scalarExpr as)
> tr (TRAlias t a cs) =
> sep [tr t
> ,text "as" <+> name a
> <+> maybe empty (parens . commaSep . map name) cs]
> tr (TRAlias t a) = sep [tr t, alias a]
> tr (TRParens t) = parens $ tr t
> tr (TRQueryExpr q) = parens $ queryExpr q
> tr (TRJoin t0 jt t1 jc) =

View file

@ -14,6 +14,7 @@
> ,makeSelect
> ,CombineOp(..)
> ,Corresponding(..)
> ,Alias(..)
> -- ** From
> ,TableRef(..)
> ,JoinType(..)
@ -152,13 +153,16 @@
> ,qeOffset :: Maybe ScalarExpr
> }
> | CombineQueryExpr
> {qe1 :: QueryExpr
> {qe0 :: QueryExpr
> ,qeCombOp :: CombineOp
> ,qeDuplicates :: Duplicates
> ,qeCorresponding :: Corresponding
> ,qe2 :: QueryExpr
> ,qe1 :: QueryExpr
> }
> | With [(Name,QueryExpr)] QueryExpr
> | With
> {qeWithRecursive :: Bool
> ,qeViews :: [(Alias,QueryExpr)]
> ,qeQueryExpression :: QueryExpr}
> | Values [[ScalarExpr]]
> deriving (Eq,Show,Read)
@ -200,7 +204,7 @@ I'm not sure if this is valid syntax or not.
> -- | from (a)
> | TRParens TableRef
> -- | from a as b(c,d)
> | TRAlias TableRef Name (Maybe [Name])
> | TRAlias TableRef Alias
> -- | from (query expr)
> | TRQueryExpr QueryExpr
> -- | from function(args)
@ -209,6 +213,9 @@ I'm not sure if this is valid syntax or not.
> | TRLateral TableRef
> deriving (Eq,Show,Read)
> data Alias = Alias Name (Maybe [Name])
> deriving (Eq,Show,Read)
TODO: add function table ref
> -- | The type of a join