1
Fork 0

Fix handling of empty statements.

This commit is contained in:
Ignat Insarov 2020-12-20 20:03:15 +05:00
parent b41d4838d4
commit 0aa28ff96a
6 changed files with 41 additions and 11 deletions
Language/SQL/SimpleSQL

View file

@ -1450,14 +1450,14 @@ TODO: change style
> topLevelQueryExpr = queryExpr <??> (id <$ semi)
> topLevelStatement :: Parser Statement
> topLevelStatement = statement <??> (id <$ semi)
> topLevelStatement = statement
-------------------------
= Statements
> statement :: Parser Statement
> statement = choice
> statementWithoutSemicolon :: Parser Statement
> statementWithoutSemicolon = choice
> [keyword_ "create" *> choice [createSchema
> ,createTable
> ,createView
@ -1488,6 +1488,9 @@ TODO: change style
> ,revoke
> ,SelectStatement <$> queryExpr
> ]
>
> statement :: Parser Statement
> statement = statementWithoutSemicolon <* optional semi <|> semi *> pure EmptyStatement
> createSchema :: Parser Statement
> createSchema = keyword_ "schema" >>
@ -1895,12 +1898,8 @@ wrapper to parse a series of statements. They must be separated by
semicolon, but for the last statement, the trailing semicolon is
optional.
TODO: change style
> statements :: Parser [Statement]
> statements = (:[]) <$> statement
> >>= optionSuffix ((semi *>) . pure)
> >>= optionSuffix (\p -> (p++) <$> statements)
> statements = many statement
----------------------------------------------

View file

@ -35,14 +35,22 @@ Try to do this when this code is ported to a modern pretty printing lib.
> prettyScalarExpr :: Dialect -> ScalarExpr -> String
> prettyScalarExpr d = render . scalarExpr d
> -- | A terminating semicolon.
> terminator :: Doc
> terminator = text ";\n"
> -- | Convert a statement ast to concrete syntax.
> prettyStatement :: Dialect -> Statement -> String
> prettyStatement d = render . statement d
> prettyStatement _ EmptyStatement = render terminator
> prettyStatement d s = render (statement d s)
> -- | Convert a list of statements to concrete syntax. A semicolon
> -- is inserted after each statement.
> prettyStatements :: Dialect -> [Statement] -> String
> prettyStatements d = render . vcat . map ((<> text ";\n") . statement d)
> prettyStatements d = render . vcat . map prettyStatementWithSemicolon
> where
> prettyStatementWithSemicolon :: Statement -> Doc
> prettyStatementWithSemicolon s = statement d s <> terminator
= scalar expressions
@ -641,6 +649,7 @@ Try to do this when this code is ported to a modern pretty printing lib.
> statement _ (StatementComment cs) = vcat $ map comment cs
> statement _ EmptyStatement = empty
== sessions

View file

@ -530,6 +530,7 @@ I'm not sure if this is valid syntax or not.
> | SetTransform
> | SetCollation -}
> | StatementComment [Comment]
> | EmptyStatement
> deriving (Eq,Show,Read,Data,Typeable)
> data DropBehaviour =