HDBC-session 0.1.1.1 → 0.1.2.0
raw patch · 2 files changed
+70/−32 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Database.HDBC.Session: bracketConnection :: (Monad m, IConnection conn) => (forall c. m c -> (c -> m ()) -> (c -> m a) -> m a) -> (forall b. IO b -> m b) -> IO conn -> (conn -> m a) -> m a
+ Database.HDBC.Session: transaction :: IConnection conn => IO conn -> (conn -> IO a) -> IO a
+ Database.HDBC.Session: withConnectionIO_ :: IConnection conn => IO conn -> (conn -> IO a) -> IO a
Files
- HDBC-session.cabal +4/−3
- src/Database/HDBC/Session.hs +66/−29
HDBC-session.cabal view
@@ -1,5 +1,5 @@ name: HDBC-session-version: 0.1.1.1+version: 0.1.2.0 synopsis: Bracketed connection for HDBC description: This package contains a base bracketed function to call close correctly against opend DB connection.@@ -8,11 +8,12 @@ license-file: LICENSE author: Kei Hibino maintainer: ex8k.hibino@gmail.com-copyright: Copyright (c) 2013-2017 Kei Hibino+copyright: Copyright (c) 2013-2018 Kei Hibino category: Database build-type: Simple cabal-version: >=1.10-tested-with: GHC == 8.2.1+tested-with: GHC == 8.4.1, GHC == 8.4.2, GHC == 8.4.3+ , GHC == 8.2.1, GHC == 8.2.2 , GHC == 8.0.1, GHC == 8.0.2 , GHC == 7.10.1, GHC == 7.10.2, GHC == 7.10.3 , GHC == 7.8.1, GHC == 7.8.2, GHC == 7.8.3, GHC == 7.8.4
src/Database/HDBC/Session.hs view
@@ -2,7 +2,7 @@ -- | -- Module : Database.HDBC.Session--- Copyright : 2013-2016 Kei Hibino+-- Copyright : 2013-2018 Kei Hibino -- License : BSD3 -- -- Maintainer : ex8k.hibino@gmail.com@@ -14,14 +14,20 @@ module Database.HDBC.Session ( -- * Bracketed session -- $bracketedSession- withConnectionCommit,- withConnectionIO, withConnectionIO',+ transaction, - withConnection,+ withConnectionIO, withConnectionIO_, + bracketConnection,+ -- * Show errors -- $showErrors- showSqlError, handleSqlError'+ showSqlError, handleSqlError',++ -- * Deprecated+ withConnection,+ withConnectionIO',+ withConnectionCommit, ) where import Database.HDBC (IConnection, handleSql,@@ -52,50 +58,81 @@ handleSqlError' = handleSql (fail . reformat . showSqlError) where reformat = ("SQL error: \n" ++) . unlines . map (" " ++) . lines --- | Run a transaction on a HDBC IConnection and close the connection.-withConnection :: (Monad m, IConnection conn)- => (forall c. m c -> (c -> m ()) -> (c -> m a) -> m a) -- ^ bracket- -> (forall b. IO b -> m b) -- ^ lift- -> IO conn -- ^ Connect action- -> (conn -> m a) -- ^ Transaction body- -> m a-withConnection bracket' lift connect tbody =- bracket' (lift open') (lift . close') bodyWithRollback+-- | Generalized session with bracketed HDBC connection.+-- Run a transaction on a HDBC IConnection and close the connection.+bracketConnection :: (Monad m, IConnection conn)+ => (forall c. m c -> (c -> m ()) -> (c -> m a) -> m a) -- ^ bracket+ -> (forall b. IO b -> m b) -- ^ lift+ -> IO conn -- ^ Connect action+ -> (conn -> m a) -- ^ Transaction body+ -> m a+bracketConnection bracket_ lift connect tbody =+ bracket_ (lift open) (lift . close) bodyWithRollback where- open' = handleSqlError' connect- close' :: IConnection conn => conn -> IO ()- close' = handleSqlError' . HDBC.disconnect+ open = handleSqlError' connect+ close :: IConnection conn => conn -> IO ()+ close = handleSqlError' . HDBC.disconnect bodyWithRollback conn =- bracket'+ bracket_ (return ()) -- Do rollback independent from driver default behavior when disconnect. (const . lift . handleSqlError' $ HDBC.rollback conn) (const $ tbody conn) +{-# DEPRECATED withConnection "use 'bracketConnection' instead of this." #-}+-- | Deprecated. use 'bracketConnection' instead of this.+withConnection :: (Monad m, IConnection conn)+ => (forall c. m c -> (c -> m ()) -> (c -> m a) -> m a)+ -> (forall b. IO b -> m b)+ -> IO conn+ -> (conn -> m a)+ -> m a+withConnection = bracketConnection++-- | Same as 'withConnectionIO' other than not wrapping transaction body in 'handleSqlError''.+withConnectionIO_ :: IConnection conn+ => IO conn -- ^ Connect action+ -> (conn -> IO a) -- ^ Transaction body+ -> IO a -- ^ Result transaction action+withConnectionIO_ = bracketConnection bracket id+ -- | Run a transaction on a HDBC 'IConnection' and close the connection.--- Simple 'IO' version.+-- Not issuing commit at last, so if you need, issue commit manually in transaction body. withConnectionIO :: IConnection conn => IO conn -- ^ Connect action -> (conn -> IO a) -- ^ Transaction body -> IO a -- ^ Result transaction action-withConnectionIO = withConnection bracket id+withConnectionIO connect body = withConnectionIO_ connect $ handleSqlError' . body --- | Same as 'withConnectionIO' other than issuing commit at the end of transaction body.+{-# DEPRECATED withConnectionIO' "use 'withConnectionIO' instead of this." #-}+-- | Deprecated. use 'withConnectionIO' instead of this.+withConnectionIO' :: IConnection conn+ => IO conn -- ^ Connect action+ -> (conn -> IO a) -- ^ Transaction body+ -> IO a -- ^ Result transaction action+withConnectionIO' = withConnectionIO++-- | Run a transaction on a HDBC 'IConnection' and commit at last, and then close the connection. -- In other words, the transaction with no exception is committed. -- Handy defintion for simple transactions.+transaction :: IConnection conn+ => IO conn -- ^ Connect action+ -> (conn -> IO a) -- ^ Transaction body+ -> IO a -- ^ Result transaction action+transaction conn body =+ withConnectionIO conn $ \c -> do+ x <- body c+ HDBC.commit c+ return x++{-# DEPRECATED withConnectionCommit "use 'transaction' instead of this." #-}+-- | Deprecated. use 'transaction' instead of this. withConnectionCommit :: IConnection conn => IO conn -- ^ Connect action -> (conn -> IO a) -- ^ Transaction body -> IO a -- ^ Result transaction action withConnectionCommit conn body =- withConnectionIO conn $ \c -> do+ withConnectionIO_ conn $ \c -> do x <- body c HDBC.commit c return x---- | Same as 'withConnectionIO' other than wrapping transaction body in 'handleSqlError''.-withConnectionIO' :: IConnection conn- => IO conn -- ^ Connect action- -> (conn -> IO a) -- ^ Transaction body- -> IO a -- ^ Result transaction action-withConnectionIO' connect body = withConnectionIO connect $ handleSqlError' . body