on master help sign in
Expand on `select` needing a connection

Fixes #64 (hopefully!)

commit4e9e90d066c528d4ed740ee1b20d5d0c3ed521f0 authorOllie Charles <ollie@ocharles.org.uk> dateMon 21 Jun 2021, 11:38 (5 years ago) parent269974e
1 file changed+16−13
docs/tutorial.rst+16−13
docs/tutorial.rst+16−13
@@ -204,10 +204,13 @@ Recall we defined ``projectAuthorId`` as ``Column f AuthorId``. Now we have
204204We'll see more about ``Expr`` soon, but you can think of ``Expr a`` as "SQL
205205expressions of type ``a``\".
206206
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::
208210
209− >>> :t select c (each projectSchema)
210− select c (each projectSchema) :: MonadIO m => m [Project Result]
211+ >>> Right conn <- acquire "user=postgres"
212+ >>> :t select conn (each projectSchema)
213+ select conn (each projectSchema) :: MonadIO m => m [Project Result]
211214
212215When we ``select`` things containing ``Expr``\s, Rel8 builds a new response
213216table with the ``Result`` interpretation. This means you'll get back plain
@@ -222,7 +225,7 @@ wrappping type at all.
222225
223226Putting this all together, we can run our first query::
224227
225− >>> select c (each projectSchema) >>= mapM_ print
228+ >>> select conn (each projectSchema) >>= mapM_ print
226229 Project {projectAuthorId = 1, projectName = "rel8"}
227230 Project {projectAuthorId = 2, projectName = "aeson"}
228231 Project {projectAuthorId = 2, projectName = "text"}
@@ -232,7 +235,7 @@ sometimes we're only interested in a subset of the columns of a table. To
232235restrict the returned columns, we can specify a projection by using ``Query``\s
233236``Functor`` instance::
234237
235− >>> select c $ projectName <$> each projectSchema
238+ >>> select conn $ projectName <$> each projectSchema
236239 ["rel8","aeson","text"]
237240
238241Joins
@@ -248,7 +251,7 @@ We can do this by simply calling ``each`` twice, and then returning a tuple of
248251their results::
249252
250253 >>> :{
251− mapM_ print =<< select c do
254+ mapM_ print =<< select conn do
252255 author <- each authorSchema
253256 project <- each projectSchema
254257 return (projectName project, authorName author)
@@ -267,7 +270,7 @@ This isn't quite right, though, as we have ended up pairing up the wrong
267270projects and authors. To fix this, we can use ``where_`` to restrict the
268271returned rows. We could write::
269272
270− select c $ do
273+ select conn $ do
271274 author <- each authorSchema
272275 project <- each projectSchema
273276 where_ $ projectAuthorId project ==. authorId author
@@ -284,7 +287,7 @@ for the particular joins in your database. In our case, this would be::
284287Our final query is then::
285288
286289 >>> :{
287− mapM_ print =<< select c do
290+ mapM_ print =<< select conn do
288291 author <- each authorSchema
289292 project <- projectsForAuthor author
290293 return (projectName project, authorName author)
@@ -303,7 +306,7 @@ transformer to allow for the possibility of the join to fail.
303306In our test database, we can see that there's another author who we haven't
304307seen yet::
305308
306− >>> select c $ authorName <$> each authorSchema
309+ >>> select conn $ authorName <$> each authorSchema
307310 ["Ollie","Bryan O'Sullivan","Emily Pillmore"]
308311
309312Emily 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
311314original query by wrapping the ``projectsForAuthor`` call with ``optional``::
312315
313316 >>> :{
314− mapM_ print =<< select c do
317+ mapM_ print =<< select conn do
315318 author <- each authorSchema
316319 mproject <- optional $ projectsForAuthor author
317320 return (authorName author, projectName <$> mproject)
@@ -356,8 +359,8 @@ focus on the /type/ of that query::
356359 return (author, project)
357360 where
358361
359− >>> :t select c authorsAndProjects
360− select c authorsAndProjects
362+ >>> :t select conn authorsAndProjects
363+ select conn authorsAndProjects
361364 :: MonadIO m => m [(Author Result, Project Result)]
362365
363366
@@ -374,7 +377,7 @@ simply wrapping the call to ``projectsForAuthor`` with either ``some`` or
374377author to have no projects::
375378
376379 >>> :{
377− mapM_ print =<< select c do
380+ mapM_ print =<< select conn do
378381 author <- each authorSchema
379382 projectNames <- many $ projectName <$> projectsForAuthor author
380383 return (authorName author, projectNames)