Skip to content

Commit dd8514a

Browse files
committed
ext/pgsql: adding pg_set_error_context_visibility.
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
1 parent 5c78980 commit dd8514a

File tree

6 files changed

+65
-3
lines changed

6 files changed

+65
-3
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ PHP NEWS
166166
. pg_set_error_verbosity adding PGSQL_ERRORS_STATE constant. (David Carlier)
167167
. pg_convert/pg_insert E_WARNING on type errors had been converted to
168168
ValueError/TypeError exceptions. (David Carlier)
169+
. Added pg_set_error_context_visibility to set the context's visibility
170+
within the error messages. (David Carlier)
169171

170172
- Phar:
171173
. Fix memory leak in phar_rename_archive(). (stkeke)

UPGRADING

+3
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ PHP 8.3 UPGRADE NOTES
192192
. Added posix_fpathconf call to get configuration value from a file descriptor.
193193
. Added posix_eaccess call to check the effective user id's permission for a path.
194194

195+
- PGSQL:
196+
. Added pg_set_error_context_visilibity to set the visibility of the context in error messages.
197+
195198
- Random:
196199
. Added Randomizer::getBytesFromString().
197200
RFC: https://wiki.php.net/rfc/randomizer_additions

ext/pgsql/pgsql.c

+25-2
Original file line numberDiff line numberDiff line change
@@ -2833,6 +2833,29 @@ PHP_FUNCTION(pg_set_error_verbosity)
28332833
}
28342834
/* }}} */
28352835

2836+
PHP_FUNCTION(pg_set_error_context_visibility)
2837+
{
2838+
zval *pgsql_link = NULL;
2839+
zend_long visibility;
2840+
PGconn *pgsql;
2841+
pgsql_link_handle *link;
2842+
2843+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &pgsql_link, pgsql_link_ce, &visibility) == FAILURE) {
2844+
RETURN_THROWS();
2845+
}
2846+
link = Z_PGSQL_LINK_P(pgsql_link);
2847+
CHECK_PGSQL_LINK(link);
2848+
2849+
pgsql = link->conn;
2850+
2851+
if (visibility == PQSHOW_CONTEXT_NEVER || visibility & (PQSHOW_CONTEXT_ERRORS|PQSHOW_CONTEXT_ALWAYS)) {
2852+
RETURN_LONG(PQsetErrorContextVisibility(pgsql, visibility));
2853+
} else {
2854+
zend_argument_value_error(2, "must be one of PGSQL_SHOW_CONTEXT_NEVER, PGSQL_SHOW_CONTEXT_ERRORS or PGSQL_SHOW_CONTEXT_ALWAYS");
2855+
RETURN_THROWS();
2856+
}
2857+
}
2858+
28362859
/* {{{ Set client encoding */
28372860
PHP_FUNCTION(pg_set_client_encoding)
28382861
{
@@ -3331,7 +3354,7 @@ PHP_FUNCTION(pg_result_error)
33313354
RETURN_FALSE;
33323355
}
33333356

3334-
err = (char *)PQresultErrorMessage(pgsql_result);
3357+
err = PQresultErrorMessage(pgsql_result);
33353358
RETURN_STRING(err);
33363359
}
33373360
/* }}} */
@@ -3365,7 +3388,7 @@ PHP_FUNCTION(pg_result_error_field)
33653388
#endif
33663389
|PG_DIAG_CONTEXT|PG_DIAG_SOURCE_FILE|PG_DIAG_SOURCE_LINE
33673390
|PG_DIAG_SOURCE_FUNCTION)) {
3368-
field = (char *)PQresultErrorField(pgsql_result, (int)fieldcode);
3391+
field = PQresultErrorField(pgsql_result, (int)fieldcode);
33693392
if (field == NULL) {
33703393
RETURN_NULL();
33713394
} else {

ext/pgsql/pgsql.stub.php

+21
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,25 @@
462462
*/
463463
const PGSQL_PIPELINE_ABORTED = UNKNOWN;
464464
#endif
465+
466+
/* For pg_set_error_context_visibility() */
467+
468+
/**
469+
* @var int
470+
* @cvalue PQSHOW_CONTEXT_NEVER
471+
*/
472+
const PGSQL_SHOW_CONTEXT_NEVER = UNKNOWN;
473+
/**
474+
* @var int
475+
* @cvalue PQSHOW_CONTEXT_ERRORS
476+
*/
477+
const PGSQL_SHOW_CONTEXT_ERRORS = UNKNOWN;
478+
/**
479+
* @var int
480+
* @cvalue PQSHOW_CONTEXT_ALWAYS
481+
*/
482+
const PGSQL_SHOW_CONTEXT_ALWAYS = UNKNOWN;
483+
465484

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

@@ -951,6 +970,8 @@ function pg_exit_pipeline_mode(PgSql\Connection $connection): bool {}
951970
function pg_pipeline_sync(PgSql\Connection $connection): bool {}
952971
function pg_pipeline_status(PgSql\Connection $connection): int {}
953972
#endif
973+
974+
function pg_set_error_context_visibility(PgSql\Connection $connection, int $visibility): int {}
954975
}
955976

956977
namespace PgSql {

ext/pgsql/pgsql_arginfo.h

+11-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/pgsql/tests/07optional.phpt

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ if (function_exists('pg_set_error_verbosity')) {
2121
pg_set_error_verbosity($db, PGSQL_ERRORS_VERBOSE);
2222
pg_set_error_verbosity($db, PGSQL_ERRORS_SQLSTATE);
2323
}
24+
pg_set_error_context_visibility($db, PGSQL_SHOW_CONTEXT_NEVER);
25+
pg_set_error_context_visibility($db, PGSQL_SHOW_CONTEXT_ERRORS);
26+
pg_set_error_context_visibility($db, PGSQL_SHOW_CONTEXT_ALWAYS);
2427
echo "OK";
2528
?>
2629
--EXPECT--

0 commit comments

Comments
 (0)