Michael Bayer wrote:
jose soares wrote:
  
Michael Bayer wrote:
    
jose soares wrote:

      
Hi all,

I'm using Oracle and PostgreSQL with SQLAlchemy and I have some
troubles
to make the code compatible  with both of them.
Numeric sa type returns a different type with oracle and pg.

For example, in the following table I'm using the Column 'importo' with
type Numeric as:


tbl['prestazione'] = Table('prestazione', database.metadata,
        Column('id', Integer, Sequence('prestazione_seq'),
nullable=False, primary_key=True),
        Column('data', Date, nullable=False),
        Column('quantita', Numeric(15,3)),
        Column('importo', Numeric(12,3))
)

while oracle returns a float type as:

prestazione.c.importo = 12.0


postgres returns a Decimal type as:

prestazione.c.importo = Decimal("0.000")

and I have difficulties to make code compatible, because sometimes the
program raises a TypeError error as:

    TypeError: unsupported operand type(s) for +: 'Decimal' and 'float'

Is there some w.a. to avoid this thing?

        
if you're on 0.6 (or even 0.5 for that matter) the Numeric type should
be
returning Decimal in all cases from result sets.

      
I'm using 0.3.10 , I know I must to upgrade but it's not so easy. :-[
Is there something that I can do to avoid this error in 0.3.10?
    

if its 0.3 probably use TypeDecorator.
  
Your suggest is fine, Michael,
I tried this...but...


class MyType(types.TypeDecorator):
    impl = types.Numeric
    def process_bind_param(self, value, dialect):
        return decimal.Decimal(str(value))

    def process_result_value(self, value, dialect):
        return decimal.Decimal(str(value))

    def copy(self):
        return MyType(self.impl.length)

.....
    test = Table('test', database.metadata,
        Column('id', Integer, nullable=False, primary_key=True),
        Column('data', Date, nullable=False),
        Column('importo', MyType(12,3))
        )

....

insert into  test(data,id,importo) values  ('2009-01-01',2,32.331)
select * from  test

id | data                | importo
-- + ------------------- + -------
2  | 2009-01-01 00:00:00 | 32.331


$tg_admin shell

In [36]: test
Out[36]: Table('test',ThreadLocalMetaData(),Column('id',Integer(),primary_key=True,nullable=False),Column('data',Date(),nullable=False),Column('importo',MyType()),schema=None)

In [37]: type(dict(test.select(test.c.id==2).execute().fetchone())['IMPORTO'])

SELECT test.id, test.data, test.importo
FROM test
WHERE test.id = :test_id

{'test_id': 2}
Out[37]: <type 'float'>

---------------------------------

What's wrong?
j

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to