Connection is definied as follows:
database, through which you can operate on the it.
-- In order to create the connection you need to use the @connect@ function from the module for
-- your prefered backend.
data Connection
= Connection
{ connDisconnect :: IO ()
, connExecute :: String -> IO Integer
, connPrepare :: String -> IO Statement
, connQuery :: String -> IO Statement
, connTables :: IO [String]
, connDescribe :: String -> IO [FieldDef]
, connBeginTransaction :: IO ()
, connCommitTransaction :: IO ()
, connRollbackTransaction :: IO ()
, connClosed :: MVar Bool
}
So it's actually a record containing mostly IO () values? (or functions
with this return value? ) It doesn't matter because the IO () results
aren't evaluated as long as they aren't needed, right?
Would it be equivalent to have some kind of class design:
class Connection c where
connDisconnect :: c -> ()
connPrepare :: c -> String -> IO Integer
[...]
with an implementation for each kind of Database connection handle?
EG:
Data MySQLhandle=MySQLhandle Int
instance MySQLhandle handle where
connDisconnect c = MySQLCloseHandle $ MySQLhandle c
[ ... ] ?
The way it's implemented the connect function of MySQL simply adds this
handle to every functions as argument:
[...]
let connection = Connection
{ connDisconnect = mysql_close pMYSQL
, connExecute = execute pMYSQL
, connQuery = query connection pMYSQL
, connTables = tables connection pMYSQL
, connDescribe = describe connection pMYSQL
, connBeginTransaction = execute pMYSQL "begin" >> return ()
, connCommitTransaction = execute pMYSQL "commit" >> return ()
, connRollbackTransaction = execute pMYSQL "rollback" >> return ()
, connClosed = refFalse
}
return connection
where
[...]
In my imaginated case the connect method would just return the Database
handle.
Where is the difference?
Marc
|