diff --git a/sql/engines/mysql.py b/sql/engines/mysql.py index 5dd7f73b42..60c88d8af2 100644 --- a/sql/engines/mysql.py +++ b/sql/engines/mysql.py @@ -150,8 +150,12 @@ def filter_sql(self, sql='', limit_num=0): # LIMIT N, N 或LIMIT N OFFSET N limit_offset = re.compile(r'limit([\s]*\d+[\s]*)(,|offset)([\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) + limit_num = min(int(limit_num), int(sql_limit)) sql = limit_offset.sub(f'limit {limit_num};', sql) else: sql = f'{sql} limit {limit_num};' diff --git a/sql/engines/tests.py b/sql/engines/tests.py index b8b2ef11f6..243ba1732a 100644 --- a/sql/engines/tests.py +++ b/sql/engines/tests.py @@ -343,6 +343,12 @@ def test_filter_sql_with_limit(self): check_result = new_engine.filter_sql(sql=sql_without_limit, limit_num=1) self.assertEqual(check_result, 'select user from usertable limit 1;') + def test_filter_sql_with_limit_min(self): + new_engine = MysqlEngine(instance=self.ins1) + sql_without_limit = 'select user from usertable limit 10' + check_result = new_engine.filter_sql(sql=sql_without_limit, limit_num=100) + self.assertEqual(check_result, 'select user from usertable limit 10;') + 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'