+static void php_pqconn_add_eventhandler(zval *zconn, php_pqconn_object_t *conn_obj, const char *type_str, size_t type_len, zval *zevent TSRMLS_DC)
+{
+ zval **evhs;
+
+ if (SUCCESS == zend_hash_find(&conn_obj->eventhandlers, type_str, type_len + 1, (void *) &evhs)) {
+ Z_ADDREF_P(zevent);
+ add_next_index_zval(*evhs, zevent);
+ } else {
+ zval *evh;
+
+ MAKE_STD_ZVAL(evh);
+ array_init(evh);
+ Z_ADDREF_P(zevent);
+ add_next_index_zval(evh, zevent);
+ zend_hash_add(&conn_obj->eventhandlers, type_str, type_len + 1, (void *) &evh, sizeof(zval *), NULL);
+ }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqevent_construct, 0, 0, 3)
+ ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0)
+ ZEND_ARG_INFO(0, type)
+ ZEND_ARG_INFO(0, callable)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqevent, __construct) {
+ zend_error_handling zeh;
+ zval *zconn;
+ char *type_str;
+ int type_len;
+ php_pq_callback_t cb;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Osf", &zconn, php_pqconn_class_entry, &type_str, &type_len, &cb.fci, &cb.fcc)) {
+ php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
+
+ if (conn_obj->conn) {
+ php_pqevent_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ obj->onevent = ecalloc(1, sizeof(*obj->onevent));
+ php_pq_callback_addref(&cb);
+ obj->onevent->cb = cb;
+ Z_ADDREF_P(zconn);
+ obj->onevent->conn = zconn;
+ obj->onevent->type = estrdup(type_str);
+
+ php_pqconn_add_eventhandler(zconn, conn_obj, type_str, type_len, getThis() TSRMLS_CC);
+
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqevent_trigger, 0, 0, 1)
+ ZEND_ARG_ARRAY_INFO(0, args, 1)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqevent, trigger) {
+ zval *args;
+
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &args)) {
+ php_pqevent_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->onevent) {
+ zval *rv = NULL;
+
+ if (SUCCESS == zend_fcall_info_call(&obj->onevent->cb.fci, &obj->onevent->cb.fcc, &rv, args TSRMLS_CC)) {
+ if (rv) {
+ RETVAL_ZVAL(rv, 0, 1);
+ } else {
+ RETVAL_TRUE;
+ }
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Event not initialized");
+ RETVAL_FALSE;
+ }
+ }
+}
+
+static zend_function_entry php_pqevent_methods[] = {
+ PHP_ME(pqevent, __construct, ai_pqevent_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
+ PHP_ME(pqevent, trigger, ai_pqevent_trigger, ZEND_ACC_PUBLIC)
+ {0}
+};
+