@@ -1,28 +1,29 @@
11{-# LANGUAGE
22 DataKinds
3+ , MagicHash
34 , OverloadedLabels
45 , OverloadedStrings
5− , PolyKinds
66 , TypeOperators
77#-}
88
9−module Squeel.PostgreSQL.QuerySpec where
9+module Squeel.PostgreSQL.StatementSpec where
1010
1111import Data.Boolean
1212import Data.Function
1313import Data.Vinyl
14+import GHC.Exts
1415import Test.Hspec
1516
16−import Squeel.PostgreSQL.Query
17+import Squeel.PostgreSQL.Statement
1718import Squeel.PostgreSQL.Schema
1819
1920spec :: Spec
2021spec = do
2122 let
22− query `shouldRenderAs` str = renderQuery query `shouldBe` str
23+ query `shouldRenderAs` str = renderStatement query `shouldBe` str
2324 it "correctly renders a simple SELECT query" $ do
2425 let
25− statement :: Query '[] Tables Tables SumAndCol1
26+ statement :: Statement '[] Tables Tables SumAndCol1
2627 statement = select $
2728 ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
2829 `from` (#table1 & where_ true)
@@ -30,7 +31,7 @@ spec = do
3031 "SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 WHERE TRUE;"
3132 it "combines WHEREs using AND" $ do
3233 let
33− statement :: Query '[] Tables Tables SumAndCol1
34+ statement :: Statement '[] Tables Tables SumAndCol1
3435 statement = select $
3536 ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
3637 `from` (#table1 & where_ true & where_ false)
@@ -39,45 +40,45 @@ spec = do
3940 it "performs sub SELECTs" $ do
4041 let
4142 selection = ((#col1 + #col2) `As` #sum :& #col1 :& RNil) `from` #table1
42− statement :: Query '[] Tables Tables SumAndCol1
43+ statement :: Statement '[] Tables Tables SumAndCol1
4344 statement = select $ starFrom (subselect (selection `As` #sub))
4445 statement `shouldRenderAs`
4546 "SELECT * FROM SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 AS sub;"
4647 it "does LIMIT clauses" $ do
4748 let
48− statement :: Query '[] Tables Tables Columns
49+ statement :: Statement '[] Tables Tables Columns
4950 statement = select $ starFrom (#table1 & limit 1)
5051 statement `shouldRenderAs` "SELECT * FROM table1 LIMIT 1;"
5152 it "should use the minimum of given LIMITs" $ do
5253 let
53− statement :: Query '[] Tables Tables Columns
54+ statement :: Statement '[] Tables Tables Columns
5455 statement = select $ starFrom (#table1 & limit 1 & limit 2)
5556 statement `shouldRenderAs`
5657 "SELECT * FROM table1 LIMIT CASE WHEN (1 <= 2) THEN 1 ELSE 2 END;"
5758 it "should render parameters using $ signs" $ do
5859 let
59− statement :: Query '[ 'PGInt8] Tables Tables Columns
60+ statement :: Statement '[ 'PGInt8] Tables Tables Columns
6061 statement = select $ starFrom (#table1 & limit param1)
6162 statement `shouldRenderAs` "SELECT * FROM table1 LIMIT $1;"
6263 it "does OFFSET clauses" $ do
6364 let
64− statement :: Query '[] Tables Tables Columns
65+ statement :: Statement '[] Tables Tables Columns
6566 statement = select $ starFrom (#table1 & offset 1)
6667 statement `shouldRenderAs` "SELECT * FROM table1 OFFSET 1;"
6768 it "should use the sum of given OFFSETs" $ do
6869 let
69− statement :: Query '[] Tables Tables Columns
70+ statement :: Statement '[] Tables Tables Columns
7071 statement = select $ starFrom (#table1 & offset 1 & offset 2)
7172 statement `shouldRenderAs` "SELECT * FROM table1 OFFSET (1 + 2);"
7273 it "correctly render simple INSERTs" $ do
7374 let
74− statement :: Query '[] Tables Tables '[]
75+ statement :: Statement '[] Tables Tables '[]
7576 statement = insertInto #table1 $ 2 `As` #col1 :& 4 `As` #col2 :& RNil
7677 statement `shouldRenderAs`
7778 "INSERT INTO table1 (col1, col2) VALUES (2, 4);"
7879 it "should be safe against SQL injection in literal text" $ do
7980 let
80− statement :: Query '[] StudentsTable StudentsTable '[]
81+ statement :: Statement '[] StudentsTable StudentsTable '[]
8182 statement = insertInto #students $
8283 "Robert'); DROP TABLE students;" `As` #name :& RNil
8384 statement `shouldRenderAs`
@@ -90,7 +91,7 @@ spec = do
9091 :& #shippers .&. #shipperVal `As` #shipperVal :& RNil
9192 it "should render CROSS JOINs" $ do
9293 let
93− statement :: Query '[] JoinTables JoinTables ValueColumns
94+ statement :: Statement '[] JoinTables JoinTables ValueColumns
9495 statement = select $ vals `from`
9596 (tables (#orders & crossJoin #customers & crossJoin #shippers))
9697 statement `shouldRenderAs`
@@ -110,7 +111,7 @@ spec = do
110111 (#orders .&. #customerID ==* #customers .&. #customerID)
111112 & innerJoin #shippers
112113 (#orders .&. #shipperID ==* #shippers .&. #shipperID)
113− selection :: Query '[] JoinTables JoinTables ValueColumns
114+ selection :: Statement '[] JoinTables JoinTables ValueColumns
114115 selection = select $ vals `from` innerJoins
115116 selection `shouldRenderAs`
116117 "SELECT\
@@ -122,6 +123,18 @@ spec = do
122123 \ ON (orders.customerID = customers.customerID)\
123124 \ INNER JOIN shippers\
124125 \ ON (orders.shipperID = shippers.shipperID);"
126+ it "should render simple CREATE TABLE statements" $ do
127+ let
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
133+ let
134+ statement :: Statement '[] Tables '[] '[]
135+ statement = dropTable #table1
136+ statement `shouldRenderAs`
137+ "DROP TABLE table1;"
125138
126139type Columns = '[ "col1" ::: 'PGInt4, "col2" ::: 'PGInt4]
127140type Tables = '[ "table1" ::: Columns ]