From 2938f642d3ba76678219a4f1b4105ff62776c3d6 Mon Sep 17 00:00:00 2001 From: Jake Wheat Date: Sat, 1 Aug 2015 21:13:37 +0300 Subject: [PATCH] fix to work on ghc 7.8.4, 7.6.3, plus small tweaks --- LICENSE | 2 +- simple-sql-parser.cabal | 9 +- tools/Fixity.lhs | 1 + .../SQL/SimpleSQL/SQL2011DataManipulation.lhs | 174 ++++++++++++++++++ tools/SimpleSqlParserTool.lhs | 1 + 5 files changed, 182 insertions(+), 5 deletions(-) diff --git a/LICENSE b/LICENSE index d3c6f23..d6db933 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2013, Jake Wheat +Copyright (c) 2013, 2014, 2015, Jake Wheat All rights reserved. diff --git a/simple-sql-parser.cabal b/simple-sql-parser.cabal index bd30a22..1e8fed4 100644 --- a/simple-sql-parser.cabal +++ b/simple-sql-parser.cabal @@ -3,9 +3,10 @@ version: 0.5.0 synopsis: A parser for SQL. description: A parser for SQL. Parses most SQL:2011 - queries, DML, schema/DDL, transaction control, - session and access control. Please see the - homepage for more information + queries, non-query DML, DDL, access control, + transaction management and session management + syntax. Please see the homepage for more + information . homepage: http://jakewheat.github.io/simple-sql-parser/ @@ -13,7 +14,7 @@ license: BSD3 license-file: LICENSE author: Jake Wheat maintainer: jakewheatmail@gmail.com -copyright: Copyright Jake Wheat 2013, 2014 +copyright: Copyright Jake dist/build/Tests/TestsWheat 2013, 2014, 2015 category: Database,Language build-type: Simple extra-source-files: README,LICENSE,changelog diff --git a/tools/Fixity.lhs b/tools/Fixity.lhs index 1f34e91..886608d 100644 --- a/tools/Fixity.lhs +++ b/tools/Fixity.lhs @@ -155,6 +155,7 @@ then the fixer can be easier? > import Debug.Trace > import Text.Show.Pretty > import Data.List +> import Control.Applicative > import qualified Test.Tasty as T > import qualified Test.Tasty.HUnit as H diff --git a/tools/Language/SQL/SimpleSQL/SQL2011DataManipulation.lhs b/tools/Language/SQL/SimpleSQL/SQL2011DataManipulation.lhs index bacadd5..a30490d 100644 --- a/tools/Language/SQL/SimpleSQL/SQL2011DataManipulation.lhs +++ b/tools/Language/SQL/SimpleSQL/SQL2011DataManipulation.lhs @@ -215,6 +215,178 @@ Section 14 in Foundation USING ON +merge into t + using t + on a = b + merge operation specification + +merge into t as u +using (table factor | joined expression) + + MERGE INTO tablename USING table_reference ON (condition) + WHEN MATCHED THEN + UPDATE SET column1 = value1 [, column2 = value2 ...] + WHEN NOT MATCHED THEN + INSERT (column1 [, column2 ...]) VALUES (value1 [, value2 ... + +merge into t23 +using t42 +on t42.id = t23.id +when matched then + update + set t23.col1 = t42.col1 +when not matched then + insert (id, col1) + values (t42.id, t42.col1) + + + +MERGE INTO TableA u + +USING (SELECT b.Key1, b.ColB1, c.ColC1 + +FROM TableB b + +INNER JOIN TableC c ON c.KeyC1 = b.KeyB1 + +) s + +ON (u.KeyA1 = s.KeyA1) + +WHEN MATCHED THEN + +UPDATE SET u.ColA1 = s.ColB1, u.ColA2 = s.ColC1 + + +MERGE INTO Department +USING NewDept AS ND +ON nd.Department_Number = Department. +Department_Number +WHEN MATCHED THEN UPDATE +SET budget_amount = nd.Budget_Amount +WHEN NOT MATCHED THEN INSERT +VALUES +(nd.Department_Number, nd.Department_ +Name, nd.Budget_Amount, + nd.Manager_Employee_Number); + + +MERGE INTO Orders2 +USING Orders3 +ON ORDERS3.Order_Number = Orders2. +Order_Number +WHEN NOT MATCHED THEN INSERT +Orders3.order_number, Orders3. +invoice_number, + Orders3.customer_number, Orders3. +initial_order_date, + Orders3.invoice_date, Orders3. +invoice_amount); + +MERGE INTO Orders2 +USING Orders3 +ON ORDERS3.Order_Number = Orders2. +Order_Number AND 1=0 +WHEN NOT MATCHED THEN INSERT +(Orders3.order_number, Orders3.invoice_number, + Orders3.customer_number, Orders3. +initial_order_date, + Orders3.invoice_date, Orders3. +invoice_amount); + +MERGE INTO Department +USING NewDept AS ND +ON nd.Department_Number = Department. +Department_Number +WHEN MATCHED THEN UPDATE +SET budget_amount = nd.Budget_Amount +LOGGING ALL ERRORS WITH NO LIMIT; + + +MERGE INTO Department +USING + (SELECT Department_Number, +department_name, + Budget_Amount, +Manager_Employee_Number + FROM NewDept + WHERE Department_Number IN +(SELECT Department_Number + FROM Employee)) AS m +ON m.Department_Number = Department. +Department_Number +WHEN MATCHED THEN UPDATE +SET budget_amount = m.Budget_Amount +WHEN NOT MATCHED THEN INSERT +(m.Department_Number, m.Department_ +Name, m.Budget_Amount, +m.Manager_Employee_Number) +LOGGING ALL ERRORS WITH NO LIMIT; + + +MERGE INTO Customers AS c +USING Moved AS m + ON m.SSN = c.SSN +WHEN MATCHED +THEN UPDATE +SET Street = m.Street, + HouseNo = m.HouseNo, + City = m.City; + +MERGE INTO CentralOfficeAccounts AS C -- Target +USING BranchOfficeAccounts AS B -- Source + ON C.account_nbr = B.account_nbr +WHEN MATCHED THEN -- On match update + UPDATE SET C.company_name = B.company_name, + C.primary_contact = B.primary_contact, + C.contact_phone = B.contact_phone +WHEN NOT MATCHED THEN -- Add missing + INSERT (account_nbr, company_name, primary_contact, contact_phone) + VALUES (B.account_nbr, B.company_name, B.primary_contact, B.contact_phone); + +SELECT account_nbr, company_name, primary_contact, contact_phone +FROM CentralOfficeAccounts; + + + +MERGE INTO CentralOfficeAccounts AS C -- Target +USING BranchOfficeAccounts AS B -- Source + ON C.account_nbr = B.account_nbr +WHEN MATCHED -- On match update + AND (C.company_name <> B.company_name -- Additional search conditions + OR C.primary_contact <> B.primary_contact + OR C.contact_phone <> B.contact_phone) THEN + UPDATE SET C.company_name = B.company_name, + C.primary_contact = B.primary_contact, + C.contact_phone = B.contact_phone +WHEN NOT MATCHED THEN -- Add missing + INSERT (account_nbr, company_name, primary_contact, contact_phone) + VALUES (B.account_nbr, B.company_name, B.primary_contact, B.contact_phone); + + + +MERGE INTO CentralOfficeAccounts AS C -- Target +USING BranchOfficeAccounts AS B -- Source + ON C.account_nbr = B.account_nbr +WHEN MATCHED -- On match update + AND (C.company_name <> B.company_name -- Additional search conditions + OR C.primary_contact <> B.primary_contact + OR C.contact_phone <> B.contact_phone) THEN + UPDATE SET C.company_name = B.company_name, + C.primary_contact = B.primary_contact, + C.contact_phone = B.contact_phone +WHEN NOT MATCHED THEN -- Add missing + INSERT (account_nbr, company_name, primary_contact, contact_phone) + VALUES (B.account_nbr, B.company_name, B.primary_contact, B.contact_phone) +WHEN SOURCE NOT MATCHED THEN -- Delete missing from source + DELETE; + +SELECT account_nbr, company_name, primary_contact, contact_phone +FROM CentralOfficeAccounts; + + + + ::= @@ -351,6 +523,8 @@ Section 14 in Foundation DECLARE LOCAL TEMPORARY TABLE
[ ON COMMIT
ROWS ] +declare local temporary table t (a int) [on commit {preserve | delete} rows] + 14.17 ::= diff --git a/tools/SimpleSqlParserTool.lhs b/tools/SimpleSqlParserTool.lhs index be14bcc..fd86964 100644 --- a/tools/SimpleSqlParserTool.lhs +++ b/tools/SimpleSqlParserTool.lhs @@ -14,6 +14,7 @@ indent: parse then pretty print sql > import System.Exit > import Data.List > import Text.Show.Pretty +> import Control.Applicative > import Language.SQL.SimpleSQL.Pretty > import Language.SQL.SimpleSQL.Parser