[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-internals
Subject:    [PHP-DEV] PDO::PgSQL Notify support
From:       Matteo Beccati <php () beccati ! com>
Date:       2009-01-26 0:13:12
Message-ID: 497D0018.7040201 () beccati ! com
[Download RAW message or body]

Hi,

the recent email about pg_get_socket reminded me that PDO:PgSQL doesn't
have notify support. Here's a small patch and a phpt that add support
for the LISTEN/NOTIFY mechanism using PDO. The PDO::pgsqlGetNotify
method only returns an assoc array, but it could be easily modified to
mimic the behaviour of pg_get_notify.

The patch applies to PHP 5.2 HEAD. If you find it useful I can provide
patches to PHP_5_3 or PHP_6.


Cheers

-- 
Matteo Beccati

OpenX - http://www.openx.org

["notify.phpt" (text/plain)]

--TEST--
PDO PgSQL Notifies
--SKIPIF--
<?php # vim:se ft=php:
if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
require dirname(__FILE__) . '/config.inc';
require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php
//require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$pid = $db->pgsqlGetPid();
var_dump($pid > 0);

var_dump($db->pgsqlGetNotify());

$db->exec("LISTEN notify_phpt");

var_dump($db->pgsqlGetNotify());

$db->exec("NOTIFY notify_phpt");

$notify = $db->pgsqlGetNotify();

var_dump($notify['message']);
var_dump($notify['pid'] == $pid);

?>
--EXPECT--
bool(true)
bool(false)
bool(false)
string(11) "notify_phpt"
bool(true)

["pdo_pgsql_notify.diff" (text/plain)]

Index: ext/pdo_pgsql/pgsql_driver.c
===================================================================
RCS file: /repository/php-src/ext/pdo_pgsql/pgsql_driver.c,v
retrieving revision 1.53.2.14.2.12
diff -u -r1.53.2.14.2.12 pgsql_driver.c
--- ext/pdo_pgsql/pgsql_driver.c        31 Dec 2008 11:17:42 -0000      1.53.2.14.2.12
+++ ext/pdo_pgsql/pgsql_driver.c        26 Jan 2009 00:04:47 -0000
@@ -626,11 +626,56 @@
 }
 /* }}} */

+/* {{{ proto mixed PDO::pgsqlGetNotify()
+   Get asyncronous notification */
+static PHP_METHOD(PDO, pgsqlGetNotify)
+{
+       pdo_dbh_t *dbh;
+       pdo_pgsql_db_handle *H;
+       PGnotify *pgsql_notify;
+
+       dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
+       PDO_CONSTRUCT_CHECK;
+
+       H = (pdo_pgsql_db_handle *)dbh->driver_data;
+
+       PQconsumeInput(H->server);
+       pgsql_notify = PQnotifies(H->server);
+
+       if (!pgsql_notify) {
+               RETURN_FALSE;
+       }
+
+       array_init(return_value);
+       add_assoc_string(return_value, "message", pgsql_notify->relname, 1);
+       add_assoc_long(return_value, "pid", pgsql_notify->be_pid);
+
+       PQfreemem(pgsql_notify);
+}
+/* }}} */
+
+/* {{{ proto int PDO::pgsqlGetPid()
+   Get backend(server) pid */
+static PHP_METHOD(PDO, pgsqlGetPid)
+{
+       pdo_dbh_t *dbh;
+       pdo_pgsql_db_handle *H;
+
+       dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
+       PDO_CONSTRUCT_CHECK;
+
+       H = (pdo_pgsql_db_handle *)dbh->driver_data;
+
+       RETURN_LONG(PQbackendPID(H->server));
+}
+/* }}} */

 static zend_function_entry dbh_methods[] = {
        PHP_ME(PDO, pgsqlLOBCreate, NULL, ZEND_ACC_PUBLIC)
        PHP_ME(PDO, pgsqlLOBOpen, NULL, ZEND_ACC_PUBLIC)
        PHP_ME(PDO, pgsqlLOBUnlink, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(PDO, pgsqlGetNotify, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(PDO, pgsqlGetPid, NULL, ZEND_ACC_PUBLIC)
        {NULL, NULL, NULL}
 };



-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic