Skip to content

Commit 4144c98

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.
1 parent 6e04050 commit 4144c98

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

ext/pgsql/pgsql.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,6 +2833,36 @@ 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_NUM_ARGS() == 1) {
2844+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &visibility) == FAILURE) {
2845+
RETURN_THROWS();
2846+
}
2847+
link = FETCH_DEFAULT_LINK();
2848+
CHECK_DEFAULT_LINK(link);
2849+
} else {
2850+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &pgsql_link, pgsql_link_ce, &visibility) == FAILURE) {
2851+
RETURN_THROWS();
2852+
}
2853+
link = Z_PGSQL_LINK_P(pgsql_link);
2854+
CHECK_PGSQL_LINK(link);
2855+
}
2856+
2857+
pgsql = link->conn;
2858+
2859+
if (visibility & (PQSHOW_CONTEXT_NEVER|PQSHOW_CONTEXT_ERRORS|PQSHOW_CONTEXT_ALWAYS)) {
2860+
RETURN_LONG(PQsetErrorContextVisibility(pgsql, visibility));
2861+
} else {
2862+
RETURN_FALSE;
2863+
}
2864+
}
2865+
28362866
/* {{{ Set client encoding */
28372867
PHP_FUNCTION(pg_set_client_encoding)
28382868
{
@@ -3331,7 +3361,7 @@ PHP_FUNCTION(pg_result_error)
33313361
RETURN_FALSE;
33323362
}
33333363

3334-
err = (char *)PQresultErrorMessage(pgsql_result);
3364+
err = PQresultErrorMessage(pgsql_result);
33353365
RETURN_STRING(err);
33363366
}
33373367
/* }}} */
@@ -3365,7 +3395,7 @@ PHP_FUNCTION(pg_result_error_field)
33653395
#endif
33663396
|PG_DIAG_CONTEXT|PG_DIAG_SOURCE_FILE|PG_DIAG_SOURCE_LINE
33673397
|PG_DIAG_SOURCE_FUNCTION)) {
3368-
field = (char *)PQresultErrorField(pgsql_result, (int)fieldcode);
3398+
field = PQresultErrorField(pgsql_result, (int)fieldcode);
33693399
if (field == NULL) {
33703400
RETURN_NULL();
33713401
} else {

ext/pgsql/pgsql.stub.php

Lines changed: 22 additions & 0 deletions
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,9 @@ 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+
/** @param PgSql\Connection|int $connection */
975+
function pg_set_error_context_visibility($connection, int $visibility = UNKNOWN): int|false {}
954976
}
955977

956978
namespace PgSql {

ext/pgsql/pgsql_arginfo.h

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/pgsql/tests/07optional.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ 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+
if (function_exists('pg_set_error_context_visibility')) {
25+
pg_set_error_context_visibility($db, PGSQL_SHOW_CONTEXT_NEVER);
26+
pg_set_error_context_visibility($db, PGSQL_SHOW_CONTEXT_ERRORS);
27+
pg_set_error_context_visibility($db, PGSQL_SHOW_CONTEXT_ALWAYS);
28+
}
2429
echo "OK";
2530
?>
2631
--EXPECT--

0 commit comments

Comments
 (0)