From 6720d3e3a3e6bac83aeceffa6d7bf7a644058e17 Mon Sep 17 00:00:00 2001 From: Jake Wheat Date: Thu, 17 Apr 2014 19:27:18 +0300 Subject: [PATCH] add support for named host parameters --- Language/SQL/SimpleSQL/Parser.lhs | 17 +++++++++++++++++ Language/SQL/SimpleSQL/Pretty.lhs | 5 +++++ Language/SQL/SimpleSQL/Syntax.lhs | 5 +++++ tools/Language/SQL/SimpleSQL/SQL2003.lhs | 11 ++++++----- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Language/SQL/SimpleSQL/Parser.lhs b/Language/SQL/SimpleSQL/Parser.lhs index 06d95e7..612853c 100644 --- a/Language/SQL/SimpleSQL/Parser.lhs +++ b/Language/SQL/SimpleSQL/Parser.lhs @@ -152,6 +152,16 @@ use in e.g. select * from t where a = ? > parameter :: Parser ValueExpr > parameter = Parameter <$ questionMark +named parameter: + +select x from t where x > :param + +> hostParameter :: Parser ValueExpr +> hostParameter = +> HostParameter +> <$> hostParameterToken +> <*> optionMaybe (keyword "indicator" *> hostParameterToken) + == function application, aggregates and windows this represents anything which syntactically looks like regular C @@ -572,6 +582,7 @@ fragile and could at least do with some heavy explanation. > term :: Parser ValueExpr > term = choice [literal > ,parameter +> ,hostParameter > ,caseValue > ,cast > ,try specialOpKs @@ -887,6 +898,12 @@ make this choice. TODO: add "" inside quoted identifiers +parses an identifier with a : prefix. The : isn't included in the +return value + +> hostParameterToken :: Parser String +> hostParameterToken = lexeme $ char ':' *> identifier + todo: work out the symbol parsing better > symbol :: String -> Parser String diff --git a/Language/SQL/SimpleSQL/Pretty.lhs b/Language/SQL/SimpleSQL/Pretty.lhs index 2d243c5..9a53d71 100644 --- a/Language/SQL/SimpleSQL/Pretty.lhs +++ b/Language/SQL/SimpleSQL/Pretty.lhs @@ -46,6 +46,11 @@ which have been changed to try to improve the layout of the output. > valueExpr (Iden i) = name i > valueExpr Star = text "*" > valueExpr Parameter = text "?" +> valueExpr (HostParameter p i) = +> text (':':p) +> <+> maybe empty +> (\i' -> text "indicator" <+> text (':':i')) +> i > valueExpr (App f es) = name f <> parens (commaSep (map valueExpr es)) diff --git a/Language/SQL/SimpleSQL/Syntax.lhs b/Language/SQL/SimpleSQL/Syntax.lhs index 36e0e57..052a18b 100644 --- a/Language/SQL/SimpleSQL/Syntax.lhs +++ b/Language/SQL/SimpleSQL/Syntax.lhs @@ -121,6 +121,11 @@ > -- means not in was used ('a not in (1,2)') > | In Bool ValueExpr InPredValue > | Parameter -- ^ Represents a ? in a parameterized query +> | HostParameter String (Maybe String) -- ^ represents a host +> -- parameter, e.g. :a. The +> -- Maybe String is for the +> -- indicator, e.g. :var +> -- indicator :nl > deriving (Eq,Show,Read,Data,Typeable) > -- | Represents an identifier name, which can be quoted or unquoted. diff --git a/tools/Language/SQL/SimpleSQL/SQL2003.lhs b/tools/Language/SQL/SimpleSQL/SQL2003.lhs index 9e1b3c8..334a697 100644 --- a/tools/Language/SQL/SimpleSQL/SQL2003.lhs +++ b/tools/Language/SQL/SimpleSQL/SQL2003.lhs @@ -26,7 +26,7 @@ large amount of the SQL. > --,identifiers > --,typeNames > --,parenthesizedValueExpression -> --,targetSpecification +> ,targetSpecification > --,contextuallyTypeValueSpec > --,nextValueExpression > --,arrayElementReference @@ -1069,10 +1069,11 @@ TODO: review how the special keywords are parsed and add tests for these > targetSpecification :: TestItem > targetSpecification = Group "target specification" $ map (uncurry TestValueExpr) -> [(":hostparam", undefined) -> ,(":hostparam indicator :another_host_param", undefined) -> ,("?", undefined) -> ,(":h[3]", undefined) +> [(":hostparam", HostParameter "hostparam" Nothing) +> ,(":hostparam indicator :another_host_param" +> ,HostParameter "hostparam" $ Just "another_host_param") +> ,("?", Parameter) +> --,(":h[3]", Array (HostParameter "h" Nothing) [NumLit "3"]) > ] TODO: modules stuff, not sure what current_collation is