@@ -64,23 +64,27 @@ spec = do
6464 ("Robert'); DROP TABLE students;" `As` #name :& RNil)
6565 `shouldRenderAs`
6666 "INSERT INTO students (name) VALUES (E'Robert''); DROP TABLE students;');"
67− -- describe "JOINs" $ do
68− -- it "should render CROSS JOINs" $
69− -- pending
70− -- it "should render INNER JOINs" $ do
71− -- select (vals `from` joinTable) `shouldRenderAs`
72− -- "((orders\
73− -- \ INNER JOIN customers ON orders.customerID = customers.customerID)\
74− -- \ INNER JOIN shippers ON orders.shipperID = shippers.shipperID)"
75−-- #orders
76−-- & innerJoin #customers
77−-- (#orders .&. #customerID ==* #customers .&. #customerID)
78−-- & innerJoin #shippers
79−-- (#orders .&. #shipperID ==* #shippers .&. #shipperID)
80−-- `shouldRenderAs`
81−-- "((orders\
82−-- \ INNER JOIN customers ON orders.customerID = customers.customerID)\
83−-- \ INNER JOIN shippers ON orders.shipperID = shippers.shipperID)"
67+ describe "JOINs" $ do
68+ it "should render CROSS JOINs" $ do
69+ select (vals `from` crossJoins) `shouldRenderAs`
70+ "SELECT\
71+ \ orders.orderVal AS orderVal,\
72+ \ customers.customerVal AS customerVal,\
73+ \ shippers.shipperVal AS shipperVal\
74+ \ FROM orders\
75+ \ CROSS JOIN customers\
76+ \ CROSS JOIN shippers;"
77+ it "should render INNER JOINs" $ do
78+ select (vals `from` innerJoins) `shouldRenderAs`
79+ "SELECT\
80+ \ orders.orderVal AS orderVal,\
81+ \ customers.customerVal AS customerVal,\
82+ \ shippers.shipperVal AS shipperVal\
83+ \ FROM orders\
84+ \ INNER JOIN customers\
85+ \ ON (orders.customerID = customers.customerID)\
86+ \ INNER JOIN shippers\
87+ \ ON (orders.shipperID = shippers.shipperID);"
8488
8589type Columns = '[ "col1" ::: 'PGInt4, "col2" ::: 'PGInt4]
8690type Tables = '[ "table1" ::: Columns ]
@@ -110,30 +114,37 @@ type StudentsColumns = '["name" ::: 'PGText]
110114type StudentsTable = '["students" ::: StudentsColumns]
111115
112116type OrderColumns =
113− [ "orderID" ::: 'PGInt4, "orderVal" ::: 'PGText ]
117+ [ "orderID" ::: 'PGInt4
118+ , "orderVal" ::: 'PGText
119+ , "customerID" ::: 'PGInt4
120+ , "shipperID" ::: 'PGInt4
121+ ]
114122type CustomerColumns =
115123 [ "customerID" ::: 'PGInt4, "customerVal" ::: 'PGFloat4 ]
116124type ShipperColumns =
117125 [ "shipperID" ::: 'PGInt4, "shipperVal" ::: 'PGBool ]
118126type JoinTables =
119− [ "order" ::: OrderColumns
120− , "customer" ::: CustomerColumns
121− , "shipper" ::: ShipperColumns
127+ [ "shippers" ::: ShipperColumns
128+ , "customers" ::: CustomerColumns
129+ , "orders" ::: OrderColumns
122130 ]
123131type ValueColumns =
124132 [ "orderVal" ::: 'PGText
125133 , "customerVal" ::: 'PGFloat4
126134 , "shipperVal" ::: 'PGBool
127135 ]
128−-- vals :: Projection '[] JoinTables ValueColumns
129−-- vals = project $
130−-- #orders .&. #orderVal `As` #orderVal
131−-- :& #customers .&. #customerVal `As` #customerVal
132−-- :& #shippers .&. #shippersVal `As` #shippersVal :& RNil
133−-- joinTable :: Tabulation '[] JoinTables JoinTables
134−-- joinTable = tabulation $
135−-- #orders
136−-- & innerJoin #customers
137−-- (#orders .&. #customerID ==* #customers .&. #customerID)
138−-- & innerJoin #shippers
139−-- (#orders .&. #shipperID ==* #shippers .&. #shipperID)
136+vals :: Projection '[] JoinTables ValueColumns
137+vals = project $
138+ #orders .&. #orderVal `As` #orderVal
139+ :& #customers .&. #customerVal `As` #customerVal
140+ :& #shippers .&. #shipperVal `As` #shipperVal :& RNil
141+crossJoins :: Tabulation '[] JoinTables JoinTables
142+crossJoins = tabulation $
143+ #orders & crossJoin #customers & crossJoin #shippers
144+innerJoins :: Tabulation '[] JoinTables JoinTables
145+innerJoins = tabulation $
146+ #orders
147+ & innerJoin #customers
148+ (#orders .&. #customerID ==* #customers .&. #customerID)
149+ & innerJoin #shippers
150+ (#orders .&. #shipperID ==* #shippers .&. #shipperID)