on dev help sign in
working simple joins
commit1f78743cd28aab73d907720d8ba9568282122533 authorEitan Chatav <eitan.chatav@gmail.com> dateThu 1 Jun 2017, 11:07 (9 years ago) parent6d3ccd3
3 files changed+52−41
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+6−7 squeel-postgresql/src/Squeel/PostgreSQL/Schema.hs+2−1 squeel-postgresql/test/Squeel/PostgreSQL/QuerySpec.hs+44−33
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+6−7
@@ -72,8 +72,7 @@ instance HasColumn column columns ty
7272 fromLabel = getColumn
7373
7474(.&.)
75− :: forall columns column params tables table ty
76− . (HasTable table tables columns, HasColumn column columns ty)
75+ :: (HasTable table tables columns, HasColumn column columns ty)
7776 => Alias table -> Alias column -> Expression params tables ty
7877Alias table .&. Alias column = UnsafeExpression $
7978 fromString (symbolVal' table)
@@ -174,13 +173,13 @@ crossJoin
174173 -> TableRef params schema tables
175174 -> TableRef params schema (table ': tables)
176175crossJoin table tables = UnsafeTableRef $
177− "(" <> renderTableRef tables <> " CROSS JOIN " <> renderTableRef table <> ")"
176+ renderTableRef tables <> " CROSS JOIN " <> renderTableRef table
178177
179178innerJoin
180− :: TableRef params tables '[left]
181− -> Expression params '[left,right] 'PGBool
182− -> TableRef params tables (right ': rest)
183− -> TableRef params tables (left ': right ': rest)
179+ :: TableRef params schema '[table]
180+ -> Expression params (table ': tables) 'PGBool
181+ -> TableRef params schema tables
182+ -> TableRef params schema (table ': tables)
184183innerJoin table on tables = UnsafeTableRef $
185184 renderTableRef tables
186185 <> " INNER JOIN " <>
squeel-postgresql/src/Squeel/PostgreSQL/Schema.hs+2−1
@@ -79,7 +79,8 @@ type (:::) (alias :: Symbol) (ty :: polykind) = '(alias,ty)
7979
8080data Alias (alias :: Symbol) = Alias (Proxy# alias)
8181
82−instance IsLabel alias (Alias alias) where fromLabel = Alias
82+instance alias1 ~ alias2 => IsLabel alias1 (Alias alias2) where
83+ fromLabel = Alias
8384
8485data Aliased expression aliased where
8586 As
squeel-postgresql/test/Squeel/PostgreSQL/QuerySpec.hs+44−33
@@ -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)