@@ -1,28 +1,29 @@
1{-# LANGUAGE
1{-# LANGUAGE
3 , OverloadedLabels
4 , OverloadedLabels
4 , OverloadedStrings
5 , OverloadedStrings
6 , TypeOperators
6 , TypeOperators
9−module Squeel.PostgreSQL.QuerySpec where
9+module Squeel.PostgreSQL.StatementSpec where
11import Data.Boolean
11import Data.Boolean
12import Data.Function
12import Data.Function
13import Data.Vinyl
13import Data.Vinyl
14import Test.Hspec
15import Test.Hspec
16−import Squeel.PostgreSQL.Query
17+import Squeel.PostgreSQL.Statement
17import Squeel.PostgreSQL.Schema
18import Squeel.PostgreSQL.Schema
19spec :: Spec
20spec :: Spec
22− query `shouldRenderAs` str = renderQuery query `shouldBe` str
23+ query `shouldRenderAs` str = renderStatement query `shouldBe` str
23 it "correctly renders a simple SELECT query" $ do
24 it "correctly renders a simple SELECT query" $ do
25− statement :: Query '[] Tables Tables SumAndCol1
26+ statement :: Statement '[] Tables Tables SumAndCol1
26 statement = select $
27 statement = select $
27 ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
28 ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
28 `from` (#table1 & where_ true)
29 `from` (#table1 & where_ true)
@@ -30,7 +31,7 @@ spec = do
30 "SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 WHERE TRUE;"
31 "SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 WHERE TRUE;"
31 it "combines WHEREs using AND" $ do
32 it "combines WHEREs using AND" $ do
33− statement :: Query '[] Tables Tables SumAndCol1
34+ statement :: Statement '[] Tables Tables SumAndCol1
34 statement = select $
35 statement = select $
35 ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
36 ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
36 `from` (#table1 & where_ true & where_ false)
37 `from` (#table1 & where_ true & where_ false)
@@ -39,45 +40,45 @@ spec = do
39 it "performs sub SELECTs" $ do
40 it "performs sub SELECTs" $ do
41 selection = ((#col1 + #col2) `As` #sum :& #col1 :& RNil) `from` #table1
42 selection = ((#col1 + #col2) `As` #sum :& #col1 :& RNil) `from` #table1
42− statement :: Query '[] Tables Tables SumAndCol1
43+ statement :: Statement '[] Tables Tables SumAndCol1
43 statement = select $ starFrom (subselect (selection `As` #sub))
44 statement = select $ starFrom (subselect (selection `As` #sub))
44 statement `shouldRenderAs`
45 statement `shouldRenderAs`
45 "SELECT * FROM SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 AS sub;"
46 "SELECT * FROM SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 AS sub;"
46 it "does LIMIT clauses" $ do
47 it "does LIMIT clauses" $ do
48− statement :: Query '[] Tables Tables Columns
49+ statement :: Statement '[] Tables Tables Columns
49 statement = select $ starFrom (#table1 & limit 1)
50 statement = select $ starFrom (#table1 & limit 1)
50 statement `shouldRenderAs` "SELECT * FROM table1 LIMIT 1;"
51 statement `shouldRenderAs` "SELECT * FROM table1 LIMIT 1;"
51 it "should use the minimum of given LIMITs" $ do
52 it "should use the minimum of given LIMITs" $ do
53− statement :: Query '[] Tables Tables Columns
54+ statement :: Statement '[] Tables Tables Columns
54 statement = select $ starFrom (#table1 & limit 1 & limit 2)
55 statement = select $ starFrom (#table1 & limit 1 & limit 2)
55 statement `shouldRenderAs`
56 statement `shouldRenderAs`
56 "SELECT * FROM table1 LIMIT CASE WHEN (1 <= 2) THEN 1 ELSE 2 END;"
57 "SELECT * FROM table1 LIMIT CASE WHEN (1 <= 2) THEN 1 ELSE 2 END;"
57 it "should render parameters using $ signs" $ do
58 it "should render parameters using $ signs" $ do
59− statement :: Query '[ 'PGInt8] Tables Tables Columns
60+ statement :: Statement '[ 'PGInt8] Tables Tables Columns
60 statement = select $ starFrom (#table1 & limit param1)
61 statement = select $ starFrom (#table1 & limit param1)
61 statement `shouldRenderAs` "SELECT * FROM table1 LIMIT $1;"
62 statement `shouldRenderAs` "SELECT * FROM table1 LIMIT $1;"
62 it "does OFFSET clauses" $ do
63 it "does OFFSET clauses" $ do
64− statement :: Query '[] Tables Tables Columns
65+ statement :: Statement '[] Tables Tables Columns
65 statement = select $ starFrom (#table1 & offset 1)
66 statement = select $ starFrom (#table1 & offset 1)
66 statement `shouldRenderAs` "SELECT * FROM table1 OFFSET 1;"
67 statement `shouldRenderAs` "SELECT * FROM table1 OFFSET 1;"
67 it "should use the sum of given OFFSETs" $ do
68 it "should use the sum of given OFFSETs" $ do
69− statement :: Query '[] Tables Tables Columns
70+ statement :: Statement '[] Tables Tables Columns
70 statement = select $ starFrom (#table1 & offset 1 & offset 2)
71 statement = select $ starFrom (#table1 & offset 1 & offset 2)
71 statement `shouldRenderAs` "SELECT * FROM table1 OFFSET (1 + 2);"
72 statement `shouldRenderAs` "SELECT * FROM table1 OFFSET (1 + 2);"
72 it "correctly render simple INSERTs" $ do
73 it "correctly render simple INSERTs" $ do
74− statement :: Query '[] Tables Tables '[]
75+ statement :: Statement '[] Tables Tables '[]
75 statement = insertInto #table1 $ 2 `As` #col1 :& 4 `As` #col2 :& RNil
76 statement = insertInto #table1 $ 2 `As` #col1 :& 4 `As` #col2 :& RNil
76 statement `shouldRenderAs`
77 statement `shouldRenderAs`
77 "INSERT INTO table1 (col1, col2) VALUES (2, 4);"
78 "INSERT INTO table1 (col1, col2) VALUES (2, 4);"
78 it "should be safe against SQL injection in literal text" $ do
79 it "should be safe against SQL injection in literal text" $ do
80− statement :: Query '[] StudentsTable StudentsTable '[]
81+ statement :: Statement '[] StudentsTable StudentsTable '[]
81 statement = insertInto #students $
82 statement = insertInto #students $
82 "Robert'); DROP TABLE students;" `As` #name :& RNil
83 "Robert'); DROP TABLE students;" `As` #name :& RNil
83 statement `shouldRenderAs`
84 statement `shouldRenderAs`
@@ -90,7 +91,7 @@ spec = do
90 :& #shippers .&. #shipperVal `As` #shipperVal :& RNil
91 :& #shippers .&. #shipperVal `As` #shipperVal :& RNil
91 it "should render CROSS JOINs" $ do
92 it "should render CROSS JOINs" $ do
93− statement :: Query '[] JoinTables JoinTables ValueColumns
94+ statement :: Statement '[] JoinTables JoinTables ValueColumns
94 statement = select $ vals `from`
95 statement = select $ vals `from`
95 (tables (#orders & crossJoin #customers & crossJoin #shippers))
96 (tables (#orders & crossJoin #customers & crossJoin #shippers))
96 statement `shouldRenderAs`
97 statement `shouldRenderAs`
@@ -110,7 +111,7 @@ spec = do
110 (#orders .&. #customerID ==* #customers .&. #customerID)
111 (#orders .&. #customerID ==* #customers .&. #customerID)
111 & innerJoin #shippers
112 & innerJoin #shippers
112 (#orders .&. #shipperID ==* #shippers .&. #shipperID)
113 (#orders .&. #shipperID ==* #shippers .&. #shipperID)
113− selection :: Query '[] JoinTables JoinTables ValueColumns
114+ selection :: Statement '[] JoinTables JoinTables ValueColumns
114 selection = select $ vals `from` innerJoins
115 selection = select $ vals `from` innerJoins
115 selection `shouldRenderAs`
116 selection `shouldRenderAs`
@@ -122,6 +123,18 @@ spec = do
122 \ ON (orders.customerID = customers.customerID)\
123 \ ON (orders.customerID = customers.customerID)\
123 \ INNER JOIN shippers\
124 \ INNER JOIN shippers\
124 \ ON (orders.shipperID = shippers.shipperID);"
125 \ ON (orders.shipperID = shippers.shipperID);"
126+ it "should render simple CREATE TABLE statements" $ do
128+ statement :: Statement '[] '[] Tables '[]
129+ statement = createTable #table1 (proxy# :: Proxy# Columns)
130+ statement `shouldRenderAs`
131+ "CREATE TABLE table1 (col1 int4, col2 int4);"
132+ it "should render DROP TABLE statements" $ do
134+ statement :: Statement '[] Tables '[] '[]
135+ statement = dropTable #table1
136+ statement `shouldRenderAs`
137+ "DROP TABLE table1;"
126type Columns = '[ "col1" ::: 'PGInt4, "col2" ::: 'PGInt4]
139type Columns = '[ "col1" ::: 'PGInt4, "col2" ::: 'PGInt4]
127type Tables = '[ "table1" ::: Columns ]
140type Tables = '[ "table1" ::: Columns ]