Skip to content

Commit 78c7fba

Browse files
author
taochao
committed
修复mongo查询语句中解析';'异常
1 parent b8eabf9 commit 78c7fba

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

sql/engines/mongo.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -686,31 +686,36 @@ def filter_sql(self, sql='', limit_num=0):
686686
return sql.strip()
687687

688688
def query_check(self, db_name=None, sql=''):
689-
"""提交查询前的检查"""
690-
691-
if sql.startswith("explain"):
692-
sql = sql.replace("explain", "") + ".explain()"
693-
result = {'msg': '', 'bad_query': False, 'filtered_sql': sql, 'has_star': False}
694-
pattern = re.compile(
695-
r'''^db\.(\w+\.?)+(?:\([\s\S]*\)$)|^db\.getCollection\((?:\s*)(?:'|")(\w+\.?)+('|")(\s*)\)\.([A-Za-z]+)(\([\s\S]*\)$)''')
696-
m = pattern.match(sql)
697-
if m is not None:
698-
logger.debug(sql)
699-
query_dict = self.parse_query_sentence(sql)
700-
if "method" not in query_dict:
701-
result['msg'] += "错误:对不起,只支持查询相关方法"
702-
result['bad_query'] = True
703-
return result
704-
collection_name = query_dict["collection"]
705-
collection_names = self.get_all_tables(db_name).rows
706-
is_in = collection_name in collection_names # 检查表是否存在
707-
if not is_in:
708-
result['msg'] += f"\n错误: {collection_name} 文档不存在!"
689+
sql = sql.strip()
690+
# 以;作为语句结束
691+
q_sql = sql.split(";")
692+
for qq_sql in q_sql:
693+
if not qq_sql == '' and qq_sql != '\n':
694+
"""提交查询前的检查"""
695+
696+
if qq_sql.startswith("explain"):
697+
qq_sql = qq_sql.replace("explain", "") + ".explain()"
698+
result = {'msg': '', 'bad_query': False, 'filtered_sql': qq_sql, 'has_star': False}
699+
pattern = re.compile(
700+
r'''^db\.(\w+\.?)+(?:\([\s\S]*\)$)|^db\.getCollection\((?:\s*)(?:'|")(\w+\.?)+('|")(\s*)\)\.([A-Za-z]+)(\([\s\S]*\)$)''')
701+
m = pattern.match(qq_sql)
702+
if m is not None:
703+
logger.debug(qq_sql)
704+
query_dict = self.parse_query_sentence(qq_sql)
705+
if "method" not in query_dict:
706+
result['msg'] += "错误:对不起,只支持查询相关方法"
707+
result['bad_query'] = True
708+
return result
709+
collection_name = query_dict["collection"]
710+
collection_names = self.get_all_tables(db_name).rows
711+
is_in = collection_name in collection_names # 检查表是否存在
712+
if not is_in:
713+
result['msg'] += f"\n错误: {collection_name} 文档不存在!"
714+
result['bad_query'] = True
715+
return result
716+
else:
717+
result['msg'] += '请检查语句的正确性! 请使用原生查询语句'
709718
result['bad_query'] = True
710-
return result
711-
else:
712-
result['msg'] += '请检查语句的正确性! 请使用原生查询语句'
713-
result['bad_query'] = True
714719
return result
715720

716721
def query(self, db_name=None, sql='', limit_num=0, close_conn=True, **kwargs):

0 commit comments

Comments
 (0)