+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten, 0, 0, 1)
+ ZEND_ARG_INFO(0, channel)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, unlisten)
+{
+ zend_error_handling zeh;
+ char *channel_str;
+ int channel_len;
+ STATUS rv;
+
+ zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
+ rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &channel_str, &channel_len);
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+
+ if (SUCCESS == rv) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (!obj->intern) {
+ throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
+ } else if (SUCCESS == zend_hash_del(&obj->intern->listeners, channel_str, channel_len + 1)) {
+ PGresult *res = unlisten(obj->intern->conn, channel_str, channel_len);
+
+ if (res) {
+ php_pqres_success(res TSRMLS_CC);
+ PHP_PQclear(res);
+ }
+ }
+ }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten_async, 0, 0, 1)
+ ZEND_ARG_INFO(0, channel)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqconn, unlistenAsync) {
+ zend_error_handling zeh;
+ char *channel_str;
+ int channel_len;
+ STATUS rv;
+
+ zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
+ rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &channel_str, &channel_len);
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+
+ if (SUCCESS == rv) {
+ php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (!obj->intern) {
+ throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
+ } else {
+ char *quoted_channel = PQescapeIdentifier(obj->intern->conn, channel_str, channel_len);
+
+ if (!quoted_channel) {
+ throw_exce(EX_ESCAPE TSRMLS_CC, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj->intern->conn));
+ } else {
+ smart_str cmd = {0};
+
+ smart_str_appends(&cmd, "UNLISTEN ");
+ smart_str_appends(&cmd, quoted_channel);
+ smart_str_0(&cmd);
+
+ if (!PQsendQuery(obj->intern->conn, cmd.c)) {
+ throw_exce(EX_IO TSRMLS_CC, "Failed to uninstall listener (%s)", PHP_PQerrorMessage(obj->intern->conn));
+ } else {
+ obj->intern->poller = PQconsumeInput;
+ zend_hash_del(&obj->intern->listeners, channel_str, channel_len + 1);
+ }
+
+ smart_str_free(&cmd);
+ PQfreemem(quoted_channel);
+ php_pqconn_notify_listeners(obj TSRMLS_CC);
+ }
+ }
+ }
+}
+