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 1 commit
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
34 changes: 32 additions & 2 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -2833,6 +2833,36 @@ 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_NUM_ARGS() == 1) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &visibility) == FAILURE) {
RETURN_THROWS();
}
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Considering we have deprecated the fetching of the default link I would rather not introduce a new function with this functionality. Would also make the stubs correct.

Copy link
Member Author

Choose a reason for hiding this comment

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

oky doky

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|PQSHOW_CONTEXT_ERRORS|PQSHOW_CONTEXT_ALWAYS)) {
RETURN_LONG(PQsetErrorContextVisibility(pgsql, visibility));
} else {
RETURN_FALSE;
}
Copy link
Member

Choose a reason for hiding this comment

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

Make this a ValueError in case of a wrong value?

}

/* {{{ Set client encoding */
PHP_FUNCTION(pg_set_client_encoding)
{
Expand Down Expand Up @@ -3331,7 +3361,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 +3395,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($connection, int $visibility = UNKNOWN): int|false {}
Copy link
Member

@Girgias Girgias Jun 13, 2023

Choose a reason for hiding this comment

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

Since the visibility is not optional anymore and it doesn't return false on failure.

Suggested change
/** @param PgSql\Connection|int $connection */
function pg_set_error_context_visibility($connection, int $visibility = UNKNOWN): int|false {}
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.

5 changes: 5 additions & 0 deletions ext/pgsql/tests/07optional.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ if (function_exists('pg_set_error_verbosity')) {
pg_set_error_verbosity($db, PGSQL_ERRORS_VERBOSE);
pg_set_error_verbosity($db, PGSQL_ERRORS_SQLSTATE);
}
if (function_exists('pg_set_error_context_visibility')) {
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);
}
Copy link
Member

Choose a reason for hiding this comment

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

It looks like it's always defined?

echo "OK";
?>
--EXPECT--
Expand Down