|
10 | 10 | from sqlalchemy.dialects.postgresql.base import PGDDLCompiler |
11 | 11 | from sqlalchemy.dialects.postgresql.base import PGIdentifierPreparer |
12 | 12 | from sqlalchemy.dialects.postgresql.base import PGTypeCompiler |
| 13 | +from sqlalchemy.dialects.postgresql.base import PGExecutionContext |
| 14 | +from sqlalchemy.dialects.postgresql import DATE as _PG_DATE |
| 15 | +from sqlalchemy.dialects.postgresql import BOOLEAN as _PG_BOOLEAN |
13 | 16 | from sqlalchemy.exc import NoSuchTableError |
14 | 17 | from sqlalchemy import schema as sa_schema |
15 | 18 | from sqlalchemy import types as sqltypes |
|
19 | 22 | from sqlalchemy.sql import operators |
20 | 23 | from sqlalchemy.sql.compiler import OPERATORS |
21 | 24 |
|
| 25 | +import datetime as _dt |
| 26 | + |
| 27 | + |
| 28 | +class _GaussDBOdbcDate(sqltypes.TypeDecorator): |
| 29 | + """Date type that normalises ODBC driver datetime returns to date. |
| 30 | +
|
| 31 | + The GaussDB ODBC driver on Windows returns ``datetime.datetime`` |
| 32 | + for DATE columns instead of ``datetime.date``. pyodbc's |
| 33 | + ``add_output_converter`` cannot fix this because the driver already |
| 34 | + converted the raw bytes to a Python object before pyodbc sees it. |
| 35 | +
|
| 36 | + This TypeDecorator wraps the standard PG DATE type and adds a |
| 37 | + result processor that strips the time component. |
| 38 | + """ |
| 39 | + |
| 40 | + impl = _PG_DATE |
| 41 | + cache_ok = True |
| 42 | + |
| 43 | + def process_result_value(self, value, dialect): |
| 44 | + if value is None: |
| 45 | + return value |
| 46 | + if isinstance(value, _dt.datetime): |
| 47 | + return value.date() |
| 48 | + if isinstance(value, _dt.date): |
| 49 | + return value |
| 50 | + return value |
| 51 | + |
| 52 | + |
| 53 | +class _GaussDBOdbcBoolean(sqltypes.TypeDecorator): |
| 54 | + """Boolean type that normalises ODBC driver integer returns to bool. |
| 55 | +
|
| 56 | + The GaussDB ODBC driver returns ``int`` (1/0) for boolean columns. |
| 57 | + SQLAlchemy's default ``Boolean.result_processor`` is a no-op when |
| 58 | + ``supports_native_boolean`` is True, so we must add our own. |
| 59 | + """ |
| 60 | + |
| 61 | + impl = _PG_BOOLEAN |
| 62 | + cache_ok = True |
| 63 | + |
| 64 | + def process_result_value(self, value, dialect): |
| 65 | + if value is None: |
| 66 | + return value |
| 67 | + if isinstance(value, bool): |
| 68 | + return value |
| 69 | + if isinstance(value, (bytes, bytearray)): |
| 70 | + value = value.decode("utf-8") |
| 71 | + if isinstance(value, str): |
| 72 | + return value.strip().lower() in ("1", "t", "true", "y") |
| 73 | + return bool(value) |
| 74 | + |
22 | 75 |
|
23 | 76 | class GaussDBCompiler(PGCompiler): |
24 | 77 | def visit_concat_op_binary(self, binary, operator_, **kw): |
@@ -173,9 +226,6 @@ def visit_large_binary(self, type_, **kw): |
173 | 226 | return super().visit_large_binary(type_, **kw) |
174 | 227 |
|
175 | 228 |
|
176 | | -from sqlalchemy.dialects.postgresql.base import PGExecutionContext |
177 | | - |
178 | | - |
179 | 229 | class GaussDBMExecutionContext(PGExecutionContext): |
180 | 230 | def get_lastrowid(self): |
181 | 231 | try: |
@@ -218,9 +268,13 @@ class GaussDBDialect(PGDialect): |
218 | 268 | gaussdb_compatibility = None |
219 | 269 |
|
220 | 270 | # Register GaussDB M-compat binary types for reflection. |
| 271 | + # Override date/boolean with ODBC-aware variants that normalise |
| 272 | + # driver returns (datetime→date, int→bool). |
221 | 273 | ischema_names = dict(PGDialect.ischema_names) |
222 | 274 | ischema_names["blob"] = LargeBinary |
223 | 275 | ischema_names["longblob"] = LargeBinary |
| 276 | + ischema_names["date"] = _GaussDBOdbcDate |
| 277 | + ischema_names["boolean"] = _GaussDBOdbcBoolean |
224 | 278 |
|
225 | 279 | def initialize(self, connection): |
226 | 280 | super().initialize(connection) |
|
0 commit comments