Merge branch 'v1.1.x'
[m6w6/ext-pq] / src / php_pqconn_event.c
index 64d94b7..802a9e4 100644 (file)
@@ -16,8 +16,7 @@
 
 #include <php.h>
 
-#define SMART_STR_PREALLOC 256
-#include <ext/standard/php_smart_str.h>
+#include <Zend/zend_smart_str.h>
 
 #include <libpq-events.h>
 
 #include "php_pqstm.h"
 #include "php_pqres.h"
 
-static int apply_event(void *p, void *a TSRMLS_DC)
+static int apply_event(zval *p, void *a)
 {
-       php_pq_callback_t *cb = p;
+       php_pq_callback_t *cb = Z_PTR_P(p);
        zval *args = a;
-       zval *retval = NULL;
+       zval rv;
 
-       zend_fcall_info_args(&cb->fci, args TSRMLS_CC);
-       zend_fcall_info_call(&cb->fci, &cb->fcc, &retval, NULL TSRMLS_CC);
-       if (retval) {
-               zval_ptr_dtor(&retval);
-       }
+       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 TSRMLS_DC)
+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;
@@ -56,7 +55,7 @@ static inline PGresult *relisten(PGconn *conn, const char *channel_str, size_t c
                smart_str_appends(&cmd, quoted_channel);
                smart_str_0(&cmd);
 
-               res = PQexec(conn, cmd.c);
+               res = PQexec(conn, smart_str_v(&cmd));
 
                smart_str_free(&cmd);
                PQfreemem(quoted_channel);
@@ -65,10 +64,10 @@ static inline PGresult *relisten(PGconn *conn, const char *channel_str, size_t c
        return res;
 }
 
-static int apply_relisten(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
+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->arKey, key->nKeyLength - 1 TSRMLS_CC);
+       PGresult *res = relisten(obj->intern->conn, key->key->val, key->key->len);
 
        if (res) {
                php_pqres_clear(res);
@@ -77,12 +76,12 @@ static int apply_relisten(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_k
        return ZEND_HASH_APPLY_KEEP;
 }
 
-static int apply_reprepare(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
+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 = *(php_pqstm_object_t **) p;
+       php_pqstm_t *stm = Z_PTR_P(p);
 
-       php_pqconn_prepare(NULL, obj, stm->name, stm->query, stm->params TSRMLS_CC);
+       php_pqconn_prepare(NULL, obj, stm->name, stm->query, stm->params);
 
        return ZEND_HASH_APPLY_KEEP;
 }
@@ -92,24 +91,22 @@ static void php_pqconn_event_connreset(PGEventConnReset *event)
        php_pqconn_event_data_t *data = PQinstanceData(event->conn, php_pqconn_event);
 
        if (data) {
-               HashTable *evhs;
-               TSRMLS_DF(data);
+               zval *zevhs;
 
                /* restore listeners */
-               zend_hash_apply_with_arguments(&data->obj->intern->listeners TSRMLS_CC, apply_relisten, 1, data->obj);
+               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 TSRMLS_CC, apply_reprepare, 1, data->obj);
+               zend_hash_apply_with_arguments(&data->obj->intern->statements, apply_reprepare, 1, data->obj);
 
                /* eventhandler */
-               if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("reset"), (void *) &evhs)) {
-                       zval *args, *connection = NULL;
-
-                       MAKE_STD_ZVAL(args);
-                       array_init(args);
-                       php_pq_object_to_zval(data->obj, &connection TSRMLS_CC);
-                       add_next_index_zval(args, connection);
-                       zend_hash_apply_with_argument(evhs, apply_event, args TSRMLS_CC);
+               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(Z_ARRVAL_P(zevhs), apply_event, &args);
                        zval_ptr_dtor(&args);
                }
        }
@@ -120,33 +117,29 @@ 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;
-               TSRMLS_DF(data);
-
-               php_pqres_init_instance_data(event->result, data->obj, &obj TSRMLS_CC);
+               php_pqres_object_t *obj = php_pqres_init_instance_data(event->result, data->obj);
+               zval *zevhs;
 
                /* event listener */
-               if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("result"), (void *) &evhs)) {
-                       zval *args, *connection = NULL, *res = NULL;
-
-                       MAKE_STD_ZVAL(args);
-                       array_init(args);
-                       php_pq_object_to_zval(data->obj, &connection TSRMLS_CC);
-                       add_next_index_zval(args, connection);
-                       php_pq_object_to_zval(obj, &res TSRMLS_CC);
-                       add_next_index_zval(args, res);
-                       zend_hash_apply_with_argument(evhs, apply_event, args TSRMLS_CC);
+               if ((zevhs = zend_hash_str_find(&data->obj->intern->eventhandlers, ZEND_STRL("result")))) {
+                       zval args, connection, res;
+
+                       array_init(&args);
+                       php_pq_object_to_zval(data->obj, &connection);
+                       add_next_index_zval(&args, &connection);
+                       php_pq_object_to_zval(obj, &res);
+                       add_next_index_zval(&args, &res);
+                       zend_hash_apply_with_argument(Z_ARRVAL_P(zevhs), apply_event, &args);
                        zval_ptr_dtor(&args);
                }
 
                /* async callback */
                if (php_pq_callback_is_enabled(&data->obj->intern->onevent)) {
-                       zval *res = NULL;
+                       zval res;
 
-                       php_pq_object_to_zval(obj, &res TSRMLS_CC);
-                       zend_fcall_info_argn(&data->obj->intern->onevent.fci TSRMLS_CC, 1, &res);
-                       zend_fcall_info_call(&data->obj->intern->onevent.fci, &data->obj->intern->onevent.fcc, NULL, NULL TSRMLS_CC);
+                       php_pq_object_to_zval(obj, &res);
+                       zend_fcall_info_argn(&data->obj->intern->onevent.fci, 1, &res);
+                       zend_fcall_info_call(&data->obj->intern->onevent.fci, &data->obj->intern->onevent.fcc, NULL, NULL);
                        zval_ptr_dtor(&res);
                }
 
@@ -159,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);
        }
 }
 
@@ -181,12 +176,12 @@ int php_pqconn_event(PGEventId id, void *e, void *data)
        return 1;
 }
 
-php_pqconn_event_data_t *php_pqconn_event_data_init(php_pqconn_object_t *obj TSRMLS_DC)
+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;
-       TSRMLS_CF(data);
+       data->res = NULL;
 
        return data;
 }
@@ -196,18 +191,16 @@ void php_pqconn_notice_recv(void *p, const PGresult *res)
        php_pqconn_event_data_t *data = p;
 
        if (data) {
-               HashTable *evhs;
-               TSRMLS_DF(data);
-
-               if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("notice"), (void *) &evhs)) {
-                       zval *args, *connection = NULL;
-
-                       MAKE_STD_ZVAL(args);
-                       array_init(args);
-                       php_pq_object_to_zval(data->obj, &connection TSRMLS_CC);
-                       add_next_index_zval(args, connection);
-                       add_next_index_string(args, PHP_PQresultErrorMessage(res), 1);
-                       zend_hash_apply_with_argument(evhs, apply_event, args TSRMLS_CC);
+               zval *zevhs;
+
+               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(Z_ARRVAL_P(zevhs), apply_event, &args);
                        zval_ptr_dtor(&args);
                }
        }