From: Michael Wallner Date: Thu, 7 Feb 2013 13:51:13 +0000 (+0100) Subject: adapt persistent handle API X-Git-Tag: v0.1.0~18 X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-pq;a=commitdiff_plain;h=0f05986a1ec876755421f2f5122e63f74fb5b682 adapt persistent handle API --- diff --git a/src/php_pq.c b/src/php_pq.c index a465eed..ae74214 100644 --- a/src/php_pq.c +++ b/src/php_pq.c @@ -52,7 +52,6 @@ static int php_pqconn_event(PGEventId id, void *e, void *data); ZEND_DECLARE_MODULE_GLOBALS(pq) */ - /* {{{ PHP_INI */ /* Remove comments and fill if you need to have entries in php.ini @@ -522,53 +521,11 @@ static void php_pq_object_delref(void *o TSRMLS_DC) zend_objects_store_del_ref_by_handle_ex(obj->zv.handle, obj->zv.handlers TSRMLS_CC); } -static int apply_unlisten(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 *); - char *quoted_channel = PQescapeIdentifier(obj->intern->conn, key->arKey, key->nKeyLength - 1); - - if (quoted_channel) { - PGresult *res; - char *cmd; - - spprintf(&cmd, 0, "UNLISTEN %s", quoted_channel); - if ((res = PQexec(obj->intern->conn, cmd))) { - PHP_PQclear(res); - } - - efree(cmd); - PQfreemem(quoted_channel); - } - - return ZEND_HASH_APPLY_REMOVE; -} - -static void php_pqconn_notice_ignore(void *p, const PGresult *res) -{ -} - static void php_pqconn_object_free(void *o TSRMLS_DC) { php_pqconn_object_t *obj = o; if (obj->intern) { - php_pqconn_event_data_t *evdata; - PGresult *res; - - if ((evdata = PQinstanceData(obj->intern->conn, php_pqconn_event))) { - memset(evdata, 0, sizeof(*evdata)); - efree(evdata); - PQsetInstanceData(obj->intern->conn, php_pqconn_event, NULL); - } - PQsetNoticeReceiver(obj->intern->conn, php_pqconn_notice_ignore, NULL); - - /* clean up async results */ - while ((res = PQgetResult(obj->intern->conn))) { - PHP_PQclear(res); - } - - zend_hash_apply_with_arguments(&obj->intern->listeners TSRMLS_CC, apply_unlisten, 1, obj); - php_resource_factory_handle_dtor(&obj->intern->factory, obj->intern->conn TSRMLS_CC); php_resource_factory_dtor(&obj->intern->factory); php_pq_callback_dtor(&obj->intern->onevent); @@ -1756,19 +1713,22 @@ static php_pqconn_event_data_t *php_pqconn_event_data_init(php_pqconn_object_t * static void php_pqconn_notice_recv(void *p, const PGresult *res) { php_pqconn_event_data_t *data = p; - zval **evhs; - TSRMLS_DF(data); - if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("notice"), (void *) &evhs)) { - zval *args, *connection = NULL; + if (data) { + zval **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(Z_ARRVAL_PP(evhs), apply_event, args TSRMLS_CC); - zval_ptr_dtor(&args); + 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(Z_ARRVAL_PP(evhs), apply_event, args TSRMLS_CC); + zval_ptr_dtor(&args); + } } } @@ -1780,16 +1740,32 @@ typedef struct php_pqconn_resource_factory_data { static void *php_pqconn_resource_factory_ctor(void *data, void *init_arg TSRMLS_DC) { php_pqconn_resource_factory_data_t *o = init_arg; + PGconn *conn = NULL;; if (o->flags & PHP_PQCONN_ASYNC) { - return PQconnectStart(o->dsn); + conn = PQconnectStart(o->dsn); } else { - return PQconnectdb(o->dsn); + conn = PQconnectdb(o->dsn); + } + + if (conn) { + PQregisterEventProc(conn, php_pqconn_event, "ext-pq", NULL); } + + return conn; } static void php_pqconn_resource_factory_dtor(void *opaque, void *handle TSRMLS_DC) { + php_pqconn_event_data_t *evdata = PQinstanceData(handle, php_pqconn_event); + + /* we don't care for anthing, except free'ing evdata */ + if (evdata) { + PQsetInstanceData(handle, php_pqconn_event, NULL); + memset(evdata, 0, sizeof(*evdata)); + efree(evdata); + } + PQfinish(handle); } @@ -1799,29 +1775,74 @@ static php_resource_factory_ops_t php_pqconn_resource_factory_ops = { php_pqconn_resource_factory_dtor }; -static void php_pqconn_persistent_resource_factory_dtor(void *opaque, void *handle TSRMLS_DC) +static void php_pqconn_wakeup(php_persistent_handle_factory_t *f, void **handle TSRMLS_DC) { +} + +static int apply_unlisten(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 *); + char *quoted_channel = PQescapeIdentifier(obj->intern->conn, key->arKey, key->nKeyLength - 1); + + if (quoted_channel) { + PGresult *res; + char *cmd; + + spprintf(&cmd, 0, "UNLISTEN %s", quoted_channel); + if ((res = PQexec(obj->intern->conn, cmd))) { + PHP_PQclear(res); + } + + efree(cmd); + PQfreemem(quoted_channel); + } + + return ZEND_HASH_APPLY_REMOVE; +} + +static void php_pqconn_notice_ignore(void *p, const PGresult *res) +{ +} + +static void php_pqconn_retire(php_persistent_handle_factory_t *f, void **handle TSRMLS_DC) +{ + php_pqconn_event_data_t *evdata = PQinstanceData(*handle, php_pqconn_event); PGresult *res; + /* go away */ + PQsetInstanceData(*handle, php_pqconn_event, NULL); + + /* ignore notices */ + PQsetNoticeReceiver(*handle, php_pqconn_notice_ignore, NULL); - /* clean up */ - if ((res = PQexec(handle, PQtransactionStatus(handle) == PQTRANS_IDLE ? "RESET ALL" : "ROLLBACK; RESET ALL"))) { + /* clean up async results */ + while ((res = PQgetResult(*handle))) { PHP_PQclear(res); } - /* release to the pool, if the connection is alive */ - if (CONNECTION_OK == PQstatus(handle)) { - php_persistent_handle_release(opaque, handle TSRMLS_CC); - } else { - PQfinish(handle); + /* clean up transaction & session */ + switch (PQtransactionStatus(*handle)) { + case PQTRANS_IDLE: + res = PQexec(*handle, "RESET ALL"); + break; + default: + res = PQexec(*handle, "ROLLBACK; RESET ALL"); + break; } -} -static php_resource_factory_ops_t php_pqconn_persistent_resource_factory_ops = { - (php_resource_factory_handle_ctor_t) php_persistent_handle_acquire, - NULL, - php_pqconn_persistent_resource_factory_dtor -}; + if (res) { + PHP_PQclear(res); + } + + if (evdata) { + /* clean up notify listeners */ + zend_hash_apply_with_arguments(&evdata->obj->intern->listeners TSRMLS_CC, apply_unlisten, 1, evdata->obj); + + /* release instance data */ + memset(evdata, 0, sizeof(*evdata)); + efree(evdata); + } +} ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct, 0, 0, 1) ZEND_ARG_INFO(0, dsn) @@ -1845,19 +1866,18 @@ static PHP_METHOD(pqconn, __construct) { zend_hash_init(&obj->intern->eventhandlers, 0, NULL, ZVAL_PTR_DTOR, 0); if (flags & PHP_PQCONN_PERSISTENT) { - php_persistent_handle_factory_t *phf = php_persistent_handle_concede(NULL, ZEND_STRL("pq\\Connection"), dsn_str, dsn_len TSRMLS_CC); - php_resource_factory_init(&obj->intern->factory, &php_pqconn_persistent_resource_factory_ops, phf, (void (*)(void*)) php_persistent_handle_abandon); - } else{ + php_persistent_handle_factory_t *phf = php_persistent_handle_concede(NULL, ZEND_STRL("pq\\Connection"), dsn_str, dsn_len, php_pqconn_wakeup, php_pqconn_retire TSRMLS_CC); + php_resource_factory_init(&obj->intern->factory, php_persistent_handle_get_resource_factory_ops(), phf, (void (*)(void*)) php_persistent_handle_abandon); + } else { php_resource_factory_init(&obj->intern->factory, &php_pqconn_resource_factory_ops, NULL, NULL); } + if (flags & PHP_PQCONN_ASYNC) { obj->intern->poller = (int (*)(PGconn*)) PQconnectPoll; } obj->intern->conn = php_resource_factory_handle_ctor(&obj->intern->factory, &rfdata TSRMLS_CC); - PQregisterEventProc(obj->intern->conn, php_pqconn_event, "ext-pq", NULL); - /* the connection might be persistent, so reset event_proc instance data */ PQsetInstanceData(obj->intern->conn, php_pqconn_event, evdata); PQsetNoticeReceiver(obj->intern->conn, php_pqconn_notice_recv, evdata); @@ -5017,7 +5037,7 @@ static PHP_MINIT_FUNCTION(pq) zend_declare_class_constant_long(php_pqcopy_class_entry, ZEND_STRL("FROM_STDIN"), PHP_PQCOPY_FROM_STDIN TSRMLS_CC); zend_declare_class_constant_long(php_pqcopy_class_entry, ZEND_STRL("TO_STDOUT"), PHP_PQCOPY_TO_STDOUT TSRMLS_CC); - php_persistent_handle_provide(ZEND_STRL("pq\\Connection"), &php_pqconn_resource_factory_ops, NULL, NULL); + php_persistent_handle_provide(ZEND_STRL("pq\\Connection"), &php_pqconn_resource_factory_ops, NULL, NULL TSRMLS_CC); /* REGISTER_INI_ENTRIES(); @@ -5073,6 +5093,7 @@ const zend_function_entry pq_functions[] = { static zend_module_dep pq_module_deps[] = { ZEND_MOD_REQUIRED("raphf") + ZEND_MOD_REQUIRED("spl") ZEND_MOD_END };