diff --git a/jaydebeapi/__init__.py b/jaydebeapi/__init__.py index 7af7d59..484a1d1 100644 --- a/jaydebeapi/__init__.py +++ b/jaydebeapi/__init__.py @@ -663,12 +663,29 @@ def _java_to_py(java_method): return getattr(java_val, java_method)() return to_py +def _java_to_py_bigdecimal(java_method): + def to_py(rs, col): + java_val = rs.getObject(col) + if java_val is None: + return + if hasattr(java_val, 'scale'): + scale = getattr(java_val, 'scale')() + if scale == 0: + return getattr(java_val, 'longValue')() + else: + return getattr(java_val, java_method)() + else: + return getattr(java_val, java_method)() + return to_py + _to_double = _java_to_py('doubleValue') _to_int = _java_to_py('intValue') _to_boolean = _java_to_py('booleanValue') +_to_decimal = _java_to_py_bigdecimal('doubleValue') + def _init_types(types_map): global _jdbc_name_to_const _jdbc_name_to_const = types_map @@ -698,8 +715,8 @@ _DEFAULT_CONVERTERS = { 'TIME': _to_time, 'DATE': _to_date, 'BINARY': _to_binary, - 'DECIMAL': _to_double, - 'NUMERIC': _to_double, + 'DECIMAL': _to_decimal, + 'NUMERIC': _to_decimal, 'DOUBLE': _to_double, 'FLOAT': _to_double, 'TINYINT': _to_int, diff --git a/test/data/create_simple_decimal.sql b/test/data/create_simple_decimal.sql new file mode 100644 index 0000000..7f47130 --- /dev/null +++ b/test/data/create_simple_decimal.sql @@ -0,0 +1,5 @@ +create table customer ( +"CUST_ID" INTEGER not null, +"USER_ID" DECIMAL(18,0) not null, +primary key ("CUST_ID") +); diff --git a/test/data/insert_simple_decimal.sql b/test/data/insert_simple_decimal.sql new file mode 100644 index 0000000..c3cd478 --- /dev/null +++ b/test/data/insert_simple_decimal.sql @@ -0,0 +1 @@ +insert into CUSTOMER (CUST_ID, USER_ID) values (1, 12345); diff --git a/test/test_integration.py b/test/test_integration.py index c317ce5..ccb3193 100644 --- a/test/test_integration.py +++ b/test/test_integration.py @@ -70,6 +70,7 @@ class IntegrationTestBase(object): def tearDown(self): cursor = self.conn.cursor() cursor.execute("drop table ACCOUNT"); + cursor.execute("drop table CUSTOMER"); self.conn.close() def test_execute_and_fetch_no_data(self): @@ -207,11 +208,21 @@ class IntegrationTestBase(object): cursor.execute("select * from ACCOUNT") self.assertEqual(cursor.rowcount, -1) + def test_simple_decimal_print(self): + cursor = self.conn.cursor() + cursor.execute("select * from CUSTOMER") + result = cursor.fetchall() + cursor.close() + exp = [(1, 12345)] + self.assertEqual(result, exp) + class SqliteTestBase(IntegrationTestBase): def setUpSql(self): self.sql_file(os.path.join(_THIS_DIR, 'data', 'create.sql')) self.sql_file(os.path.join(_THIS_DIR, 'data', 'insert.sql')) + self.sql_file(os.path.join(_THIS_DIR, 'data', 'create_simple_decimal.sql')) + self.sql_file(os.path.join(_THIS_DIR, 'data', 'insert_simple_decimal.sql')) def test_execute_type_blob(self): cursor = self.conn.cursor() @@ -275,6 +286,8 @@ class HsqldbTest(IntegrationTestBase, unittest.TestCase): def setUpSql(self): self.sql_file(os.path.join(_THIS_DIR, 'data', 'create_hsqldb.sql')) self.sql_file(os.path.join(_THIS_DIR, 'data', 'insert.sql')) + self.sql_file(os.path.join(_THIS_DIR, 'data', 'create_simple_decimal.sql')) + self.sql_file(os.path.join(_THIS_DIR, 'data', 'insert_simple_decimal.sql')) class PropertiesDriverArgsPassingTest(unittest.TestCase):