Carlos Ribeiro wrote:
> On Mon, 06 Dec 2004 10:15:34 -0600, Ian Bicking <ianb@...> wrote:
>>Carlos Ribeiro wrote:
>>>I just downloaded & installed the decimal module (the same from Python
>>>2.4) for Python 2.3. I know that SQLObject has a DecimalCol type. I've
>>>tried to check the code that actually fetches decimal values from the
>>>DB, but I got a little bit lost on SQLObject internals at this point.
>>>I trie to grep for Decimal and found only a few references, so I
>>>assume it's not full implemented yet.
>>One way would be to handle the conversion through the driver. This
>>should be doable with psycopg. Some way you have to get the value from
>>the database, probably as a string, then convert it to decimal.
>>Obviously it's not very helpful if you get it from the database as a
>>float, then convert it to a decimal.
>>Right now there's no good hooks for coercing something before it is
>>returned. E.g., in Postgres I think that'd be something like "SELECT
>>(CAST dec_column AS TEXT) AS dec_column", which would keep psycopg from
>>converting to a float.
> Humm. Decimals should always be passed around either as native
> decimals or as strings; the Decimal constructor takes a string as a
> parameter, and that's the only way to preserve precision. There are a
> few ways out of this problem:
> 1) Rely on the DB driver working with native decimals (perhaps with
> some patches, as you suggested, to trick the DB to use a string as an
> intermediate value). That's a definitive solution but I'm not sure
> even how to start it.
It depends what backends you are targetting. If Postgres/psycopg, you
should be able to modify its type coercion fairly easily. For other
database drivers, I'm not sure. There's no standard DB API way to
manage type conversion, but I imagine there are interested people in
each case, and workaround solutions. This seems like the easiest
course, and the end result (getting drivers to return decimal types
natively) will benefit everyone.
Ian Bicking / ianb@... / http://blog.ianbicking.org