on dev help sign in
add `tuples`
456d103Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+7−2
@@ -57,6 +57,7 @@ module Squeal.PostgreSQL.PQ
57 , getValue
57 , getValue
58 , getRow
58 , getRow
59 , getRows
59 , getRows
60+ , ntuples
60 , nextRow
61 , nextRow
61 , firstRow
62 , firstRow
62 , liftResult
63 , liftResult
@@ -505,7 +506,7 @@ getRow
505 -> Result columns
506 -> Result columns
506 -- ^ result
507 -- ^ result
507 -> io y
508 -> io y
508−getRow (RowNumber r) (Result result :: Result columns) = liftBase $do
509+getRow (RowNumber r) (Result result :: Result columns) = liftBase $ do
509 numRows <- LibPQ.ntuples result
510 numRows <- LibPQ.ntuples result
510 when (numRows < r) $ error $
511 when (numRows < r) $ error $
511 "getRow: expected at least " <> show r <> "rows but only saw "
512 "getRow: expected at least " <> show r <> "rows but only saw "
@@ -516,8 +517,12 @@ getRow (RowNumber r) (Result result :: Result columns) = liftBase $do
516 Nothing -> error "getRow: found unexpected length"
517 Nothing -> error "getRow: found unexpected length"
517 Just row -> return $ fromRow @columns row
518 Just row -> return $ fromRow @columns row
518
519
520+-- | Returns the number of rows (tuples) in the query result.
521+ntuples :: MonadBase IO io => Result columns -> io RowNumber
522+ntuples (Result result) = liftBase $ RowNumber <$> LibPQ.ntuples result
523+
519-- | Intended to be used for unfolding in streaming libraries, `nextRow`
524-- | Intended to be used for unfolding in streaming libraries, `nextRow`
520−-- takes a total number of rows (which can be found with `LibPQ.ntuples`)
525+-- takes a total number of rows (which can be found with `ntuples`)
521-- and a `Result` and given a row number if it's too large returns `Nothing`,
526-- and a `Result` and given a row number if it's too large returns `Nothing`,
522-- otherwise returning the row along with the next row number.
527-- otherwise returning the row along with the next row number.
523nextRow
528nextRow