1
Fork 0

add support for in list, and fix code for in query expr

This commit is contained in:
Jake Wheat 2013-12-13 21:00:06 +02:00
parent 00269617b3
commit 386d835cf8
4 changed files with 57 additions and 26 deletions
Language/SQL/SimpleSQL

View file

@ -132,7 +132,7 @@ digitse[+-]digits
> blacklist = ["as", "from", "where", "having", "group", "order"
> ,"inner", "left", "right", "full", "natural", "join"
> ,"on", "using", "when", "then", "case", "end", "order"
> ,"limit", "offset"]
> ,"limit", "offset", "in"]
TODO: talk about what must be in the blacklist, and what doesn't need
to be.
@ -174,6 +174,18 @@ to be.
> prefixCast = try (CastOp <$> typeName
> <*> stringLiteral)
> inSuffix :: ScalarExpr -> P ScalarExpr
> inSuffix e =
> In
> <$> inty
> <*> return e
> <*> parens (choice
> [InQueryExpr <$> queryExpr
> ,InList <$> commaSep1 scalarExpr])
> where
> inty = try $ choice [True <$ keyword_ "in"
> ,False <$ keyword_ "not" <* keyword_ "in"]
> subquery :: P ScalarExpr
> subquery =
> choice
@ -182,7 +194,6 @@ to be.
> where
> sqkw = try $ choice
> [SqExists <$ keyword_ "exists"
> ,SqIn <$ keyword_ "in"
> ,SqAll <$ try (keyword_ "all")
> ,SqAny <$ keyword_ "any"
> ,SqSome <$ keyword_ "some"]
@ -232,7 +243,10 @@ to be.
> ,identifier
> ,sparens]
> trysuffix e = try (suffix e) <|> return e
> suffix e0 = (makeOp e0 <$> opSymbol <*> factor) >>= trysuffix
> suffix e0 = choice
> [makeOp e0 <$> opSymbol <*> factor
> ,inSuffix e0
> ] >>= trysuffix
> opSymbol = choice (map (try . symbol) binOpSymbolNames
> ++ map (try . keyword) binOpKeywordNames)
> makeOp e0 op e1 = Op op [e0,e1]

View file

@ -59,12 +59,20 @@ back into SQL source text. It attempts to format the output nicely.
> (case ty of
> SqSq -> empty
> SqExists -> text "exists"
> SqIn -> text "in"
> SqAll -> text "all"
> SqSome -> text "some"
> SqAny -> text "any"
> ) <+> parens (queryExpr qe)
> scalarExpr (In b se x) =
> sep [scalarExpr se
> ,if b then empty else text "not"
> ,text "in"
> ,parens (nest 4 $
> case x of
> InList es -> commaSep $ map scalarExpr es
> InQueryExpr qe -> queryExpr qe)]
= query expressions
> queryExpr :: QueryExpr -> Doc

View file

@ -3,6 +3,7 @@
> (ScalarExpr(..)
> ,TypeName(..)
> ,SubQueryExprType(..)
> ,InThing(..)
> ,QueryExpr(..)
> ,makeSelect
> ,Duplicates(..)
@ -28,11 +29,16 @@
> | Cast ScalarExpr TypeName
> | CastOp TypeName String
> | SubQueryExpr SubQueryExprType QueryExpr
> | In Bool -- true if in, false if not in
> ScalarExpr InThing
> deriving (Eq,Show)
> data TypeName = TypeName String deriving (Eq,Show)
> data InThing = InList [ScalarExpr]
> | InQueryExpr QueryExpr
> deriving (Eq,Show)
> data SubQueryExprType = SqExists | SqIn | SqSq | SqAll | SqSome | SqAny
> data SubQueryExprType = SqExists | SqSq | SqAll | SqSome | SqAny
> deriving (Eq,Show)
> data QueryExpr