Skip to content

ext/pgsql: adding pg_set_error_context_visibility. #11395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
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
3 changes: 3 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,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
22 changes: 22 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,9 @@ 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

/** @param PgSql\Connection|int $connection */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not needed any more now.
Feel free to remove this when merging if CI is happy.

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