Skip to content

PdoPgsql::setNoticeCallback() #14299

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

Merged
merged 7 commits into from
Jun 9, 2024
Merged
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
14 changes: 7 additions & 7 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -169,32 +169,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)

Expand Down
14 changes: 7 additions & 7 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -261,22 +261,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
Expand Down Expand Up @@ -544,7 +544,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:
Expand Down
17 changes: 15 additions & 2 deletions ext/pdo/php_pdo.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,24 @@ PHP_MINFO_FUNCTION(pdo);

#define LONG_CONST(c) (zend_long) c

#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 (!dbh->driver) { \
zend_throw_error(NULL, "%s object is uninitialized", ZSTR_VAL(Z_OBJ(EX(This))->ce->name)); \
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 */
30 changes: 30 additions & 0 deletions ext/pdo_pgsql/pdo_pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions ext/pdo_pgsql/pdo_pgsql.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ public function lobUnlink(string $oid): bool {}
public function getNotify(int $fetchMode = \PDO::FETCH_DEFAULT, int $timeoutMilliseconds = 0): array|false {}

public function getPid(): int {}

public function setNoticeCallback(?callable $callback): void {}
}
8 changes: 7 additions & 1 deletion ext/pdo_pgsql/pdo_pgsql_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions ext/pdo_pgsql/pgsql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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();
}
/* }}} */

Expand Down
5 changes: 3 additions & 2 deletions ext/pdo_pgsql/pgsql_driver.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function pgsqlGetNotify(int $fetchMode = PDO::FETCH_DEFAULT, int $timeout
/** @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.
*/
Comment on lines +37 to +39
Copy link
Member

Choose a reason for hiding this comment

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

This might make sense to add to other drivers in a follow-up PR.

}
8 changes: 1 addition & 7 deletions ext/pdo_pgsql/pgsql_driver_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ext/pdo_pgsql/php_pdo_pgsql_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
27 changes: 21 additions & 6 deletions ext/pdo_pgsql/tests/issue78621.inc
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
<?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");
$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));
Expand Down
4 changes: 2 additions & 2 deletions ext/pdo_pgsql/tests/issue78621.phpt
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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';
?>
Expand Down
6 changes: 3 additions & 3 deletions ext/pdo_pgsql/tests/issue78621_closure.phpt
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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;
}
}
Expand Down
Loading
Loading