restore listeners and statements on connection reset
authorMichael Wallner <mike@php.net>
Tue, 17 May 2016 14:25:18 +0000 (16:25 +0200)
committerMichael Wallner <mike@php.net>
Tue, 17 May 2016 14:25:18 +0000 (16:25 +0200)
closes issue #15

package.xml
src/php_pqconn.c
src/php_pqconn.h
src/php_pqconn_event.c
src/php_pqstm.c
tests/gh-issue015_listeners.phpt [new file with mode: 0644]
tests/gh-issue015_statements.phpt [new file with mode: 0644]

index 70a4eca8d6e4cb3db7573677d554df1375fa58f9..917e7ebbfd1ff9cb5ddfa6e9787b49215dcda10f 100644 (file)
@@ -47,6 +47,7 @@
  <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)
  <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="/">
 ]]></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="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" />
     <file role="test" name="info001.phpt" />
     <file role="test" name="info002.phpt" />
     <file role="test" name="lob001.phpt" />
index 1caf4d831d19cde25a0b193193c1f0c133f614f5..ed7cbe180195ea67aaa2840e60a1e253efe116d8 100644 (file)
@@ -78,6 +78,7 @@ static void php_pqconn_object_free(void *o TSRMLS_DC)
                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);
                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);
                zend_hash_destroy(&obj->intern->converters);
                zend_hash_destroy(&obj->intern->eventhandlers);
                efree(obj->intern);
@@ -679,6 +680,7 @@ static PHP_METHOD(pqconn, __construct) {
                        obj->intern->default_auto_convert = PHP_PQRES_CONV_ALL;
 
                        zend_hash_init(&obj->intern->listeners, 0, NULL, (dtor_func_t) zend_hash_destroy, 0);
                        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);
 
                        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);
 
index acec39b7304e45585a195fde44b82e3e2803f088..195da5b45a881352b9c8d3c6ba2b4558902f2ab7 100644 (file)
@@ -27,6 +27,7 @@ typedef struct php_pqconn {
        int (*poller)(PGconn *);
        php_resource_factory_t factory;
        HashTable listeners;
        int (*poller)(PGconn *);
        php_resource_factory_t factory;
        HashTable listeners;
+       HashTable statements;
        HashTable converters;
        HashTable eventhandlers;
        php_pq_callback_t onevent;
        HashTable converters;
        HashTable eventhandlers;
        php_pq_callback_t onevent;
index 541892f5d11d2abedb715652fa1d3988e5737033..64d94b74f830877163af835c87a3dd0b436e62b8 100644 (file)
 
 #include <php.h>
 
 
 #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 <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)
 #include "php_pqres.h"
 
 static int apply_event(void *p, void *a TSRMLS_DC)
@@ -39,6 +43,50 @@ static int apply_event(void *p, void *a TSRMLS_DC)
        return ZEND_HASH_APPLY_KEEP;
 }
 
        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);
 static void php_pqconn_event_connreset(PGEventConnReset *event)
 {
        php_pqconn_event_data_t *data = PQinstanceData(event->conn, php_pqconn_event);
@@ -47,6 +95,13 @@ static void php_pqconn_event_connreset(PGEventConnReset *event)
                HashTable *evhs;
                TSRMLS_DF(data);
 
                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;
 
                if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("reset"), (void *) &evhs)) {
                        zval *args, *connection = NULL;
 
index b82adcfbe96e4f17f144d8a76597f8d6364bb1a2..0f74b2b65ef7aaa238179663588abaedf640d0d1 100644 (file)
@@ -63,6 +63,7 @@ static void php_pqstm_deallocate(php_pqstm_object_t *obj, zend_bool async, zend_
                }
 
                obj->intern->allocated = 0;
                }
 
                obj->intern->allocated = 0;
+               zend_hash_del(&obj->intern->conn->intern->statements, obj->intern->name, strlen(obj->intern->name)+1);
        }
 }
 
        }
 }
 
@@ -164,6 +165,8 @@ php_pqstm_t *php_pqstm_init(php_pqconn_object_t *conn, const char *name, const c
 
        ZEND_INIT_SYMTABLE(&stm->bound);
 
 
        ZEND_INIT_SYMTABLE(&stm->bound);
 
+       zend_hash_add(&conn->intern->statements, name, strlen(name)+1, &stm, sizeof(stm), NULL);
+
        return stm;
 }
 
        return stm;
 }
 
@@ -420,7 +423,7 @@ static PHP_METHOD(pqstm, deallocateAsync)
        php_pqstm_deallocate_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
 }
 
        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;
 {
        zend_error_handling zeh;
        ZEND_RESULT_CODE rv;
@@ -443,6 +446,9 @@ static zend_always_inline void php_pqstm_prepare_handler(INTERNAL_FUNCTION_PARAM
 
                        if (SUCCESS == rv) {
                                obj->intern->allocated = 1;
 
                        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);
                        }
                }
        }
                        }
                }
        }
diff --git a/tests/gh-issue015_listeners.phpt b/tests/gh-issue015_listeners.phpt
new file mode 100644 (file)
index 0000000..5506b21
--- /dev/null
@@ -0,0 +1,70 @@
+--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
diff --git a/tests/gh-issue015_statements.phpt b/tests/gh-issue015_statements.phpt
new file mode 100644 (file)
index 0000000..b54664a
--- /dev/null
@@ -0,0 +1,42 @@
+--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