php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #78621
Patch pgsqlSetNoticeCallback.8.diff revision 2021-03-11 17:35 UTC by guillaume-php at outters dot eu
Patch pgsqlSetNoticeCallback.7.diff revision 2019-10-02 05:05 UTC by guillaume-php at outters dot eu

Patch pgsqlSetNoticeCallback.7.diff for PDO PgSQL Bug #78621

Patch version 2019-10-02 05:05 UTC

Return to Bug #78621 | Download this patch
Patch Revisions:

Developer: guillaume-php@outters.eu

diff -ruw ext/pdo_pgsql/pgsql_driver.c ext/pdo_pgsql/pgsql_driver.c
--- ext/pdo_pgsql/pgsql_driver.c	2015-09-01 22:09:37.000000000 +0200
+++ ext/pdo_pgsql/pgsql_driver.c	2019-10-01 05:18:31.085807000 +0200
@@ -98,7 +98,19 @@
 
 static void _pdo_pgsql_notice(pdo_dbh_t *dbh, const char *message) /* {{{ */
 {
-/*	pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; */
+	int ret;
+	zval zarg;
+	zval retval;
+	pdo_pgsql_fci * fc;
+	if ((fc = ((pdo_pgsql_db_handle *)dbh->driver_data)->notice_callback)) {
+		ZVAL_STRINGL(&zarg, (char *) message, strlen(message));
+		fc->fci.param_count = 1;
+		fc->fci.params = &zarg;
+		fc->fci.retval = &retval;
+		if ((ret = zend_call_function(&fc->fci, &fc->fcc TSRMLS_CC)) == FAILURE) {
+			pdo_raise_impl_error(dbh, NULL, "HY000", "could not call user-supplied function" TSRMLS_CC);
+		}
+	}
 }
 /* }}} */
 
@@ -975,6 +990,55 @@
 }
 /* }}} */
 
+/* {{{ proto bool PDO::pgsqlSetNoticeCallback(mixed callback)
+   Sets a callback to receive DB notices (after client_min_messages has been set) */
+static PHP_METHOD(PDO, pgsqlSetNoticeCallback)
+{
+	zval *callback;
+	zend_string *cbname;
+	pdo_dbh_t *dbh;
+	pdo_pgsql_db_handle *H;
+	int ret;
+	pdo_pgsql_fci *fc;
+
+	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &callback)) {
+		RETURN_FALSE;
+	}
+	
+	dbh = Z_PDO_DBH_P(getThis());
+	PDO_CONSTRUCT_CHECK;
+	
+	H = (pdo_pgsql_db_handle *)dbh->driver_data;
+
+	if (Z_TYPE_P(callback) == IS_NULL) {
+		if (H->notice_callback) {
+			efree(H->notice_callback);
+			H->notice_callback = NULL;
+		}
+		RETURN_TRUE;
+	} else {
+		if (!(fc = H->notice_callback)) {
+			fc = (pdo_pgsql_fci*)ecalloc(1, sizeof(pdo_pgsql_fci));
+		} else {
+			memcpy(&fc->fcc, &empty_fcall_info_cache, sizeof(fc->fcc));
+		}
+
+		if (FAILURE == zend_fcall_info_init(callback, 0, &fc->fci, &fc->fcc, &cbname, NULL TSRMLS_CC)) {
+			php_error_docref(NULL TSRMLS_CC, E_WARNING, "function '%s' is not callable", ZSTR_VAL(cbname));
+			zend_string_release_ex(cbname, 0);
+			efree(fc);
+			H->notice_callback = NULL;
+			RETURN_FALSE;
+		}
+		zend_string_release_ex(cbname, 0);
+
+		H->notice_callback = fc;
+
+		RETURN_TRUE;
+	}
+}
+/* }}} */
+
 
 static const zend_function_entry dbh_methods[] = {
 	PHP_ME(PDO, pgsqlLOBCreate, NULL, ZEND_ACC_PUBLIC)
@@ -984,6 +1048,7 @@
 	PHP_ME(PDO, pgsqlCopyToFile, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(PDO, pgsqlGetNotify, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(PDO, pgsqlGetPid, NULL, ZEND_ACC_PUBLIC)
+	PHP_ME(PDO, pgsqlSetNoticeCallback, NULL, ZEND_ACC_PUBLIC)
 	PHP_FE_END
 };
 
@@ -1107,7 +1172,7 @@
 		goto cleanup;
 	}
 
-	PQsetNoticeProcessor(H->server, (void(*)(void*,const char*))_pdo_pgsql_notice, (void *)&dbh);
+	PQsetNoticeProcessor(H->server, (void(*)(void*,const char*))_pdo_pgsql_notice, (void *)dbh);
 
 	H->attached = 1;
 	H->pgoid = -1;
diff -ruw ext/pdo_pgsql/php_pdo_pgsql_int.h ext/pdo_pgsql/php_pdo_pgsql_int.h
--- ext/pdo_pgsql/php_pdo_pgsql_int.h	2015-09-01 22:09:37.000000000 +0200
+++ ext/pdo_pgsql/php_pdo_pgsql_int.h	2019-10-01 05:18:31.085743000 +0200
@@ -36,6 +36,11 @@
 	char *errmsg;
 } pdo_pgsql_error_info;
 
+typedef struct {
+	zend_fcall_info fci;
+	zend_fcall_info_cache fcc;
+} pdo_pgsql_fci;
+
 /* stuff we use in a pgsql database handle */
 typedef struct {
 	PGconn		*server;
@@ -51,6 +56,7 @@
 	zend_bool       emulate_prepares;
 	zend_bool       disable_native_prepares; /* deprecated since 5.6 */
 	zend_bool       disable_prepares;
+	pdo_pgsql_fci * notice_callback;
 } pdo_pgsql_db_handle;
 
 typedef struct {
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 04 18:01:31 2024 UTC