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

[ 功能建议 ]mongodb查询优化 #1252

Closed
weideguo opened this issue Nov 26, 2021 · 2 comments
Closed

[ 功能建议 ]mongodb查询优化 #1252

weideguo opened this issue Nov 26, 2021 · 2 comments

Comments

@weideguo
Copy link
Collaborator

目前遇到的问题/使用障碍

mongodb的查询尾部存在分号或者前后存在空格时处理错误,以及点击执行计划时处理错误。
注意到有一个pr( #1198 ),但感觉处理得不够优雅

希望如何解决/实现它

补丁 mongo.py.20211126.patch

diff --git a/sql/engines/mongo.py b/sql/engines/mongo.py
index 86e7c99..460ee96 100644
--- a/sql/engines/mongo.py
+++ b/sql/engines/mongo.py
@@ -698,12 +698,13 @@ class MongoEngine(EngineBase):
 
     def query_check(self, db_name=None, sql=''):
         """提交查询前的检查"""
-
+        sql = sql.strip()
         if sql.startswith("explain"):
             sql = sql.replace("explain", "") + ".explain()"
+            sql = re.sub("[;\s]*.explain\(\)", ".explain()", sql).strip()
         result = {'msg': '', 'bad_query': False, 'filtered_sql': sql, 'has_star': False}
         pattern = re.compile(
-            r'''^db\.(\w+\.?)+(?:\([\s\S]*\)$)|^db\.getCollection\((?:\s*)(?:'|")(\w+\.?)+('|")(\s*)\)\.([A-Za-z]+)(\([\s\S]*\)$)''')
+            r'''^db\.(\w+\.?)+(?:\([\s\S]*\)(\s*;*)$)|^db\.getCollection\((?:\s*)(?:'|")(\w+\.?)+('|")(\s*)\)\.([A-Za-z]+)(\([\s\S]*\)(\s*;*)$)''')
         m = pattern.match(sql)
         if m is not None:
             logger.debug(sql)

应用补丁
patch -p1 < mongo.py.20211126.patch

@weideguo
Copy link
Collaborator Author

sql = sql[7:]+".explain()"
re.sub("[;\s]*.explain\(\)$", ".explain()", sql).strip()

防止查询中存在explain字符串时出现错误,如explain db.a.find({"a":"explain()"})

@hhyo
Copy link
Owner

hhyo commented Nov 28, 2021

代码优化可以提pr沟通,感谢贡献

weideguo added a commit to weideguo/Archery that referenced this issue Nov 28, 2021
hhyo added a commit that referenced this issue Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants