@@ -204,10 +204,13 @@ Recall we defined ``projectAuthorId`` as ``Column f AuthorId``. Now we have
204We'll see more about ``Expr`` soon, but you can think of ``Expr a`` as "SQL
204We'll see more about ``Expr`` soon, but you can think of ``Expr a`` as "SQL
205expressions of type ``a``\".
205expressions of type ``a``\".
207−To execute this ``Query``, we pass it to ``select``::
207+To execute this ``Query``, we pass it to ``select``. This function takes both a
208+database connection (which can be obtained using hasql's ``acquire`` function),
209+and the ``Query`` to run::
211+ >>> Right conn <- acquire "user=postgres"
209− >>> :t select c (each projectSchema)
212+ >>> :t select conn (each projectSchema)
210− select c (each projectSchema) :: MonadIO m => m [Project Result]
213+ select conn (each projectSchema) :: MonadIO m => m [Project Result]
212When we ``select`` things containing ``Expr``\s, Rel8 builds a new response
215When we ``select`` things containing ``Expr``\s, Rel8 builds a new response
213table with the ``Result`` interpretation. This means you'll get back plain
216table with the ``Result`` interpretation. This means you'll get back plain
@@ -222,7 +225,7 @@ wrappping type at all.
223Putting this all together, we can run our first query::
226Putting this all together, we can run our first query::
225− >>> select c (each projectSchema) >>= mapM_ print
228+ >>> select conn (each projectSchema) >>= mapM_ print
226 Project {projectAuthorId = 1, projectName = "rel8"}
229 Project {projectAuthorId = 1, projectName = "rel8"}
227 Project {projectAuthorId = 2, projectName = "aeson"}
230 Project {projectAuthorId = 2, projectName = "aeson"}
228 Project {projectAuthorId = 2, projectName = "text"}
231 Project {projectAuthorId = 2, projectName = "text"}
@@ -232,7 +235,7 @@ sometimes we're only interested in a subset of the columns of a table. To
232restrict the returned columns, we can specify a projection by using ``Query``\s
235restrict the returned columns, we can specify a projection by using ``Query``\s
233``Functor`` instance::
236``Functor`` instance::
235− >>> select c $ projectName <$> each projectSchema
238+ >>> select conn $ projectName <$> each projectSchema
236 ["rel8","aeson","text"]
239 ["rel8","aeson","text"]
@@ -248,7 +251,7 @@ We can do this by simply calling ``each`` twice, and then returning a tuple of
248their results::
251their results::
251− mapM_ print =<< select c do
254+ mapM_ print =<< select conn do
252 author <- each authorSchema
255 author <- each authorSchema
253 project <- each projectSchema
256 project <- each projectSchema
254 return (projectName project, authorName author)
257 return (projectName project, authorName author)
@@ -267,7 +270,7 @@ This isn't quite right, though, as we have ended up pairing up the wrong
267projects and authors. To fix this, we can use ``where_`` to restrict the
270projects and authors. To fix this, we can use ``where_`` to restrict the
268returned rows. We could write::
271returned rows. We could write::
270− select c $ do
273+ select conn $ do
271 author <- each authorSchema
274 author <- each authorSchema
272 project <- each projectSchema
275 project <- each projectSchema
273 where_ $ projectAuthorId project ==. authorId author
276 where_ $ projectAuthorId project ==. authorId author
@@ -284,7 +287,7 @@ for the particular joins in your database. In our case, this would be::
284Our final query is then::
287Our final query is then::
287− mapM_ print =<< select c do
290+ mapM_ print =<< select conn do
288 author <- each authorSchema
291 author <- each authorSchema
289 project <- projectsForAuthor author
292 project <- projectsForAuthor author
290 return (projectName project, authorName author)
293 return (projectName project, authorName author)
@@ -303,7 +306,7 @@ transformer to allow for the possibility of the join to fail.
303In our test database, we can see that there's another author who we haven't
306In our test database, we can see that there's another author who we haven't
304seen yet::
307seen yet::
306− >>> select c $ authorName <$> each authorSchema
309+ >>> select conn $ authorName <$> each authorSchema
307 ["Ollie","Bryan O'Sullivan","Emily Pillmore"]
310 ["Ollie","Bryan O'Sullivan","Emily Pillmore"]
309Emily wasn't returned in our earlier query because - in our database - she
312Emily wasn't returned in our earlier query because - in our database - she
@@ -311,7 +314,7 @@ doesn't have any registered projects. We can account for this partiality in our
311original query by wrapping the ``projectsForAuthor`` call with ``optional``::
314original query by wrapping the ``projectsForAuthor`` call with ``optional``::
314− mapM_ print =<< select c do
317+ mapM_ print =<< select conn do
315 author <- each authorSchema
318 author <- each authorSchema
316 mproject <- optional $ projectsForAuthor author
319 mproject <- optional $ projectsForAuthor author
317 return (authorName author, projectName <$> mproject)
320 return (authorName author, projectName <$> mproject)
@@ -356,8 +359,8 @@ focus on the /type/ of that query::
356 return (author, project)
359 return (author, project)
359− >>> :t select c authorsAndProjects
362+ >>> :t select conn authorsAndProjects
360− select c authorsAndProjects
363+ select conn authorsAndProjects
361 :: MonadIO m => m [(Author Result, Project Result)]
364 :: MonadIO m => m [(Author Result, Project Result)]
@@ -374,7 +377,7 @@ simply wrapping the call to ``projectsForAuthor`` with either ``some`` or
374author to have no projects::
377author to have no projects::
377− mapM_ print =<< select c do
380+ mapM_ print =<< select conn do
378 author <- each authorSchema
381 author <- each authorSchema
379 projectNames <- many $ projectName <$> projectsForAuthor author
382 projectNames <- many $ projectName <$> projectsForAuthor author
380 return (authorName author, projectNames)
383 return (authorName author, projectNames)