From c52334943fbff1580e9ee7e840923676857ed059 Mon Sep 17 00:00:00 2001 From: Jake Wheat Date: Tue, 17 Dec 2013 13:27:16 +0200 Subject: [PATCH] add support for values --- Language/SQL/SimpleSQL/Parser.lhs | 4 +++- Language/SQL/SimpleSQL/Pretty.lhs | 3 +++ Language/SQL/SimpleSQL/Syntax.lhs | 2 ++ tools/Language/SQL/SimpleSQL/Postgres.lhs | 5 ++--- tools/Language/SQL/SimpleSQL/QueryExprComponents.lhs | 8 ++++++++ 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Language/SQL/SimpleSQL/Parser.lhs b/Language/SQL/SimpleSQL/Parser.lhs index f9373c9..2d3a909 100644 --- a/Language/SQL/SimpleSQL/Parser.lhs +++ b/Language/SQL/SimpleSQL/Parser.lhs @@ -599,7 +599,7 @@ and union, etc.. > queryExpr :: P QueryExpr > queryExpr = > choice [with -> ,select >>= optionSuffix queryExprSuffix] +> ,choice [values,select] >>= optionSuffix queryExprSuffix] > where > select = try (keyword_ "select") >> > Select @@ -612,6 +612,8 @@ and union, etc.. > <*> option [] orderBy > <*> optionMaybe limit > <*> optionMaybe offset +> values = try (keyword_ "values") +> >> Values <$> commaSep (parens (commaSep scalarExpr)) > queryExprSuffix :: QueryExpr -> P QueryExpr > queryExprSuffix qe = diff --git a/Language/SQL/SimpleSQL/Pretty.lhs b/Language/SQL/SimpleSQL/Pretty.lhs index 8c2e656..427a8aa 100644 --- a/Language/SQL/SimpleSQL/Pretty.lhs +++ b/Language/SQL/SimpleSQL/Pretty.lhs @@ -177,6 +177,9 @@ > (vcat $ punctuate comma $ flip map withs $ \(n,q) -> > name n <+> text "as" <+> parens (queryExpr q)) > ,queryExpr qe] +> queryExpr (Values vs) = +> text "values" +> <+> nest 7 (commaSep (map (parens . commaSep . map scalarExpr) vs)) > selectList :: [(Maybe Name, ScalarExpr)] -> Doc > selectList is = commaSep $ map si is diff --git a/Language/SQL/SimpleSQL/Syntax.lhs b/Language/SQL/SimpleSQL/Syntax.lhs index a47cd27..0b48d26 100644 --- a/Language/SQL/SimpleSQL/Syntax.lhs +++ b/Language/SQL/SimpleSQL/Syntax.lhs @@ -159,6 +159,8 @@ > ,qe2 :: QueryExpr > } > | With [(Name,QueryExpr)] QueryExpr +> | Values [[ScalarExpr]] + > deriving (Eq,Show,Read) TODO: add queryexpr parens to deal with e.g. diff --git a/tools/Language/SQL/SimpleSQL/Postgres.lhs b/tools/Language/SQL/SimpleSQL/Postgres.lhs index 8610bb9..bb4b6dc 100644 --- a/tools/Language/SQL/SimpleSQL/Postgres.lhs +++ b/tools/Language/SQL/SimpleSQL/Postgres.lhs @@ -122,7 +122,7 @@ queries section > ,"SELECT DISTINCT select_list t" -> --,"VALUES (1, 'one'), (2, 'two'), (3, 'three');" -- values list +> ,"VALUES (1, 'one'), (2, 'two'), (3, 'three');" > ,"SELECT 1 AS column1, 'one' AS column2\n\ > \UNION ALL\n\ @@ -130,8 +130,7 @@ queries section > \UNION ALL\n\ > \SELECT 3, 'three';" -> --,"SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (num,letter);" -> -- values list +> ,"SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (num,letter);" > ,"WITH regional_sales AS (\n\ > \ SELECT region, SUM(amount) AS total_sales\n\ diff --git a/tools/Language/SQL/SimpleSQL/QueryExprComponents.lhs b/tools/Language/SQL/SimpleSQL/QueryExprComponents.lhs index 7a061b0..8eb288f 100644 --- a/tools/Language/SQL/SimpleSQL/QueryExprComponents.lhs +++ b/tools/Language/SQL/SimpleSQL/QueryExprComponents.lhs @@ -23,6 +23,7 @@ These are a few misc tests which don't fit anywhere else. > ,limit > ,combos > ,withQueries +> ,values > ] @@ -185,3 +186,10 @@ These are a few misc tests which don't fit anywhere else. > ms1 = ms "a" "t" > ms2 = ms "a" "u" > ms3 = ms "a" "x" + +> values :: TestItem +> values = Group "values" $ map (uncurry TestQueryExpr) +> [("values (1,2),(3,4)" +> ,Values [[NumLit "1", NumLit "2"] +> ,[NumLit "3", NumLit "4"]]) +> ]