[DB-SIG] Inheriting from a DBAPI 2.0 Cursor Object
Peter L. Buschman
plblists at iotk.com
Thu Sep 25 21:19:21 EDT 2003
Chis:
Thanks so much. I had been so focused on finding an inheritance solution (
difficult since the Connection and Cursor objects are not exposed
directly in DBAPI ) that I completely missed simply wrapping the relevant
objects. DBAPI is not that complex, so wrapping the methods is not
that much extra code.
--Peter
At 10:56 AM 9/25/2003 -0700, Chris Cogdon wrote:
>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
>
>
>_______________________________________________
>DB-SIG maillist - DB-SIG at python.org
>http://mail.python.org/mailman/listinfo/db-sig
More information about the DB-SIG
mailing list