From 67c8f8a7c684514fa2b463677b406c5022949a12 Mon Sep 17 00:00:00 2001 From: chalupaul Date: Tue, 11 Nov 2025 17:06:57 -0600 Subject: [PATCH] Added text() to raw query so it is bindable. has_table() relies on valid exception handling. Rewrote with proper filtering to return expected error code. --- Makefile | 2 +- pyproject.toml | 2 +- sqlalchemy_aurora_data_api/__init__.py | 12 +++++++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 0d080fc..4b70cf3 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ SHELL=/bin/bash lint: - ruff . + ruff check . test: lint python ./test/test.py -v diff --git a/pyproject.toml b/pyproject.toml index 5d4d966..4cf533e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,4 +1,4 @@ [tool.ruff] line-length=120 -[tool.ruff.per-file-ignores] +[tool.ruff.lint.per-file-ignores] "sqlalchemy_aurora_data_api/__init__.py" = ["E401", "F401"] diff --git a/sqlalchemy_aurora_data_api/__init__.py b/sqlalchemy_aurora_data_api/__init__.py index dc99ad3..83fa637 100644 --- a/sqlalchemy_aurora_data_api/__init__.py +++ b/sqlalchemy_aurora_data_api/__init__.py @@ -4,7 +4,7 @@ import json, datetime, re -from sqlalchemy import cast, func, util +from sqlalchemy import cast, func, util, text import sqlalchemy.sql.sqltypes as sqltypes from sqlalchemy.dialects.postgresql.base import PGDialect from sqlalchemy.dialects.postgresql import JSON, JSONB, UUID, DATE, TIME, TIMESTAMP, ARRAY, ENUM @@ -144,10 +144,16 @@ def import_dbapi(cls): return aurora_data_api def _detect_charset(self, connection): - return connection.execute("SHOW VARIABLES LIKE 'character_set_client'").fetchone()[1] + return connection.execute(text("SHOW VARIABLES LIKE 'character_set_client'")).fetchone()[1] def _extract_error_code(self, exception): - return exception.args[0].value + error_str = str(exception.args[0]) + match = re.search(r"Error code:\s*(\d+);\s*SQLState:\s*([A-Z0-9]+)", error_str) + error_code = None + if match: + error_code = int(match.group(1)) + # sql_state = match.group(2) + return error_code class AuroraPostgresDataAPIDialect(PGDialect):