2121WHERE Artist='Acme Band' AND SongTitle='PartiQL Rocks'
2222"""
2323import logging
24- import re
2524from .dml_sql import DmlBase
25+ from .json_parser import jsonArray , jsonObject
2626from .common import KeyWords , Tokens
27- from pyparsing import Forward , Group , OneOrMore , Opt , Regex
27+ from .util import flatten_list
28+ from pyparsing import (
29+ Forward ,
30+ Group ,
31+ OneOrMore ,
32+ Opt ,
33+ Literal ,
34+ ZeroOrMore ,
35+ )
2836from typing import Any , Dict
2937
3038_logger = logging .getLogger (__name__ ) # type: ignore
3139
3240
3341class DmlUpdate (DmlBase ):
3442
35- # Define SET operation: SET followed by content until next SET/REMOVE/WHERE
43+ _COMMA = Literal ("," )
44+
45+ _COLUMN_UPDATE_RVAL = (jsonObject | jsonArray | DmlBase ._COLUMN_RVAL )(
46+ "column_update_rvalue"
47+ ).set_name ("column_update_rvalue" )
48+
49+ _OPERATION_CONTENT = Group (
50+ DmlBase ._COLUMN + KeyWords .EQUAL_TO + _COLUMN_UPDATE_RVAL
51+ )("op_content" ).set_name ("op_content" )
52+
3653 _SET_OPERATION = Group (
37- KeyWords .SET
38- + Regex (r".*?(?=\s+(?:SET|REMOVE|WHERE))" , re .IGNORECASE | re .DOTALL )(
39- "set_content"
40- ).set_name ("set_content" )
41- )("set_op" )
54+ KeyWords .SET + _OPERATION_CONTENT + ZeroOrMore (_COMMA + _OPERATION_CONTENT )
55+ )("set_op" ).set_name ("set_op" )
4256
43- # Define REMOVE operation: REMOVE followed by content until next SET/REMOVE/WHERE
4457 _REMOVE_OPERATION = Group (
45- KeyWords .REMOVE
46- + Regex (r".*?(?=\s+(?:SET|REMOVE|WHERE))" , re .IGNORECASE | re .DOTALL )(
47- "remove_content"
48- ).set_name ("remove_content" )
49- )("remove_op" )
58+ KeyWords .REMOVE + _OPERATION_CONTENT + ZeroOrMore (_COMMA + _OPERATION_CONTENT )
59+ )("remove_op" ).set_name ("remove_op" )
5060
51- # Multiple SET or REMOVE operations
52- _OPERATIONS = Group (OneOrMore (_SET_OPERATION | _REMOVE_OPERATION ))("operations" )
61+ _OPERATIONS = OneOrMore (_SET_OPERATION | _REMOVE_OPERATION )("operations" ).set_name (
62+ "operations"
63+ )
5364
5465 _UPDATE_STATEMENT = (
5566 KeyWords .UPDATE
@@ -76,15 +87,7 @@ def transform(self) -> Dict[str, Any]:
7687
7788 table_ = '"%s"' % table_name_
7889
79- # Build the operations part from multiple SET/REMOVE operations
80- operations_parts = []
81- for op in operations_ :
82- if "set_op" == op .get_name ():
83- operations_parts .append ("SET %s" % op ["set_content" ].strip ())
84- elif "remove_op" == op .get_name ():
85- operations_parts .append ("REMOVE %s" % op ["remove_content" ].strip ())
86-
87- operations_str = " " .join (operations_parts )
90+ operations_str = " " .join (str (c ) for c in flatten_list (operations_ .as_list ()))
8891
8992 where_conditions = self .root_parse_results .get ("where_conditions" , None )
9093 if where_conditions is not None :
0 commit comments