Add Pdo\Pgsql::setNoticeCallback() (#14299)

This moves the new method from magically being added to the PDO class from the driver to just be available on the dedicated subclass. 

Drive-by fixes to NEWS and UPGRADING
This commit is contained in:
Guillaume Outters 2024-06-09 04:04:51 +02:00 committed by GitHub
parent 25579a8616
commit a9259c0496
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 221 additions and 51 deletions

14
NEWS
View file

@ -170,32 +170,32 @@ PHP NEWS
- PDO_DBLIB:
. Fixed setAttribute and getAttribute. (SakiTakamachi)
. Added class PdoDbLib. (danack, kocsismate)
. Added class Pdo\DbLib. (danack, kocsismate)
- PDO_FIREBIRD:
. Fixed setAttribute and getAttribute. (SakiTakamachi)
. Feature: Add transaction isolation level and mode settings to pdo_firebird.
(SakiTakamachi)
. Added class PdoFirebird. (danack, kocsismate)
. Added class Pdo\Firebird. (danack, kocsismate)
- PDO_MYSQL:
. Fixed setAttribute and getAttribute. (SakiTakamachi)
. Added class PdoMysql. (danack, kocsismate)
. Added class Pdo\Mysql. (danack, kocsismate)
- PDO_ODBC:
. Added class PdoOdbc. (danack, kocsismate)
. Added class Pdo\Odbc. (danack, kocsismate)
- PDO_PGSQL:
. Fixed GH-12423, DSN credentials being prioritized over the user/password
PDO constructor arguments. (SakiTakamachi)
. Fixed native float support with pdo_pgsql query results. (Yurunsoft)
. Added class PdoPgsql. (danack, kocsismate)
. Added class Pdo\Pgsql. (danack, kocsismate)
. Retrieve the memory usage of the query result resource. (KentarouTakeda)
. Added PDO::pgsqlSetNoticeCallBack method to receive DB notices.
. Added Pdo\Pgsql::setNoticeCallBack method to receive DB notices.
(outtersg)
- PDO_SQLITE:
. Added class PdoSqlite. (danack, kocsismate)
. Added class Pdo\Sqlite. (danack, kocsismate)
. Fixed bug #81227 (PDO::inTransaction reports false when in transaction).
(nielsdos)

View file

@ -263,22 +263,22 @@ PHP 8.4 UPGRADE NOTES
or by invoking their constructor directly.
- PDO_DBLIB:
. Added class PdoDbLib.
. Added class Pdo\DbLib.
- PDO_FIREBIRD:
. Added class PdoFirebird.
. Added class Pdo\Firebird.
- PDO_MYSQL:
. Added class PdoMysql.
. Added class Pdo\Mysql.
- PDO_ODBC:
. Added class PdoOdbc.
. Added class Pdo\Odbc.
- PDO_PGSQL:
. Added class PdoPgsql.
. Added class Pdo\Pgsql.
- PDO_SQLITE:
. Added class PdoSqlite.
. Added class Pdo\Sqlite.
- POSIX:
. Added constant POSIX_SC_CHILD_MAX
@ -546,7 +546,7 @@ PHP 8.4 UPGRADE NOTES
energy consumption) of the current process and pcntl_setqos_class to set it.
- PDO_PGSQL:
. Added PDO::pgsqlSetNoticeCallback to allow a callback to be triggered on
. Added Pdo\Pgsql::setNoticeCallback() to allow a callback to be triggered on
every notice sent (e.g. RAISE NOTICE).
- PGSQL:

View file

@ -55,11 +55,24 @@ PHP_MINFO_FUNCTION(pdo);
#define LONG_CONST(c) (zend_long) c
#define PDO_CONSTRUCT_CHECK \
if (!dbh->driver) { \
#define PDO_CONSTRUCT_CHECK_COND dbh->driver
#define PDO_CONSTRUCT_CHECK_FAIL() \
{ \
zend_throw_error(NULL, "%s object is uninitialized", ZSTR_VAL(Z_OBJ(EX(This))->ce->name)); \
} \
#define PDO_CONSTRUCT_CHECK \
if (!(PDO_CONSTRUCT_CHECK_COND)) { \
PDO_CONSTRUCT_CHECK_FAIL(); \
RETURN_THROWS(); \
} \
#define PDO_CONSTRUCT_CHECK_WITH_CLEANUP(cleanup) \
if (!(PDO_CONSTRUCT_CHECK_COND)) { \
PDO_CONSTRUCT_CHECK_FAIL(); \
goto cleanup; \
} \
#endif /* PHP_PDO_H */

View file

@ -145,6 +145,36 @@ PHP_METHOD(Pdo_Pgsql, getPid)
pgsqlGetPid_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* Sets a callback to receive DB notices (after client_min_messages has been set */
PHP_METHOD(Pdo_Pgsql, setNoticeCallback)
{
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fcc = empty_fcall_info_cache;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "F!", &fci, &fcc)) {
RETURN_THROWS();
}
pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
PDO_CONSTRUCT_CHECK_WITH_CLEANUP(cleanup);
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
pdo_pgsql_cleanup_notice_callback(H);
if (ZEND_FCC_INITIALIZED(fcc)) {
H->notice_callback = emalloc(sizeof(zend_fcall_info_cache));
zend_fcc_dup(H->notice_callback, &fcc);
}
return;
cleanup:
if (ZEND_FCC_INITIALIZED(fcc)) {
zend_fcc_dtor(&fcc);
}
RETURN_THROWS();
}
/* true global environment */
/* {{{ PHP_MINIT_FUNCTION */

View file

@ -56,4 +56,6 @@ class Pgsql extends PDO
public function getNotify(int $fetchMode = \PDO::FETCH_DEFAULT, int $timeoutMilliseconds = 0): array|false {}
public function getPid(): int {}
public function setNoticeCallback(?callable $callback): void {}
}

View file

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 519530b2da60dbd7a70af6d7c566d93930cfbb24 */
* Stub hash: 92d66416beab5094d0ca70565e691eacc3049d9c */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Pdo_Pgsql_escapeIdentifier, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, input, IS_STRING, 0)
@ -50,6 +50,10 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Pdo_Pgsql_getPid, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Pdo_Pgsql_setNoticeCallback, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, callback, IS_CALLABLE, 1)
ZEND_END_ARG_INFO()
ZEND_METHOD(Pdo_Pgsql, escapeIdentifier);
ZEND_METHOD(Pdo_Pgsql, copyFromArray);
ZEND_METHOD(Pdo_Pgsql, copyFromFile);
@ -60,6 +64,7 @@ ZEND_METHOD(Pdo_Pgsql, lobOpen);
ZEND_METHOD(Pdo_Pgsql, lobUnlink);
ZEND_METHOD(Pdo_Pgsql, getNotify);
ZEND_METHOD(Pdo_Pgsql, getPid);
ZEND_METHOD(Pdo_Pgsql, setNoticeCallback);
static const zend_function_entry class_Pdo_Pgsql_methods[] = {
ZEND_ME(Pdo_Pgsql, escapeIdentifier, arginfo_class_Pdo_Pgsql_escapeIdentifier, ZEND_ACC_PUBLIC)
@ -72,6 +77,7 @@ static const zend_function_entry class_Pdo_Pgsql_methods[] = {
ZEND_ME(Pdo_Pgsql, lobUnlink, arginfo_class_Pdo_Pgsql_lobUnlink, ZEND_ACC_PUBLIC)
ZEND_ME(Pdo_Pgsql, getNotify, arginfo_class_Pdo_Pgsql_getNotify, ZEND_ACC_PUBLIC)
ZEND_ME(Pdo_Pgsql, getPid, arginfo_class_Pdo_Pgsql_getPid, ZEND_ACC_PUBLIC)
ZEND_ME(Pdo_Pgsql, setNoticeCallback, arginfo_class_Pdo_Pgsql_setNoticeCallback, ZEND_ACC_PUBLIC)
ZEND_FE_END
};

View file

@ -131,7 +131,7 @@ static void pdo_pgsql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *i
}
/* }}} */
static void pdo_pgsql_cleanup_notice_callback(pdo_pgsql_db_handle *H) /* {{{ */
void pdo_pgsql_cleanup_notice_callback(pdo_pgsql_db_handle *H) /* {{{ */
{
if (H->notice_callback) {
zend_fcc_dtor(H->notice_callback);
@ -1241,8 +1241,7 @@ PHP_METHOD(PDO_PGSql_Ext, pgsqlGetPid)
}
/* }}} */
/* {{{ proto void PDO::pgsqlSetNoticeCallback(mixed callback)
Sets a callback to receive DB notices (after client_min_messages has been set) */
/* {{{ Sets a callback to receive DB notices (after client_min_messages has been set) */
PHP_METHOD(PDO_PGSql_Ext, pgsqlSetNoticeCallback)
{
zend_fcall_info fci = empty_fcall_info;
@ -1252,7 +1251,7 @@ PHP_METHOD(PDO_PGSql_Ext, pgsqlSetNoticeCallback)
}
pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
PDO_CONSTRUCT_CHECK;
PDO_CONSTRUCT_CHECK_WITH_CLEANUP(cleanup);
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
@ -1262,6 +1261,14 @@ PHP_METHOD(PDO_PGSql_Ext, pgsqlSetNoticeCallback)
H->notice_callback = emalloc(sizeof(zend_fcall_info_cache));
zend_fcc_dup(H->notice_callback, &fcc);
}
return;
cleanup:
if (ZEND_FCC_INITIALIZED(fcc)) {
zend_fcc_dtor(&fcc);
}
RETURN_THROWS();
}
/* }}} */

View file

@ -34,6 +34,7 @@ class PDO_PGSql_Ext {
/** @tentative-return-type */
public function pgsqlGetPid(): int {}
/** @tentative-return-type */
public function pgsqlSetNoticeCallback(?callable $callback): void {}
/* Do NOT add new methods here. See https://wiki.php.net/rfc/pdo_driver_specific_subclasses
* Any new feature should be declared only on Pdo\Pgsql.
*/
}

View file

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 14174ab18f198b9916f83986d10c93b657d8ffb9 */
* Stub hash: dd20abc5d8580d72b25bfb3c598b1ca54a501fcc */
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_PDO_PGSql_Ext_pgsqlCopyFromArray, 0, 2, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, tableName, IS_STRING, 0)
@ -46,10 +46,6 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_PDO_PGSql_Ext_pgsqlGetPid, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_PDO_PGSql_Ext_pgsqlSetNoticeCallback, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, callback, IS_CALLABLE, 1)
ZEND_END_ARG_INFO()
ZEND_METHOD(PDO_PGSql_Ext, pgsqlCopyFromArray);
ZEND_METHOD(PDO_PGSql_Ext, pgsqlCopyFromFile);
ZEND_METHOD(PDO_PGSql_Ext, pgsqlCopyToArray);
@ -59,7 +55,6 @@ ZEND_METHOD(PDO_PGSql_Ext, pgsqlLOBOpen);
ZEND_METHOD(PDO_PGSql_Ext, pgsqlLOBUnlink);
ZEND_METHOD(PDO_PGSql_Ext, pgsqlGetNotify);
ZEND_METHOD(PDO_PGSql_Ext, pgsqlGetPid);
ZEND_METHOD(PDO_PGSql_Ext, pgsqlSetNoticeCallback);
static const zend_function_entry class_PDO_PGSql_Ext_methods[] = {
ZEND_ME(PDO_PGSql_Ext, pgsqlCopyFromArray, arginfo_class_PDO_PGSql_Ext_pgsqlCopyFromArray, ZEND_ACC_PUBLIC)
@ -71,6 +66,5 @@ static const zend_function_entry class_PDO_PGSql_Ext_methods[] = {
ZEND_ME(PDO_PGSql_Ext, pgsqlLOBUnlink, arginfo_class_PDO_PGSql_Ext_pgsqlLOBUnlink, ZEND_ACC_PUBLIC)
ZEND_ME(PDO_PGSql_Ext, pgsqlGetNotify, arginfo_class_PDO_PGSql_Ext_pgsqlGetNotify, ZEND_ACC_PUBLIC)
ZEND_ME(PDO_PGSql_Ext, pgsqlGetPid, arginfo_class_PDO_PGSql_Ext_pgsqlGetPid, ZEND_ACC_PUBLIC)
ZEND_ME(PDO_PGSql_Ext, pgsqlSetNoticeCallback, arginfo_class_PDO_PGSql_Ext_pgsqlSetNoticeCallback, ZEND_ACC_PUBLIC)
ZEND_FE_END
};

View file

@ -108,6 +108,8 @@ enum pdo_pgsql_specific_constants {
php_stream *pdo_pgsql_create_lob_stream(zval *pdh, int lfd, Oid oid);
extern const php_stream_ops pdo_pgsql_lob_stream_ops;
void pdo_pgsql_cleanup_notice_callback(pdo_pgsql_db_handle *H);
void pdo_libpq_version(char *buf, size_t len);
void pdo_pgsql_close_lob_streams(pdo_dbh_t *dbh);

View file

@ -1,9 +1,14 @@
<?php
require_once dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
require_once dirname(__FILE__) . '/config.inc';
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
if (!isset($db)) {
$db = new Pdo\Pgsql($config['ENV']['PDOTEST_DSN']);
}
if (!isset($rounds) || empty($rounds)) {
$rounds = [ null, 'Re' ];
}
attach($db);
attach($db, array_shift($rounds));
$db->beginTransaction();
$db->exec("set client_min_messages to notice");
@ -11,10 +16,20 @@ $db->exec("create temporary table t (a varchar(3))");
$db->exec("create function hey() returns trigger as \$\$ begin new.a := 'oh'; raise notice 'I tampered your data, did you know?'; return new; end; \$\$ language plpgsql");
$db->exec("create trigger hop before insert on t for each row execute procedure hey()");
$db->exec("insert into t values ('ah')");
attach($db, 'Re');
$db->exec("delete from t");
$db->exec("insert into t values ('ah')");
$db->pgsqlSetNoticeCallback(null);
while (count($rounds)) {
try {
attach($db, array_shift($rounds));
} catch (Throwable $err) {
echo "Caught ".get_class($err).": ".$err->getMessage()."\n";
}
try {
$db->exec("delete from t");
$db->exec("insert into t values ('ah')");
} catch (Throwable $err) {
echo "Caught ".get_class($err)." ".$err->getMessage()."\n";
}
}
$db->setNoticeCallback(null);
$db->exec("delete from t");
$db->exec("insert into t values ('ah')");
var_dump($db->query("select * from t")->fetchAll(PDO::FETCH_ASSOC));

View file

@ -1,5 +1,5 @@
--TEST--
pgsqlSetNoticeCallback catches Postgres "raise notice".
Pdo\Pgsql::setNoticeCallback catches Postgres "raise notice".
--SKIPIF--
<?php
if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
@ -13,7 +13,7 @@ function disp($message) { echo trim($message)."\n"; }
function dispRe($message) { echo "Re".trim($message)."\n"; }
function attach($db, $prefix = '')
{
$db->pgsqlSetNoticeCallback('disp'.$prefix);
$db->setNoticeCallback('disp'.$prefix);
}
require dirname(__FILE__) . '/issue78621.inc';
?>

View file

@ -1,5 +1,5 @@
--TEST--
pgsqlSetNoticeCallback catches Postgres "raise notice".
Pdo\Pgsql::setNoticeCallback catches Postgres "raise notice".
--SKIPIF--
<?php
if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
@ -16,13 +16,13 @@ function attach($db, $prefix = '')
switch($flavor)
{
case 0:
$db->pgsqlSetNoticeCallback(function($message) use($prefix) { echo $prefix.trim($message)."\n"; });
$db->setNoticeCallback(function($message) use($prefix) { echo $prefix.trim($message)."\n"; });
// https://github.com/php/php-src/pull/4823#pullrequestreview-335623806
$eraseCallbackMemoryHere = (object)[1];
break;
case 1:
$closure = function($message) use($prefix) { echo $prefix.'('.get_class($this).')'.trim($message)."\n"; };
$db->pgsqlSetNoticeCallback($closure->bindTo(new \stdClass));
$db->setNoticeCallback($closure->bindTo(new \stdClass));
break;
}
}

View file

@ -1,5 +1,5 @@
--TEST--
pgsqlSetNoticeCallback catches Postgres "raise notice".
Pdo\Pgsql::setNoticeCallback catches Postgres "raise notice".
--SKIPIF--
<?php
if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
@ -16,26 +16,24 @@ class Logger
public function __call(string $method, array $args)
{
$realMethod = strtr($method, [ 'whatever' => 'disp' ]);
if (!method_exists($this, $realMethod)) {
throw new BadMethodCallException('Call to undefined method '.__CLASS__.'::'.$realMethod);
}
echo "$method trampoline for $realMethod\n";
return call_user_func_array([ $this, $realMethod ], $args);
}
}
$logger = new Logger();
function attach($db, $prefix = '')
function attach($db, $method)
{
global $logger;
global $flavor;
switch($flavor)
{
case 0: $db->pgsqlSetNoticeCallback([ $logger, 'disp'.$prefix ]); break;
case 1: $db->pgsqlSetNoticeCallback([ $logger, 'whatever'.$prefix ]); break;
}
$db->setNoticeCallback([ $logger, $method ]);
}
echo "Testing with method explicitely plugged:\n";
$flavor = 0;
$rounds = [ 'disp', 'dispRe' ];
require dirname(__FILE__) . '/issue78621.inc';
echo "Testing with a bit of magic:\n";
++$flavor;
$rounds = [ 'whatever', 'whateverRe', 'unexisting' ];
require dirname(__FILE__) . '/issue78621.inc';
?>
--EXPECT--
@ -55,6 +53,7 @@ whatever trampoline for disp
NOTICE: I tampered your data, did you know?
whateverRe trampoline for dispRe
ReNOTICE: I tampered your data, did you know?
Caught BadMethodCallException Call to undefined method Logger::unexisting
array(1) {
[0]=>
array(1) {

View file

@ -0,0 +1,43 @@
--TEST--
Pdo\Pgsql::setNoticeCallback()
--EXTENSIONS--
pdo
pdo_pgsql
--SKIPIF--
<?php
require __DIR__ . '/config.inc';
require dirname(__DIR__, 2) . '/pdo/tests/pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php
require_once __DIR__ . "/config.inc";
$db = new Pdo\Pgsql($config['ENV']['PDOTEST_DSN']);
function disp($message) { echo trim($message)."\n"; }
function attach($db, $callback) { $db->setNoticeCallback($callback); }
$rounds = [
'disp', // Correct.
3, // Error, so the old callback is kept, and will be used in the call that follows the caught error.
null, // No callback. Hopefully this clears everything.
'wouldAnyoneNameAFunctionThatWay', // So this one will crash and *no output will follow*.
];
require __DIR__ . '/issue78621.inc';
?>
--EXPECTF--
NOTICE: I tampered your data, did you know?
Caught TypeError: %s: Argument #1 ($callback) %s
NOTICE: I tampered your data, did you know?
Caught TypeError: %s: Argument #1 ($callback) %s
array(1) {
[0]=>
array(1) {
["a"]=>
string(2) "oh"
}
}
Done

View file

@ -0,0 +1,58 @@
--TEST--
Pdo\Pgsql::setNoticeCallback() use F ZPP for trampoline callback and does not leak
--EXTENSIONS--
pdo
pdo_pgsql
--SKIPIF--
<?php
require __DIR__ . '/config.inc';
require dirname(__DIR__, 2) . '/pdo/tests/pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php
require_once __DIR__ . "/config.inc";
$db = new Pdo\Pgsql($config['ENV']['PDOTEST_DSN']);
function disp($message) { echo trim($message)."\n"; }
function attach($db, $callback)
{
if (is_array($callback) && $callback[0] === null) {
$callback = $callback[1];
$rc = new ReflectionClass(Pdo\Pgsql::class);
$db = $rc->newInstanceWithoutConstructor();
}
$db->setNoticeCallback($callback);
}
class T { public function z($m) { echo $m."\n"; } public function __call($m, $p) { echo "bah $m\n"; } }
$t = new T;
$rounds = [
[ $t, 'disp' ],
3, // Error; but then as the old callback is kept, it will be used in the call that follows the caught error.
null, // No callback: clear everything.
'wouldAnyoneNameAFunctionThisWay', // So this one will crash and *no output will follow*.
[ null, [ $t, 'disp' ] ], // Valid callback on an unvalid Pdo.
[ $t, 'disp' ],
];
require __DIR__ . '/issue78621.inc';
?>
--EXPECTF--
bah disp
Caught TypeError: %s: Argument #1 ($callback) %s
bah disp
Caught TypeError: %s: Argument #1 ($callback) %s
Caught Error: %s object is uninitialized
bah disp
array(1) {
[0]=>
array(1) {
["a"]=>
string(2) "oh"
}
}
Done