module Opaleye.Trans
( OpaleyeT (..)
, runOpaleyeT
,
Transaction
, transaction
, run
,
query
, queryFirst
,
insert
, insertMany
, insertReturning
, insertReturningFirst
, insertManyReturning
,
update
, updateReturning
, updateReturningFirst
,
withConn
,
liftBase
, MonadBase
, liftIO
, MonadIO
, ask
, Int64
) where
import Control.Monad.Base (MonadBase, liftBase)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Reader (MonadReader, ReaderT (..),
ask)
import Control.Monad.Trans (MonadTrans (..))
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
import Opaleye
newtype OpaleyeT m a = OpaleyeT { unOpaleyeT :: ReaderT Connection m a }
deriving (Functor, Applicative, Monad, MonadTrans, MonadIO, MonadReader Connection)
instance MonadBase b m => MonadBase b (OpaleyeT m) where
liftBase = lift . liftBase
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 = do
conn <- ask
liftIO (f conn)
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 (Transaction t) = withConn $ runReaderT t
withConnIO :: (Connection -> IO a) -> Transaction a
withConnIO f = Transaction (ReaderT f)
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 -> runInsert 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 -> runInsertReturning 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
:: (MonadIO m, Default QueryRunner a b)
=> Table w r
-> (r -> a)
-> [w]
-> OpaleyeT m [[b]]
insertManyReturning t ret ws =
transaction (mapM (insertReturning t ret) ws)
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 returned haskells
=> Table w r
-> (r -> w)
-> (r -> Column PGBool)
-> (r -> returned)
-> Transaction [haskells]
updateReturning table r2w predicate r2returned =
withConnIO (\c -> runUpdateReturning c table r2w predicate r2returned)
updateReturningFirst
:: Default QueryRunner returned haskells
=> Table w r
-> (r -> w)
-> (r -> Column PGBool)
-> (r -> returned)
-> Transaction (Maybe haskells)
updateReturningFirst table r2w predicate r2returned =
listToMaybe <$> updateReturning table r2w predicate r2returned