module Opaleye.Trans
( OpaleyeT (..)
, runOpaleyeT
,
Transaction
, transaction
, run
,
query
, queryFirst
,
insert
, insertMany
, insertReturning
, insertReturningFirst
, insertManyReturning
,
update
, updateReturning
, updateReturningFirst
,
delete
,
withConn
,
liftIO
, MonadIO
, ask
, Int64
) where
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Reader (MonadReader, ReaderT (..),
ask)
import Control.Monad.Trans (MonadTrans (..))
import Control.Monad.Catch (MonadCatch, MonadThrow)
import Data.Maybe (listToMaybe)
import Data.Profunctor.Product.Default (Default)
import Database.PostgreSQL.Simple (Connection, withTransaction)
import qualified Database.PostgreSQL.Simple as PSQL
import GHC.Int (Int64)
import Opaleye
newtype OpaleyeT m a = OpaleyeT { unOpaleyeT :: ReaderT Connection m a }
deriving ( Functor, Applicative, Monad, MonadTrans, MonadIO
, MonadReader Connection, MonadCatch, MonadThrow
)
runOpaleyeT :: PSQL.Connection -> OpaleyeT m a -> m a
runOpaleyeT c = flip runReaderT c . unOpaleyeT
withConn :: MonadIO m => (Connection -> IO a) -> OpaleyeT m a
withConn f = ask >>= liftIO . f
newtype Transaction a = Transaction { unTransaction :: ReaderT Connection IO a }
deriving (Functor, Applicative, Monad, MonadReader Connection)
transaction :: MonadIO m => Transaction a -> OpaleyeT m a
transaction (Transaction t) = withConn $ \conn ->
withTransaction conn (runReaderT t conn)
run :: MonadIO m => Transaction a -> OpaleyeT m a
run = withConn . runReaderT . unTransaction
withConnIO :: (Connection -> IO a) -> Transaction a
withConnIO = Transaction . ReaderT
query :: Default QueryRunner a b => Query a -> Transaction [b]
query q = withConnIO (`runQuery` q)
queryFirst :: Default QueryRunner a b => Query a -> Transaction (Maybe b)
queryFirst q = listToMaybe <$> query q
insert :: Table w r -> w -> Transaction Int64
insert t w = withConnIO (\c -> runInsertMany c t [w])
insertMany :: Table w r -> [w] -> Transaction Int64
insertMany t ws = withConnIO (\c -> runInsertMany c t ws)
insertReturning :: Default QueryRunner a b => Table w r -> (r -> a) -> w -> Transaction [b]
insertReturning t ret w = withConnIO (\c -> runInsertManyReturning c t [w] ret)
insertReturningFirst :: Default QueryRunner a b => Table w r -> (r -> a) -> w -> Transaction (Maybe b)
insertReturningFirst t ret w = listToMaybe <$> insertReturning t ret w
insertManyReturning :: Default QueryRunner a b => Table w r -> [w] -> (r -> a) -> Transaction [b]
insertManyReturning t ws ret = withConnIO (\c -> runInsertManyReturning c t ws ret)
update :: Table w r -> (r -> w) -> (r -> Column PGBool) -> Transaction Int64
update t r2w predicate = withConnIO (\c -> runUpdate c t r2w predicate)
updateReturning :: Default QueryRunner a b
=> Table w r
-> (r -> w)
-> (r -> Column PGBool)
-> (r -> a)
-> Transaction [b]
updateReturning table r2w predicate r2returned = withConnIO (\c -> runUpdateReturning c table r2w predicate r2returned)
updateReturningFirst :: Default QueryRunner a b
=> Table w r
-> (r -> w)
-> (r -> Column PGBool)
-> (r -> a)
-> Transaction (Maybe b)
updateReturningFirst table r2w predicate r2returned = listToMaybe <$> updateReturning table r2w predicate r2returned
delete :: Table a b -> (b -> Column PGBool) -> Transaction Int64
delete table r2b = withConnIO (\c -> runDelete c table r2b)