X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fphp_pqconn_event.c;h=802a9e482914235a3eeb39a081261b014871de4d;hb=0ca37857fc5461a939cbcea0e40ccf6247b9966a;hp=07fb173295dc44fdb5a0323dfe98b477e5bf35a1;hpb=6e28a8741be3fccdaca960e492c31bc26837a4ed;p=m6w6%2Fext-pq diff --git a/src/php_pqconn_event.c b/src/php_pqconn_event.c index 07fb173..802a9e4 100644 --- a/src/php_pqconn_event.c +++ b/src/php_pqconn_event.c @@ -16,12 +16,15 @@ #include +#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(zval *p, void *a) @@ -33,25 +36,77 @@ static int apply_event(zval *p, void *a) ZVAL_NULL(&rv); zend_fcall_info_args(&cb->fci, args); zend_fcall_info_call(&cb->fci, &cb->fcc, &rv, NULL); + zend_fcall_info_args_clear(&cb->fci, 0); zval_ptr_dtor(&rv); return ZEND_HASH_APPLY_KEEP; } + +static inline PGresult *relisten(PGconn *conn, const char *channel_str, size_t channel_len) +{ + 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, smart_str_v(&cmd)); + + smart_str_free(&cmd); + PQfreemem(quoted_channel); + } + + return res; +} + +static int apply_relisten(zval *p, 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->key->val, key->key->len); + + if (res) { + php_pqres_clear(res); + } + + return ZEND_HASH_APPLY_KEEP; +} + +static int apply_reprepare(zval *p, 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 = Z_PTR_P(p); + + php_pqconn_prepare(NULL, obj, stm->name, stm->query, stm->params); + + 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); if (data) { - HashTable *evhs; + zval *zevhs; - if ((evhs = zend_hash_str_find_ptr(&data->obj->intern->eventhandlers, ZEND_STRS("reset")))) { + /* restore listeners */ + zend_hash_apply_with_arguments(&data->obj->intern->listeners, apply_relisten, 1, data->obj); + + /* restore statements */ + zend_hash_apply_with_arguments(&data->obj->intern->statements, apply_reprepare, 1, data->obj); + + /* eventhandler */ + if ((zevhs = zend_hash_str_find(&data->obj->intern->eventhandlers, ZEND_STRL("reset")))) { zval args, connection; array_init(&args); php_pq_object_to_zval(data->obj, &connection); add_next_index_zval(&args, &connection); - zend_hash_apply_with_argument(evhs, apply_event, &args); + zend_hash_apply_with_argument(Z_ARRVAL_P(zevhs), apply_event, &args); zval_ptr_dtor(&args); } } @@ -62,13 +117,11 @@ static void php_pqconn_event_resultcreate(PGEventResultCreate *event) php_pqconn_event_data_t *data = PQinstanceData(event->conn, php_pqconn_event); if (data) { - php_pqres_object_t *obj; - HashTable *evhs; - - php_pqres_init_instance_data(event->result, data->obj, &obj); + php_pqres_object_t *obj = php_pqres_init_instance_data(event->result, data->obj); + zval *zevhs; /* event listener */ - if ((evhs = zend_hash_str_find_ptr(&data->obj->intern->eventhandlers, ZEND_STRL("result")))) { + if ((zevhs = zend_hash_str_find(&data->obj->intern->eventhandlers, ZEND_STRL("result")))) { zval args, connection, res; array_init(&args); @@ -76,12 +129,12 @@ static void php_pqconn_event_resultcreate(PGEventResultCreate *event) add_next_index_zval(&args, &connection); php_pq_object_to_zval(obj, &res); add_next_index_zval(&args, &res); - zend_hash_apply_with_argument(evhs, apply_event, &args); + zend_hash_apply_with_argument(Z_ARRVAL_P(zevhs), apply_event, &args); zval_ptr_dtor(&args); } /* async callback */ - if (data->obj->intern->onevent.fci.size > 0) { + if (php_pq_callback_is_enabled(&data->obj->intern->onevent)) { zval res; php_pq_object_to_zval(obj, &res); @@ -99,6 +152,8 @@ static void php_pqconn_event_resultdestroy(PGEventResultDestroy *event) if (obj) { obj->intern->res = NULL; + assert(GC_REFCOUNT(&obj->zo)); + php_pq_object_delref(obj); } } @@ -126,6 +181,7 @@ php_pqconn_event_data_t *php_pqconn_event_data_init(php_pqconn_object_t *obj) php_pqconn_event_data_t *data = emalloc(sizeof(*data)); data->obj = obj; + data->res = NULL; return data; } @@ -135,16 +191,16 @@ void php_pqconn_notice_recv(void *p, const PGresult *res) php_pqconn_event_data_t *data = p; if (data) { - HashTable *evhs; + zval *zevhs; - if ((evhs = zend_hash_str_find_ptr(&data->obj->intern->eventhandlers, ZEND_STRL("notice")))) { + if ((zevhs = zend_hash_str_find(&data->obj->intern->eventhandlers, ZEND_STRL("notice")))) { zval args, connection; array_init(&args); php_pq_object_to_zval(data->obj, &connection); add_next_index_zval(&args, &connection); add_next_index_string(&args, PHP_PQresultErrorMessage(res)); - zend_hash_apply_with_argument(evhs, apply_event, &args); + zend_hash_apply_with_argument(Z_ARRVAL_P(zevhs), apply_event, &args); zval_ptr_dtor(&args); } }