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

Chris Cogdon chris at cogdon.org
Thu Sep 25 13:56:54 EDT 2003


On Thursday, Sep 25, 2003, at 09:52 US/Pacific, Peter L. Buschman wrote:

> 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?

The 'connect' function would need to return one of your OWN object 
types, which can either 'wrap' or 'inherit' from the real type. Once 
you do that, then you can define all the methods how you please, even 
if its just to call the wrapped object (or, if you're inheriting, you 
can just omit it).

For example, here's a rough outline of how I'd do it myself:

In 'driver'

def new ( connection_type ):
	if connection_type = "mxodbc":
		return MxodbcDriver ()

class MxodbcDriver:

	def connect ( self, *argc, **argk ):
		return MxodbcWrapper ( db ):

# although, I dont know why you'd want to do it the above way... you 
could always specifiy BOTH the database type, and the arguments, in the 
'new' function.


class MxodbcWrapper:

	def __init__ ( self, *argc, *argk ):
		self.db = mxodbc ( *argc, **argk )

	def cursor ( self ):
		# do wrapper specific stuff
		return MxodbcCursor ( self )


class MxodbcCursor:
	
	def __init__ ( self, wrapper, cursor ):
		self.wrapper = wrapper
		self.cursor = self.wrapper.db.cursor () # This gets the real DB's 
cursor

	def execute ( self, args ):
		# self.wrapper can be used to get access to the wrapper, or 
underlying DB
		# self.cursor can be used to get to the real cursor

		# wrapper specific stuff

		self.cursor.execute ( args )



-- 
    ("`-/")_.-'"``-._        Chris Cogdon <chris at cogdon.org>
     . . `; -._    )-;-,_`)
    (v_,)'  _  )`-.\  ``-'
   _.- _..-_/ / ((.'
((,.-'   ((,/   fL




More information about the DB-SIG mailing list