44
55from ..error import SqlSyntaxError
66from .builder import BuilderFactory
7+ from .delete_builder import DeleteExecutionPlan
8+ from .delete_handler import DeleteParseResult
79from .handler import BaseHandler , HandlerFactory
810from .insert_builder import InsertExecutionPlan
911from .insert_handler import InsertParseResult
@@ -34,7 +36,8 @@ class MongoSQLParserVisitor(PartiQLParserVisitor):
3436 def __init__ (self ) -> None :
3537 super ().__init__ ()
3638 self ._parse_result = QueryParseResult .for_visitor ()
37- self ._insert_parse_result = InsertParseResult .for_insert_visitor ()
39+ self ._insert_parse_result = InsertParseResult .for_visitor ()
40+ self ._delete_parse_result = DeleteParseResult .for_visitor ()
3841 # Track current statement kind generically so UPDATE/DELETE can reuse this
3942 self ._current_operation : str = "select" # expected values: select | insert | update | delete
4043 self ._handlers = self ._initialize_handlers ()
@@ -47,17 +50,20 @@ def _initialize_handlers(self) -> Dict[str, BaseHandler]:
4750 "from" : HandlerFactory .get_visitor_handler ("from" ),
4851 "where" : HandlerFactory .get_visitor_handler ("where" ),
4952 "insert" : HandlerFactory .get_visitor_handler ("insert" ),
53+ "delete" : HandlerFactory .get_visitor_handler ("delete" ),
5054 }
5155
5256 @property
5357 def parse_result (self ) -> QueryParseResult :
5458 """Get the current parse result"""
5559 return self ._parse_result
5660
57- def parse_to_execution_plan (self ) -> Union [QueryExecutionPlan , InsertExecutionPlan ]:
61+ def parse_to_execution_plan (self ) -> Union [QueryExecutionPlan , InsertExecutionPlan , DeleteExecutionPlan ]:
5862 """Convert the parse result to an execution plan using BuilderFactory."""
5963 if self ._current_operation == "insert" :
6064 return self ._build_insert_plan ()
65+ elif self ._current_operation == "delete" :
66+ return self ._build_delete_plan ()
6167
6268 return self ._build_query_plan ()
6369
@@ -91,6 +97,19 @@ def _build_insert_plan(self) -> InsertExecutionPlan:
9197
9298 return builder .build ()
9399
100+ def _build_delete_plan (self ) -> DeleteExecutionPlan :
101+ """Build a DELETE execution plan from DELETE parsing."""
102+ _logger .debug (
103+ f"Building DELETE plan with collection: { self ._delete_parse_result .collection } , "
104+ f"filters: { self ._delete_parse_result .filter_conditions } "
105+ )
106+ builder = BuilderFactory .create_delete_builder ().collection (self ._delete_parse_result .collection )
107+
108+ if self ._delete_parse_result .filter_conditions :
109+ builder .filter_conditions (self ._delete_parse_result .filter_conditions )
110+
111+ return builder .build ()
112+
94113 def visitRoot (self , ctx : PartiQLParser .RootContext ) -> Any :
95114 """Visit root node and process child nodes"""
96115 _logger .debug ("Starting to parse SQL query" )
@@ -167,6 +186,57 @@ def visitInsertStatementLegacy(self, ctx: PartiQLParser.InsertStatementLegacyCon
167186 return handler .handle_visitor (ctx , self ._insert_parse_result )
168187 return self .visitChildren (ctx )
169188
189+ def visitFromClauseSimpleExplicit (self , ctx : PartiQLParser .FromClauseSimpleExplicitContext ) -> Any :
190+ """Handle FROM clause (explicit form) in DELETE statements."""
191+ if self ._current_operation == "delete" :
192+ handler = self ._handlers .get ("delete" )
193+ if handler :
194+ return handler .handle_from_clause_explicit (ctx , self ._delete_parse_result )
195+ return self .visitChildren (ctx )
196+
197+ def visitFromClauseSimpleImplicit (self , ctx : PartiQLParser .FromClauseSimpleImplicitContext ) -> Any :
198+ """Handle FROM clause (implicit form) in DELETE statements."""
199+ if self ._current_operation == "delete" :
200+ handler = self ._handlers .get ("delete" )
201+ if handler :
202+ return handler .handle_from_clause_implicit (ctx , self ._delete_parse_result )
203+ return self .visitChildren (ctx )
204+
205+ def visitWhereClause (self , ctx : PartiQLParser .WhereClauseContext ) -> Any :
206+ """Handle WHERE clause (generic form used in DELETE, UPDATE)."""
207+ _logger .debug ("Processing WHERE clause (generic)" )
208+ try :
209+ # For DELETE, use the delete handler
210+ if self ._current_operation == "delete" :
211+ handler = self ._handlers .get ("delete" )
212+ if handler :
213+ return handler .handle_where_clause (ctx , self ._delete_parse_result )
214+ return {}
215+ else :
216+ # For other operations, use the where handler
217+ handler = self ._handlers ["where" ]
218+ if handler :
219+ result = handler .handle_visitor (ctx , self ._parse_result )
220+ _logger .debug (f"Extracted filter conditions: { result } " )
221+ return result
222+ return {}
223+ except Exception as e :
224+ _logger .warning (f"Error processing WHERE clause: { e } " )
225+ return {}
226+
227+ def visitDeleteCommand (self , ctx : PartiQLParser .DeleteCommandContext ) -> Any :
228+ """Handle DELETE statements."""
229+ _logger .debug ("Processing DELETE statement" )
230+ self ._current_operation = "delete"
231+ # Reset delete parse result for this statement
232+ self ._delete_parse_result = DeleteParseResult .for_visitor ()
233+ # Use delete handler if available
234+ handler = self ._handlers .get ("delete" )
235+ if handler :
236+ handler .handle_visitor (ctx , self ._delete_parse_result )
237+ # Visit children to process FROM and WHERE clauses
238+ return self .visitChildren (ctx )
239+
170240 def visitOrderByClause (self , ctx : PartiQLParser .OrderByClauseContext ) -> Any :
171241 """Handle ORDER BY clause for sorting"""
172242 _logger .debug ("Processing ORDER BY clause" )
0 commit comments