| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Database.PostgreSQL.Tx.Internal
Contents
Synopsis
- data TxException = TxException {
- errcode :: Maybe String
- cause :: SomeException
- type family TxEnvs (xs :: [*]) r :: Constraint where ...
- class TxEnv a r where
- lookupTxEnv :: r -> a
- newtype TxM r a = UnsafeTxM {
- unsafeUnTxM :: ReaderT r IO a
- unsafeRunIOInTxM :: IO a -> TxM r a
- unsafeMkTxM :: (r -> IO a) -> TxM r a
- unsafeMksTxM :: TxEnv a r => (a -> IO b) -> TxM r b
- unsafeRunTxM :: r -> TxM r a -> IO a
- unsafeWithRunInIOTxM :: ((forall a. TxM r a -> IO a) -> IO b) -> TxM r b
- askTxEnv :: TxEnv a r => TxM r a
- unsafeLookupTxEnvIO :: TxEnv a r => r -> IO a
- throwExceptionTx :: Exception e => e -> TxM r a
- mapExceptionTx :: (Exception e, Exception e') => (e -> Maybe e') -> TxM r a -> TxM r a
- errcode'serialization_failure :: String
- errcode'deadlock_detected :: String
- hasErrcode :: (String -> Bool) -> TxException -> Bool
- shouldRetryTx :: TxException -> Bool
- unsafeMkTxException :: Exception e => (e -> Maybe String) -> e -> TxException
Disclaimer
Changes to this module will not be reflected in the library's version updates.
Internals
data TxException Source #
Unified exception type thrown from the database.
Each database backend may throw different types of exceptions.
As a user of postgresql-tx, ideally we should be able to
detect exceptions from the database without needing to catch
the database backend's exception type.
The errcode field allows us to introspect the postgresql
errcode; see https://www.postgresql.org/docs/current/errcodes-appendix.html
If you need to inspect the exact exception thrown by a database
backend, use the cause field.
Constructors
| TxException | |
Fields
| |
Instances
| Show TxException Source # | |
Defined in Database.PostgreSQL.Tx.Internal Methods showsPrec :: Int -> TxException -> ShowS # show :: TxException -> String # showList :: [TxException] -> ShowS # | |
| Exception TxException Source # | |
Defined in Database.PostgreSQL.Tx.Internal Methods toException :: TxException -> SomeException # fromException :: SomeException -> Maybe TxException # displayException :: TxException -> String # | |
type family TxEnvs (xs :: [*]) r :: Constraint where ... Source #
Type family which allows for specifying several TxEnv constraints as
a type-level list.
Since: 0.2.0.0
class TxEnv a r where Source #
A type class for specifying how to acquire an environment value
to be used for running an implementation of a database library.
For example, your database library will likely require some sort of
connection value to discharge its effects; in this case, you'd want to
define an instance of TxEnv MyDBEnv Connection and use TxM MyDBEnv
as your monad for executing transactions.
Note that implementations should take care and ensure that multiple
instances are compatible with one another. For example, let's say you
have instances for both TxEnv E PgSimple.Connection and
TxEnv E LibPQ.Connection; if both of these implementations are grabbing
connections from a pool, you will end up with each of those database
libraries using different connections, and thus, would be running in
separate transactions!
Since: 0.2.0.0
Methods
lookupTxEnv :: r -> a Source #
Acquire a value a via the reader environment r which assists in
running a TxM in a transaction.
Since: 0.2.0.0
The transaction monad. Unifies all database integrations, regardless of
library, into a single monad. The r type parameter represents the reader
environment needed for applicable database libraries. For example,
postgresql-simple needs a Connection to run its functions, so
its interface will require that we can obtain a Connection from the r
using the TxEnv type class.
Since: 0.2.0.0
Constructors
| UnsafeTxM | |
Instances
| Monad (TxM r) Source # | |
| Functor (TxM r) Source # | |
| MonadFail (TxM r) Source # | |
Defined in Database.PostgreSQL.Tx.Internal | |
| Applicative (TxM r) Source # | |
| (TypeError ('Text "MonadIO is banned in TxM; use 'unsafeRunIOInTxM' if you are sure this is safe IO") :: Constraint) => MonadIO (TxM r) Source # | The Since: 0.1.0.0 |
Defined in Database.PostgreSQL.Tx.Internal | |
| Semigroup a => Semigroup (TxM r a) Source # | |
| Monoid a => Monoid (TxM r a) Source # | |
unsafeRunIOInTxM :: IO a -> TxM r a Source #
unsafeMkTxM :: (r -> IO a) -> TxM r a Source #
unsafeMksTxM :: TxEnv a r => (a -> IO b) -> TxM r b Source #
Similar to unsafeMkTxM but allows for constructing a TxM with a
reader function using a specific value from the environment.
Use this function with care - arbitrary IO should only be run
within a transaction when truly necessary.
Since: 0.2.0.0
unsafeRunTxM :: r -> TxM r a -> IO a Source #
unsafeLookupTxEnvIO :: TxEnv a r => r -> IO a Source #
Analogous to lookupTxEnv but can be run in IO instead of TxM.
Since: 0.2.0.0
throwExceptionTx :: Exception e => e -> TxM r a Source #
Throw an exception.
Since: 0.2.0.0
mapExceptionTx :: (Exception e, Exception e') => (e -> Maybe e') -> TxM r a -> TxM r a Source #
Catch an exception and map it to another exception type before rethrowing.
Since: 0.2.0.0
errcode'serialization_failure :: String Source #
PostgreSQL errcode for serialization_failure.
errcode'deadlock_detected :: String Source #
PostgreSQL errcode for deadlock_detected.
hasErrcode :: (String -> Bool) -> TxException -> Bool Source #
Checks if the errcode of a TxException matches the supplied predicate.
If the errcode is Nothing, returns False.
shouldRetryTx :: TxException -> Bool Source #
Useful as a predicate to indicate when to retry transactions which are
run at isolation level serializable
unsafeMkTxException :: Exception e => (e -> Maybe String) -> e -> TxException Source #
Construct a TxException from an errcode accessing function
and the cause exception.
Note that this function should only be used by libraries
which are implementing a database backend for postgresql-tx.