on dev help sign in
add `tuples`
commit456d103887459972001b87a6c9cbc520d0385b69 authorEitan Chatav <eitan.chatav@gmail.com> dateTue 29 Aug 2017, 12:42 (9 years ago) parent586a9fe
1 file changed+7−2
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+7−2
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+7−2
@@ -57,6 +57,7 @@ module Squeal.PostgreSQL.PQ
5757 , getValue
5858 , getRow
5959 , getRows
60+ , ntuples
6061 , nextRow
6162 , firstRow
6263 , liftResult
@@ -505,7 +506,7 @@ getRow
505506 -> Result columns
506507 -- ^ result
507508 -> io y
508−getRow (RowNumber r) (Result result :: Result columns) = liftBase $do
509+getRow (RowNumber r) (Result result :: Result columns) = liftBase $ do
509510 numRows <- LibPQ.ntuples result
510511 when (numRows < r) $ error $
511512 "getRow: expected at least " <> show r <> "rows but only saw "
@@ -516,8 +517,12 @@ getRow (RowNumber r) (Result result :: Result columns) = liftBase $do
516517 Nothing -> error "getRow: found unexpected length"
517518 Just row -> return $ fromRow @columns row
518519
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+
519524-- | 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`)
521526-- and a `Result` and given a row number if it's too large returns `Nothing`,
522527-- otherwise returning the row along with the next row number.
523528nextRow