<license uri="http://copyfree.org/content/standard/licenses/2bsd/license.txt">BSD-2-Clause</license>
<notes><![CDATA[
* Added public readonly array pq\Result::$diag property, listing PQresultErrorField details (gh-issue #14)
+* Restore listeners and prepared statements after a connection reset (gh-issue #15)
]]></notes>
<contents>
<dir name="/">
<file role="test" name="exceptions001.phpt" />
<file role="test" name="exceptions002.phpt" />
<file role="test" name="fetch001.phpt" />
+ <file role="test" name="gh-issue015_listeners.phpt" />
+ <file role="test" name="gh-issue015_statements.phpt" />
<file role="test" name="info001.phpt" />
<file role="test" name="info002.phpt" />
<file role="test" name="lob001.phpt" />
php_resource_factory_handle_dtor(&obj->intern->factory, obj->intern->conn TSRMLS_CC);
php_resource_factory_dtor(&obj->intern->factory);
zend_hash_destroy(&obj->intern->listeners);
+ zend_hash_destroy(&obj->intern->statements);
zend_hash_destroy(&obj->intern->converters);
zend_hash_destroy(&obj->intern->eventhandlers);
efree(obj->intern);
obj->intern->default_auto_convert = PHP_PQRES_CONV_ALL;
zend_hash_init(&obj->intern->listeners, 0, NULL, (dtor_func_t) zend_hash_destroy, 0);
+ zend_hash_init(&obj->intern->statements, 0, NULL, NULL, 0);
zend_hash_init(&obj->intern->converters, 0, NULL, ZVAL_PTR_DTOR, 0);
zend_hash_init(&obj->intern->eventhandlers, 0, NULL, (dtor_func_t) zend_hash_destroy, 0);
int (*poller)(PGconn *);
php_resource_factory_t factory;
HashTable listeners;
+ HashTable statements;
HashTable converters;
HashTable eventhandlers;
php_pq_callback_t onevent;
#include <php.h>
+#define SMART_STR_PREALLOC 256
+#include <ext/standard/php_smart_str.h>
+
#include <libpq-events.h>
#include "php_pq.h"
#include "php_pq_misc.h"
#include "php_pq_object.h"
#include "php_pqconn_event.h"
+#include "php_pqstm.h"
#include "php_pqres.h"
static int apply_event(void *p, void *a TSRMLS_DC)
return ZEND_HASH_APPLY_KEEP;
}
+
+static inline PGresult *relisten(PGconn *conn, const char *channel_str, size_t channel_len TSRMLS_DC)
+{
+ char *quoted_channel = PQescapeIdentifier(conn, channel_str, channel_len);
+ PGresult *res = NULL;
+
+ if (quoted_channel) {
+ smart_str cmd = {0};
+
+ smart_str_appends(&cmd, "LISTEN ");
+ smart_str_appends(&cmd, quoted_channel);
+ smart_str_0(&cmd);
+
+ res = PQexec(conn, cmd.c);
+
+ smart_str_free(&cmd);
+ PQfreemem(quoted_channel);
+ }
+
+ return res;
+}
+
+static int apply_relisten(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
+{
+ php_pqconn_object_t *obj = va_arg(argv, php_pqconn_object_t *);
+ PGresult *res = relisten(obj->intern->conn, key->arKey, key->nKeyLength - 1 TSRMLS_CC);
+
+ if (res) {
+ php_pqres_clear(res);
+ }
+
+ return ZEND_HASH_APPLY_KEEP;
+}
+
+static int apply_reprepare(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
+{
+ php_pqconn_object_t *obj = va_arg(argv, php_pqconn_object_t *);
+ php_pqstm_t *stm = *(php_pqstm_object_t **) p;
+
+ php_pqconn_prepare(NULL, obj, stm->name, stm->query, stm->params TSRMLS_CC);
+
+ return ZEND_HASH_APPLY_KEEP;
+}
+
static void php_pqconn_event_connreset(PGEventConnReset *event)
{
php_pqconn_event_data_t *data = PQinstanceData(event->conn, php_pqconn_event);
HashTable *evhs;
TSRMLS_DF(data);
+ /* restore listeners */
+ zend_hash_apply_with_arguments(&data->obj->intern->listeners TSRMLS_CC, apply_relisten, 1, data->obj);
+
+ /* restore statements */
+ zend_hash_apply_with_arguments(&data->obj->intern->statements TSRMLS_CC, apply_reprepare, 1, data->obj);
+
+ /* eventhandler */
if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("reset"), (void *) &evhs)) {
zval *args, *connection = NULL;
}
obj->intern->allocated = 0;
+ zend_hash_del(&obj->intern->conn->intern->statements, obj->intern->name, strlen(obj->intern->name)+1);
}
}
ZEND_INIT_SYMTABLE(&stm->bound);
+ zend_hash_add(&conn->intern->statements, name, strlen(name)+1, &stm, sizeof(stm), NULL);
+
return stm;
}
php_pqstm_deallocate_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
-static zend_always_inline void php_pqstm_prepare_handler(INTERNAL_FUNCTION_PARAMETERS, zend_bool async)
+static inline void php_pqstm_prepare_handler(INTERNAL_FUNCTION_PARAMETERS, zend_bool async)
{
zend_error_handling zeh;
ZEND_RESULT_CODE rv;
if (SUCCESS == rv) {
obj->intern->allocated = 1;
+
+ zend_hash_add(&obj->intern->conn->intern->statements,
+ obj->intern->name, strlen(obj->intern->name)+1, &obj, sizeof(obj), NULL);
}
}
}
--- /dev/null
+--TEST--
+restore listeners on reset
+--SKIPIF--
+<?php
+include "_skipif.inc";
+?>
+--INI--
+date.timezone=UTC
+--FILE--
+<?php
+echo "Test\n";
+
+include "_setup.inc";
+
+$c = new pq\Connection(PQ_DSN);
+
+$c->listen("notify", function($channel, $message) {
+ printf("%s: %s\n", $channel, $message);
+});
+$c->on(pq\Connection::EVENT_RESET, function($conn) {
+ printf("Connection was reset\n");
+});
+$c->notify("notify", "Gotcha!");
+$c->resetAsync();
+
+// wait until the stream becomes writable
+$w = array($c->socket);
+$r = $e = null;
+
+if (stream_select($r, $w, $e, null)) {
+
+ // loop until the connection is established
+ while (true) {
+
+ switch ($c->poll()) {
+
+ case pq\Connection::POLLING_READING:
+ // we should wait for the stream to be read-ready
+ $r = array($c->socket);
+ stream_select($r, $w, $e, NULL);
+ break;
+
+ case pq\Connection::POLLING_WRITING:
+ // we should wait for the stream to be write-ready
+ $w = array($c->socket);
+ $r = $e = null;
+ stream_select($r, $w, $e, null);
+ break;
+
+ case pq\Connection::POLLING_FAILED:
+ printf("Connection failed: %s\n", $c->errorMessage);
+ break 2;
+
+ case pq\Connection::POLLING_OK:
+ printf("Connection completed\n");
+ break 2;
+ }
+ }
+}
+$c->notify("notify", "Do you miss me?");
+$c->exec("");
+?>
+===DONE===
+--EXPECT--
+Test
+notify: Gotcha!
+Connection was reset
+Connection completed
+notify: Do you miss me?
+===DONE===
\ No newline at end of file
--- /dev/null
+--TEST--
+restore statements on reset
+--SKIPIF--
+<?php
+include "_skipif.inc";
+?>
+--INI--
+date.timezone=UTC
+--FILE--
+<?php
+echo "Test\n";
+
+include "_setup.inc";
+
+$c = new pq\Connection(PQ_DSN);
+
+$s = $c->prepare("test", "SELECT 1");
+$c->on(pq\Connection::EVENT_RESET, function($conn) {
+ printf("Connection was reset\n");
+});
+
+var_dump($s->exec()->fetchRow());
+
+$c->reset();
+
+// Fatal error: Uncaught exception 'pq\Exception\DomainException' with message 'ERROR: prepared statement "test" does not exist'
+var_dump($s->exec()->fetchRow());
+
+?>
+===DONE===
+--EXPECT--
+Test
+array(1) {
+ [0]=>
+ int(1)
+}
+Connection was reset
+array(1) {
+ [0]=>
+ int(1)
+}
+===DONE===
\ No newline at end of file