Skip to content

Commit 1cba814

Browse files
committed
Fix NameError: translator not defined and ensure consistent session handling
- Fix critical NameError in _execute_embedded_async. - Ensure session_id is passed to all IRISExecutor calls in protocol handler. - Refactor translator usage to be consistent across execution paths. - Add configurable connection pooling and LRU query caching. - Verify transaction visibility with regression tests.
1 parent cba9e84 commit 1cba814

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

src/iris_pgwire/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
caretdev/sqlalchemy-iris.
77
"""
88

9-
__version__ = "1.2.8"
9+
__version__ = "1.2.9"
1010
__author__ = "Thomas Dyar <thomas.dyar@intersystems.com>"
1111

1212
# Don't import server/protocol in __init__ to avoid sys.modules conflicts

src/iris_pgwire/iris_executor.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -796,9 +796,8 @@ def _sync_execute_many():
796796

797797
try:
798798
# Feature 022: Apply PostgreSQL→IRIS transaction verb translation
799-
transaction_translator = TransactionTranslator()
800-
transaction_translated_sql = transaction_translator.translate_transaction_command(
801-
sql
799+
transaction_translated_sql = (
800+
self.transaction_translator.translate_transaction_command(sql)
802801
)
803802

804803
# Feature 021: Apply PostgreSQL→IRIS SQL normalization
@@ -920,12 +919,11 @@ def _sync_execute_many():
920919

921920
try:
922921
# Get pooled connection
923-
connection = self._get_pooled_connection()
922+
connection = self._get_pooled_connection(session_id=session_id)
924923

925924
# Feature 022: Apply PostgreSQL→IRIS transaction verb translation
926-
transaction_translator = TransactionTranslator()
927-
transaction_translated_sql = transaction_translator.translate_transaction_command(
928-
sql
925+
transaction_translated_sql = (
926+
self.transaction_translator.translate_transaction_command(sql)
929927
)
930928

931929
# Feature 021: Apply PostgreSQL→IRIS SQL normalization
@@ -1080,7 +1078,13 @@ def _split_multi_row_insert(self, sql: str) -> list[str]:
10801078

10811079
return statements
10821080

1083-
def _safe_execute(self, sql: str, params: list | None = None, is_embedded: bool = True) -> Any:
1081+
def _safe_execute(
1082+
self,
1083+
sql: str,
1084+
params: list | None = None,
1085+
is_embedded: bool = True,
1086+
session_id: str | None = None,
1087+
) -> Any:
10841088
"""Execute SQL with DDL idempotency handling."""
10851089
import iris
10861090

@@ -1127,7 +1131,7 @@ def __iter__(self):
11271131

11281132
else:
11291133
# External mode - use DBAPI cursor
1130-
connection = self._get_pooled_connection()
1134+
connection = self._get_pooled_connection(session_id=session_id)
11311135
cursor = connection.cursor()
11321136
try:
11331137
if params:
@@ -2854,9 +2858,8 @@ def _sync_execute():
28542858
self._get_iris_connection()
28552859

28562860
# 1. Transaction Translation
2857-
transaction_translator = TransactionTranslator()
2858-
transaction_translated_sql = transaction_translator.translate_transaction_command(
2859-
sql
2861+
transaction_translated_sql = (
2862+
self.transaction_translator.translate_transaction_command(sql)
28602863
)
28612864

28622865
# 2. SQL Normalization
@@ -2866,7 +2869,7 @@ def _sync_execute():
28662869
optimized_sql = normalized_sql
28672870

28682871
# Log transaction translation metrics
2869-
txn_metrics = transaction_translator.get_translation_metrics()
2872+
txn_metrics = self.transaction_translator.get_translation_metrics()
28702873
logger.info(
28712874
"Transaction verb translation applied",
28722875
total_translations=txn_metrics["total_translations"],
@@ -2878,7 +2881,7 @@ def _sync_execute():
28782881
)
28792882

28802883
# Log normalization metrics
2881-
norm_metrics = translator.get_normalization_metrics()
2884+
norm_metrics = self.sql_translator.get_normalization_metrics()
28822885
logger.info(
28832886
"SQL normalization applied",
28842887
identifiers_normalized=norm_metrics["identifier_count"],
@@ -3078,17 +3081,23 @@ def _sync_execute():
30783081
f"Executing intermediate statement: {stmt[:80]}...",
30793082
session_id=session_id,
30803083
)
3081-
self._safe_execute(stmt, optimized_params, is_embedded=True)
3084+
self._safe_execute(
3085+
stmt, optimized_params, is_embedded=True, session_id=session_id
3086+
)
30823087

30833088
# Execute last statement and capture results
30843089
last_stmt = statements[-1]
30853090
logger.debug(
30863091
f"Executing final statement: {last_stmt[:80]}...", session_id=session_id
30873092
)
3088-
result = self._safe_execute(last_stmt, optimized_params, is_embedded=True)
3093+
result = self._safe_execute(
3094+
last_stmt, optimized_params, is_embedded=True, session_id=session_id
3095+
)
30893096
else:
30903097
# Single statement - execute normally
3091-
result = self._safe_execute(optimized_sql, optimized_params, is_embedded=True)
3098+
result = self._safe_execute(
3099+
optimized_sql, optimized_params, is_embedded=True, session_id=session_id
3100+
)
30923101

30933102
# RETURNING emulation: After INSERT/UPDATE/DELETE, fetch the affected row(s)
30943103
if returning_operation and returning_columns:
@@ -3684,10 +3693,14 @@ def _sync_external_execute():
36843693

36853694
# Execute all statements except the last
36863695
for stmt in statements[:-1]:
3687-
self._safe_execute(stmt, optimized_params, is_embedded=False)
3696+
self._safe_execute(
3697+
stmt, optimized_params, is_embedded=False, session_id=session_id
3698+
)
36883699

36893700
# Execute last statement and capture cursor
3690-
cursor = self._safe_execute(statements[-1], optimized_params, is_embedded=False)
3701+
cursor = self._safe_execute(
3702+
statements[-1], optimized_params, is_embedded=False, session_id=session_id
3703+
)
36913704

36923705
# RETURNING emulation
36933706
if returning_operation and returning_columns:

src/iris_pgwire/protocol.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,15 +1299,15 @@ async def _handle_single_statement(self, query: str, send_ready: bool = True):
12991299
# Handle transaction commands first (no IRIS execution needed)
13001300
query_upper = query.upper().strip()
13011301
if query_upper in ("BEGIN", "START TRANSACTION"):
1302-
await self.iris_executor.begin_transaction()
1302+
await self.iris_executor.begin_transaction(session_id=self.connection_id)
13031303
await self.send_transaction_response("BEGIN", send_ready=send_ready)
13041304
return
13051305
elif query_upper in ("COMMIT", "END"):
1306-
await self.iris_executor.commit_transaction()
1306+
await self.iris_executor.commit_transaction(session_id=self.connection_id)
13071307
await self.send_transaction_response("COMMIT", send_ready=send_ready)
13081308
return
13091309
elif query_upper == "ROLLBACK":
1310-
await self.iris_executor.rollback_transaction()
1310+
await self.iris_executor.rollback_transaction(session_id=self.connection_id)
13111311
await self.send_transaction_response("ROLLBACK", send_ready=send_ready)
13121312
return
13131313

@@ -1373,7 +1373,9 @@ async def _handle_single_statement(self, query: str, send_ready: bool = True):
13731373
)
13741374

13751375
# Execute translated SQL against IRIS
1376-
result = await self.iris_executor.execute_query(final_sql)
1376+
result = await self.iris_executor.execute_query(
1377+
final_sql, session_id=self.connection_id
1378+
)
13771379

13781380
# Add translation metadata to result for debugging/monitoring
13791381
if translation_result.get("translation_used"):
@@ -2379,7 +2381,7 @@ async def flush_batch(self):
23792381
return
23802382

23812383
await self.iris_executor.execute_many(
2382-
sql_to_exec, params_to_exec, session_id=f"batch_{self.connection_id}"
2384+
sql_to_exec, params_to_exec, session_id=self.connection_id
23832385
)
23842386
except Exception as e:
23852387
logger.error("Batch flush failed", connection_id=self.connection_id, error=str(e))
@@ -3035,7 +3037,9 @@ async def handle_describe_message(self, body: bytes):
30353037
param_count=param_count,
30363038
)
30373039

3038-
result = await self.iris_executor.execute_query(query, params=dummy_params)
3040+
result = await self.iris_executor.execute_query(
3041+
query, params=dummy_params, session_id=self.connection_id
3042+
)
30393043

30403044
if result.get("success") and result.get("columns"):
30413045
await self.send_row_description(result["columns"])
@@ -3105,7 +3109,9 @@ async def handle_describe_message(self, body: bytes):
31053109
) or self.iris_executor.sql_parser.is_show_statement(query):
31063110
try:
31073111
result = await self.iris_executor.execute_query(
3108-
query, params=portal.get("params", [])
3112+
query,
3113+
params=portal.get("params", []),
3114+
session_id=self.connection_id,
31093115
)
31103116
if result.get("success") and result.get("columns"):
31113117
result_formats = portal.get("result_formats", [])
@@ -3240,13 +3246,13 @@ async def handle_execute_message(self, body: bytes):
32403246
)
32413247

32423248
if transaction_type == "BEGIN":
3243-
await self.iris_executor.begin_transaction()
3249+
await self.iris_executor.begin_transaction(session_id=self.connection_id)
32443250
await self.send_transaction_response_extended_protocol("BEGIN")
32453251
elif transaction_type == "COMMIT":
3246-
await self.iris_executor.commit_transaction()
3252+
await self.iris_executor.commit_transaction(session_id=self.connection_id)
32473253
await self.send_transaction_response_extended_protocol("COMMIT")
32483254
elif transaction_type == "ROLLBACK":
3249-
await self.iris_executor.rollback_transaction()
3255+
await self.iris_executor.rollback_transaction(session_id=self.connection_id)
32503256
await self.send_transaction_response_extended_protocol("ROLLBACK")
32513257
return
32523258

@@ -3319,7 +3325,7 @@ async def handle_execute_message(self, body: bytes):
33193325
await self.flush_batch()
33203326

33213327
result = await self.iris_executor.execute_query(
3322-
query, params=params if params else None
3328+
query, params=params if params else None, session_id=self.connection_id
33233329
)
33243330

33253331
if result["success"]:

0 commit comments

Comments
 (0)