PostgreSQL 语法检查而不运行查询

发布于 2024-12-18 04:57:30 字数 256 浏览 0 评论 0原文

我想验证包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

一场春暖 2024-12-25 04:57:30

我最近编写了一个实用程序来静态检查 PostgreSQL 的 SQL 语法。它利用 ecpg(postgres 的嵌入式 SQL C 预处理器)来检查 SQL 语法,因此它使用与 Postgres 本身内置的完全相同的解析器。

您可以在 github 上查看:http://github.com/markdrago/pgsanity。您可以浏览一下自述文件,以更好地了解它的工作原理并获取如何安装它的说明。以下是如何使用 pgsanity 的简短示例:

$ 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"

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:

$ 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"
╭ゆ眷念 2024-12-25 04:57:30

使用此技巧来验证 PostgreSQL 代码语法:

DO $SYNTAX_CHECK$ BEGIN RETURN;
    -- insert your SQL code here
END; $SYNTAX_CHECK$;

Function is_sql(sql text)(链接到我的 GitHub)

Use this trick to validate PostgreSQL code syntax:

DO $SYNTAX_CHECK$ BEGIN RETURN;
    -- insert your SQL code here
END; $SYNTAX_CHECK$;

Function is_sql(sql text) (link is to my GitHub)

伏妖词 2024-12-25 04:57:30

一种方法是将其放入最后回滚的事务中:

BEGIN;
<query>;
<query>;
<query>;
ROLLBACK;

请注意,有些效果无法回滚,例如 dblink 调用,或写入文件系统或递增序列的任何内容。

我建议克隆您的数据库以进行测试。

One way would be to put it into a transaction that you roll back at the end:

BEGIN;
<query>;
<query>;
<query>;
ROLLBACK;

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.

以往的大感动 2024-12-25 04:57:30

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

神仙妹妹 2024-12-25 04:57:30

我通常使用Mimer在线SQL验证器,唯一的就是它检查SQL语法对于标准 SQL :

  • SQL-92
  • SQL-99
  • SQL-03

并且不特定于 PostgreSQL ...但是,如果您按照标准编写代码,则可以使用它并且它运行良好...

I'm usually use Mimer online SQL validator, the only thing is that it check SQL syntax for standard SQL :

  • SQL-92
  • SQL-99
  • SQL-03

and not specific for the PostgreSQL ... However if you write code following the standard you can use it and it work well ...

自由如风 2024-12-25 04:57:30

一个很棒的实用程序来验证 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.

踏月而来 2024-12-25 04:57:30

你可以把它包起来
SELECT 1 () AS a WHERE 1 = 0;

验证会失败,但实际上不会执行。这是一个查询计划示例:

Result  (cost=0.00..0.01 rows=1 width=0)
  One-Time Filter: false

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:

Result  (cost=0.00..0.01 rows=1 width=0)
  One-Time Filter: false
夏日落 2024-12-25 04:57:30

您可以在 postgresql 函数旁边运行查询并最终引发异常。所有更改都将回滚。例如:

CREATE OR REPLACE FUNCTION run_test(_sp character varying)
  RETURNS character varying AS
$BODY$
BEGIN
  EXECUTE 'SELECT ' || _sp;
  RAISE EXCEPTION '#OK';
EXCEPTION
  WHEN others THEN
    RETURN SQLERRM;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

另一个解决方案 - 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:

CREATE OR REPLACE FUNCTION run_test(_sp character varying)
  RETURNS character varying AS
$BODY$
BEGIN
  EXECUTE 'SELECT ' || _sp;
  RAISE EXCEPTION '#OK';
EXCEPTION
  WHEN others THEN
    RETURN SQLERRM;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

Another sollution - plpgsql_check extension (on github), the next incarnation of pgpsql_lint

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文