Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复distinct关键字的报错 #2425

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading