Re: [cx-oracle-users] Modifying fetched data python types for zope adaptor
Brought to you by:
atuining
From: Amaury F. d'A. <ama...@gm...> - 2006-11-09 23:14:18
|
Hello Anthony, Anthony Tuininga 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? Some wild thoughts, without any consideration of the diffulty to implement: cx_Oracle types should be subclassable: the Cursor class and the Variable types. When subclassing, users should be able to provide: - OracleCursor.variableFactory(self, oracleType, <dataLength_and others?>): the base implementation ends up by calling the current C function "Variable_New" and returns its value. Of course, when subclassing, users will want to substitute their own udt_Variable class. - OracleCursor.createRow(self): the base implementation is "Cursor_CreateTuple". As an example of subclassing, I could copy it into a subclass of tuple, with a custom __getattribute__ handling the column names... - xxxVariable.getvalue and setvalue. Here can take place the conversions to/from Zope datatypes. Now, for the implementation: The setValueProc and getValueProc members move from udt_VariableType to udt_Variable. Variable_New then checks for derived methods: if so, it replaces setValueProc and getValueProc with appropriate wrappers like: PyObject *derivedGetValue(udt_Variable *var, unsigned pos) { return PyObject_CallMethod(var, "getvalue", "i", pos); } The advantage of this method is that advanced users can write their own C code, derive DateTimeVar in C, and directly access the OCIDate* buffer, with no performance penalty. Thank you for having read so far. Now my brain is too hot, I'd better go to bed... -- Amaury Forgeot d'Arc |