PostgreSQL 语法检查而不运行查询
我想验证包含 sql 查询的文件的语法,然后才能将它们提交到我的 CVS 项目中。
为了做到这一点,我有一个 commitinfo 脚本,但我很难确定 sql 命令是否有效。 psql
似乎没有 dryrun 模式,并且从语法(位于源代码中)构建我自己的 postgresql-dialact 测试器似乎是一个漫长的过程。
脚本可能包含多个查询,因此 EXPLAIN
不能包含在它们周围。
有什么提示吗?
I want to verify the syntax of files containing sql queries before they can be committed in my CVS project.
In order to do that, I have a commitinfo script, but I have trouble finding out if the sql commands are valid. psql
does not seem to have a dryrun mode, and constructing my own postgresql-dialact tester from the grammar (that is in the source) seems like a long stretch.
The scripts may contain multiple queries, so an EXPLAIN
cannot be wrapped around them.
Any hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我最近编写了一个实用程序来静态检查 PostgreSQL 的 SQL 语法。它利用 ecpg(postgres 的嵌入式 SQL C 预处理器)来检查 SQL 语法,因此它使用与 Postgres 本身内置的完全相同的解析器。
您可以在 github 上查看:http://github.com/markdrago/pgsanity。您可以浏览一下自述文件,以更好地了解它的工作原理并获取如何安装它的说明。以下是如何使用 pgsanity 的简短示例:
I recently wrote up a utility to statically check the syntax of SQL for PostgreSQL. It leverages ecpg, the embedded SQL C preproccessor for postgres, to check the SQL syntax, so it uses the exact same parser that is built in to Postgres itself.
You can check it out on github: http://github.com/markdrago/pgsanity. You can give the README a skim to get a better idea of how it works and to get directions for how to install it. Here's a short example of how pgsanity can be used:
使用此技巧来验证 PostgreSQL 代码语法:
Function is_sql(sql text)(链接到我的 GitHub)
Use this trick to validate PostgreSQL code syntax:
Function is_sql(sql text) (link is to my GitHub)
一种方法是将其放入最后回滚的事务中:
请注意,有些效果无法回滚,例如 dblink 调用,或写入文件系统或递增序列的任何内容。
我建议克隆您的数据库以进行测试。
One way would be to put it into a transaction that you roll back at the end:
Be aware that there are some effects that cannot be rolled back, like dblink calls, or anything written to the file system or incremented sequences.
I would advise cloning your database for testing purposes.
EXPLAIN(不带 ANALYZE)将解析查询并准备执行计划,但不实际执行它。
https://www.postgresql.org/docs/current/static/sql -解释.html
EXPLAIN (without ANALYZE) will parse the query and prepare an execution plan, without actually executing it.
https://www.postgresql.org/docs/current/static/sql-explain.html
我通常使用Mimer在线SQL验证器,唯一的就是它检查SQL语法对于标准 SQL :
并且不特定于 PostgreSQL ...但是,如果您按照标准编写代码,则可以使用它并且它运行良好...
I'm usually use Mimer online SQL validator, the only thing is that it check SQL syntax for standard SQL :
and not specific for the PostgreSQL ... However if you write code following the standard you can use it and it work well ...
一个很棒的实用程序来验证 SQL 语法:SQL Fiddle
支持 MySQL、Oracle、PostgreSQL、SQLite、MS SQL。
A wonderful utility to verify SQL syntax: SQL Fiddle
Supports MySQL, Oracle, PostgreSQL, SQLite, MS SQL.
你可以把它包起来
SELECT 1 () AS a WHERE 1 = 0;
验证会失败,但实际上不会执行。这是一个查询计划示例:
You could just wrap it in
SELECT 1 ( <your query> ) AS a WHERE 1 = 0;
It'll fail on validation but it won't actually execute. Here's an example query plan:
您可以在 postgresql 函数旁边运行查询并最终引发异常。所有更改都将回滚。例如:
另一个解决方案 -
plpgsql_check
扩展(在 github 上),下一个pgpsql_lint 的化身You can run queries iside postgresql function and raise exception in the end. All changes will be rolled back. For example:
Another sollution -
plpgsql_check
extension (on github), the next incarnation of pgpsql_lint