Add Cursor::closeAsync()
[m6w6/ext-pq] / src / php_pqcur.c
index 16057e8c5420a20d4117efa8057b4ddbb53e9bdd..fec703c40b9d5bca5c22e2d0d2418144f3682816 100644 (file)
@@ -29,9 +29,9 @@ zend_class_entry *php_pqcur_class_entry;
 static zend_object_handlers php_pqcur_object_handlers;
 static HashTable php_pqcur_object_prophandlers;
 
-static void cur_close(php_pqcur_object_t *obj TSRMLS_DC)
+static void cur_close(php_pqcur_object_t *obj, zend_bool async, zend_bool silent TSRMLS_DC)
 {
-       if (obj->intern->open) {
+       if (obj->intern->open && obj->intern->conn->intern) {
                PGresult *res;
                smart_str cmd = {0};
 
@@ -39,15 +39,59 @@ static void cur_close(php_pqcur_object_t *obj TSRMLS_DC)
                smart_str_appends(&cmd, obj->intern->name);
                smart_str_0(&cmd);
 
-               if ((res = PQexec(obj->intern->conn->intern->conn, cmd.c))) {
-                       PHP_PQclear(res);
+               if (async) {
+                       if (PQsendQuery(obj->intern->conn->intern->conn, cmd.c)) {
+                               obj->intern->conn->intern->poller = PQconsumeInput;
+                               php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
+                       } else if (!silent) {
+                               throw_exce(EX_IO TSRMLS_CC, "Failed to close cursor (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+                       }
+               } else {
+                       if ((res = PQexec(obj->intern->conn->intern->conn, cmd.c))) {
+                               PHP_PQclear(res);
+                       } else if (!silent) {
+                               throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to close cursor (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+                       }
                }
-               smart_str_free(&cmd);
 
+               smart_str_free(&cmd);
                obj->intern->open = 0;
        }
 }
 
+static void cur_open(INTERNAL_FUNCTION_PARAMETERS, zend_bool async)
+{
+       zend_error_handling zeh;
+       STATUS rv;
+
+       zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
+       rv = zend_parse_parameters_none();
+       zend_restore_error_handling(&zeh TSRMLS_CC);
+
+       if (rv == FAILURE) {
+               return;
+       }
+
+       php_pqcur_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+       if (!obj->intern) {
+               throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Cursor not initialized");
+               return;
+       } else if (obj->intern->open) {
+               return;
+       }
+
+       if (async) {
+               rv = php_pqconn_declare_async(NULL, obj->intern->conn, obj->intern->decl TSRMLS_CC);
+       } else {
+               rv = php_pqconn_declare(NULL, obj->intern->conn, obj->intern->decl TSRMLS_CC);
+       }
+
+       if (rv == SUCCESS) {
+               obj->intern->open = 1;
+       }
+}
+
 static void cur_fetch_or_move(INTERNAL_FUNCTION_PARAMETERS, const char *action, zend_bool async)
 {
        char *spec_str = "1";
@@ -110,7 +154,7 @@ static void php_pqcur_object_free(void *o TSRMLS_DC)
        fprintf(stderr, "FREE cur(#%d) %p (conn: %p)\n", obj->zv.handle, obj, obj->intern->conn);
 #endif
        if (obj->intern) {
-               cur_close(obj TSRMLS_CC);
+               cur_close(obj, 0, 1 TSRMLS_CC);
                php_pq_object_delref(obj->intern->conn TSRMLS_CC);
                efree(obj->intern->decl);
                efree(obj->intern->name);
@@ -165,7 +209,7 @@ static void php_pqcur_object_read_connection(zval *object, void *o, zval *return
 
 char *php_pqcur_declare_str(const char *name_str, size_t name_len, unsigned flags, const char *query_str, size_t query_len)
 {
-       size_t decl_len = name_len + query_len + sizeof("DECLARE BINARY INSENSITIVE NO SCROLL  CURSOR WITHOUT HOLD FOR ");
+       size_t decl_len = name_len + query_len + sizeof("DECLARE  BINARY INSENSITIVE NO SCROLL CURSOR WITH HOLD FOR ");
        char *decl_str;
 
        decl_str = emalloc(decl_len);
@@ -237,6 +281,20 @@ static PHP_METHOD(pqcur, __construct) {
 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_open, 0, 0, 0)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(pqcur, open)
+{
+       cur_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_openAsync, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqcur, openAsync)
+{
+       cur_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_close, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqcur, close)
 {
        zend_error_handling zeh;
        STATUS rv;
@@ -250,17 +308,15 @@ static PHP_METHOD(pqcur, open)
 
                if (!obj->intern) {
                        throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Cursor not initialized");
-               } else if (!obj->intern->open) {
-                       if (SUCCESS == php_pqconn_declare(NULL, obj->intern->conn, obj->intern->decl TSRMLS_CC)) {
-                               obj->intern->open = 1;
-                       }
+               } else {
+                       cur_close(obj, 0, 0 TSRMLS_CC);
                }
        }
 }
 
-ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_close, 0, 0, 0)
+ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_closeAsync, 0, 0, 0)
 ZEND_END_ARG_INFO();
-static PHP_METHOD(pqcur, close)
+static PHP_METHOD(pqcur, closeAsync)
 {
        zend_error_handling zeh;
        STATUS rv;
@@ -275,7 +331,7 @@ static PHP_METHOD(pqcur, close)
                if (!obj->intern) {
                        throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Cursor not initialized");
                } else {
-                       cur_close(obj TSRMLS_CC);
+                       cur_close(obj, 1, 0 TSRMLS_CC);
                }
        }
 }
@@ -317,7 +373,9 @@ static PHP_METHOD(pqcur, moveAsync)
 static zend_function_entry php_pqcur_methods[] = {
        PHP_ME(pqcur, __construct, ai_pqcur___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
        PHP_ME(pqcur, open, ai_pqcur_open, ZEND_ACC_PUBLIC)
+       PHP_ME(pqcur, openAsync, ai_pqcur_open, ZEND_ACC_PUBLIC)
        PHP_ME(pqcur, close, ai_pqcur_close, ZEND_ACC_PUBLIC)
+       PHP_ME(pqcur, closeAsync, ai_pqcur_closeAsync, ZEND_ACC_PUBLIC)
        PHP_ME(pqcur, fetch, ai_pqcur_fetch, ZEND_ACC_PUBLIC)
        PHP_ME(pqcur, move, ai_pqcur_move, ZEND_ACC_PUBLIC)
        PHP_ME(pqcur, fetchAsync, ai_pqcur_fetchAsync, ZEND_ACC_PUBLIC)