Rob*_*bAu 39 sql postgresql cvs syntax parsing
我想验证包含sql查询的文件的语法,然后才能在我的CVS项目中提交它们.
为了做到这一点,我有一个commitinfo脚本,但我很难找到sql命令是否有效.psql
似乎没有dryrun模式,并且从语法(在源代码中)构建我自己的postgresql-dialact测试器似乎是一个很长的延伸.
脚本可能包含多个查询,因此EXPLAIN
不能包含它们.
任何提示?
Mar*_*ago 45
我最近编写了一个实用程序来静态检查PostgreSQL的SQL语法.它利用ecpg(postgres的嵌入式SQL C预处理程序)来检查SQL语法,因此它使用了Postgres本身内置的完全相同的解析器.
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:
$ pgsanity good1.sql good2.sql bad.sql
bad.sql: line 1: ERROR: syntax error at or near "bogus_token"
$ find -name '*.sql' | xargs pgsanity
./sql/bad1.sql: line 59: ERROR: syntax error at or near ";"
./sql/bad2.sql: line 41: ERROR: syntax error at or near "insert"
./sql/bad3.sql: line 57: ERROR: syntax error at or near "update"
Run Code Online (Sandbox Code Playgroud)
Rin*_*nat 27
使用这个技巧来验证 PostgreSQL 代码语法:
DO $SYNTAX_CHECK$ BEGIN RETURN;
-- insert your SQL code here
END; $SYNTAX_CHECK$;
Run Code Online (Sandbox Code Playgroud)
Erw*_*ter 13
一种方法是将它放入最后回滚的事务中:
BEGIN;
<query>;
<query>;
<query>;
ROLLBACK;
Run Code Online (Sandbox Code Playgroud)
请注意,有些效果无法回滚,例如dblink调用,或者写入文件系统或增加序列的任何内容.
我建议克隆您的数据库以进行测试.
EXPLAIN(没有ANALYZE)将解析查询并准备执行计划,而不实际执行它.
https://www.postgresql.org/docs/current/static/sql-explain.html
我通常使用Mimer在线SQL验证器,唯一的是它检查标准SQL的SQL语法:
并不是特定于PostgreSQL ...但是如果您按照标准编写代码,您可以使用它并且它运行良好...
归档时间: |
|
查看次数: |
37003 次 |
最近记录: |