@@ -101,11 +101,23 @@ def _replace_placeholders(self, obj: Any, parameters: Sequence[Any]) -> Any:
101101 def _execute_execution_plan (
102102 self ,
103103 execution_plan : QueryExecutionPlan ,
104- db : Any ,
104+ connection : Any = None ,
105105 parameters : Optional [Sequence [Any ]] = None ,
106106 ) -> Optional [Dict [str , Any ]]:
107- """Execute a QueryExecutionPlan against MongoDB using db.command"""
107+ """Execute a QueryExecutionPlan against MongoDB using db.command
108+
109+ Args:
110+ execution_plan: QueryExecutionPlan to execute
111+ connection: Connection object (for session and database access)
112+ parameters: Parameters for placeholder replacement
113+ """
108114 try :
115+ # Get database from connection
116+ if not connection :
117+ raise OperationalError ("No connection provided" )
118+
119+ db = connection .database
120+
109121 # Get database
110122 if not execution_plan .collection :
111123 raise ProgrammingError ("No collection specified in query" )
@@ -144,8 +156,11 @@ def _execute_execution_plan(
144156
145157 _logger .debug (f"Executing MongoDB command: { find_command } " )
146158
147- # Execute find command directly
148- result = db .command (find_command )
159+ # Execute find command with session if in transaction
160+ if connection and connection .session and connection .session .in_transaction :
161+ result = db .command (find_command , session = connection .session )
162+ else :
163+ result = db .command (find_command )
149164
150165 # Create command result
151166 return result
@@ -182,7 +197,7 @@ def execute(
182197 # Parse the query
183198 self ._execution_plan = self ._parse_sql (processed_query )
184199
185- return self ._execute_execution_plan (self ._execution_plan , connection . database , processed_params )
200+ return self ._execute_execution_plan (self ._execution_plan , connection , processed_params )
186201
187202
188203class InsertExecution (ExecutionStrategy ):
@@ -224,10 +239,16 @@ def _replace_placeholders(
224239 def _execute_execution_plan (
225240 self ,
226241 execution_plan : InsertExecutionPlan ,
227- db : Any ,
242+ connection : Any = None ,
228243 parameters : Optional [Union [Sequence [Any ], Dict [str , Any ]]] = None ,
229244 ) -> Optional [Dict [str , Any ]]:
230245 try :
246+ # Get database from connection
247+ if not connection :
248+ raise OperationalError ("No connection provided" )
249+
250+ db = connection .database
251+
231252 if not execution_plan .collection :
232253 raise ProgrammingError ("No collection specified in insert" )
233254
@@ -238,7 +259,11 @@ def _execute_execution_plan(
238259
239260 _logger .debug (f"Executing MongoDB insert command: { command } " )
240261
241- return db .command (command )
262+ # Execute with session if in transaction
263+ if connection and connection .session and connection .session .in_transaction :
264+ return db .command (command , session = connection .session )
265+ else :
266+ return db .command (command )
242267 except PyMongoError as e :
243268 _logger .error (f"MongoDB insert failed: { e } " )
244269 raise DatabaseError (f"Insert execution failed: { e } " )
@@ -259,7 +284,7 @@ def execute(
259284
260285 self ._execution_plan = self ._parse_sql (context .query )
261286
262- return self ._execute_execution_plan (self ._execution_plan , connection . database , parameters )
287+ return self ._execute_execution_plan (self ._execution_plan , connection , parameters )
263288
264289
265290class DeleteExecution (ExecutionStrategy ):
@@ -293,10 +318,16 @@ def _parse_sql(self, sql: str) -> Any:
293318 def _execute_execution_plan (
294319 self ,
295320 execution_plan : Any ,
296- db : Any ,
321+ connection : Any = None ,
297322 parameters : Optional [Union [Sequence [Any ], Dict [str , Any ]]] = None ,
298323 ) -> Optional [Dict [str , Any ]]:
299324 try :
325+ # Get database from connection
326+ if not connection :
327+ raise OperationalError ("No connection provided" )
328+
329+ db = connection .database
330+
300331 if not execution_plan .collection :
301332 raise ProgrammingError ("No collection specified in delete" )
302333
@@ -312,7 +343,11 @@ def _execute_execution_plan(
312343
313344 _logger .debug (f"Executing MongoDB delete command: { command } " )
314345
315- return db .command (command )
346+ # Execute with session if in transaction
347+ if connection and connection .session and connection .session .in_transaction :
348+ return db .command (command , session = connection .session )
349+ else :
350+ return db .command (command )
316351 except PyMongoError as e :
317352 _logger .error (f"MongoDB delete failed: { e } " )
318353 raise DatabaseError (f"Delete execution failed: { e } " )
@@ -333,7 +368,7 @@ def execute(
333368
334369 self ._execution_plan = self ._parse_sql (context .query )
335370
336- return self ._execute_execution_plan (self ._execution_plan , connection . database , parameters )
371+ return self ._execute_execution_plan (self ._execution_plan , connection , parameters )
337372
338373
339374class UpdateExecution (ExecutionStrategy ):
@@ -367,10 +402,16 @@ def _parse_sql(self, sql: str) -> Any:
367402 def _execute_execution_plan (
368403 self ,
369404 execution_plan : Any ,
370- db : Any ,
405+ connection : Any = None ,
371406 parameters : Optional [Union [Sequence [Any ], Dict [str , Any ]]] = None ,
372407 ) -> Optional [Dict [str , Any ]]:
373408 try :
409+ # Get database from connection
410+ if not connection :
411+ raise OperationalError ("No connection provided" )
412+
413+ db = connection .database
414+
374415 if not execution_plan .collection :
375416 raise ProgrammingError ("No collection specified in update" )
376417
@@ -406,7 +447,11 @@ def _execute_execution_plan(
406447
407448 _logger .debug (f"Executing MongoDB update command: { command } " )
408449
409- return db .command (command )
450+ # Execute with session if in transaction
451+ if connection and connection .session and connection .session .in_transaction :
452+ return db .command (command , session = connection .session )
453+ else :
454+ return db .command (command )
410455 except PyMongoError as e :
411456 _logger .error (f"MongoDB update failed: { e } " )
412457 raise DatabaseError (f"Update execution failed: { e } " )
@@ -427,7 +472,7 @@ def execute(
427472
428473 self ._execution_plan = self ._parse_sql (context .query )
429474
430- return self ._execute_execution_plan (self ._execution_plan , connection . database , parameters )
475+ return self ._execute_execution_plan (self ._execution_plan , connection , parameters )
431476
432477
433478class ExecutionPlanFactory :
0 commit comments