From b68c116839cf852832cef28f3cd479ac7622293c Mon Sep 17 00:00:00 2001
From: Jake Wheat <jakewheatmail@gmail.com>
Date: Thu, 19 Dec 2013 10:44:20 +0200
Subject: [PATCH] add support for ? for parameterized queries

---
 Language/SQL/SimpleSQL/Fixity.lhs            | 1 +
 Language/SQL/SimpleSQL/Parser.lhs            | 8 ++++++++
 Language/SQL/SimpleSQL/Pretty.lhs            | 1 +
 Language/SQL/SimpleSQL/Syntax.lhs            | 1 +
 tools/Language/SQL/SimpleSQL/ScalarExprs.lhs | 7 +++++++
 5 files changed, 18 insertions(+)

diff --git a/Language/SQL/SimpleSQL/Fixity.lhs b/Language/SQL/SimpleSQL/Fixity.lhs
index 58a38d5..77b01e5 100644
--- a/Language/SQL/SimpleSQL/Fixity.lhs
+++ b/Language/SQL/SimpleSQL/Fixity.lhs
@@ -78,6 +78,7 @@ the fixity code.
 >     -- TODO fix me
 >     (SpecialOpK {}) -> str ('v':show e)
 >     Iden {} -> str ('v':show e)
+>     Parameter -> str ('v':show e)
 >     StringLit {} -> str ('v':show e)
 >     NumLit {} -> str ('v':show e)
 >     App n es -> HSE.App (var ('f':name n)) $ ltoh es
diff --git a/Language/SQL/SimpleSQL/Parser.lhs b/Language/SQL/SimpleSQL/Parser.lhs
index c88c158..1104c1c 100644
--- a/Language/SQL/SimpleSQL/Parser.lhs
+++ b/Language/SQL/SimpleSQL/Parser.lhs
@@ -137,6 +137,13 @@ in any scalar expression context.
 > star :: P ScalarExpr
 > star = Star <$ symbol "*"
 
+== parameter
+
+use in e.g. select * from t where a = ?
+
+> parameter :: P ScalarExpr
+> parameter = Parameter <$ symbol "?"
+
 == function application, aggregates and windows
 
 this represents anything which syntactically looks like regular C
@@ -591,6 +598,7 @@ could at least do with some heavy explanation.
 
 > factor :: P ScalarExpr
 > factor = choice [literal
+>                 ,parameter
 >                 ,scase
 >                 ,cast
 >                 ,try specialOpKs
diff --git a/Language/SQL/SimpleSQL/Pretty.lhs b/Language/SQL/SimpleSQL/Pretty.lhs
index c0e86b2..0eff223 100644
--- a/Language/SQL/SimpleSQL/Pretty.lhs
+++ b/Language/SQL/SimpleSQL/Pretty.lhs
@@ -40,6 +40,7 @@
 >     <+> maybe empty (parens . text . show ) p
 > scalarExpr (Iden i) = name i
 > scalarExpr Star = text "*"
+> scalarExpr Parameter = text "?"
 
 > scalarExpr (App f es) = name f <> parens (commaSep (map scalarExpr es))
 
diff --git a/Language/SQL/SimpleSQL/Syntax.lhs b/Language/SQL/SimpleSQL/Syntax.lhs
index d461896..2f749db 100644
--- a/Language/SQL/SimpleSQL/Syntax.lhs
+++ b/Language/SQL/SimpleSQL/Syntax.lhs
@@ -115,6 +115,7 @@
 >       -- | in list literal and in subquery, if the bool is false it
 >       -- means not in was used ('a not in (1,2)')
 >     | In Bool ScalarExpr InPredValue
+>     | Parameter -- ^ Represents a ? in a parameterized query
 >       deriving (Eq,Show,Read)
 
 > -- | Represents an identifier name, which can be quoted or unquoted.
diff --git a/tools/Language/SQL/SimpleSQL/ScalarExprs.lhs b/tools/Language/SQL/SimpleSQL/ScalarExprs.lhs
index 0e13325..6c13b01 100644
--- a/tools/Language/SQL/SimpleSQL/ScalarExprs.lhs
+++ b/tools/Language/SQL/SimpleSQL/ScalarExprs.lhs
@@ -12,6 +12,7 @@ Tests for parsing scalar expressions
 >     [literals
 >     ,identifiers
 >     ,star
+>     ,parameter
 >     ,dots
 >     ,app
 >     ,caseexp
@@ -56,6 +57,12 @@ Tests for parsing scalar expressions
 >     --,("ROW(t.*,42)", App "ROW" [Star2 "t", NumLit "42"])
 >     ]
 
+> parameter :: TestItem
+> parameter = Group "parameter" $ map (uncurry TestScalarExpr)
+>     [("?", Parameter)
+>     ]
+
+
 > dots :: TestItem
 > dots = Group "dot" $ map (uncurry TestScalarExpr)
 >     [("t.a", BinOp (Iden "t") "." (Iden "a"))