Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
devnexen committed Mar 30, 2025
commit c77c2431759311247c929a81e71e13c6b19f6ea3
3 changes: 3 additions & 0 deletions ext/pgsql/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ if test "$PHP_PGSQL" != "no"; then
PHP_CHECK_LIBRARY([pq], [PQclosePrepared],
[AC_DEFINE([HAVE_PG_CLOSE_STMT], [1], [PostgreSQL 17 or later])],,
[$PGSQL_LIBS])
PHP_CHECK_LIBRARY([pq], [PQresultVerboseErrorMessage],
[AC_DEFINE([HAVE_PG_RESULT_VERBOSE_ERROR_MESSAGE], [1], [PostgreSQL 14 or later])],,
[$PGSQL_LIBS])

old_CFLAGS=$CFLAGS
CFLAGS="$CFLAGS $PGSQL_CFLAGS"
Expand Down
36 changes: 36 additions & 0 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -6347,3 +6347,39 @@ PHP_FUNCTION(pg_close_stmt)
}
}
#endif

#if defined(HAVE_PG_RESULT_VERBOSE_ERROR_MESSAGE)
PHP_FUNCTION(pg_result_verbose_error)
{
zval *result;
pgsql_result_handle *pg_result;
zend_long verbosity, visibility;
char *err = NULL;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
Z_PARAM_LONG(verbosity)
Z_PARAM_LONG(visibility)
ZEND_PARSE_PARAMETERS_END();

if (!(verbosity & (PQERRORS_TERSE|PQERRORS_DEFAULT|PQERRORS_VERBOSE|PQERRORS_SQLSTATE))) {
zend_argument_value_error(2, "verbosity must be one of the PQERRORS_* constants");
RETURN_THROWS();
}
if (visibility < PQSHOW_CONTEXT_NEVER || !(visibility & (PQSHOW_CONTEXT_ERRORS|PQSHOW_CONTEXT_ALWAYS))) {
zend_argument_value_error(3, "visibility must be one of the PQSHOW_CONTEXT_* constants");
RETURN_THROWS();
}

pg_result = Z_PGSQL_RESULT_P(result);
CHECK_PGSQL_RESULT(pg_result);

err = PQresultVerboseErrorMessage(pg_result->result, verbosity, visibility);
if (UNEXPECTED(!err)) {
RETURN_FALSE;
} else {
RETVAL_STRING(err);
PQfreemem(err);
}
}
#endif
4 changes: 4 additions & 0 deletions ext/pgsql/pgsql.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,10 @@ function pg_set_chunked_rows_size(PgSql\Connection $connection, int $size): bool
#ifdef HAVE_PG_CLOSE_STMT
function pg_close_stmt(Pgsql\Connection $connection, string $statement_name): Pgsql\Result|false {}
#endif
#ifdef HAVE_PG_RESULT_ERROR_MESSAGE
/** @refcount 1 */
function pg_result_verbose_error(PgSql\Result $result, int $verbosity, int $visibility): string|false {}
#endif
}

namespace PgSql {
Expand Down
16 changes: 15 additions & 1 deletion ext/pgsql/pgsql_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading