Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Synopsis
- data Conn = forall conn.IConnection conn => Conn {
- db :: Database
- implicitCommit :: Bool
- connection :: conn
- connect :: forall conn. IConnection conn => Database -> conn -> Conn
- data Database
- type ConnectionPool = Pool Conn
- createConnPool :: IConnection conn => Database -> String -> (String -> IO conn) -> Double -> Int -> IO ConnectionPool
- withResource :: Pool a -> (a -> IO r) -> IO r
Documentation
This module defines a wrapper around an HDBC IConnection.
Using this wrapper Conn
simplifies the signature of the functions in the GP
module.
It allows to use any HDBC connection without having to define a new function for each connection type.
It also provides additional attributes to the connection, like the database type and the implicit commit flag.
These attributes can be used to implement database specific functionality, modify transaction behaviour, etc.
This code has been inspired by the HDBC ConnectionWrapper and some parts have been copied verbatim from the HDBC Database.HDBC.Types module.
This module also defines a ConnectionPool type, which provides basic connection pooling functionality.
A wrapper around an HDBC IConnection.
forall conn.IConnection conn => Conn | |
|
Instances
IConnection Conn Source # | manually implement the IConnection type class for the Conn type. |
Defined in Database.GP.Conn disconnect :: Conn -> IO () # runRaw :: Conn -> String -> IO () # run :: Conn -> String -> [SqlValue] -> IO Integer # prepare :: Conn -> String -> IO Statement # hdbcDriverName :: Conn -> String # hdbcClientVer :: Conn -> String # proxiedClientName :: Conn -> String # proxiedClientVer :: Conn -> String # dbServerVer :: Conn -> String # dbTransactionSupport :: Conn -> Bool # getTables :: Conn -> IO [String] # describeTable :: Conn -> String -> IO [(String, SqlColDesc)] # |
connect :: forall conn. IConnection conn => Database -> conn -> Conn Source #
a smart constructor for the Conn type.
An enumeration of the supported database types.
Instances
Enum Database Source # | |
Show Database Source # | |
Eq Database Source # | |
type ConnectionPool = Pool Conn Source #
A pool of connections.
:: IConnection conn | |
=> Database | the database type e.g. Postgres, MySQL, SQLite |
-> String | the connection string |
-> (String -> IO conn) | a function that takes a connection string and returns an IConnection |
-> Double | the time (in seconds) to keep idle connections open |
-> Int | the maximum number of connections to keep open |
-> IO ConnectionPool | the resulting connection pool |
Creates a connection pool.
withResource :: Pool a -> (a -> IO r) -> IO r #
Take a resource from the pool, perform an action with it and return it to the pool afterwards.
- If the pool has an idle resource available, it is used immediately.
- Otherwise, if the maximum number of resources has not yet been reached, a new resource is created and used.
- If the maximum number of resources has been reached, this function blocks until a resource becomes available.
If the action throws an exception of any type, the resource is destroyed and not returned to the pool.
It probably goes without saying that you should never manually destroy a pooled resource, as doing so will almost certainly cause a subsequent user (who expects the resource to be valid) to throw an exception.