From ced29e30bc0a77487890bf4f1c4d85d79b7e0533 Mon Sep 17 00:00:00 2001 From: mxlxm Date: Sun, 29 Aug 2021 20:20:35 +0800 Subject: [PATCH] support offset fix #847,#739,#647. --- sql/engines/mysql.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/sql/engines/mysql.py b/sql/engines/mysql.py index 415b42626a..f9c0f02d32 100644 --- a/sql/engines/mysql.py +++ b/sql/engines/mysql.py @@ -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: