From: Carlos R. <car...@gm...> - 2004-12-06 17:00:06
|
On Mon, 06 Dec 2004 10:15:34 -0600, Ian Bicking <ia...@co...> 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. 2) Forget decimals on the DB -- store everything as strings, and convert to native Decimals on reading & writing. This works, but in this case I can't rely on the DB itself for any arithmetic manipulation. That will cause problems with aggregate SQL clauses (SUM, MAX, etc.). I really don't know what to do. Implementing Decimal support in SQLObject would be nice, but I fear that it will take a lot of time and require a lot of knowledge about individual database drivers. What a problem... -- Carlos Ribeiro Consultoria em Projetos blog: http://rascunhosrotos.blogspot.com blog: http://pythonnotes.blogspot.com mail: car...@gm... mail: car...@ya... |