on dev help sign in
working simple joins
1f78743Eitan Chatav
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+6−7
@@ -72,8 +72,7 @@ instance HasColumn column columns ty
72 fromLabel = getColumn
72 fromLabel = getColumn
73
73
74(.&.)
74(.&.)
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)
77 => Alias table -> Alias column -> Expression params tables ty
76 => Alias table -> Alias column -> Expression params tables ty
78Alias table .&. Alias column = UnsafeExpression $
77Alias table .&. Alias column = UnsafeExpression $
79 fromString (symbolVal' table)
78 fromString (symbolVal' table)
@@ -174,13 +173,13 @@ crossJoin
174 -> TableRef params schema tables
173 -> TableRef params schema tables
175 -> TableRef params schema (table ': tables)
174 -> TableRef params schema (table ': tables)
176crossJoin table tables = UnsafeTableRef $
175crossJoin table tables = UnsafeTableRef $
177− "(" <> renderTableRef tables <> " CROSS JOIN " <> renderTableRef table <> ")"
176+ renderTableRef tables <> " CROSS JOIN " <> renderTableRef table
178
177
179innerJoin
178innerJoin
180− :: TableRef params tables '[left]
179+ :: TableRef params schema '[table]
181− -> Expression params '[left,right] 'PGBool
180+ -> Expression params (table ': tables) 'PGBool
182− -> TableRef params tables (right ': rest)
181+ -> TableRef params schema tables
183− -> TableRef params tables (left ': right ': rest)
182+ -> TableRef params schema (table ': tables)
184innerJoin table on tables = UnsafeTableRef $
183innerJoin table on tables = UnsafeTableRef $
185 renderTableRef tables
184 renderTableRef tables
186 <> " INNER JOIN " <>
185 <> " INNER JOIN " <>
squeel-postgresql/src/Squeel/PostgreSQL/Schema.hs+2−1
@@ -79,7 +79,8 @@ type (:::) (alias :: Symbol) (ty :: polykind) = '(alias,ty)
79
79
80data Alias (alias :: Symbol) = Alias (Proxy# alias)
80data Alias (alias :: Symbol) = Alias (Proxy# alias)
81
81
82−instance IsLabel alias (Alias alias) where fromLabel = Alias
82+instance alias1 ~ alias2 => IsLabel alias1 (Alias alias2) where
83+ fromLabel = Alias
83
84
84data Aliased expression aliased where
85data Aliased expression aliased where
85 As
86 As
squeel-postgresql/test/Squeel/PostgreSQL/QuerySpec.hs+44−33
@@ -64,23 +64,27 @@ spec = do
64 ("Robert'); DROP TABLE students;" `As` #name :& RNil)
64 ("Robert'); DROP TABLE students;" `As` #name :& RNil)
65 `shouldRenderAs`
65 `shouldRenderAs`
66 "INSERT INTO students (name) VALUES (E'Robert''); DROP TABLE students;');"
66 "INSERT INTO students (name) VALUES (E'Robert''); DROP TABLE students;');"
67− -- describe "JOINs" $ do
67+ describe "JOINs" $ do
68− -- it "should render CROSS JOINs" $
68+ it "should render CROSS JOINs" $ do
69− -- pending
70− -- it "should render INNER JOINs" $ do
71− -- select (vals `from` joinTable) `shouldRenderAs`
69+ select (vals `from` crossJoins) `shouldRenderAs`
72− -- "((orders\
70+ "SELECT\
73− -- \ INNER JOIN customers ON orders.customerID = customers.customerID)\
71+ \ orders.orderVal AS orderVal,\
74− -- \ INNER JOIN shippers ON orders.shipperID = shippers.shipperID)"
72+ \ customers.customerVal AS customerVal,\
75−-- #orders
73+ \ shippers.shipperVal AS shipperVal\
76−-- & innerJoin #customers
74+ \ FROM orders\
77−-- (#orders .&. #customerID ==* #customers .&. #customerID)
75+ \ CROSS JOIN customers\
78−-- & innerJoin #shippers
76+ \ CROSS JOIN shippers;"
79−-- (#orders .&. #shipperID ==* #shippers .&. #shipperID)
77+ it "should render INNER JOINs" $ do
80−-- `shouldRenderAs`
78+ select (vals `from` innerJoins) `shouldRenderAs`
79+ "SELECT\
80+ \ orders.orderVal AS orderVal,\
81+ \ customers.customerVal AS customerVal,\
82+ \ shippers.shipperVal AS shipperVal\
81−-- "((orders\
83+ \ FROM orders\
84+ \ INNER JOIN customers\
82−-- \ INNER JOIN customers ON orders.customerID = customers.customerID)\
85+ \ ON (orders.customerID = customers.customerID)\
86+ \ INNER JOIN shippers\
83−-- \ INNER JOIN shippers ON orders.shipperID = shippers.shipperID)"
87+ \ ON (orders.shipperID = shippers.shipperID);"
84
88
85type Columns = '[ "col1" ::: 'PGInt4, "col2" ::: 'PGInt4]
89type Columns = '[ "col1" ::: 'PGInt4, "col2" ::: 'PGInt4]
86type Tables = '[ "table1" ::: Columns ]
90type Tables = '[ "table1" ::: Columns ]
@@ -110,30 +114,37 @@ type StudentsColumns = '["name" ::: 'PGText]
110type StudentsTable = '["students" ::: StudentsColumns]
114type StudentsTable = '["students" ::: StudentsColumns]
111
115
112type OrderColumns =
116type OrderColumns =
117+ [ "orderID" ::: 'PGInt4
113− [ "orderID" ::: 'PGInt4, "orderVal" ::: 'PGText ]
118+ , "orderVal" ::: 'PGText
119+ , "customerID" ::: 'PGInt4
120+ , "shipperID" ::: 'PGInt4
121+ ]
114type CustomerColumns =
122type CustomerColumns =
115 [ "customerID" ::: 'PGInt4, "customerVal" ::: 'PGFloat4 ]
123 [ "customerID" ::: 'PGInt4, "customerVal" ::: 'PGFloat4 ]
116type ShipperColumns =
124type ShipperColumns =
117 [ "shipperID" ::: 'PGInt4, "shipperVal" ::: 'PGBool ]
125 [ "shipperID" ::: 'PGInt4, "shipperVal" ::: 'PGBool ]
118type JoinTables =
126type JoinTables =
127+ [ "shippers" ::: ShipperColumns
128+ , "customers" ::: CustomerColumns
119− [ "order" ::: OrderColumns
129+ , "orders" ::: OrderColumns
120− , "customer" ::: CustomerColumns
121− , "shipper" ::: ShipperColumns
122 ]
130 ]
123type ValueColumns =
131type ValueColumns =
124 [ "orderVal" ::: 'PGText
132 [ "orderVal" ::: 'PGText
125 , "customerVal" ::: 'PGFloat4
133 , "customerVal" ::: 'PGFloat4
126 , "shipperVal" ::: 'PGBool
134 , "shipperVal" ::: 'PGBool
127 ]
135 ]
128−-- vals :: Projection '[] JoinTables ValueColumns
136+vals :: Projection '[] JoinTables ValueColumns
129−-- vals = project $
137+vals = project $
130−-- #orders .&. #orderVal `As` #orderVal
138+ #orders .&. #orderVal `As` #orderVal
131−-- :& #customers .&. #customerVal `As` #customerVal
139+ :& #customers .&. #customerVal `As` #customerVal
132−-- :& #shippers .&. #shippersVal `As` #shippersVal :& RNil
140+ :& #shippers .&. #shipperVal `As` #shipperVal :& RNil
133−-- joinTable :: Tabulation '[] JoinTables JoinTables
141+crossJoins :: Tabulation '[] JoinTables JoinTables
142+crossJoins = tabulation $
143+ #orders & crossJoin #customers & crossJoin #shippers
144+innerJoins :: Tabulation '[] JoinTables JoinTables
134−-- joinTable = tabulation $
145+innerJoins = tabulation $
135−-- #orders
146+ #orders
136−-- & innerJoin #customers
147+ & innerJoin #customers
137−-- (#orders .&. #customerID ==* #customers .&. #customerID)
148+ (#orders .&. #customerID ==* #customers .&. #customerID)
138−-- & innerJoin #shippers
149+ & innerJoin #shippers
139−-- (#orders .&. #shipperID ==* #shippers .&. #shipperID)
150+ (#orders .&. #shipperID ==* #shippers .&. #shipperID)