用 Python 解析 SQL [英] Parsing SQL with Python

查看:30
本文介绍了用 Python 解析 SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在非关系数据存储之上创建一个 SQL 接口.非关系数据存储,但以关系方式访问数据是有意义的.

I want to create a SQL interface on top of a non-relational data store. Non-relational data store, but it makes sense to access the data in a relational manner.

我正在研究使用 ANTLR 来生成将 SQL 表示为关系代数表达式的 AST.然后通过评估/遍历树返回数据.

I am looking into using ANTLR to produce an AST that represents the SQL as a relational algebra expression. Then return data by evaluating/walking the tree.

我以前从未实现过解析器,因此我想就如何最好地实现 SQL 解析器和求值器提供一些建议.

I have never implemented a parser before, and I would therefore like some advice on how to best implement a SQL parser and evaluator.

  • 上述方法听起来是否正确?
  • 我还应该研究其他工具/库吗?像 PLYPyparsing.
  • 感谢提供对我有帮助的文章、书籍或源代码的指针.

更新:

我使用 pyparsing 实现了一个简单的 SQL 解析器.结合对我的数据存储实现关系操作的 Python 代码,这相当简单.

I implemented a simple SQL parser using pyparsing. Combined with Python code that implement the relational operations against my data store, this was fairly simple.

正如我在其中一条评论中所说,练习的目的是让数据可供报告引擎使用.为此,我可能需要实现一个 ODBC 驱动程序.这可能需要大量工作.

As I said in one of the comments, the point of the exercise was to make the data available to reporting engines. To do this, I probably will need to implement an ODBC driver. This is probably a lot of work.

推荐答案

我已经深入研究了这个问题.Python-sqlparse 是一个非验证解析器,它并不是您真正需要的.antlr 中的示例需要大量工作才能在 python 中转换为漂亮的 ast.sql 标准语法在这里,但自己转换它们将是一项全职工作,而且是可能您只需要其中的一个子集,即不需要连接.您也可以尝试查看 gadfly(一个 python sql 数据库),但我避免了它,因为他们使用了他们的自己的解析工具.

I have looked into this issue quite extensively. Python-sqlparse is a non validating parser which is not really what you need. The examples in antlr need a lot of work to convert to a nice ast in python. The sql standard grammers are here, but it would be a full time job to convert them yourself and it is likely that you would only need a subset of them i.e no joins. You could try looking at the gadfly (a python sql database) as well, but I avoided it as they used their own parsing tool.

就我而言,我基本上只需要一个 where 子句.我尝试了用 pyparsing 编写的 booleneo(一个布尔表达式解析器),但最终从头开始使用 pyparsing.Mark Rushakoff 的 reddit 帖子中的第一个链接给出了一个使用它的 sql 示例.Whoosh 全文搜索引擎也使用它,但我还没有查看来源以了解如何使用.

For my case, I only essentially needed a where clause. I tried booleneo (a boolean expression parser) written with pyparsing but ended up using pyparsing from scratch. The first link in the reddit post of Mark Rushakoff gives a sql example using it. Whoosh a full text search engine also uses it but I have not looked at the source to see how.

Pyparsing 非常易于使用,您可以非常轻松地将其自定义为与 sql 不完全相同(大多数语法您将不需要).我不喜欢 ply 因为它使用命名约定使用了一些魔法.

Pyparsing is very easy to use and you can very easily customize it to not be exactly the same as sql (most of the syntax you will not need). I did not like ply as it uses some magic using naming conventions.

简而言之,试试 pyparsing,它很可能足够强大,足以满足您的需求,并且与 Python 的简单集成(具有简单的回调和错误处理)将使体验变得非常轻松.

In short give pyparsing a try, it will most likely be powerful enough to do what you need and the simple integration with python (with easy callbacks and error handling) will make the experience pretty painless.

这篇关于用 Python 解析 SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆