From 6466865f7b3d3aff024488dd97c029a27ba98fd9 Mon Sep 17 00:00:00 2001 From: jesaerys Date: Mon, 29 Feb 2016 22:11:05 -0600 Subject: [PATCH 1/2] conversion support for more java types TINYINT is mapped to NUMBER, and all of the types mapped to DB API type are assigned to a conversion function. --- jaydebeapi/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/jaydebeapi/__init__.py b/jaydebeapi/__init__.py index 51c2b84..b284527 100644 --- a/jaydebeapi/__init__.py +++ b/jaydebeapi/__init__.py @@ -257,7 +257,7 @@ def _map_jdbc_type_to_dbapi(cls, jdbc_type_const): BINARY = DBAPITypeObject('BINARY', 'BLOB', 'LONGVARBINARY', 'VARBINARY') -NUMBER = DBAPITypeObject('BOOLEAN', 'BIGINT', 'INTEGER', 'SMALLINT') +NUMBER = DBAPITypeObject('BOOLEAN', 'BIGINT', 'INTEGER', 'SMALLINT', 'TINYINT') FLOAT = DBAPITypeObject('FLOAT', 'REAL', 'DOUBLE') @@ -627,11 +627,17 @@ def _init_converters(types_map): 'TIME': _to_time, 'DATE': _to_date, 'BINARY': _to_binary, + 'BLOB': _to_binary, + 'LONGVARBINARY': _to_binary, + 'VARBINARY': _to_binary, 'DECIMAL': _to_double, 'NUMERIC': _to_double, 'DOUBLE': _to_double, 'FLOAT': _to_double, + 'REAL': _to_double, 'INTEGER': _to_int, 'SMALLINT': _to_int, + 'BIGINT': _to_int, + 'TINYINT': _to_int, 'BOOLEAN': _java_to_py('booleanValue'), } From 54946f65953fd3b258a347ac6ea9c7c5b78d32ea Mon Sep 17 00:00:00 2001 From: jesaerys Date: Mon, 29 Feb 2016 22:12:21 -0600 Subject: [PATCH 2/2] return datetime objects instead of strings DB API 2.0 suggests using the datetime module to represent dates and times. Note that parsing the java value string is ~2x faster than 1) using datetime.datetime.strptime on java_val.toString() and 2) constructing datetime.datetime directory with java_val.getYear(), java_val.getMonth(), etc. --- jaydebeapi/__init__.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/jaydebeapi/__init__.py b/jaydebeapi/__init__.py index b284527..5f71ad6 100644 --- a/jaydebeapi/__init__.py +++ b/jaydebeapi/__init__.py @@ -559,22 +559,34 @@ def _to_datetime(rs, col): java_val = rs.getTimestamp(col) if not java_val: return - d = datetime.datetime.strptime(str(java_val)[:19], "%Y-%m-%d %H:%M:%S") - d = d.replace(microsecond=int(str(java_val.getNanos())[:6])) - return str(d) + elements = (java_val + .toString() + .replace('-', ' ') + .replace(':', ' ') + .replace('.', ' ') + .split() + ) + return datetime.datetime(*(int(element) for element in elements)) def _to_time(rs, col): - java_val = rs.getTime(col) + java_val = rs.getTimestamp(col) if not java_val: return - return str(java_val) + elements = (java_val + .toString() + .split()[1] + .replace(':', ' ') + .replace('.', ' ') + .split() + ) + return datetime.time(*(int(element) for element in elements)) def _to_date(rs, col): java_val = rs.getDate(col) if not java_val: return - d = datetime.datetime.strptime(str(java_val)[:10], "%Y-%m-%d") - return d.strftime("%Y-%m-%d") + elements = java_val.toString().split('-') + return datetime.date(*(int(element) for element in elements)) def _to_binary(rs, col): java_val = rs.getObject(col)