@@ -64,6 +64,8 @@ module Squeal.PostgreSQL.PQ
64 , resultErrorCode
64 , resultErrorCode
65 -- * Exceptions
65 -- * Exceptions
66 , SquealException (..)
66 , SquealException (..)
67 , catchSqueal
69 , catchSqueal
68 , handleSqueal
70 , handleSqueal
@@ -398,7 +400,7 @@ instance (MonadIO io, schemas0 ~ schemas, schemas1 ~ schemas)
398 Nothing -> throw $ ResultException
400 Nothing -> throw $ ResultException
399 "manipulateParams: LibPQ.execParams returned no results"
401 "manipulateParams: LibPQ.execParams returned no results"
400 Just result -> do
402 Just result -> do
401− tryResult result
403+ okResult_ result
402 return $ K (K result)
404 return $ K (K result)
404 traversePrepared
406 traversePrepared
@@ -409,7 +411,7 @@ instance (MonadIO io, schemas0 ~ schemas, schemas1 ~ schemas)
409 case prepResultMaybe of
411 case prepResultMaybe of
410 Nothing -> throw $ ResultException
412 Nothing -> throw $ ResultException
411 "traversePrepared: LibPQ.prepare returned no results"
413 "traversePrepared: LibPQ.prepare returned no results"
412− Just prepResult -> tryResult prepResult
414+ Just prepResult -> okResult_ prepResult
413 results <- for list $ \ params -> do
415 results <- for list $ \ params -> do
415 toParam' encoding = (encodingBytes encoding,LibPQ.Binary)
417 toParam' encoding = (encodingBytes encoding,LibPQ.Binary)
@@ -419,13 +421,13 @@ instance (MonadIO io, schemas0 ~ schemas, schemas1 ~ schemas)
419 Nothing -> throw $ ResultException
421 Nothing -> throw $ ResultException
420 "traversePrepared: LibPQ.execParams returned no results"
422 "traversePrepared: LibPQ.execParams returned no results"
421 Just result -> do
423 Just result -> do
422− tryResult result
424+ okResult_ result
423 return $ K result
425 return $ K result
424 deallocResultMaybe <- LibPQ.exec conn ("DEALLOCATE " <> temp <> ";")
426 deallocResultMaybe <- LibPQ.exec conn ("DEALLOCATE " <> temp <> ";")
425 case deallocResultMaybe of
427 case deallocResultMaybe of
426 Nothing -> throw $ ResultException
428 Nothing -> throw $ ResultException
427 "traversePrepared: LibPQ.exec DEALLOCATE returned no results"
429 "traversePrepared: LibPQ.exec DEALLOCATE returned no results"
428− Just deallocResult -> tryResult deallocResult
430+ Just deallocResult -> okResult_ deallocResult
429 return (K results)
431 return (K results)
431 traversePrepared_
433 traversePrepared_
@@ -436,7 +438,7 @@ instance (MonadIO io, schemas0 ~ schemas, schemas1 ~ schemas)
436 case prepResultMaybe of
438 case prepResultMaybe of
437 Nothing -> throw $ ResultException
439 Nothing -> throw $ ResultException
438 "traversePrepared_: LibPQ.prepare returned no results"
440 "traversePrepared_: LibPQ.prepare returned no results"
439− Just prepResult -> tryResult prepResult
441+ Just prepResult -> okResult_ prepResult
440 for_ list $ \ params -> do
442 for_ list $ \ params -> do
442 toParam' encoding = (encodingBytes encoding, LibPQ.Binary)
444 toParam' encoding = (encodingBytes encoding, LibPQ.Binary)
@@ -445,12 +447,12 @@ instance (MonadIO io, schemas0 ~ schemas, schemas1 ~ schemas)
445 case resultMaybe of
447 case resultMaybe of
446 Nothing -> throw $ ResultException
448 Nothing -> throw $ ResultException
447 "traversePrepared_: LibPQ.execParams returned no results"
449 "traversePrepared_: LibPQ.execParams returned no results"
448− Just result -> tryResult result
450+ Just result -> okResult_ result
449 deallocResultMaybe <- LibPQ.exec conn ("DEALLOCATE " <> temp <> ";")
451 deallocResultMaybe <- LibPQ.exec conn ("DEALLOCATE " <> temp <> ";")
450 case deallocResultMaybe of
452 case deallocResultMaybe of
451 Nothing -> throw $ ResultException
453 Nothing -> throw $ ResultException
452 "traversePrepared: LibPQ.exec DEALLOCATE returned no results"
454 "traversePrepared: LibPQ.exec DEALLOCATE returned no results"
453− Just deallocResult -> tryResult deallocResult
455+ Just deallocResult -> okResult_ deallocResult
454 return (K ())
456 return (K ())
456 liftPQ pq = PQ $ \ (K conn) -> do
458 liftPQ pq = PQ $ \ (K conn) -> do
@@ -613,24 +615,24 @@ resultErrorCode
613 -> io (Maybe ByteString)
615 -> io (Maybe ByteString)
614resultErrorCode = liftResult (flip LibPQ.resultErrorField LibPQ.DiagSqlstate)
616resultErrorCode = liftResult (flip LibPQ.resultErrorField LibPQ.DiagSqlstate)
616−-- | `Exception`s that can be thrown by Squeal.
618+-- | the state of LibPQ
617−data SquealException
619+data PQState = PQState
619 { sqlExecStatus :: LibPQ.ExecStatus
620 { sqlExecStatus :: LibPQ.ExecStatus
620 , sqlStateCode :: Maybe ByteString
621 , sqlStateCode :: Maybe ByteString
621 -- ^ https://www.postgresql.org/docs/current/static/errcodes-appendix.html
622 -- ^ https://www.postgresql.org/docs/current/static/errcodes-appendix.html
622 , sqlErrorMessage :: Maybe ByteString
623 , sqlErrorMessage :: Maybe ByteString
623− }
624+ } deriving (Eq, Show)
626+-- | `Exception`s that can be thrown by Squeal.
628+ = PQException PQState
624 | ResultException Text
629 | ResultException Text
625 | ParseException Text
630 | ParseException Text
626 deriving (Eq, Show)
631 deriving (Eq, Show)
627instance Exception SquealException
632instance Exception SquealException
631− => LibPQ.Result
634+okResult_ :: MonadIO io => LibPQ.Result -> io ()
633−tryResult result = liftIO $ do
635+okResult_ result = liftIO $ do
634 status <- LibPQ.resultStatus result
636 status <- LibPQ.resultStatus result
635 case status of
637 case status of
636 LibPQ.CommandOk -> return ()
638 LibPQ.CommandOk -> return ()
@@ -638,7 +640,12 @@ tryResult result = liftIO $ do
639 stateCode <- LibPQ.resultErrorField result LibPQ.DiagSqlstate
641 stateCode <- LibPQ.resultErrorField result LibPQ.DiagSqlstate
640 msg <- LibPQ.resultErrorMessage result
642 msg <- LibPQ.resultErrorMessage result
641− throw $ PQException status stateCode msg
643+ throw . PQException $ PQState status stateCode msg
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
643-- | Catch `SquealException`s.
650-- | Catch `SquealException`s.
644catchSqueal
651catchSqueal