# A short patch for pgsql extension

- Thread: https://externals.io/message/13830
- Original archive: https://news-web.php.net/php.internals/13830
- Started by ilia@obnovlenie.ru on 2004-11-13T17:37:42+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #13830 — ilia@obnovlenie.ru — 2004-11-13T17:37:42+00:00

URL: https://externals.io/message/13830#13830

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.

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 */
