+ 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);
+ }
+ }
+}
+
+typedef struct php_pqconn_resource_factory_data {
+ char *dsn;
+ long flags;
+} php_pqconn_resource_factory_data_t;
+
+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) {
+ conn = PQconnectStart(o->dsn);
+ } else {
+ 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);
+}
+
+static php_resource_factory_ops_t php_pqconn_resource_factory_ops = {
+ php_pqconn_resource_factory_ctor,
+ NULL,
+ php_pqconn_resource_factory_dtor
+};
+
+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 async results */
+ while ((res = PQgetResult(*handle))) {
+ PHP_PQclear(res);
+ }
+
+ /* clean up transaction & session */
+ switch (PQtransactionStatus(*handle)) {
+ case PQTRANS_IDLE:
+ res = PQexec(*handle, "RESET ALL");
+ break;
+ default:
+ res = PQexec(*handle, "ROLLBACK; RESET ALL");
+ break;
+ }
+
+ 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);