[Zope] [OT] Example of Forcing Type coercion on a DCOracle2 column?

Matthew T. Kromer matt@zope.com
Wed, 19 Mar 2003 16:50:13 -0500

John Ziniti wrote:

> Hello --
> The DCOracle2 Documentation states that it is possible to force
> type coercion on a result column to particular type, but does not
> give an example of how to do this.  Does anyone have any
> suggestions?  I am trying to force an XMLTYPE column into
> a CLOB.
> TIA,
> John Ziniti

It doesn't go into detail because you really, really don't want to 
unless you know what you are doing.

What the DCOracle2 type coercion does is assert to Oracle that the 
python object is whatever type you say it is.

What this is most useful for is changing a string into something else -- 
but you have to be very careful that your input data is in fact properly 

For example, if I say

import DCOracle2
i = DCOracle2.TypeCoercion("foo", "SQLT_INT")

I'm creating a coerced variable "i" that will claim to be an int.  In 
fact, when Oracle goes to read it, it will read 0x666f6f00 (depending on 
the byte order).  This may or may not be 1718578944 -- but it certainly 
isn't a very useful value.  The coercion is applied AFTER the normal 
python type->oracle data type binding happens, ie it really just affects 
the bind declaration.  If I coerce the number 1, I'm really referring to 
an address in memory pointing to a value containing 0x00000001 with a 
length of four bytes.  When oracle is passed this pointer, its told what 
the data type is it points at.  The TypeCoercion overrides what the 
declared type is, but doesn't *reformat* the data.

Why would you want to do this?  Well, maybe you want to handle the 
binary conversion of values yourself.  The underlying C module dco2 will 
let you get at the raw memory behind a result, so you could extend 
DCOracle2 to understand new types without writing additional C code.

Matt Kromer
Zope Corporation  http://www.zope.com/