print decimal as long if scale is zero

optimize numeric and decimal displaying

add unit test case for print simple decimal

close the cursor
master
Hongjiang Zhang 2020-05-21 15:49:49 +08:00
parent ff60a141cb
commit cac40267fc
4 changed files with 38 additions and 2 deletions

View File

@ -663,12 +663,29 @@ def _java_to_py(java_method):
return getattr(java_val, java_method)() return getattr(java_val, java_method)()
return to_py 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_double = _java_to_py('doubleValue')
_to_int = _java_to_py('intValue') _to_int = _java_to_py('intValue')
_to_boolean = _java_to_py('booleanValue') _to_boolean = _java_to_py('booleanValue')
_to_decimal = _java_to_py_bigdecimal('doubleValue')
def _init_types(types_map): def _init_types(types_map):
global _jdbc_name_to_const global _jdbc_name_to_const
_jdbc_name_to_const = types_map _jdbc_name_to_const = types_map
@ -698,8 +715,8 @@ _DEFAULT_CONVERTERS = {
'TIME': _to_time, 'TIME': _to_time,
'DATE': _to_date, 'DATE': _to_date,
'BINARY': _to_binary, 'BINARY': _to_binary,
'DECIMAL': _to_double, 'DECIMAL': _to_decimal,
'NUMERIC': _to_double, 'NUMERIC': _to_decimal,
'DOUBLE': _to_double, 'DOUBLE': _to_double,
'FLOAT': _to_double, 'FLOAT': _to_double,
'TINYINT': _to_int, 'TINYINT': _to_int,

View File

@ -0,0 +1,5 @@
create table customer (
"CUST_ID" INTEGER not null,
"USER_ID" DECIMAL(18,0) not null,
primary key ("CUST_ID")
);

View File

@ -0,0 +1 @@
insert into CUSTOMER (CUST_ID, USER_ID) values (1, 12345);

View File

@ -70,6 +70,7 @@ class IntegrationTestBase(object):
def tearDown(self): def tearDown(self):
cursor = self.conn.cursor() cursor = self.conn.cursor()
cursor.execute("drop table ACCOUNT"); cursor.execute("drop table ACCOUNT");
cursor.execute("drop table CUSTOMER");
self.conn.close() self.conn.close()
def test_execute_and_fetch_no_data(self): def test_execute_and_fetch_no_data(self):
@ -207,11 +208,21 @@ class IntegrationTestBase(object):
cursor.execute("select * from ACCOUNT") cursor.execute("select * from ACCOUNT")
self.assertEqual(cursor.rowcount, -1) 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): class SqliteTestBase(IntegrationTestBase):
def setUpSql(self): def setUpSql(self):
self.sql_file(os.path.join(_THIS_DIR, 'data', 'create.sql')) 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', '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): def test_execute_type_blob(self):
cursor = self.conn.cursor() cursor = self.conn.cursor()
@ -275,6 +286,8 @@ class HsqldbTest(IntegrationTestBase, unittest.TestCase):
def setUpSql(self): def setUpSql(self):
self.sql_file(os.path.join(_THIS_DIR, 'data', 'create_hsqldb.sql')) 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', '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): class PropertiesDriverArgsPassingTest(unittest.TestCase):