1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2-- | A simplified client interface for Project:M36 database access.
3module ProjectM36.Client.Simple (
4 simpleConnectProjectM36,
5 simpleConnectProjectM36At,
23 C.Atomable(toAtom, fromAtom),
25 C.PersistenceStrategy(..),
26 C.NotificationCallback,
27 C.emptyNotificationCallback,
28 C.DatabaseContextExpr(..),
29 C.RelationalExprBase(..)
32import Control.Exception.Base
33import Control.Monad ((<=<))
34import Control.Monad.Reader
36import qualified ProjectM36.Client as C
37import ProjectM36.Error
39type DbConn = (C.SessionId, C.Connection)
41newtype Db a = Db {runDb :: ReaderT DbConn IO a}
42 deriving (Functor, Applicative, Monad, MonadIO)
44-- This exception type should never be observable by the API users.
45-- It merely carries errors which end up as RelError at the end of a transaction.
46newtype TransactionCancelled = TransactionCancelled DbError deriving Show
47instance Exception TransactionCancelled
49-- | A simple alternative to 'connectProjectM36' which includes simple session management.
50simpleConnectProjectM36At :: HeadName -> C.ConnectionInfo -> IO (Either DbError DbConn)
51simpleConnectProjectM36At headName connInfo = do
52 eConn <- C.connectProjectM36 connInfo
54 Left err -> pure (Left (ConnError err))
56 eSess <- C.createSessionAtHead conn headName
60 pure (Left (RelError err))
61 Right sess -> pure (Right (sess, conn))
63-- | Same as 'simpleConnectProjectM36At' but always connects to the @master@ branch.
64simpleConnectProjectM36 :: C.ConnectionInfo -> IO (Either DbError DbConn)
65simpleConnectProjectM36 = simpleConnectProjectM36At "master"
67-- | Closes the database connection.
68close :: DbConn -> IO ()
69close (_ , conn) = C.close conn
71-- | Runs a Db monad which may include some database updates. If an exception or error occurs, the transaction is rolled back. Otherwise, the transaction is committed to the head of the current branch.
72withTransaction :: DbConn -> Db a -> IO (Either DbError a)
73withTransaction sessconn = withTransactionUsing sessconn UnionMergeStrategy
75-- | Same as 'withTransaction' except that the merge strategy can be specified.
76withTransactionUsing :: DbConn -> MergeStrategy -> Db a -> IO (Either DbError a)
77withTransactionUsing (sess, conn) strat dbm = do
78 eHeadName <- C.headName sess conn
80 Left err -> pure (Left (RelError err))
82 let successFunc = C.autoMergeToHead sess conn strat headName
83 block = runReaderT (runDb dbm) (sess, conn)
84 handler :: TransactionCancelled -> IO (Either DbError a)
85 handler (TransactionCancelled err) = pure (Left err)
87 ret <- C.withTransaction sess conn (Right <$> block) successFunc
89 Left err -> pure (Left (RelError err))
90 Right val -> pure (Right val)
92-- | A union of connection and other errors that can be returned from 'withTransaction'.
93data DbError = ConnError C.ConnectionError |
94 RelError RelationalError |
98-- | Execute a 'DatabaseContextExpr' in the 'DB' monad. Database context expressions manipulate the state of the database. In case of an error, the transaction is terminated and the connection's session is rolled back.
99execute :: C.DatabaseContextExpr -> Db ()
100execute = orCancelTransaction <=< executeOrErr
102-- | Run a 'RelationalExpr' query in the 'DB' monad. Relational expressions perform read-only queries against the current database state.
103query :: C.RelationalExpr -> Db Relation
104query = orCancelTransaction <=< queryOrErr
106-- | Run a 'DatabaseContextExpr' update expression. If there is an error, just return it without cancelling the current transaction.
107executeOrErr :: C.DatabaseContextExpr -> Db (Either RelationalError ())
108executeOrErr expr = Db $ do
110 lift $ C.executeDatabaseContextExpr sess conn expr
112-- | Run a 'RelationalExpr' query expression. If there is an error, just return it without cancelling the transaction.
113queryOrErr :: C.RelationalExpr -> Db (Either RelationalError Relation)
114queryOrErr expr = Db $ do
116 lift $ C.executeRelationalExpr sess conn expr
118-- | Unconditionally roll back the current transaction and throw an exception to terminate the execution of the Db monad.
120rollback = cancelTransaction TransactionRolledBack
122-- | Cancel a transaction and carry some error information with it.
123cancelTransaction :: DbError -> Db a
124cancelTransaction err = liftIO $ throwIO (TransactionCancelled err)
126-- | Converts the 'Either' result from a 'Db' action into an immediate cancel in the case of error.
127orCancelTransaction :: Either RelationalError a -> Db a
128orCancelTransaction = either (cancelTransaction . RelError) pure
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list