{-# LANGUAGE RankNTypes #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Database.PostgreSQL.Tx.Query
  ( PgQueryEnv
  , PgQueryM
  , Logger
  , module Database.PostgreSQL.Tx.Query
  , module Database.PostgreSQL.Tx.Query.Internal.Reexport
  ) where

import Control.Exception (Exception)
import Data.Int (Int64)
import Database.PostgreSQL.Tx (TxM)
import Database.PostgreSQL.Tx.Query.Internal
import Database.PostgreSQL.Tx.Query.Internal.Reexport
import GHC.Stack (HasCallStack)
import qualified Database.PostgreSQL.Query as Query
import qualified Database.PostgreSQL.Simple.Transaction as Simple
import qualified Database.PostgreSQL.Tx.Simple as Tx.Simple

-- | Analogue of 'Query.pgWithTransaction'.
--
-- @since 0.1.0.0
pgWithTransaction
  :: (PgQueryEnv r, HasCallStack)
  => r -> (HasCallStack => TxM r a) -> IO a
pgWithTransaction :: r -> (HasCallStack => TxM r a) -> IO a
pgWithTransaction = (HasCallStack => UnsafePgQueryIO r a -> UnsafePgQueryIO r a)
-> r -> TxM r a -> IO a
forall r a.
(PgQueryEnv r, HasCallStack) =>
(HasCallStack => UnsafePgQueryIO r a -> UnsafePgQueryIO r a)
-> r -> TxM r a -> IO a
unsafeRunPgQueryTransaction HasCallStack => UnsafePgQueryIO r a -> UnsafePgQueryIO r a
forall (m :: * -> *) a.
(HasPostgres m, MonadBaseControl IO m, TransactionSafe m) =>
m a -> m a
Query.pgWithTransaction

-- | Analogue of 'Query.pgWithTransactionMode'.
--
-- @since 0.1.0.0
pgWithTransactionMode
  :: (PgQueryEnv r, HasCallStack)
  => Simple.TransactionMode -> r -> (HasCallStack => TxM r a) -> IO a
pgWithTransactionMode :: TransactionMode -> r -> (HasCallStack => TxM r a) -> IO a
pgWithTransactionMode m :: TransactionMode
m = (HasCallStack => UnsafePgQueryIO r a -> UnsafePgQueryIO r a)
-> r -> TxM r a -> IO a
forall r a.
(PgQueryEnv r, HasCallStack) =>
(HasCallStack => UnsafePgQueryIO r a -> UnsafePgQueryIO r a)
-> r -> TxM r a -> IO a
unsafeRunPgQueryTransaction (TransactionMode -> UnsafePgQueryIO r a -> UnsafePgQueryIO r a
forall (m :: * -> *) a.
(HasPostgres m, MonadBaseControl IO m, TransactionSafe m) =>
TransactionMode -> m a -> m a
Query.pgWithTransactionMode TransactionMode
m)

-- | Analogue of 'Query.pgWithTransactionSerializable'
-- Unlike @postgresql-query@, uses 'shouldRetryTx' to also retry
-- on @deadlock_detected@, not just @serialization_failure@.
--
-- Note that any 'IO' that occurs inside the 'TxM' may be executed multiple times.
--
-- @since 0.2.0.0
pgWithTransactionSerializable
  :: (PgQueryEnv r, HasCallStack)
  => r -> (HasCallStack => TxM r a) -> IO a
pgWithTransactionSerializable :: r -> (HasCallStack => TxM r a) -> IO a
pgWithTransactionSerializable = r -> (HasCallStack => TxM r a) -> IO a
forall r a.
(PgSimpleEnv r, HasCallStack) =>
r -> (HasCallStack => TxM r a) -> IO a
Tx.Simple.withTransactionSerializable

-- | Analogue of 'Query.pgWithTransactionModeRetry'.
-- You should generally prefer 'pgWithTransactionSerializable'.
--
-- Note that any 'IO' that occurs inside the 'TxM' may be executed multiple times.
--
-- @since 0.2.0.0
pgWithTransactionModeRetry
  :: (PgQueryEnv r, Exception e, HasCallStack)
  => Simple.TransactionMode -> (e -> Bool) -> r -> (HasCallStack => TxM r a) -> IO a
pgWithTransactionModeRetry :: TransactionMode
-> (e -> Bool) -> r -> (HasCallStack => TxM r a) -> IO a
pgWithTransactionModeRetry = TransactionMode
-> (e -> Bool) -> r -> (HasCallStack => TxM r a) -> IO a
forall e r a.
(Exception e, PgSimpleEnv r, HasCallStack) =>
TransactionMode
-> (e -> Bool) -> r -> (HasCallStack => TxM r a) -> IO a
Tx.Simple.withTransactionModeRetry

-- | Analogue of 'Query.pgWithSavepoint'.
--
-- @since 0.2.0.0
pgWithSavepoint :: (HasCallStack => PgQueryM a) -> PgQueryM a
pgWithSavepoint :: (HasCallStack => PgQueryM a) -> PgQueryM a
pgWithSavepoint x :: HasCallStack => PgQueryM a
x =
  UnsafePgQueryIO r a -> TxM r a
forall r a. HasCallStack => UnsafePgQueryIO r a -> TxM r a
unsafeFromPgQueryIO
    (UnsafePgQueryIO r a -> TxM r a) -> UnsafePgQueryIO r a -> TxM r a
forall a b. (a -> b) -> a -> b
$ UnsafePgQueryIO r a -> UnsafePgQueryIO r a
forall (m :: * -> *) a.
(HasPostgres m, MonadBaseControl IO m, TransactionSafe m) =>
m a -> m a
Query.pgWithSavepoint
    (UnsafePgQueryIO r a -> UnsafePgQueryIO r a)
-> UnsafePgQueryIO r a -> UnsafePgQueryIO r a
forall a b. (a -> b) -> a -> b
$ TxM r a -> UnsafePgQueryIO r a
forall r a. HasCallStack => TxM r a -> UnsafePgQueryIO r a
unsafeToPgQueryIO TxM r a
HasCallStack => PgQueryM a
x

-- | Analogue of 'Query.pgQuery'.
--
-- @since 0.1.0.0
pgQuery :: (ToSqlBuilder q, FromRow r, HasCallStack) => q -> PgQueryM [r]
pgQuery :: q -> PgQueryM [r]
pgQuery = UnsafePgQueryIO r [r] -> TxM r [r]
forall r a. HasCallStack => UnsafePgQueryIO r a -> TxM r a
unsafeFromPgQueryIO (UnsafePgQueryIO r [r] -> TxM r [r])
-> (q -> UnsafePgQueryIO r [r]) -> q -> TxM r [r]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. q -> UnsafePgQueryIO r [r]
forall (m :: * -> *) q r.
(HasPostgres m, MonadLogger m, ToSqlBuilder q, FromRow r) =>
q -> m [r]
Query.pgQuery

-- | Analogue of 'Query.pgQueryWithMasker'.
--
-- @since 0.2.0.0
pgQueryWithMasker :: (ToSqlBuilder q, FromRow r, HasCallStack) => LogMasker -> q -> PgQueryM [r]
pgQueryWithMasker :: LogMasker -> q -> PgQueryM [r]
pgQueryWithMasker masker :: LogMasker
masker = UnsafePgQueryIO r [r] -> TxM r [r]
forall r a. HasCallStack => UnsafePgQueryIO r a -> TxM r a
unsafeFromPgQueryIO (UnsafePgQueryIO r [r] -> TxM r [r])
-> (q -> UnsafePgQueryIO r [r]) -> q -> TxM r [r]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogMasker -> q -> UnsafePgQueryIO r [r]
forall (m :: * -> *) q r.
(HasPostgres m, MonadLogger m, ToSqlBuilder q, FromRow r) =>
LogMasker -> q -> m [r]
Query.pgQueryWithMasker LogMasker
masker

-- | Analogue of 'Query.pgExecute'.
--
-- @since 0.1.0.0
pgExecute :: (ToSqlBuilder q, HasCallStack) => q -> PgQueryM Int64
pgExecute :: q -> PgQueryM Int64
pgExecute = UnsafePgQueryIO r Int64 -> TxM r Int64
forall r a. HasCallStack => UnsafePgQueryIO r a -> TxM r a
unsafeFromPgQueryIO (UnsafePgQueryIO r Int64 -> TxM r Int64)
-> (q -> UnsafePgQueryIO r Int64) -> q -> TxM r Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. q -> UnsafePgQueryIO r Int64
forall (m :: * -> *) q.
(HasPostgres m, MonadLogger m, ToSqlBuilder q) =>
q -> m Int64
Query.pgExecute

-- | Analogue of 'Query.pgExecuteWithMasker'.
--
-- @since 0.2.0.0
pgExecuteWithMasker :: (ToSqlBuilder q, HasCallStack) => LogMasker -> q -> PgQueryM Int64
pgExecuteWithMasker :: LogMasker -> q -> PgQueryM Int64
pgExecuteWithMasker masker :: LogMasker
masker = UnsafePgQueryIO r Int64 -> TxM r Int64
forall r a. HasCallStack => UnsafePgQueryIO r a -> TxM r a
unsafeFromPgQueryIO (UnsafePgQueryIO r Int64 -> TxM r Int64)
-> (q -> UnsafePgQueryIO r Int64) -> q -> TxM r Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogMasker -> q -> UnsafePgQueryIO r Int64
forall (m :: * -> *) q.
(HasPostgres m, MonadLogger m, ToSqlBuilder q) =>
LogMasker -> q -> m Int64
Query.pgExecuteWithMasker LogMasker
masker