@@ -64,6 +64,8 @@ module Squeal.PostgreSQL.PQ
6464 , resultErrorCode
6565 -- * Exceptions
6666 , SquealException (..)
67+ , PQState (..)
68+ , okResult
6769 , catchSqueal
6870 , handleSqueal
6971 ) where
@@ -398,7 +400,7 @@ instance (MonadIO io, schemas0 ~ schemas, schemas1 ~ schemas)
398400 Nothing -> throw $ ResultException
399401 "manipulateParams: LibPQ.execParams returned no results"
400402 Just result -> do
401− tryResult result
403+ okResult_ result
402404 return $ K (K result)
403405
404406 traversePrepared
@@ -409,7 +411,7 @@ instance (MonadIO io, schemas0 ~ schemas, schemas1 ~ schemas)
409411 case prepResultMaybe of
410412 Nothing -> throw $ ResultException
411413 "traversePrepared: LibPQ.prepare returned no results"
412− Just prepResult -> tryResult prepResult
414+ Just prepResult -> okResult_ prepResult
413415 results <- for list $ \ params -> do
414416 let
415417 toParam' encoding = (encodingBytes encoding,LibPQ.Binary)
@@ -419,13 +421,13 @@ instance (MonadIO io, schemas0 ~ schemas, schemas1 ~ schemas)
419421 Nothing -> throw $ ResultException
420422 "traversePrepared: LibPQ.execParams returned no results"
421423 Just result -> do
422− tryResult result
424+ okResult_ result
423425 return $ K result
424426 deallocResultMaybe <- LibPQ.exec conn ("DEALLOCATE " <> temp <> ";")
425427 case deallocResultMaybe of
426428 Nothing -> throw $ ResultException
427429 "traversePrepared: LibPQ.exec DEALLOCATE returned no results"
428− Just deallocResult -> tryResult deallocResult
430+ Just deallocResult -> okResult_ deallocResult
429431 return (K results)
430432
431433 traversePrepared_
@@ -436,7 +438,7 @@ instance (MonadIO io, schemas0 ~ schemas, schemas1 ~ schemas)
436438 case prepResultMaybe of
437439 Nothing -> throw $ ResultException
438440 "traversePrepared_: LibPQ.prepare returned no results"
439− Just prepResult -> tryResult prepResult
441+ Just prepResult -> okResult_ prepResult
440442 for_ list $ \ params -> do
441443 let
442444 toParam' encoding = (encodingBytes encoding, LibPQ.Binary)
@@ -445,12 +447,12 @@ instance (MonadIO io, schemas0 ~ schemas, schemas1 ~ schemas)
445447 case resultMaybe of
446448 Nothing -> throw $ ResultException
447449 "traversePrepared_: LibPQ.execParams returned no results"
448− Just result -> tryResult result
450+ Just result -> okResult_ result
449451 deallocResultMaybe <- LibPQ.exec conn ("DEALLOCATE " <> temp <> ";")
450452 case deallocResultMaybe of
451453 Nothing -> throw $ ResultException
452454 "traversePrepared: LibPQ.exec DEALLOCATE returned no results"
453− Just deallocResult -> tryResult deallocResult
455+ Just deallocResult -> okResult_ deallocResult
454456 return (K ())
455457
456458 liftPQ pq = PQ $ \ (K conn) -> do
@@ -613,24 +615,24 @@ resultErrorCode
613615 -> io (Maybe ByteString)
614616resultErrorCode = liftResult (flip LibPQ.resultErrorField LibPQ.DiagSqlstate)
615617
616−-- | `Exception`s that can be thrown by Squeal.
617−data SquealException
618− = PQException
618+-- | the state of LibPQ
619+data PQState = PQState
619620 { sqlExecStatus :: LibPQ.ExecStatus
620621 , sqlStateCode :: Maybe ByteString
621622 -- ^ https://www.postgresql.org/docs/current/static/errcodes-appendix.html
622623 , sqlErrorMessage :: Maybe ByteString
623− }
624+ } deriving (Eq, Show)
625+
626+-- | `Exception`s that can be thrown by Squeal.
627+data SquealException
628+ = PQException PQState
624629 | ResultException Text
625630 | ParseException Text
626631 deriving (Eq, Show)
627632instance Exception SquealException
628633
629−tryResult
630− :: MonadIO io
631− => LibPQ.Result
632− -> io ()
633−tryResult result = liftIO $ do
634+okResult_ :: MonadIO io => LibPQ.Result -> io ()
635+okResult_ result = liftIO $ do
634636 status <- LibPQ.resultStatus result
635637 case status of
636638 LibPQ.CommandOk -> return ()
@@ -638,7 +640,12 @@ tryResult result = liftIO $ do
638640 _ -> do
639641 stateCode <- LibPQ.resultErrorField result LibPQ.DiagSqlstate
640642 msg <- LibPQ.resultErrorMessage result
641− throw $ PQException status stateCode msg
643+ throw . PQException $ PQState status stateCode msg
644+
645+-- | Check if a `Result`'s status is either `LibPQ.CommandOk`
646+-- or `LibPQ.TuplesOk` otherwise `throw` a `PQException`.
647+okResult :: MonadIO io => K LibPQ.Result row -> io ()
648+okResult = okResult_ . unK
642649
643650-- | Catch `SquealException`s.
644651catchSqueal