Skip to content

Commit 18a8d3f

Browse files
authored
Fix timestamp bug on jaydebeapi (#92)
* Fix timestamp bug on jaydebeapi * ignore coverage
1 parent dde8601 commit 18a8d3f

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

local_data_api/resources/jdbc/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@
3636
TIMESTAMP = [JDBCType.TIMESTAMP, JDBCType.TIMESTAMP_WITH_TIMEZONE]
3737

3838

39+
def _fixed_to_datetime(rs: Any, col: Any) -> Optional[str]: # pragma: no cover
40+
"""
41+
jaydebeapi has a bug that can't be parsed datetime correctly.
42+
This workaround will be removed when the PR is merged.
43+
https://github.com/baztian/jaydebeapi/pull/177
44+
"""
45+
java_val = rs.getTimestamp(col)
46+
if not java_val:
47+
return None
48+
import datetime
49+
50+
d = datetime.datetime.strptime(str(java_val)[:19], "%Y-%m-%d %H:%M:%S")
51+
d = d.replace(microsecond=int(str(java_val.getNanos()).zfill(9)[:6]))
52+
return str(d)
53+
54+
55+
jaydebeapi._DEFAULT_CONVERTERS['TIMESTAMP'] = _fixed_to_datetime
56+
57+
3958
def attach_thread_to_jvm() -> None:
4059
"https://github.com/baztian/jaydebeapi/issues/14#issuecomment-261489331"
4160

local_data_api/resources/jdbc/mysql.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from typing import Any, Optional
22

3-
import jaydebeapi
43
from sqlalchemy.dialects import mysql
54
from sqlalchemy.engine import Dialect
65

76
from local_data_api.models import Field
8-
from local_data_api.resources.jdbc import JDBC
7+
from local_data_api.resources.jdbc import JDBC, jaydebeapi
98
from local_data_api.resources.resource import register_resource_type
109

1110

local_data_api/resources/jdbc/postgres.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from typing import Any, Optional, Tuple
22

3-
import jaydebeapi
4-
from jpype import java
53
from sqlalchemy.dialects import postgresql
64
from sqlalchemy.engine import Dialect
75

86
from local_data_api.models import Field
9-
from local_data_api.resources.jdbc import JDBC
7+
from local_data_api.resources.jdbc import JDBC, jaydebeapi
108
from local_data_api.resources.resource import register_resource_type
119

1210
PG_TYPES: Tuple[str, ...] = (

scripts/integration-test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ function test {
100100
--resource-arn $RDS_DATA_API_CLIENT_RESOURCE_ARN \
101101
--secret-arn $RDS_DATA_API_CLIENT_SECRETARN \
102102
--include-result-metadata \
103-
--sql $'SELECT CAST(\'2021-03-10 22:41:04.968123+02\' AS TIMESTAMPTZ) AS value' \
104-
| jq -e '.records[0][0].stringValue == "2021-03-10 20:41:04.968"'
103+
--sql $'SELECT CAST(\'2021-03-10 22:41:04.068123+02\' AS TIMESTAMPTZ) AS value' \
104+
| jq -e '.records[0][0].stringValue == "2021-03-10 20:41:04.068"'
105105
fi
106106

107107
if [ "$db" = "mysql" ]

0 commit comments

Comments
 (0)