@@ -33,7 +33,8 @@ a `MonadPQ` constraint for running a `Manipulation` or `Query`.
3333
3434module Squeal.PostgreSQL.PQ
3535 ( -- * Connection
36− connectdb
36+ LibPQ.Connection
37+ , connectdb
3738 , finish
3839 , withConnection
3940 , lowerConnection
@@ -47,22 +48,17 @@ module Squeal.PostgreSQL.PQ
4748 , PQRun
4849 , pqliftWith
4950 -- * Result
50− , RowNumber (RowNumber, unRowNumber)
51− , ColumnNumber (UnsafeColumnNumber, getColumnNumber)
52− , HasColumnNumber (columnNumber)
53− , getValue
51+ , LibPQ.Result
52+ , LibPQ.Row
5453 , getRow
5554 , getRows
56− , ntuples
5755 , nextRow
5856 , firstRow
5957 , liftResult
58+ , ntuples
59+ , LibPQ.ExecStatus (..)
6060 , resultStatus
6161 , resultErrorMessage
62− -- * Re-export
63− , LibPQ.Connection
64− , LibPQ.Result
65− , LibPQ.ExecStatus (..)
6662 ) where
6763
6864import Control.Exception.Lifted
@@ -76,8 +72,6 @@ import Data.Kind
7672import Data.Monoid
7773import Data.Traversable
7874import Generics.SOP
79−import GHC.Exts hiding (fromList)
80−import GHC.TypeLits
8175
8276import qualified Database.PostgreSQL.LibPQ as LibPQ
8377
@@ -504,54 +498,15 @@ instance MonadBaseControl b m => MonadBaseControl b (PQ schema schema m) where
504498 pqliftWith $ \ run -> liftBaseWith $ \ runInBase -> f $ runInBase . run
505499 restoreM = PQ . const . restoreM
506500
507−-- | Just newtypes around a `CInt`
508−newtype RowNumber = RowNumber { unRowNumber :: LibPQ.Row }
509−
510−-- | In addition to being newtypes around a `CInt`, a `ColumnNumber` is
511−-- parameterized by a `Nat`ural number and acts as an index into a row.
512−newtype ColumnNumber (n :: Nat) (cs :: [k]) (c :: k) =
513− UnsafeColumnNumber { getColumnNumber :: LibPQ.Column }
514−
515−-- | >>> getColumnNumber (columnNumber @5 @'[_,_,_,_,_,_])
516−-- Col 5
517−class KnownNat n => HasColumnNumber n columns column
518− | n columns -> column where
519− columnNumber :: ColumnNumber n columns column
520− columnNumber =
521− UnsafeColumnNumber . fromIntegral $ natVal' (proxy# :: Proxy# n)
522−instance {-# OVERLAPPING #-} HasColumnNumber 0 (column1:columns) column1
523−instance {-# OVERLAPPABLE #-}
524− (KnownNat n, HasColumnNumber (n-1) columns column)
525− => HasColumnNumber n (column' : columns) column
526−
527−-- | Get a single value corresponding to a given row and column number
528−-- from a `Result`.
529−getValue
530− :: (FromColumnValue colty y, MonadBase IO io)
531− => RowNumber -- ^ row
532− -> ColumnNumber n columns colty -- ^ col
533− -> K LibPQ.Result columns -- ^ result
534− -> io y
535−getValue
536− (RowNumber r)
537− (UnsafeColumnNumber c :: ColumnNumber n columns colty)
538− (K result)
539− = fmap (fromColumnValue @colty . K) $ liftBase $ do
540− numRows <- LibPQ.ntuples result
541− when (numRows < r) $ error $
542− "getValue: expected at least " <> show r <> "rows but only saw "
543− <> show numRows
544− LibPQ.getvalue result r c
545−
546501-- | Get a row corresponding to a given row number from a `Result`.
547502getRow
548503 :: (FromRow columns y, MonadBase IO io)
549− => RowNumber
504+ => LibPQ.Row
550505 -- ^ row
551506 -> K LibPQ.Result columns
552507 -- ^ result
553508 -> io y
554−getRow (RowNumber r) (K result :: K LibPQ.Result columns) = liftBase $ do
509+getRow r (K result :: K LibPQ.Result columns) = liftBase $ do
555510 numRows <- LibPQ.ntuples result
556511 when (numRows < r) $ error $
557512 "getRow: expected at least " <> show r <> "rows but only saw "
@@ -562,27 +517,23 @@ getRow (RowNumber r) (K result :: K LibPQ.Result columns) = liftBase $ do
562517 Nothing -> error "getRow: found unexpected length"
563518 Just row -> return $ fromRow @columns row
564519
565−-- | Returns the number of rows (tuples) in the query result.
566−ntuples :: MonadBase IO io => K LibPQ.Result columns -> io RowNumber
567−ntuples (K result) = liftBase $ RowNumber <$> LibPQ.ntuples result
568−
569520-- | Intended to be used for unfolding in streaming libraries, `nextRow`
570521-- takes a total number of rows (which can be found with `ntuples`)
571522-- and a `Result` and given a row number if it's too large returns `Nothing`,
572523-- otherwise returning the row along with the next row number.
573524nextRow
574525 :: (FromRow columns y, MonadBase IO io)
575− => RowNumber -- ^ total number of rows
526+ => LibPQ.Row -- ^ total number of rows
576527 -> K LibPQ.Result columns -- ^ result
577− -> RowNumber -- ^ row number
578− -> io (Maybe (RowNumber,y))
579−nextRow (RowNumber total) (K result :: K LibPQ.Result columns) (RowNumber r)
528+ -> LibPQ.Row -- ^ row number
529+ -> io (Maybe (LibPQ.Row,y))
530+nextRow total (K result :: K LibPQ.Result columns) r
580531 = liftBase $ if r >= total then return Nothing else do
581532 let len = fromIntegral (lengthSList (Proxy @columns))
582533 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
583534 case fromList row' of
584535 Nothing -> error "nextRow: found unexpected length"
585− Just row -> return $ Just (RowNumber (r+1), fromRow @columns row)
536+ Just row -> return $ Just (r+1, fromRow @columns row)
586537
587538-- | Get all rows from a `Result`.
588539getRows
@@ -619,6 +570,10 @@ liftResult
619570 -> K LibPQ.Result results -> io x
620571liftResult f (K result) = liftBase $ f result
621572
573+-- | Returns the number of rows (tuples) in the query result.
574+ntuples :: MonadBase IO io => K LibPQ.Result columns -> io LibPQ.Row
575+ntuples = liftResult LibPQ.ntuples
576+
622577-- | Returns the result status of the command.
623578resultStatus :: MonadBase IO io => K LibPQ.Result results -> io LibPQ.ExecStatus
624579resultStatus = liftResult LibPQ.resultStatus