{-# LANGUAGE OverlappingInstances #-}
{-# OPTIONS_GHC -fno-warn-deprecated-flags #-}
module Database.PostgreSQL.PQTypes.Class (
MonadDB(..)
) where
import Control.Applicative
import Control.Monad.Trans
import Control.Monad.Trans.Control
import Prelude
import Database.PostgreSQL.PQTypes.FromRow
import Database.PostgreSQL.PQTypes.Internal.Connection
import Database.PostgreSQL.PQTypes.Internal.Notification
import Database.PostgreSQL.PQTypes.Internal.QueryResult
import Database.PostgreSQL.PQTypes.SQL.Class
import Database.PostgreSQL.PQTypes.Transaction.Settings
class (Applicative m, Monad m) => MonadDB m where
runQuery :: IsSQL sql => sql -> m Int
getLastQuery :: m SomeSQL
getConnectionStats :: m ConnectionStats
getQueryResult :: FromRow row => m (Maybe (QueryResult row))
clearQueryResult :: m ()
getTransactionSettings :: m TransactionSettings
setTransactionSettings :: TransactionSettings -> m ()
getNotification :: Int -> m (Maybe Notification)
withNewConnection :: m a -> m a
instance (
Applicative (t m)
, Monad (t m)
, MonadTrans t
, MonadTransControl t
, MonadDB m
) => MonadDB (t m) where
runQuery = lift . runQuery
getLastQuery = lift getLastQuery
getConnectionStats = lift getConnectionStats
getQueryResult = lift getQueryResult
clearQueryResult = lift clearQueryResult
getTransactionSettings = lift getTransactionSettings
setTransactionSettings = lift . setTransactionSettings
getNotification = lift . getNotification
withNewConnection m = controlT $ \run -> withNewConnection (run m)
{-# INLINE runQuery #-}
{-# INLINE getLastQuery #-}
{-# INLINE getConnectionStats #-}
{-# INLINE getQueryResult #-}
{-# INLINE clearQueryResult #-}
{-# INLINE getTransactionSettings #-}
{-# INLINE setTransactionSettings #-}
{-# INLINE getNotification #-}
{-# INLINE withNewConnection #-}
controlT :: (MonadTransControl t, Monad (t m), Monad m)
=> (Run t -> m (StT t a)) -> t m a
controlT f = liftWith f >>= restoreT . return