[PATCH] pg_result_error_field

php.internals

Илья Кантор

21 years ago
In many situations one needs to know what caused the error And probably receive additional information about it (e.g which foreign key constraint failed). I attach tiny patches giving access to PQresultErrorField function. Error messages may need adjustment. Tested on PHP 5.0.2 Example of usage: (from DB class) pg_send_query($this->link, 'INSERT INTO b VALUES(5)'); $res = pg_get_result($this->link); echo pg_result_error_field($res, PGSQL_DIAG_MESSAGE_DETAIL)); Patches (see attach): --- ../../../../../../lang/php5/work/php-5.0.2/ext/pgsql/pgsql.c Wed May 12 16:49:47 2004 +++ pgsql.c Sat Nov 13 19:51:05 2004 @@ -124,6 +124,7 @@ PHP_FE(pg_get_pid, NULL) /* error message functions */ PHP_FE(pg_result_error, NULL) + PHP_FE(pg_result_error_field, NULL) PHP_FE(pg_last_error, NULL) PHP_FE(pg_last_notice, NULL) /* copy functions */ @@ -438,6 +439,19 @@ REGISTER_LONG_CONSTANT("PGSQL_SEEK_SET", SEEK_SET, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PGSQL_SEEK_CUR", SEEK_CUR, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PGSQL_SEEK_END", SEEK_END, CONST_CS | CONST_PERSISTENT); + /* For pg_result_error_field() */ + REGISTER_LONG_CONSTANT("PGSQL_DIAG_SEVERITY", PG_DIAG_SEVERITY, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_DIAG_SQLSTATE", PG_DIAG_SQLSTATE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_DIAG_MESSAGE_PRIMARY", PG_DIAG_MESSAGE_PRIMARY, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_DIAG_MESSAGE_DETAIL", PG_DIAG_MESSAGE_DETAIL, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_DIAG_MESSAGE_HINT", PG_DIAG_MESSAGE_HINT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_DIAG_STATEMENT_POSITION", PG_DIAG_STATEMENT_POSITION, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_DIAG_INTERNAL_POSITION", PG_DIAG_INTERNAL_POSITION, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_DIAG_INTERNAL_QUERY", PG_DIAG_INTERNAL_QUERY, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_DIAG_CONTEXT", PG_DIAG_CONTEXT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_FILE", PG_DIAG_SOURCE_FILE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_LINE", PG_DIAG_SOURCE_LINE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_FUNCTION", PG_DIAG_SOURCE_FUNCTION, CONST_CS | CONST_PERSISTENT); /* For pg_result_status() return value type */ REGISTER_LONG_CONSTANT("PGSQL_STATUS_LONG", PGSQL_STATUS_LONG, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PGSQL_STATUS_STRING", PGSQL_STATUS_STRING, CONST_CS | CONST_PERSISTENT); @@ -2990,6 +3004,55 @@ } /* }}} */ #endif + +/* {{{ proto string pg_result_error_field(resource result, long code) + Get diagnosis message associated with the result */ +PHP_FUNCTION(pg_result_error_field) +{ + zval *result; + long code; + PGresult *pgsql_result; + pgsql_result_handle *pg_result; + char *err = NULL; + + if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "rl", + &result, &code) == FAILURE) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot parse params"); + RETURN_FALSE; + } + + if (code != PG_DIAG_SEVERITY && + code != PG_DIAG_SQLSTATE && + code != PG_DIAG_MESSAGE_PRIMARY && + code != PG_DIAG_MESSAGE_DETAIL && + code != PG_DIAG_MESSAGE_HINT && + code != PG_DIAG_STATEMENT_POSITION && + code != PG_DIAG_INTERNAL_POSITION && + code != PG_DIAG_INTERNAL_QUERY && + code != PG_DIAG_CONTEXT && + code != PG_DIAG_SOURCE_FILE && + code != PG_DIAG_SOURCE_LINE && + code != PG_DIAG_SOURCE_FUNCTION + ) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid diag code"); + RETURN_FALSE; + } + + + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + + pgsql_result = pg_result->result; + if (!pgsql_result) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid result suppied"); + RETURN_FALSE; + } + + + err = (char *)PQresultErrorField(pgsql_result, code); + + RETURN_STRING(err,1); +} +/* }}} */ /* {{{ proto string pg_result_error(resource result) Get error message associated with result */ --- ../../../../../../lang/php5/work/php-5.0.2/ext/pgsql/php_pgsql.h Thu Jan 8 17:32:40 2004 +++ php_pgsql.h Sat Nov 13 19:50:06 2004 @@ -105,6 +105,7 @@ PHP_FUNCTION(pg_get_pid); /* error message functions */ PHP_FUNCTION(pg_result_error); +PHP_FUNCTION(pg_result_error_field); PHP_FUNCTION(pg_last_error); PHP_FUNCTION(pg_last_notice); /* copy functions */

Antony Dovgal

21 years ago
On Sun, 14 Nov 2004 00:22:35 +0300 Илья Кантор <ilia@obnovlenie.ru> wrote:
> > In many situations one needs to know what caused the error > And probably receive additional information about it > (e.g which foreign key constraint failed). > > I attach tiny patches giving access to > PQresultErrorField function. > > Error messages may need adjustment. > > Tested on PHP 5.0.2 > > Example of usage: (from DB class) > > pg_send_query($this->link, 'INSERT INTO b VALUES(5)'); > $res = pg_get_result($this->link); > echo pg_result_error_field($res, PGSQL_DIAG_MESSAGE_DETAIL));
This patch doesn't compile for me with Postgres 7.4, because PGSQL_DIAG_* macros first appeared in 8.0 beta1, as far as I can see. So, please, add appropriate ifdefs and checks to config.m4 to make it compile with older versions. Btw, I think it would be reasonable to wait for stable realease of 8.0, as PG developers could change this macros and/or they behaviour.
-- Wbr, Antony Dovgal aka tony2001 tony2001@phpclub.net || antony@dovgal.com

Marcus Börger

21 years ago
Hello Antony, Sunday, November 14, 2004, 2:15:07 PM, you wrote:
> On Sun, 14 Nov 2004 00:22:35 +0300 > Илья Кантор <ilia@obnovlenie.ru> wrote:
>> >> In many situations one needs to know what caused the error >> And probably receive additional information about it >> (e.g which foreign key constraint failed). >> >> I attach tiny patches giving access to >> PQresultErrorField function. >> >> Error messages may need adjustment. >> >> Tested on PHP 5.0.2 >> >> Example of usage: (from DB class) >> >> pg_send_query($this->link, 'INSERT INTO b VALUES(5)'); >> $res = pg_get_result($this->link); >> echo pg_result_error_field($res, PGSQL_DIAG_MESSAGE_DETAIL));
> This patch doesn't compile for me with Postgres 7.4, because > PGSQL_DIAG_* macros first appeared in 8.0 beta1, as far as > I can see.
> So, please, add appropriate ifdefs and checks to config.m4 to > make it compile with older versions.
> Btw, I think it would be reasonable to wait for stable realease > of 8.0, as PG developers could change this macros and/or they behaviour.
Also please no patches for 5.0 or even 4. New functionality can only be done for HEAD.
-- Best regards, Marcus mailto:helly@php.net