1
Fork 0

work on parsing tpch

add parse function for parsing multiple query expressions separated by
  semicolon
add parser for substring operator
tweak the layout for pretty printing binary operators
add sql file with tpch queries in it
add simple exe to parse and pretty print multiple query expressions
This commit is contained in:
Jake Wheat 2013-12-14 00:34:05 +02:00
parent f08f4eb13b
commit d005dc1706
5 changed files with 754 additions and 6 deletions
Language/SQL/SimpleSQL

View file

@ -3,6 +3,7 @@
> module Language.SQL.SimpleSQL.Parser
> (parseQueryExpr
> ,parseScalarExpr
> ,parseQueryExprs
> ,ParseError(..)) where
> import Text.Groom
@ -28,6 +29,15 @@
> $ parse (setPos p *> whiteSpace
> *> queryExpr <* eof) f src
> parseQueryExprs :: FilePath
> -> Maybe (Int,Int)
> -> String
> -> Either ParseError [QueryExpr]
> parseQueryExprs f p src =
> either (Left . convParseError src) Right
> $ parse (setPos p *> whiteSpace
> *> queryExprs <* eof) f src
> parseScalarExpr :: FilePath
> -> Maybe (Int,Int)
> -> String
@ -209,6 +219,16 @@ to be.
> *> scalarExpr'))
> where makeOp n e = SpecialOp "extract" [Iden n, e]
> substring :: P ScalarExpr
> substring = try (keyword_ "substring") >>
> parens (makeOp <$> scalarExpr'
> <*> (keyword_ "from"
> *> scalarExpr')
> <*> (keyword_ "for"
> *> scalarExpr')
> )
> where makeOp a b c = SpecialOp "substring" [a,b,c]
> inSuffix :: ScalarExpr -> P ScalarExpr
> inSuffix e =
> In
@ -327,6 +347,7 @@ postgresql handles this
> ,scase
> ,cast
> ,extract
> ,substring
> ,subquery
> ,prefixUnaryOp
> ,(try app) >>= windowSuffix
@ -547,6 +568,14 @@ attempt to fix the precedence and associativity. Doesn't work
> <*> queryExpr
> ,return qe]
> queryExprs :: P [QueryExpr]
> queryExprs = do
> qe <- queryExpr
> choice [[qe] <$ eof
> ,symbol ";" *>
> choice [[qe] <$ eof
> ,(:) qe <$> queryExprs]]
------------------------------------------------
= helper functions

View file

@ -53,22 +53,29 @@ back into SQL source text. It attempts to format the output nicely.
> <+> orderBy od)
> scalarExpr (SpecialOp nm [a,b,c]) | nm `elem` ["between", "not between"] =
> sep [scalarExpr a
> ,text nm <+> scalarExpr b
> ,text "and" <+> scalarExpr c]
> scalarExpr a <+> text nm <+> scalarExpr b <+> text "and" <+> scalarExpr c
> scalarExpr (SpecialOp "extract" [a,n]) =
> text "extract" <> parens (scalarExpr a
> <+> text "from"
> <+> scalarExpr n)
> scalarExpr (SpecialOp "substring" [a,s,e]) =
> text "substring" <> parens (scalarExpr a
> <+> text "from"
> <+> scalarExpr s
> <+> text "for"
> <+> scalarExpr e)
> scalarExpr (SpecialOp nm es) =
> text nm <+> parens (commaSep $ map scalarExpr es)
> scalarExpr (PrefixOp f e) = text f <+> scalarExpr e
> scalarExpr (PostfixOp f e) = scalarExpr e <+> text f
> scalarExpr (BinOp "and" e0 e1) =
> sep [scalarExpr e0, text "and" <+> scalarExpr e1]
> scalarExpr (BinOp f e0 e1) =
> sep [scalarExpr e0, text f, scalarExpr e1]
> scalarExpr e0 <+> text f <+> scalarExpr e1
> scalarExpr (Case t ws els) =
> sep [text "case" <+> maybe empty scalarExpr t