[DB-SIG] Inheriting from a DBAPI 2.0 Cursor Object

Peter L. Buschman plb at iotk.com
Thu Sep 25 12:52:24 EDT 2003

Maybe I just haven't been programming in Python long enough, but this has 
me stumped...

I am writing a DBAPI 2.0 compliant database abstraction layer that wraps 
most of the available DBAPI 2.0 drivers
for Python and presents a consistent paramstyle, connection parameters, 
etc., to the end-user.  So far, I've been successful
in importing my driver object and connection method, but the connection and 
cursor objects are just passed-through from
the underlying DBAPI driver.

The next thing I want to do is override the execute() method of the cursor 
object so that it passes the sql statement and
parameters through a function that converts from the abstraction layer's 
paramstyle to the underlying driver's paramstyle,
but I'm not quite sure how to do this.

Here is what an example looks like today:

	import iotk.dal as dal # Database Abstraction Layer

	driver = dal.driver.new('mxodbc') # Or mysqldb, psycopg, whatever...

	conn = driver.connect( dsn='foo' )

	cursor = conn.cursor()

	cursor.execute('SELECT * FROM sometable' WHERE SOMETHING = ?', ( 1 ) )

My connect() function is simply a wrapper around the underlying driver's 
connect method that is smart enough to know
what driver it is talking to and what parameters to use.  The object 
returned is simply one of that driver's connection objects.

How would I best define my own connection and cursor objects such that I 
can inherit from the underlying driver but also
override those few methods like execute to make the driver access as 
transparent as possible?


More information about the DB-SIG mailing list