Skip to content

Commit

Permalink
ext/pgsql: adding pg_set_error_context_visibility.
Browse files Browse the repository at this point in the history
another level of context for pg_last_error/pg_result_error() to include
or not the context in those. PQSHOW_CONTEXT_ERRORS being the default.

Close GH-11395
  • Loading branch information
devnexen committed Jun 13, 2023
1 parent 5c78980 commit dd8514a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ PHP NEWS
. pg_set_error_verbosity adding PGSQL_ERRORS_STATE constant. (David Carlier)
. pg_convert/pg_insert E_WARNING on type errors had been converted to
ValueError/TypeError exceptions. (David Carlier)
. Added pg_set_error_context_visibility to set the context's visibility
within the error messages. (David Carlier)

- Phar:
. Fix memory leak in phar_rename_archive(). (stkeke)
Expand Down
3 changes: 3 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ PHP 8.3 UPGRADE NOTES
. Added posix_fpathconf call to get configuration value from a file descriptor.
. Added posix_eaccess call to check the effective user id's permission for a path.

- PGSQL:
. Added pg_set_error_context_visilibity to set the visibility of the context in error messages.

- Random:
. Added Randomizer::getBytesFromString().
RFC: https://wiki.php.net/rfc/randomizer_additions
Expand Down
27 changes: 25 additions & 2 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -2833,6 +2833,29 @@ PHP_FUNCTION(pg_set_error_verbosity)
}
/* }}} */

PHP_FUNCTION(pg_set_error_context_visibility)
{
zval *pgsql_link = NULL;
zend_long visibility;
PGconn *pgsql;
pgsql_link_handle *link;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &pgsql_link, pgsql_link_ce, &visibility) == FAILURE) {
RETURN_THROWS();
}
link = Z_PGSQL_LINK_P(pgsql_link);
CHECK_PGSQL_LINK(link);

pgsql = link->conn;

if (visibility == PQSHOW_CONTEXT_NEVER || visibility & (PQSHOW_CONTEXT_ERRORS|PQSHOW_CONTEXT_ALWAYS)) {
RETURN_LONG(PQsetErrorContextVisibility(pgsql, visibility));
} else {
zend_argument_value_error(2, "must be one of PGSQL_SHOW_CONTEXT_NEVER, PGSQL_SHOW_CONTEXT_ERRORS or PGSQL_SHOW_CONTEXT_ALWAYS");
RETURN_THROWS();
}
}

/* {{{ Set client encoding */
PHP_FUNCTION(pg_set_client_encoding)
{
Expand Down Expand Up @@ -3331,7 +3354,7 @@ PHP_FUNCTION(pg_result_error)
RETURN_FALSE;
}

err = (char *)PQresultErrorMessage(pgsql_result);
err = PQresultErrorMessage(pgsql_result);
RETURN_STRING(err);
}
/* }}} */
Expand Down Expand Up @@ -3365,7 +3388,7 @@ PHP_FUNCTION(pg_result_error_field)
#endif
|PG_DIAG_CONTEXT|PG_DIAG_SOURCE_FILE|PG_DIAG_SOURCE_LINE
|PG_DIAG_SOURCE_FUNCTION)) {
field = (char *)PQresultErrorField(pgsql_result, (int)fieldcode);
field = PQresultErrorField(pgsql_result, (int)fieldcode);
if (field == NULL) {
RETURN_NULL();
} else {
Expand Down
21 changes: 21 additions & 0 deletions ext/pgsql/pgsql.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,25 @@
*/
const PGSQL_PIPELINE_ABORTED = UNKNOWN;
#endif

/* For pg_set_error_context_visibility() */

/**
* @var int
* @cvalue PQSHOW_CONTEXT_NEVER
*/
const PGSQL_SHOW_CONTEXT_NEVER = UNKNOWN;
/**
* @var int
* @cvalue PQSHOW_CONTEXT_ERRORS
*/
const PGSQL_SHOW_CONTEXT_ERRORS = UNKNOWN;
/**
* @var int
* @cvalue PQSHOW_CONTEXT_ALWAYS
*/
const PGSQL_SHOW_CONTEXT_ALWAYS = UNKNOWN;


function pg_connect(string $connection_string, int $flags = 0): PgSql\Connection|false {}

Expand Down Expand Up @@ -951,6 +970,8 @@ function pg_exit_pipeline_mode(PgSql\Connection $connection): bool {}
function pg_pipeline_sync(PgSql\Connection $connection): bool {}
function pg_pipeline_status(PgSql\Connection $connection): int {}
#endif

function pg_set_error_context_visibility(PgSql\Connection $connection, int $visibility): int {}
}

namespace PgSql {
Expand Down
12 changes: 11 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.

3 changes: 3 additions & 0 deletions ext/pgsql/tests/07optional.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ if (function_exists('pg_set_error_verbosity')) {
pg_set_error_verbosity($db, PGSQL_ERRORS_VERBOSE);
pg_set_error_verbosity($db, PGSQL_ERRORS_SQLSTATE);
}
pg_set_error_context_visibility($db, PGSQL_SHOW_CONTEXT_NEVER);
pg_set_error_context_visibility($db, PGSQL_SHOW_CONTEXT_ERRORS);
pg_set_error_context_visibility($db, PGSQL_SHOW_CONTEXT_ALWAYS);
echo "OK";
?>
--EXPECT--
Expand Down

0 comments on commit dd8514a

Please sign in to comment.