@@ -10,23 +10,23 @@ def convert_children(mybatis_mapper, child, **kwargs):
1010 Get children info
1111 :param mybatis_mapper:
1212 :param child:
13- :param kwargs:
13+ :param kwargs: native: parse follow the native rules
1414 :return:
1515 """
1616 if child .tag in query_types :
1717 return convert_parameters (child , text = True , tail = True )
1818 elif child .tag == 'include' :
19- return convert_include (mybatis_mapper , child , properties = kwargs . get ( 'properties' ) )
19+ return convert_include (mybatis_mapper , child , ** kwargs )
2020 elif child .tag == 'if' :
21- return convert_if (mybatis_mapper , child )
21+ return convert_if (mybatis_mapper , child , ** kwargs )
2222 elif child .tag in ('choose' , 'when' , 'otherwise' ):
23- return convert_choose_when_otherwise (mybatis_mapper , child )
23+ return convert_choose_when_otherwise (mybatis_mapper , child , ** kwargs )
2424 elif child .tag in ('trim' , 'where' , 'set' ):
25- return convert_trim_where_set (mybatis_mapper , child )
25+ return convert_trim_where_set (mybatis_mapper , child , ** kwargs )
2626 elif child .tag == 'foreach' :
27- return convert_foreach (mybatis_mapper , child )
27+ return convert_foreach (mybatis_mapper , child , ** kwargs )
2828 elif child .tag == 'bind' :
29- return convert_bind (child )
29+ return convert_bind (child , ** kwargs )
3030 else :
3131 return ''
3232
@@ -39,7 +39,7 @@ def convert_parameters(child, text=False, tail=False):
3939 :param tail:
4040 :return:
4141 """
42- p = re .compile ('\S' )
42+ p = re .compile (r '\S' )
4343 # Remove empty info
4444 child_text = child .text if child .text else ''
4545 child_tail = child .tail if child .tail else ''
@@ -66,9 +66,9 @@ def convert_parameters(child, text=False, tail=False):
6666 return convert_string
6767
6868
69- def convert_include (mybatis_mapper , child , properties = None ):
69+ def convert_include (mybatis_mapper , child , ** kwargs ):
7070 # Add Properties
71- properties = properties if properties else dict ()
71+ properties = kwargs . get ( ' properties' ) if kwargs . get ( ' properties' ) else dict ()
7272 for next_child in child :
7373 if next_child .tag == 'property' :
7474 properties [next_child .attrib .get ('name' )] = next_child .attrib .get ('value' )
@@ -85,40 +85,49 @@ def convert_include(mybatis_mapper, child, properties=None):
8585 # add include text
8686 convert_string += convert_parameters (child , text = True )
8787 for next_child in include_child :
88- convert_string += convert_children (mybatis_mapper , next_child , properties = properties )
88+ kwargs ['properties' ] = properties
89+ convert_string += convert_children (mybatis_mapper , next_child , ** kwargs )
8990 # add include tail
9091 convert_string += convert_parameters (child , tail = True )
9192 return convert_string
9293
9394
94- def convert_if (mybatis_mapper , child ):
95+ def convert_if (mybatis_mapper , child , ** kwargs ):
9596 convert_string = ''
9697 test = child .attrib .get ('test' )
9798 # Add if text
9899 convert_string += convert_parameters (child , text = True )
99100 for next_child in child :
100- convert_string += convert_children (mybatis_mapper , next_child )
101+ convert_string += convert_children (mybatis_mapper , next_child , ** kwargs )
101102 convert_string += '-- if(' + test + ')\n '
102103 # Add if tail
103104 convert_string += convert_parameters (child , tail = True )
104105 return convert_string
105106
106107
107- def convert_choose_when_otherwise (mybatis_mapper , child ):
108+ def convert_choose_when_otherwise (mybatis_mapper , child , ** kwargs ):
109+ # native
110+ native = kwargs .get ('native' )
111+ when_element_cnt = kwargs .get ('when_element_cnt' , 0 )
108112 convert_string = ''
109113 for next_child in child :
110114 if next_child .tag == 'when' :
111- test = next_child .attrib .get ('test' )
112- convert_string += convert_parameters (next_child , text = True , tail = True )
113- convert_string += '-- if(' + test + ')'
115+ if native and when_element_cnt >= 1 :
116+ break
117+ else :
118+ test = next_child .attrib .get ('test' )
119+ convert_string += convert_parameters (next_child , text = True , tail = True )
120+ convert_string += '-- if(' + test + ')'
121+ when_element_cnt += 1
122+ kwargs ['when_element_cnt' ] = when_element_cnt
114123 elif next_child .tag == 'otherwise' :
115124 convert_string += convert_parameters (next_child , text = True , tail = True )
116125 convert_string += '-- otherwise'
117- convert_string += convert_children (mybatis_mapper , next_child )
126+ convert_string += convert_children (mybatis_mapper , next_child , ** kwargs )
118127 return convert_string
119128
120129
121- def convert_trim_where_set (mybatis_mapper , child ):
130+ def convert_trim_where_set (mybatis_mapper , child , ** kwargs ):
122131 if child .tag == 'trim' :
123132 prefix = child .attrib .get ('prefix' )
124133 suffix = child .attrib .get ('suffix' )
@@ -142,7 +151,7 @@ def convert_trim_where_set(mybatis_mapper, child):
142151 convert_string += convert_parameters (child , text = True )
143152 # Convert children first
144153 for next_child in child :
145- convert_string += convert_children (mybatis_mapper , next_child )
154+ convert_string += convert_children (mybatis_mapper , next_child , ** kwargs )
146155 # Remove prefixOverrides
147156 if prefix_overrides :
148157 regex = r'^[\s]*?({})' .format (prefix_overrides )
@@ -162,7 +171,7 @@ def convert_trim_where_set(mybatis_mapper, child):
162171 return convert_string
163172
164173
165- def convert_foreach (mybatis_mapper , child ):
174+ def convert_foreach (mybatis_mapper , child , ** kwargs ):
166175 collection = child .attrib .get ('collection' )
167176 item = child .attrib .get ('item' )
168177 index = child .attrib .get ('index' )
@@ -173,15 +182,15 @@ def convert_foreach(mybatis_mapper, child):
173182 # Add foreach text
174183 convert_string += convert_parameters (child , text = True )
175184 for next_child in child :
176- convert_string += convert_children (mybatis_mapper , next_child )
185+ convert_string += convert_children (mybatis_mapper , next_child , ** kwargs )
177186 # Add two items
178187 convert_string = open + convert_string + separator + convert_string + close
179188 # Add foreach tail
180189 convert_string += convert_parameters (child , tail = True )
181190 return convert_string
182191
183192
184- def convert_bind (child ):
193+ def convert_bind (child , ** kwargs ):
185194 """
186195 :param child:
187196 :return:
0 commit comments