Skip to content

Commit

Permalink
Fixed Bug #42614 (PDO_pgsql: add pg_get_notify support)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbeccati committed Jun 4, 2013
1 parent baabd11 commit b62b8b4
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 0 deletions.
81 changes: 81 additions & 0 deletions ext/pdo_pgsql/pgsql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "main/php_network.h"
#include "pdo/php_pdo.h"
#include "pdo/php_pdo_driver.h"
#include "pdo/php_pdo_error.h"
Expand Down Expand Up @@ -1006,6 +1007,84 @@ static PHP_METHOD(PDO, pgsqlLOBUnlink)
}
/* }}} */

/* {{{ proto mixed PDO::pgsqlGetNotify([ int $result_type = PDO::FETCH_USE_DEFAULT] [, int $ms_timeout = 0 ]])
Get asyncronous notification */
static PHP_METHOD(PDO, pgsqlGetNotify)
{
pdo_dbh_t *dbh;
pdo_pgsql_db_handle *H;
long result_type = PDO_FETCH_USE_DEFAULT;
long ms_timeout = 0;
PGnotify *pgsql_notify;

if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll",
&result_type, &ms_timeout)) {
RETURN_FALSE;
}

dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
PDO_CONSTRUCT_CHECK;

if (result_type == PDO_FETCH_USE_DEFAULT) {
result_type = dbh->default_fetch_type;
}

if (result_type != PDO_FETCH_BOTH && result_type != PDO_FETCH_ASSOC && result_type != PDO_FETCH_NUM) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid result type");
RETURN_FALSE;
}

if (ms_timeout < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid timeout");
RETURN_FALSE;
}

H = (pdo_pgsql_db_handle *)dbh->driver_data;

PQconsumeInput(H->server);
pgsql_notify = PQnotifies(H->server);

if (ms_timeout && !pgsql_notify) {
php_pollfd_for_ms(PQsocket(H->server), PHP_POLLREADABLE, ms_timeout);

PQconsumeInput(H->server);
pgsql_notify = PQnotifies(H->server);
}

if (!pgsql_notify) {
RETURN_FALSE;
}

array_init(return_value);
if (result_type == PDO_FETCH_NUM || result_type == PDO_FETCH_BOTH) {
add_index_string(return_value, 0, pgsql_notify->relname, 1);
add_index_long(return_value, 1, pgsql_notify->be_pid);
}
if (result_type == PDO_FETCH_ASSOC || result_type == PDO_FETCH_BOTH) {
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 const zend_function_entry dbh_methods[] = {
PHP_ME(PDO, pgsqlLOBCreate, NULL, ZEND_ACC_PUBLIC)
Expand All @@ -1015,6 +1094,8 @@ static const zend_function_entry dbh_methods[] = {
PHP_ME(PDO, pgsqlCopyFromFile, NULL, ZEND_ACC_PUBLIC)
PHP_ME(PDO, pgsqlCopyToArray, NULL, ZEND_ACC_PUBLIC)
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_FE_END
};

Expand Down
109 changes: 109 additions & 0 deletions ext/pdo_pgsql/tests/getnotify.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
--TEST--
PDO PgSQL LISTEN/NOTIFY support
--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);

// pgsqlGetPid should return something meaningful
$pid = $db->pgsqlGetPid();
var_dump($pid > 0);

// No listen, no notifies
var_dump($db->pgsqlGetNotify());

// Listen started, no notifies
$db->exec("LISTEN notifies_phpt");
var_dump($db->pgsqlGetNotify());

// No parameters, use default PDO::FETCH_NUM
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_NUM);
$db->exec("NOTIFY notifies_phpt");
$notify = $db->pgsqlGetNotify();
var_dump(count($notify));
var_dump($notify[0]);
var_dump($notify[1] == $pid);

// No parameters, use default PDO::FETCH_ASSOC
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$db->exec("NOTIFY notifies_phpt");
$notify = $db->pgsqlGetNotify();
var_dump(count($notify));
var_dump($notify['message']);
var_dump($notify['pid'] == $pid);

// Test PDO::FETCH_NUM as parameter
$db->exec("NOTIFY notifies_phpt");
$notify = $db->pgsqlGetNotify(PDO::FETCH_NUM);
var_dump(count($notify));
var_dump($notify[0]);
var_dump($notify[1] == $pid);

// Test PDO::FETCH_ASSOC as parameter
$db->exec("NOTIFY notifies_phpt");
$notify = $db->pgsqlGetNotify(PDO::FETCH_ASSOC);
var_dump(count($notify));
var_dump($notify['message']);
var_dump($notify['pid'] == $pid);

// Test PDO::FETCH_BOTH as parameter
$db->exec("NOTIFY notifies_phpt");
$notify = $db->pgsqlGetNotify(PDO::FETCH_BOTH);
var_dump(count($notify));
var_dump($notify['message']);
var_dump($notify['pid'] == $pid);
var_dump($notify[0]);
var_dump($notify[1] == $pid);

// Verify that there are no notifies queued
var_dump($db->pgsqlGetNotify());


// Test second parameter, should wait 2 seconds because no notify is queued
$t = microtime(1);
$notify = $db->pgsqlGetNotify(PDO::FETCH_ASSOC, 1000);
var_dump((microtime(1) - $t) >= 1);
var_dump($notify);

// Test second parameter, should return immediately because a notify is queued
$db->exec("NOTIFY notifies_phpt");
$t = microtime(1);
$notify = $db->pgsqlGetNotify(PDO::FETCH_ASSOC, 5000);
var_dump((microtime(1) - $t) < 1);
var_dump(count($notify));

?>
--EXPECT--
bool(true)
bool(false)
bool(false)
int(2)
string(13) "notifies_phpt"
bool(true)
int(2)
string(13) "notifies_phpt"
bool(true)
int(2)
string(13) "notifies_phpt"
bool(true)
int(2)
string(13) "notifies_phpt"
bool(true)
int(4)
string(13) "notifies_phpt"
bool(true)
string(13) "notifies_phpt"
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
int(2)

0 comments on commit b62b8b4

Please sign in to comment.