on master help sign in
Expand on `select` needing a connection
4e9e90dOllie Charles
docs/tutorial.rst+16−13
@@ -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``\".
206
206
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::
208
210
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]
211
214
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.
222
225
223Putting this all together, we can run our first query::
226Putting this all together, we can run our first query::
224
227
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::
234
237
235− >>> select c $ projectName <$> each projectSchema
238+ >>> select conn $ projectName <$> each projectSchema
236 ["rel8","aeson","text"]
239 ["rel8","aeson","text"]
237
240
238Joins
241Joins
@@ -248,7 +251,7 @@ We can do this by simply calling ``each`` twice, and then returning a tuple of
248their results::
251their results::
249
252
250 >>> :{
253 >>> :{
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::
269
272
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::
285
288
286 >>> :{
289 >>> :{
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::
305
308
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"]
308
311
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``::
312
315
313 >>> :{
316 >>> :{
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)
357 where
360 where
358
361
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)]
362
365
363
366
@@ -374,7 +377,7 @@ simply wrapping the call to ``projectsForAuthor`` with either ``some`` or
374author to have no projects::
377author to have no projects::
375
378
376 >>> :{
379 >>> :{
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)