Re: [cx-oracle-users] Modifying fetched data python types for zope adaptor
Brought to you by:
atuining
From: Andy H. <and...@gm...> - 2006-11-09 05:13:32
|
Hi Anthony, thanks for the fast response. I'd thought about doing something similar to your suggestion for subclassing cx_Oracle.Cursor and converting the types on the fly, but like you said there's a performance hit. I'll probably implement this anyway so old versions of cx_Oracle can be used. Your second suggestion is definitely something that'd be much appreciated by me. Both the psycopg2 and MySQLdb modules implement something similar. Here's a quick summary of what they do: With MySQLdb you can define a dict, keyed on the modules database types (much like cx_Oracle.NUMBER) with the values being callables which return the desired objects type. The callable is passed a string representation of the data returned, and it returns a python object of the desired type. The conversion dict is passed as an argument to MySQLdb connect method. See: http://sourceforge.net/docman/display_doc.php?docid=32071&group_id=22307#functions-and-attributesfor more details (search for conv) psycopg2 defines a bunch of methods in its extensions module which do pretty much the same thing. You define a casting method by tieing a function to a Postgresql type via the types OID using the method psycopg2.extensions.new_type and then psycopyg2.extensions.regster_type. As with MySQLdb a string representing the returned data is passed to the conversion function, and the conversion function returns a python object. More info at http://initd.org/tracker/psycopg/browser/psycopg2/trunk/doc/extensions.rstand search for "Type casting of SQL types into Python values". Which really is pretty much the same as your settype(cx_Oracle.DATETIME, <YourTypeOrMethod>). I guess the one thing they do in common is pass a string to the conversion method, although it'd probably be as simple to pass cx_Oracles native value for the given type, i.e. a datetime (or cx_Oracle.Date) for cx_Oracle.DATETIME and so on. After all if I'm defining the conversion methods then I should know what I'd be getting. Thanks again in advance. Sorry for going on so long there. Andy On 11/9/06, Anthony Tuininga <ant...@gm...> wrote: > > There is no way of hooking into the returning of date objects. You'll > have to hook the entire result set. You can do that quite easily by > subclassing cx_Oracle.Cursor and overriding fetchone(), fetchmany() > and fetchall() to transform any datetime.datetime entities into Zopes > DateTime objects. Clearly there is a performance penalty in doing > this. If you can suggest an alternative that would eliminate the > performance penalty I'd be happy to entertain it. Perhaps something > along the lines of settype(cx_Oracle.DATETIME, <YourTypeOrMethod>) > which would then call <YourTypeOrMethod> with a particular set of > parameters? Any thoughts on this? |