Skip to content

Commit c77c243

Browse files
committed
ext/pgsql: introducing pg_result_verbose_error().
unlike pg_set_error_context_visibility()/pg_set_error_verbosity(), it is affecting only a particular PgSql\Result instance rather than the whole PgSql\Connection, e.g. debugging a problematic SQL request. Available since postgresql 14.
1 parent 335c0b3 commit c77c243

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

ext/pgsql/config.m4

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ if test "$PHP_PGSQL" != "no"; then
3131
PHP_CHECK_LIBRARY([pq], [PQclosePrepared],
3232
[AC_DEFINE([HAVE_PG_CLOSE_STMT], [1], [PostgreSQL 17 or later])],,
3333
[$PGSQL_LIBS])
34+
PHP_CHECK_LIBRARY([pq], [PQresultVerboseErrorMessage],
35+
[AC_DEFINE([HAVE_PG_RESULT_VERBOSE_ERROR_MESSAGE], [1], [PostgreSQL 14 or later])],,
36+
[$PGSQL_LIBS])
3437

3538
old_CFLAGS=$CFLAGS
3639
CFLAGS="$CFLAGS $PGSQL_CFLAGS"

ext/pgsql/pgsql.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6347,3 +6347,39 @@ PHP_FUNCTION(pg_close_stmt)
63476347
}
63486348
}
63496349
#endif
6350+
6351+
#if defined(HAVE_PG_RESULT_VERBOSE_ERROR_MESSAGE)
6352+
PHP_FUNCTION(pg_result_verbose_error)
6353+
{
6354+
zval *result;
6355+
pgsql_result_handle *pg_result;
6356+
zend_long verbosity, visibility;
6357+
char *err = NULL;
6358+
6359+
ZEND_PARSE_PARAMETERS_START(1, 1)
6360+
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
6361+
Z_PARAM_LONG(verbosity)
6362+
Z_PARAM_LONG(visibility)
6363+
ZEND_PARSE_PARAMETERS_END();
6364+
6365+
if (!(verbosity & (PQERRORS_TERSE|PQERRORS_DEFAULT|PQERRORS_VERBOSE|PQERRORS_SQLSTATE))) {
6366+
zend_argument_value_error(2, "verbosity must be one of the PQERRORS_* constants");
6367+
RETURN_THROWS();
6368+
}
6369+
if (visibility < PQSHOW_CONTEXT_NEVER || !(visibility & (PQSHOW_CONTEXT_ERRORS|PQSHOW_CONTEXT_ALWAYS))) {
6370+
zend_argument_value_error(3, "visibility must be one of the PQSHOW_CONTEXT_* constants");
6371+
RETURN_THROWS();
6372+
}
6373+
6374+
pg_result = Z_PGSQL_RESULT_P(result);
6375+
CHECK_PGSQL_RESULT(pg_result);
6376+
6377+
err = PQresultVerboseErrorMessage(pg_result->result, verbosity, visibility);
6378+
if (UNEXPECTED(!err)) {
6379+
RETURN_FALSE;
6380+
} else {
6381+
RETVAL_STRING(err);
6382+
PQfreemem(err);
6383+
}
6384+
}
6385+
#endif

ext/pgsql/pgsql.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,10 @@ function pg_set_chunked_rows_size(PgSql\Connection $connection, int $size): bool
973973
#ifdef HAVE_PG_CLOSE_STMT
974974
function pg_close_stmt(Pgsql\Connection $connection, string $statement_name): Pgsql\Result|false {}
975975
#endif
976+
#ifdef HAVE_PG_RESULT_ERROR_MESSAGE
977+
/** @refcount 1 */
978+
function pg_result_verbose_error(PgSql\Result $result, int $verbosity, int $visibility): string|false {}
979+
#endif
976980
}
977981

978982
namespace PgSql {

ext/pgsql/pgsql_arginfo.h

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)