fix +/* feature/bug prefix
[m6w6/ext-pq] / src / php_pqcur.c
index 1cb5d584fc0dbf68a36d9a6857f615825c3474cd..58ffd866c65128bfedf3d433cfbded2f2fb296cf 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,20 +39,65 @@ 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;
+       ZEND_RESULT_CODE rv;
+       php_pqcur_object_t *obj;
+
+       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;
+       }
+
+       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";
        int spec_len = 1;
-       STATUS rv;
+       ZEND_RESULT_CODE rv;
        php_pq_callback_t resolver = {{0}};
        zend_error_handling zeh;
 
@@ -79,10 +124,12 @@ static void cur_fetch_or_move(INTERNAL_FUNCTION_PARAMETERS, const char *action,
 
                                if (!rc) {
                                        throw_exce(EX_IO TSRMLS_CC, "Failed to %s cursor (%s)", *action == 'f' ? "fetch from" : "move in", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+#if HAVE_PQSETSINGLEROWMODE
                                } else if (obj->intern->conn->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn->intern->conn)) {
                                        throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+#endif
                                } else {
-                                       php_pq_callback_recurse(&obj->intern->conn->intern->onevent, &resolver);
+                                       php_pq_callback_recurse(&obj->intern->conn->intern->onevent, &resolver TSRMLS_CC);
                                        obj->intern->conn->intern->poller = PQconsumeInput;
                                }
                        } else {
@@ -108,8 +155,8 @@ 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);
-               //php_pq_object_delref(obj->intern->conn 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);
                efree(obj->intern);
@@ -161,12 +208,134 @@ static void php_pqcur_object_read_connection(zval *object, void *o, zval *return
        php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
 }
 
+static void php_pqcur_object_read_query(zval *object, void *o, zval *return_value TSRMLS_DC)
+{
+       php_pqcur_object_t *obj = o;
+
+       RETVAL_STRING(obj->intern->decl + obj->intern->query_offset, 1);
+}
+
+static void php_pqcur_object_read_flags(zval *object, void *o, zval *return_value TSRMLS_DC)
+{
+       php_pqcur_object_t *obj = o;
+
+       RETVAL_LONG(obj->intern->flags);
+}
+
+char *php_pqcur_declare_str(const char *name_str, size_t name_len, unsigned flags, const char *query_str, size_t query_len, int *query_offset)
+{
+       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);
+       decl_len = slprintf(decl_str, decl_len, "DECLARE %s %s %s %s CURSOR %s FOR %s",
+                       name_str,
+                       (flags & PHP_PQ_DECLARE_BINARY) ? "BINARY" : "",
+                       (flags & PHP_PQ_DECLARE_INSENSITIVE) ? "INSENSITIVE" : "",
+                       (flags & PHP_PQ_DECLARE_NO_SCROLL) ? "NO SCROLL" :
+                                       (flags & PHP_PQ_DECLARE_SCROLL) ? "SCROLL" : "",
+                       (flags & PHP_PQ_DECLARE_WITH_HOLD) ? "WITH HOLD" : "",
+                       query_str
+       );
+
+       if (query_offset) {
+               /* sizeof() includes the terminating null byte, so no need for spaces in the string literals */
+               *query_offset = sizeof("DECLARE")
+                       + (name_len + 1)
+                       + ((flags & PHP_PQ_DECLARE_BINARY) ? sizeof("BINARY") : 1)
+                       + ((flags & PHP_PQ_DECLARE_INSENSITIVE) ? sizeof("INSENSITIVE") : 1)
+                       + ((flags & PHP_PQ_DECLARE_NO_SCROLL) ? sizeof("NO SCROLL") :
+                                       (flags & PHP_PQ_DECLARE_SCROLL) ? sizeof("SCROLL") : 1)
+                       + sizeof("CURSOR")
+                       + ((flags & PHP_PQ_DECLARE_WITH_HOLD) ? sizeof("WITH HOLD") : 1)
+                       + sizeof("FOR");
+       }
+
+       return decl_str;
+}
+
+php_pqcur_t *php_pqcur_init(php_pqconn_object_t *conn, const char *name, char *decl, int query_offset, long flags TSRMLS_DC)
+{
+       php_pqcur_t *cur = ecalloc(1, sizeof(*cur));
+
+       php_pq_object_addref(conn TSRMLS_CC);
+       cur->conn = conn;
+       cur->name = estrdup(name);
+       cur->decl = decl;
+       cur->query_offset = query_offset;
+       cur->flags = flags;
+       cur->open = 1;
+
+       return cur;
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqcur___construct, 0, 0, 4)
+       ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0)
+       ZEND_ARG_INFO(0, name)
+       ZEND_ARG_INFO(0, flags)
+       ZEND_ARG_INFO(0, query)
+       ZEND_ARG_INFO(0, async)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqcur, __construct) {
+       zend_error_handling zeh;
+       char *name_str, *query_str;
+       int name_len, query_len;
+       long flags;
+       zval *zconn;
+       ZEND_RESULT_CODE rv;
+       zend_bool async = 0;
+
+       zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
+       rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Osls|b", &zconn, php_pqconn_class_entry, &name_str, &name_len, &flags, &query_str, &query_len, &async);
+       zend_restore_error_handling(&zeh TSRMLS_CC);
+
+       if (SUCCESS == rv) {
+               php_pqcur_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+               php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
+
+               if (obj->intern) {
+                       throw_exce(EX_BAD_METHODCALL TSRMLS_CC, "pq\\Cursor already initialized");
+               } if (!conn_obj->intern) {
+                       throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
+               } else {
+                       int query_offset;
+                       char *decl = php_pqcur_declare_str(name_str, name_len, flags, query_str, query_len, &query_offset);
+
+                       if (async) {
+                               rv = php_pqconn_declare_async(zconn, conn_obj, decl TSRMLS_CC);
+                       } else {
+                               rv = php_pqconn_declare(zconn, conn_obj, decl TSRMLS_CC);
+                       }
+
+                       if (SUCCESS != rv) {
+                               efree(decl);
+                       } else {
+                               obj->intern = php_pqcur_init(conn_obj, name_str, decl, query_offset, flags TSRMLS_CC);
+                       }
+               }
+       }
+}
+
 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;
+       ZEND_RESULT_CODE rv;
 
        zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
        rv = zend_parse_parameters_none();
@@ -176,21 +345,19 @@ static PHP_METHOD(pqcur, open)
                php_pqcur_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
                if (!obj->intern) {
-                       throw_exce(EX_UNINITIALIZED, "pq\\Cursor not initialized");
-               } else if (!obj->intern->open) {
-                       if (SUCCESS == php_pqconn_declare(NULL, obj->intern->conn, obj->intern->decl)) {
-                               obj->intern->open = 1;
-                       }
+                       throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Cursor not initialized");
+               } 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;
+       ZEND_RESULT_CODE rv;
 
        zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
        rv = zend_parse_parameters_none();
@@ -200,9 +367,9 @@ static PHP_METHOD(pqcur, close)
                php_pqcur_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
                if (!obj->intern) {
-                       throw_exce(EX_UNINITIALIZED, "pq\\Cursor not initialized");
+                       throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Cursor not initialized");
                } else {
-                       cur_close(obj TSRMLS_CC);
+                       cur_close(obj, 1, 0 TSRMLS_CC);
                }
        }
 }
@@ -242,8 +409,11 @@ 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)
@@ -275,7 +445,7 @@ PHP_MINIT_FUNCTION(pqcur)
        php_pqcur_object_handlers.get_properties = php_pq_object_properties;
        php_pqcur_object_handlers.get_debug_info = php_pq_object_debug_info;
 
-       zend_hash_init(&php_pqcur_object_prophandlers, 2, NULL, NULL, 1);
+       zend_hash_init(&php_pqcur_object_prophandlers, 4, NULL, NULL, 1);
 
        zend_declare_class_constant_long(php_pqcur_class_entry, ZEND_STRL("BINARY"), PHP_PQ_DECLARE_BINARY TSRMLS_CC);
        zend_declare_class_constant_long(php_pqcur_class_entry, ZEND_STRL("INSENSITIVE"), PHP_PQ_DECLARE_INSENSITIVE TSRMLS_CC);
@@ -291,6 +461,14 @@ PHP_MINIT_FUNCTION(pqcur)
        ph.read = php_pqcur_object_read_connection;
        zend_hash_add(&php_pqcur_object_prophandlers, "connection", sizeof("connection"), (void *) &ph, sizeof(ph), NULL);
 
+       zend_declare_property_null(php_pqcur_class_entry, ZEND_STRL("query"), ZEND_ACC_PUBLIC TSRMLS_CC);
+       ph.read = php_pqcur_object_read_query;
+       zend_hash_add(&php_pqcur_object_prophandlers, "query", sizeof("query"), (void *) &ph, sizeof(ph), NULL);
+
+       zend_declare_property_null(php_pqcur_class_entry, ZEND_STRL("flags"), ZEND_ACC_PUBLIC TSRMLS_CC);
+       ph.read = php_pqcur_object_read_flags;
+       zend_hash_add(&php_pqcur_object_prophandlers, "flags", sizeof("flags"), (void *) &ph, sizeof(ph), NULL);
+
        return SUCCESS;
 }