Skip to content

Commit

Permalink
support offset (#1172)
Browse files Browse the repository at this point in the history
* support offset
fix #847,#739,#647.

* update limit ut

Co-authored-by: 小圈圈 <rtttte@qq.com>
  • Loading branch information
mxlxm and hhyo authored Sep 16, 2021
1 parent ae54ae6 commit f370593
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
18 changes: 13 additions & 5 deletions sql/engines/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,25 @@ def filter_sql(self, sql='', limit_num=0):
sql = sql.rstrip(';').strip()
if re.match(r"^select", sql, re.I):
# LIMIT N
limit_n = re.compile(r'limit([\s]*\d+[\s]*)$', re.I)
# LIMIT N, N 或LIMIT N OFFSET N
limit_offset = re.compile(r'limit([\s]*\d+[\s]*)(,|offset)([\s]*\d+[\s]*)$', re.I)
limit_n = re.compile(r'limit\s+(\d+)\s*$', re.I)
# LIMIT M OFFSET N
limit_offset = re.compile(r'limit\s+(\d+)\s+offset\s+(\d+)\s*$', re.I)
# LIMIT M,N
offset_comma_limit = re.compile(r'limit\s+(\d+)\s*,\s*(\d+)\s*$', re.I)
if limit_n.search(sql):
sql_limit = limit_n.search(sql).group(1)
limit_num = min(int(limit_num), int(sql_limit))
sql = limit_n.sub(f'limit {limit_num};', sql)
elif limit_offset.search(sql):
sql_limit = limit_offset.search(sql).group(3)
sql_limit = limit_offset.search(sql).group(1)
sql_offset = limit_offset.search(sql).group(2)
limit_num = min(int(limit_num), int(sql_limit))
sql = limit_offset.sub(f'limit {limit_num};', sql)
sql = limit_offset.sub(f'limit {limit_num} offset {sql_offset};', sql)
elif offset_comma_limit.search(sql):
sql_offset = offset_comma_limit.search(sql).group(1)
sql_limit = offset_comma_limit.search(sql).group(2)
limit_num = min(int(limit_num), int(sql_limit))
sql = offset_comma_limit.sub(f'limit {sql_offset},{limit_num};', sql)
else:
sql = f'{sql} limit {limit_num};'
else:
Expand Down
6 changes: 3 additions & 3 deletions sql/engines/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,19 +355,19 @@ def test_filter_sql_with_limit_offset(self):
new_engine = MysqlEngine(instance=self.ins1)
sql_without_limit = 'select user from usertable limit 10 offset 100'
check_result = new_engine.filter_sql(sql=sql_without_limit, limit_num=1)
self.assertEqual(check_result, 'select user from usertable limit 1;')
self.assertEqual(check_result, 'select user from usertable limit 1 offset 100;')

def test_filter_sql_with_limit_nn(self):
new_engine = MysqlEngine(instance=self.ins1)
sql_without_limit = 'select user from usertable limit 10, 100'
check_result = new_engine.filter_sql(sql=sql_without_limit, limit_num=1)
self.assertEqual(check_result, 'select user from usertable limit 1;')
self.assertEqual(check_result, 'select user from usertable limit 10,1;')

def test_filter_sql_upper(self):
new_engine = MysqlEngine(instance=self.ins1)
sql_without_limit = 'SELECT USER FROM usertable LIMIT 10, 100'
check_result = new_engine.filter_sql(sql=sql_without_limit, limit_num=1)
self.assertEqual(check_result, 'SELECT USER FROM usertable limit 1;')
self.assertEqual(check_result, 'SELECT USER FROM usertable limit 10,1;')

def test_filter_sql_not_select(self):
new_engine = MysqlEngine(instance=self.ins1)
Expand Down

0 comments on commit f370593

Please sign in to comment.