Skip to content

Commit

Permalink
support offset
Browse files Browse the repository at this point in the history
  • Loading branch information
mxlxm committed Aug 29, 2021
1 parent 9d49810 commit ced29e3
Showing 1 changed file with 13 additions and 5 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

0 comments on commit ced29e3

Please sign in to comment.