From ee432d79ba541844d6a216e856df43073460680f Mon Sep 17 00:00:00 2001 From: Jake Wheat Date: Sat, 31 Aug 2019 12:38:24 +0100 Subject: [PATCH] update handling of fetch first and limit wrt dialects --- Language/SQL/SimpleSQL/Dialect.lhs | 3 ++- Language/SQL/SimpleSQL/Parse.lhs | 19 +++++++++++++------ Language/SQL/SimpleSQL/Pretty.lhs | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Language/SQL/SimpleSQL/Dialect.lhs b/Language/SQL/SimpleSQL/Dialect.lhs index 7e9cb5a..59a5191 100644 --- a/Language/SQL/SimpleSQL/Dialect.lhs +++ b/Language/SQL/SimpleSQL/Dialect.lhs @@ -45,7 +45,8 @@ hack for now, later will expand to flags on a feature by feature basis > -- | mysql dialect > mysql :: Dialect -> mysql = addLimit ansi2011 {diSyntaxFlavour = MySQL} +> mysql = addLimit ansi2011 {diSyntaxFlavour = MySQL +> ,diFetchFirst = False } > -- | postgresql dialect > postgres :: Dialect diff --git a/Language/SQL/SimpleSQL/Parse.lhs b/Language/SQL/SimpleSQL/Parse.lhs index f1ed63a..7eccfc7 100644 --- a/Language/SQL/SimpleSQL/Parse.lhs +++ b/Language/SQL/SimpleSQL/Parse.lhs @@ -1457,12 +1457,12 @@ allows offset and fetch in either order > fetch :: Parser ScalarExpr > fetch = fetchFirst <|> limit > where -> fetchFirst = guardDialect [ANSI2011] +> fetchFirst = guardDialect diFetchFirst > *> fs *> scalarExpr <* ro > fs = makeKeywordTree ["fetch first", "fetch next"] > ro = makeKeywordTree ["rows only", "row only"] > -- todo: not in ansi sql dialect -> limit = guardDialect [MySQL] *> +> limit = guardDialect diLimit *> > keyword_ "limit" *> scalarExpr == common table expressions @@ -2216,7 +2216,14 @@ that looks identical to this), then it isn't treated as a keyword at all. When there is some overlap (e.g. 'set'), then there is either special case parsing code to handle this (in the case of set), or it is not treated as a keyword (not perfect, but if it more or less -works, ok for now) +works, ok for now). + +It is possible to have a problem if you remove something which is a +keyword from this list, and still want to parse statements using it +as a keyword - for instance, removing things like 'from' or 'as', +will likely mean many things don't parse anymore. + + ----------- @@ -2229,10 +2236,10 @@ different parsers can be used for different dialects > type Parser = GenParser Token ParseState -> guardDialect :: [SyntaxFlavour] -> Parser () -> guardDialect ds = do +> guardDialect :: (Dialect -> Bool) -> Parser () +> guardDialect f = do > d <- getState -> guard (diSyntaxFlavour d `elem` ds) +> guard (f d) TODO: the ParseState and the Dialect argument should be turned into a flags struct. Part (or all?) of this struct is the dialect diff --git a/Language/SQL/SimpleSQL/Pretty.lhs b/Language/SQL/SimpleSQL/Pretty.lhs index 0c7e21c..abc1d1c 100644 --- a/Language/SQL/SimpleSQL/Pretty.lhs +++ b/Language/SQL/SimpleSQL/Pretty.lhs @@ -337,7 +337,7 @@ which have been changed to try to improve the layout of the output. > ] > where > fetchFirst = -> me (\e -> if diSyntaxFlavour dia == MySQL +> me (\e -> if diLimit dia > then text "limit" <+> scalarExpr dia e > else text "fetch first" <+> scalarExpr dia e > <+> text "rows only") fe