Skip to content
This repository was archived by the owner on Oct 26, 2022. It is now read-only.

Commit 4a18abd

Browse files
committed
add a parameter to control whether the parse follows the native rules fix #7
1 parent cbe677f commit 4a18abd

File tree

5 files changed

+67
-27
lines changed

5 files changed

+67
-27
lines changed

mybatis_mapper2sql/convert.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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:

mybatis_mapper2sql/generate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def get_statement(mybatis_mapper, result_type='raw', **kwargs):
2828
"""
2929
Get SQL Statements from Mapper
3030
:param mybatis_mapper:
31-
:param kwargs: sqlparse format kwargs
31+
:param kwargs: sqlparse format kwargs /native: parse follow the native rules
3232
:param result_type: raw|list
3333
:return:
3434
"""
@@ -64,10 +64,10 @@ def get_child_statement(mybatis_mapper, child_id, **kwargs):
6464
# get sql
6565
statement = ''
6666
child = mybatis_mapper.get(child_id)
67-
statement += convert_children(mybatis_mapper, child)
67+
statement += convert_children(mybatis_mapper, child, **kwargs)
6868
# The child element has children
6969
for next_child in child:
70-
statement += convert_children(mybatis_mapper, next_child)
70+
statement += convert_children(mybatis_mapper, next_child, **kwargs)
7171
return sqlparse.format(statement, **kwargs)
7272

7373

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def run_tests(self):
2525

2626
setup(
2727
name='mybatis-mapper2sql',
28-
version='0.1.8',
28+
version='0.1.9',
2929
author='hhyo',
3030
author_email='[email protected]',
3131
url='http://github.com/hhyo/mybatis-mapper2sql',

tests/test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ def test_bind(self):
9090
self.statement = mybatis_mapper2sql.get_child_statement(self.mapper, child_id=self.child_id, reindent=True)
9191
print(self.statement)
9292

93+
def test_choose_native(self):
94+
self.child_id = 'testChooseNative'
95+
print("============{}============".format(self.child_id))
96+
self.statement = mybatis_mapper2sql.get_child_statement(self.mapper, child_id=self.child_id,
97+
reindent=True, native=True)
98+
print(self.statement)
99+
93100

94101
if __name__ == '__main__':
95102
unittest.main()

tests/test.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,28 @@
215215
</if>
216216
</trim>
217217
</insert>
218+
<select id="testChooseNative">
219+
SELECT
220+
name,
221+
category,
222+
price
223+
FROM
224+
fruits
225+
<where>
226+
<choose>
227+
<when test="name != null">
228+
AND name = #{name}
229+
</when>
230+
<when test="category == 'banana'">
231+
AND category = #{category}
232+
<if test="price != null and price !=''">
233+
AND price = ${price}
234+
</if>
235+
</when>
236+
<otherwise>
237+
AND category = 'apple'
238+
</otherwise>
239+
</choose>
240+
</where>
241+
</select>
218242
</mapper>

0 commit comments

Comments
 (0)