X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-pq;a=blobdiff_plain;f=src%2Fphp_pqconn_event.c;h=64d94b74f830877163af835c87a3dd0b436e62b8;hp=541892f5d11d2abedb715652fa1d3988e5737033;hb=df586b558795570822225a3a458bd91a1f7ab433;hpb=1b5358df410e66224c7e8251d286134b0f3f59d3 diff --git a/src/php_pqconn_event.c b/src/php_pqconn_event.c index 541892f..64d94b7 100644 --- a/src/php_pqconn_event.c +++ b/src/php_pqconn_event.c @@ -16,12 +16,16 @@ #include +#define SMART_STR_PREALLOC 256 +#include + #include #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) @@ -39,6 +43,50 @@ 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); @@ -47,6 +95,13 @@ static void php_pqconn_event_connreset(PGEventConnReset *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;