Zope’s Database Connection Wrappers
Wednesday, 15 February 2006
Zope’s database connections are like onions. Take out the outer layer, and you find another onion. Take out another layer, and you find,… another onion.
For example, ZODBCDA - a Zope product used to enable ODBC access - has 3 classes all representing database connections. A schematic would help:
class Connection(Shared.DC.ZRDB.Connection.Connection) [ZODBCDA/DA.py]
|
+--- isinstance(self._v_database_connection, Products.ZODBCDA.db.DB)
|
|
+--- (self.connection, Products.ZODBCDA.sql.SQLConnection)
In summary,
- you create an instance of Connection, which you mount on the ZODB
- upon first access of the Connection instance (via __call__), it creates a db.DB instance, and caches it as _v_database_connection
- the query is then sent to the innermost SQL connection, which is a python wrapper around an ODBC database connection.
app.Products['ZODBCDA'].manage_addConnection(’dbConnection’)
db = app.dbConnection()
–> db.connection = SQLConnection(”localhost”, “sa”, “”)
db.query(’SELECT * FROM USERS’)
–> SQLExecDirect(db.connection. ‘SELECT * FROM USERS’)