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

Commit

Permalink
add a parameter to control whether the parse follows the native rules f…
Browse files Browse the repository at this point in the history
…ix #7
  • Loading branch information
hhyo committed Aug 25, 2019
1 parent cbe677f commit 4a18abd
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 27 deletions.
55 changes: 32 additions & 23 deletions mybatis_mapper2sql/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ def convert_children(mybatis_mapper, child, **kwargs):
Get children info
:param mybatis_mapper:
:param child:
:param kwargs:
:param kwargs: native: parse follow the native rules
:return:
"""
if child.tag in query_types:
return convert_parameters(child, text=True, tail=True)
elif child.tag == 'include':
return convert_include(mybatis_mapper, child, properties=kwargs.get('properties'))
return convert_include(mybatis_mapper, child, **kwargs)
elif child.tag == 'if':
return convert_if(mybatis_mapper, child)
return convert_if(mybatis_mapper, child, **kwargs)
elif child.tag in ('choose', 'when', 'otherwise'):
return convert_choose_when_otherwise(mybatis_mapper, child)
return convert_choose_when_otherwise(mybatis_mapper, child, **kwargs)
elif child.tag in ('trim', 'where', 'set'):
return convert_trim_where_set(mybatis_mapper, child)
return convert_trim_where_set(mybatis_mapper, child, **kwargs)
elif child.tag == 'foreach':
return convert_foreach(mybatis_mapper, child)
return convert_foreach(mybatis_mapper, child, **kwargs)
elif child.tag == 'bind':
return convert_bind(child)
return convert_bind(child, **kwargs)
else:
return ''

Expand All @@ -39,7 +39,7 @@ def convert_parameters(child, text=False, tail=False):
:param tail:
:return:
"""
p = re.compile('\S')
p = re.compile(r'\S')
# Remove empty info
child_text = child.text if child.text else ''
child_tail = child.tail if child.tail else ''
Expand All @@ -66,9 +66,9 @@ def convert_parameters(child, text=False, tail=False):
return convert_string


def convert_include(mybatis_mapper, child, properties=None):
def convert_include(mybatis_mapper, child, **kwargs):
# Add Properties
properties = properties if properties else dict()
properties = kwargs.get('properties') if kwargs.get('properties') else dict()
for next_child in child:
if next_child.tag == 'property':
properties[next_child.attrib.get('name')] = next_child.attrib.get('value')
Expand All @@ -85,40 +85,49 @@ def convert_include(mybatis_mapper, child, properties=None):
# add include text
convert_string += convert_parameters(child, text=True)
for next_child in include_child:
convert_string += convert_children(mybatis_mapper, next_child, properties=properties)
kwargs['properties'] = properties
convert_string += convert_children(mybatis_mapper, next_child, **kwargs)
# add include tail
convert_string += convert_parameters(child, tail=True)
return convert_string


def convert_if(mybatis_mapper, child):
def convert_if(mybatis_mapper, child, **kwargs):
convert_string = ''
test = child.attrib.get('test')
# Add if text
convert_string += convert_parameters(child, text=True)
for next_child in child:
convert_string += convert_children(mybatis_mapper, next_child)
convert_string += convert_children(mybatis_mapper, next_child, **kwargs)
convert_string += '-- if(' + test + ')\n'
# Add if tail
convert_string += convert_parameters(child, tail=True)
return convert_string


def convert_choose_when_otherwise(mybatis_mapper, child):
def convert_choose_when_otherwise(mybatis_mapper, child, **kwargs):
# native
native = kwargs.get('native')
when_element_cnt = kwargs.get('when_element_cnt', 0)
convert_string = ''
for next_child in child:
if next_child.tag == 'when':
test = next_child.attrib.get('test')
convert_string += convert_parameters(next_child, text=True, tail=True)
convert_string += '-- if(' + test + ')'
if native and when_element_cnt >= 1:
break
else:
test = next_child.attrib.get('test')
convert_string += convert_parameters(next_child, text=True, tail=True)
convert_string += '-- if(' + test + ')'
when_element_cnt += 1
kwargs['when_element_cnt'] = when_element_cnt
elif next_child.tag == 'otherwise':
convert_string += convert_parameters(next_child, text=True, tail=True)
convert_string += '-- otherwise'
convert_string += convert_children(mybatis_mapper, next_child)
convert_string += convert_children(mybatis_mapper, next_child, **kwargs)
return convert_string


def convert_trim_where_set(mybatis_mapper, child):
def convert_trim_where_set(mybatis_mapper, child, **kwargs):
if child.tag == 'trim':
prefix = child.attrib.get('prefix')
suffix = child.attrib.get('suffix')
Expand All @@ -142,7 +151,7 @@ def convert_trim_where_set(mybatis_mapper, child):
convert_string += convert_parameters(child, text=True)
# Convert children first
for next_child in child:
convert_string += convert_children(mybatis_mapper, next_child)
convert_string += convert_children(mybatis_mapper, next_child, **kwargs)
# Remove prefixOverrides
if prefix_overrides:
regex = r'^[\s]*?({})'.format(prefix_overrides)
Expand All @@ -162,7 +171,7 @@ def convert_trim_where_set(mybatis_mapper, child):
return convert_string


def convert_foreach(mybatis_mapper, child):
def convert_foreach(mybatis_mapper, child, **kwargs):
collection = child.attrib.get('collection')
item = child.attrib.get('item')
index = child.attrib.get('index')
Expand All @@ -173,15 +182,15 @@ def convert_foreach(mybatis_mapper, child):
# Add foreach text
convert_string += convert_parameters(child, text=True)
for next_child in child:
convert_string += convert_children(mybatis_mapper, next_child)
convert_string += convert_children(mybatis_mapper, next_child, **kwargs)
# Add two items
convert_string = open + convert_string + separator + convert_string + close
# Add foreach tail
convert_string += convert_parameters(child, tail=True)
return convert_string


def convert_bind(child):
def convert_bind(child, **kwargs):
"""
:param child:
:return:
Expand Down
6 changes: 3 additions & 3 deletions mybatis_mapper2sql/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_statement(mybatis_mapper, result_type='raw', **kwargs):
"""
Get SQL Statements from Mapper
:param mybatis_mapper:
:param kwargs: sqlparse format kwargs
:param kwargs: sqlparse format kwargs /native: parse follow the native rules
:param result_type: raw|list
:return:
"""
Expand Down Expand Up @@ -64,10 +64,10 @@ def get_child_statement(mybatis_mapper, child_id, **kwargs):
# get sql
statement = ''
child = mybatis_mapper.get(child_id)
statement += convert_children(mybatis_mapper, child)
statement += convert_children(mybatis_mapper, child, **kwargs)
# The child element has children
for next_child in child:
statement += convert_children(mybatis_mapper, next_child)
statement += convert_children(mybatis_mapper, next_child, **kwargs)
return sqlparse.format(statement, **kwargs)


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def run_tests(self):

setup(
name='mybatis-mapper2sql',
version='0.1.8',
version='0.1.9',
author='hhyo',
author_email='rtttte@qq.com',
url='http://github.com/hhyo/mybatis-mapper2sql',
Expand Down
7 changes: 7 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ def test_bind(self):
self.statement = mybatis_mapper2sql.get_child_statement(self.mapper, child_id=self.child_id, reindent=True)
print(self.statement)

def test_choose_native(self):
self.child_id = 'testChooseNative'
print("============{}============".format(self.child_id))
self.statement = mybatis_mapper2sql.get_child_statement(self.mapper, child_id=self.child_id,
reindent=True, native=True)
print(self.statement)


if __name__ == '__main__':
unittest.main()
24 changes: 24 additions & 0 deletions tests/test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,28 @@
</if>
</trim>
</insert>
<select id="testChooseNative">
SELECT
name,
category,
price
FROM
fruits
<where>
<choose>
<when test="name != null">
AND name = #{name}
</when>
<when test="category == 'banana'">
AND category = #{category}
<if test="price != null and price !=''">
AND price = ${price}
</if>
</when>
<otherwise>
AND category = 'apple'
</otherwise>
</choose>
</where>
</select>
</mapper>

0 comments on commit 4a18abd

Please sign in to comment.