Skip to content

Commit 2ed1050

Browse files
authored
Merge pull request #54 from qweqq/patch-2
fix #53 Loss of scale when reading numeric with zero value
2 parents c829350 + 2b7040a commit 2ed1050

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/firebird/driver/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3392,7 +3392,7 @@ def _extract_db_array_to_list(self, esize: int, dtype: int, subtype: int,
33923392
elif dtype in (a.blr_short, a.blr_long, a.blr_int64):
33933393
val = (0).from_bytes(buf[bufpos:bufpos + esize], 'little', signed=True)
33943394
if subtype or scale:
3395-
val = decimal.Decimal(val) / _ten_to[abs(scale)]
3395+
val = decimal.Decimal(val).scaleb(-abs(scale))
33963396
elif dtype == a.blr_bool:
33973397
val = (0).from_bytes(buf[bufpos:bufpos + esize], 'little') == 1
33983398
elif dtype == a.blr_float:
@@ -3799,7 +3799,7 @@ def _unpack_output(self) -> tuple:
37993799
value = (0).from_bytes(buffer[offset:offset + length], 'little', signed=True)
38003800
# It's scalled integer?
38013801
if desc.subtype or desc.scale:
3802-
value = decimal.Decimal(value) / _ten_to[abs(desc.scale)]
3802+
value = decimal.Decimal(value).scaleb(-abs(desc.scale))
38033803
elif datatype == SQLDataType.DATE:
38043804
value = _util.decode_date(buffer[offset:offset+length])
38053805
elif datatype == SQLDataType.TIME:

tests/test_issues.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ def test_issue_02(db_connection):
3131
cur.execute('select C1,C2,C3 from T2 where C1 = 1')
3232
rows = cur.fetchall()
3333
assert rows == [(1, None, 1)]
34+
35+
def test_issue_53(db_connection):
36+
with db_connection.cursor() as cur:
37+
cur.execute("select cast('0.00' as numeric(9,2)) from rdb$database")
38+
numeric_val = cur.fetchone()[0]
39+
numeric_val_exponent = numeric_val.as_tuple()[2]
40+
db_connection.commit()
41+
assert numeric_val_exponent == -2

0 commit comments

Comments
 (0)