简体   繁体   English

Postgres的“ RAISE EXCEPTION”如何转换为PDOException?

[英]How is a Postgres “RAISE EXCEPTION” converted into a PDOException?

I am trying to figure out which "parts" of "RAISE EXCEPTION" statement gets mapped into which "parts" of PDOException. 我试图找出“ RAISE EXCEPTION”语句的“部分”映射到PDOException的“部分”。 Can some one explain "who" and "how" perform this conversion (or better yet, point to document which describes this or source code which performs this)? 有人可以解释“谁”和“如何”执行此转换(或者更好的是,指向描述此操作的文档或执行此操作的源代码)吗?

More specifically, I would like to know if there is a way to affect SQLSTATUS value (in my tests its always 'P0001') and/or 'ERROR:' value (which is part of the message and is always empty) 更具体地说,我想知道是否有一种方法可以影响SQLSTATUS值(在我的测试中始终为“ P0001”)和/或“ ERROR:”值(这是消息的一部分,始终为空)

Best I could manage is: 我能管理的最好的是:

RAISE EXCEPTION USING message = '<a message>', ERRCODE = 'UE001'

which on PHP side gives me message like: 在PHP方面给我的消息是:

ERROR:  NUM:UE001, DETAILS:<a message>

But I am not very happy with this. 但是我对此并不满意。 It would be great to be able to "put" 'UE001' into one of PDOException fields (so I do not have to parse it out of message string). 能够将“ UE001”“放入” PDOException字段之一将是很棒的(因此我不必从消息字符串中解析出它)。

You comments will be highly appreciated. 您的意见将不胜感激。

Use the code property of PDOException to get the SQLSTATE . 使用PDOExceptioncode属性获取SQLSTATE See the documentation for PDOException 请参阅有关PDOException的文档

To control the SQLSTATE generated by a PL/PgSQL function raising an error, you use RAISE ... SQLSTATE as per the documentation . 要控制由PL / PgSQL函数生成的SQLSTATE引发错误,请按照文档使用RAISE ... SQLSTATE

Of course, for this to work the database driver must correctly report SQLSTATE . 当然,为此,数据库驱动程序必须正确报告SQLSTATE I have verified that PDO does this at least in PHP 5.4.11 with PostgreSQL 9.2, per the following standalone example code that can be executed with the php command-line executable: 我已经验证了PDO至少可以在PostgreSQL PostgreSQL的PHP​​ 5.4.11中做到这一点,这是可以通过php命令行可执行文件执行的以下独立示例代码:

<?php
$pdo = new PDO('pgsql:');

$sql = <<<EOD
CREATE OR REPLACE FUNCTION exceptiondemo() RETURNS void AS $$
BEGIN
  RAISE SQLSTATE 'UE001' USING MESSAGE = 'error message';
END;
$$ LANGUAGE plpgsql
EOD;

$sth = $pdo->prepare($sql);
if (!$sth->execute()) {
        die("Failed to create test function\n");
}

$sql = "SELECT exceptiondemo();";

$sth = $pdo->prepare($sql);
if (!$sth->execute()) {
        $ei = $sth->errorInfo();
        die("Function call failed with SQLSTATE " . $ei[0] . ", message " . $ei[2] . "\n");

        // Shortcut way:
        // die("Function call failed with SQLSTATE " . $sth->errorCode());
}
?>

Output is: 输出为:

Function call failed with SQLSTATE UE001, message ERROR:  error message

Replace the code block from the second $sth->execute() to the end of the code with this to demonstrate that the exception handling mode works fine too: 将第二个$sth->execute()到代码末尾的代码块替换为此,以证明异常处理模式也可以正常工作:

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
        $sth->execute();
} catch (PDOException $err) {
        $ei = $err->errorInfo;
        die("Function call failed with SQLSTATE " . $ei[0] . ", message " . $ei[2] . "\n");

        // Alternate version to just get code:
        //die("Function call failed with SQLSTATE " . $err->getCode() . "\n");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM