Skip to content

Commit

Permalink
修复distinct关键字的报错
Browse files Browse the repository at this point in the history
  • Loading branch information
kang.peng committed Nov 29, 2023
1 parent e40d728 commit 0898004
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sql/engines/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ def filter_sql(self, sql="", limit_num=0):
# 对查询sql增加limit限制
if re.match(r"^select", sql_lower):
if sql_lower.find(" top ") == -1:
if sql_lower.find(" distinct ") > 0:
return sql_lower.replace(
"distinct", "distinct top {}".format(limit_num)
)
return sql_lower.replace("select", "select top {}".format(limit_num))
return sql.strip()

Expand Down
7 changes: 7 additions & 0 deletions sql/engines/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ def test_filter_sql(self):
check_result = new_engine.filter_sql(sql=banned_sql, limit_num=10)
self.assertEqual(check_result, "select top 10 user from user_table")

def test_filter_sql_with_distinct(self):
new_engine = MssqlEngine(instance=self.ins1)
# 只抽查一个函数
banned_sql = "select distinct * from user_table"
check_result = new_engine.filter_sql(sql=banned_sql, limit_num=10)
self.assertEqual(check_result, "select distinct top 10 * from user_table")

def test_execute_check(self):
new_engine = MssqlEngine(instance=self.ins1)
test_sql = (
Expand Down

0 comments on commit 0898004

Please sign in to comment.