Add deallocate() and prepare() to Statement
[m6w6/ext-pq] / src / php_pqstm.c
index 83496df4f8b75d1ad2b1719e3ff6200cd9658537..178342a640d288c54ac097b3c68d6905037bec67 100644 (file)
@@ -29,35 +29,60 @@ zend_class_entry *php_pqstm_class_entry;
 static zend_object_handlers php_pqstm_object_handlers;
 static HashTable php_pqstm_object_prophandlers;
 
-static void php_pqstm_object_free(void *o TSRMLS_DC)
+static void php_pqstm_deallocate(php_pqstm_object_t *obj, zend_bool async, zend_bool silent)
 {
-       php_pqstm_object_t *obj = o;
-#if DBG_GC
-       fprintf(stderr, "FREE stm(#%d) %p (conn(#%d): %p)\n", obj->zv.handle, obj, obj->intern->conn->zv.handle, obj->intern->conn);
-#endif
-       if (obj->intern) {
+       if (obj->intern->allocated) {
                char *quoted_name = PQescapeIdentifier(obj->intern->conn->intern->conn, obj->intern->name, strlen(obj->intern->name));
 
-               php_pq_callback_dtor(&obj->intern->conn->intern->onevent);
-
                if (quoted_name) {
-                       PGresult *res;
                        smart_str cmd = {0};
 
                        smart_str_appends(&cmd, "DEALLOCATE ");
                        smart_str_appends(&cmd, quoted_name);
                        smart_str_0(&cmd);
-                       PQfreemem(quoted_name);
 
-                       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 deallocate statement (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+                               }
+                       } else {
+                               PGresult *res;
+
+                               if ((res = PQexec(obj->intern->conn->intern->conn, cmd.c))) {
+                                       PHP_PQclear(res);
+                               } else if (!silent) {
+                                       throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to deallocate statement (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+                               }
                        }
+
+                       PQfreemem(quoted_name);
                        smart_str_free(&cmd);
                }
 
-               php_pq_object_delref(obj->intern->conn TSRMLS_CC);
+               obj->intern->allocated = 0;
+       }
+}
+
+static void php_pqstm_object_free(void *o TSRMLS_DC)
+{
+       php_pqstm_object_t *obj = o;
+#if DBG_GC
+       fprintf(stderr, "FREE stm(#%d) %p (conn(#%d): %p)\n", obj->zv.handle, obj, obj->intern->conn->zv.handle, obj->intern->conn);
+#endif
+       if (obj->intern) {
+               if (obj->intern->conn->intern) {
+                       php_pq_callback_dtor(&obj->intern->conn->intern->onevent);
+                       php_pqstm_deallocate(obj, 0, 1);
+                       php_pq_object_delref(obj->intern->conn TSRMLS_CC);
+               }
                efree(obj->intern->name);
                zend_hash_destroy(&obj->intern->bound);
+               if (obj->intern->params) {
+                       php_pq_params_free(&obj->intern->params);
+               }
                efree(obj->intern);
                obj->intern = NULL;
        }
@@ -107,10 +132,25 @@ static void php_pqstm_object_read_connection(zval *object, void *o, zval *return
        php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
 }
 
+php_pqstm_t *php_pqstm_init(php_pqconn_object_t *conn, const char *name, const char *query, php_pq_params_t *params TSRMLS_DC)
+{
+       php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
+
+       php_pq_object_addref(conn TSRMLS_CC);
+       stm->conn = conn;
+       stm->name = estrdup(name);
+       stm->params = params;
+       stm->query = estrdup(query);
+       stm->allocated = 1;
+
+       ZEND_INIT_SYMTABLE(&stm->bound);
+
+       return stm;
+}
 
 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_construct, 0, 0, 3)
-       ZEND_ARG_OBJ_INFO(0, Connection, pq\\Connection, 0)
-       ZEND_ARG_INFO(0, type)
+       ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0)
+       ZEND_ARG_INFO(0, name)
        ZEND_ARG_INFO(0, query)
        ZEND_ARG_ARRAY_INFO(0, types, 1)
        ZEND_ARG_INFO(0, async)
@@ -131,23 +171,21 @@ static PHP_METHOD(pqstm, __construct) {
                php_pqstm_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 (!conn_obj->intern) {
+               if (obj->intern) {
+                       throw_exce(EX_BAD_METHODCALL TSRMLS_CC, "pq\\Statement already initialized");
+               } else if (!conn_obj->intern) {
                        throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
                } else {
+                       php_pq_params_t *params = php_pq_params_init(&conn_obj->intern->converters, ztypes ? Z_ARRVAL_P(ztypes) : NULL, NULL TSRMLS_CC);
+
                        if (async) {
-                               rv = php_pqconn_prepare_async(zconn, conn_obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC);
+                               rv = php_pqconn_prepare_async(zconn, conn_obj, name_str, query_str, params TSRMLS_CC);
                        } else {
-                               rv = php_pqconn_prepare(zconn, conn_obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC);
+                               rv = php_pqconn_prepare(zconn, conn_obj, name_str, query_str, params TSRMLS_CC);
                        }
 
                        if (SUCCESS == rv) {
-                               php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
-
-                               php_pq_object_addref(conn_obj TSRMLS_CC);
-                               stm->conn = conn_obj;
-                               stm->name = estrdup(name_str);
-                               ZEND_INIT_SYMTABLE(&stm->bound);
-                               obj->intern = stm;
+                               obj->intern = php_pqstm_init(conn_obj, name_str, query_str, params TSRMLS_CC);
                        }
                }
        }
@@ -158,17 +196,26 @@ ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_bind, 0, 0, 2)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(pqstm, bind) {
        long param_no;
-       zval *param_ref;
+       zval **param_ref;
+       zend_error_handling zeh;
+       STATUS rv;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &param_no, &param_ref)) {
+       zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
+       rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lZ", &param_no, &param_ref);
+       zend_restore_error_handling(&zeh TSRMLS_CC);
+
+       if (SUCCESS == rv) {
                php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
                if (!obj->intern) {
                        throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement not initialized");
+               } else if (!obj->intern->allocated) {
+                       throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement has been deallocated");
                } else {
-                       Z_ADDREF_P(param_ref);
-                       zend_hash_index_update(&obj->intern->bound, param_no, (void *) &param_ref, sizeof(zval *), NULL);
-                       zend_hash_sort(&obj->intern->bound, zend_qsort, compare_index, 0 TSRMLS_CC);
+                       SEPARATE_ZVAL_TO_MAKE_IS_REF(param_ref);
+                       Z_ADDREF_PP(param_ref);
+                       zend_hash_index_update(&obj->intern->bound, param_no, (void *) param_ref, sizeof(zval *), NULL);
+                       zend_hash_sort(&obj->intern->bound, zend_qsort, php_pq_compare_index, 0 TSRMLS_CC);
                }
        }
 }
@@ -190,26 +237,14 @@ static PHP_METHOD(pqstm, exec) {
 
                if (!obj->intern) {
                        throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement not initialized");
+               } else if (!obj->intern->allocated) {
+                       throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement has been deallocated");
                } else {
-                       int count = 0;
-                       char **params = NULL;
-                       HashTable zdtor;
                        PGresult *res;
 
-                       ZEND_INIT_SYMTABLE(&zdtor);
-
-                       if (zparams) {
-                               count = php_pq_params_to_array(Z_ARRVAL_P(zparams), &params, &zdtor TSRMLS_CC);
-                       } else {
-                               count = php_pq_params_to_array(&obj->intern->bound, &params, &zdtor TSRMLS_CC);
-                       }
-
-                       res = PQexecPrepared(obj->intern->conn->intern->conn, obj->intern->name, count, (const char *const*) params, NULL, NULL, 0);
-
-                       if (params) {
-                               efree(params);
-                       }
-                       zend_hash_destroy(&zdtor);
+                       php_pq_params_set_params(obj->intern->params, zparams ? Z_ARRVAL_P(zparams) : &obj->intern->bound);
+                       res = PQexecPrepared(obj->intern->conn->intern->conn, obj->intern->name, obj->intern->params->param.count, (const char *const*) obj->intern->params->param.strings, NULL, NULL, 0);
+                       php_pq_params_set_params(obj->intern->params, NULL);
 
                        if (!res) {
                                throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to execute statement (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
@@ -240,36 +275,26 @@ static PHP_METHOD(pqstm, execAsync) {
 
                if (!obj->intern) {
                        throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement not initialized");
+               } else if (!obj->intern->allocated) {
+                       throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement has been deallocated");
                } else {
-                       int count;
-                       char **params = NULL;
-                       HashTable zdtor;
+                       int rc;
 
-                       if (zparams) {
-                               ZEND_INIT_SYMTABLE(&zdtor);
-                               count = php_pq_params_to_array(Z_ARRVAL_P(zparams), &params, &zdtor TSRMLS_CC);
-                       }
+                       php_pq_params_set_params(obj->intern->params, zparams ? Z_ARRVAL_P(zparams) : &obj->intern->bound);
+                       rc = PQsendQueryPrepared(obj->intern->conn->intern->conn, obj->intern->name, obj->intern->params->param.count, (const char *const*) obj->intern->params->param.strings, NULL, NULL, 0);
+                       php_pq_params_set_params(obj->intern->params, NULL);
 
-                       if (!PQsendQueryPrepared(obj->intern->conn->intern->conn, obj->intern->name, count, (const char *const*) params, NULL, NULL, 0)) {
+                       if (!rc) {
                                throw_exce(EX_IO TSRMLS_CC, "Failed to execute statement (%s)", 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_dtor(&obj->intern->conn->intern->onevent);
-                               if (resolver.fci.size > 0) {
-                                       obj->intern->conn->intern->onevent = resolver;
-                                       php_pq_callback_addref(&obj->intern->conn->intern->onevent);
-                               }
+                               php_pq_callback_recurse(&obj->intern->conn->intern->onevent, &resolver TSRMLS_CC);
                                obj->intern->conn->intern->poller = PQconsumeInput;
                        }
 
-                       if (params) {
-                               efree(params);
-                       }
-                       if (zparams) {
-                               zend_hash_destroy(&zdtor);
-                       }
-
                        php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
                }
        }
@@ -290,6 +315,8 @@ static PHP_METHOD(pqstm, desc) {
 
                if (!obj->intern) {
                        throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement not initialized");
+               } else if (!obj->intern->allocated) {
+                       throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement has been deallocated");
                } else {
                        PGresult *res = PQdescribePrepared(obj->intern->conn->intern->conn, obj->intern->name);
 
@@ -311,14 +338,16 @@ static PHP_METHOD(pqstm, desc) {
        }
 }
 
-ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_desc_async, 0, 0, 0)
+ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_desc_async, 0, 0, 1)
+       ZEND_ARG_INFO(0, callable)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(pqstm, descAsync) {
        zend_error_handling zeh;
+       php_pq_callback_t resolver = {{0}};
        STATUS rv;
 
        zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
-       rv = zend_parse_parameters_none();
+       rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f", &resolver.fci, &resolver.fcc);
        zend_restore_error_handling(&zeh TSRMLS_CC);
 
        if (SUCCESS == rv) {
@@ -326,25 +355,114 @@ static PHP_METHOD(pqstm, descAsync) {
 
                if (!obj->intern) {
                        throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement not initialized");
+               } else if (!obj->intern->allocated) {
+                       throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement has been deallocated");
                } else if (!PQsendDescribePrepared(obj->intern->conn->intern->conn, obj->intern->name)) {
                        throw_exce(EX_IO TSRMLS_CC, "Failed to describe statement: %s", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
                } else {
+                       php_pq_callback_recurse(&obj->intern->conn->intern->onevent, &resolver TSRMLS_CC);
                        obj->intern->conn->intern->poller = PQconsumeInput;
                        php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
                }
        }
 }
 
+static zend_always_inline void php_pqstm_deallocate_handler(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 == SUCCESS) {
+               php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               if (!obj->intern) {
+                       throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement not initialized");
+               } else {
+                       php_pqstm_deallocate(obj, async, 0 TSRMLS_CC);
+               }
+       }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_deallocate, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqstm, deallocate)
+{
+       php_pqstm_deallocate_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_deallocate_async, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqstm, deallocateAsync)
+{
+       php_pqstm_deallocate_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
+}
+
+static zend_always_inline void php_pqstm_prepare_handler(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 == SUCCESS) {
+               php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               if (!obj->intern) {
+                       throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement not initialized");
+               } else if (!obj->intern->allocated) {
+                       if (async) {
+                               rv = php_pqconn_prepare_async(NULL, obj->intern->conn, obj->intern->name, obj->intern->query, obj->intern->params TSRMLS_CC);
+                       } else {
+                               rv = php_pqconn_prepare(NULL, obj->intern->conn, obj->intern->name, obj->intern->query, obj->intern->params TSRMLS_CC);
+                       }
+
+                       if (SUCCESS == rv) {
+                               obj->intern->allocated = 1;
+                       }
+               }
+       }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_prepare, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqstm, prepare)
+{
+       php_pqstm_prepare_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_prepare_async, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqstm, prepareAsync)
+{
+       php_pqstm_prepare_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
+}
+
 static zend_function_entry php_pqstm_methods[] = {
        PHP_ME(pqstm, __construct, ai_pqstm_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
        PHP_ME(pqstm, bind, ai_pqstm_bind, ZEND_ACC_PUBLIC)
-       PHP_ME(pqstm, exec, ai_pqstm_exec, ZEND_ACC_PUBLIC)
+       PHP_ME(pqstm, deallocate, ai_pqstm_deallocate, ZEND_ACC_PUBLIC)
+       PHP_ME(pqstm, deallocateAsync, ai_pqstm_deallocate_async, ZEND_ACC_PUBLIC)
        PHP_ME(pqstm, desc, ai_pqstm_desc, ZEND_ACC_PUBLIC)
-       PHP_ME(pqstm, execAsync, ai_pqstm_exec_async, ZEND_ACC_PUBLIC)
        PHP_ME(pqstm, descAsync, ai_pqstm_desc_async, ZEND_ACC_PUBLIC)
+       PHP_ME(pqstm, exec, ai_pqstm_exec, ZEND_ACC_PUBLIC)
+       PHP_ME(pqstm, execAsync, ai_pqstm_exec_async, ZEND_ACC_PUBLIC)
+       PHP_ME(pqstm, prepare, ai_pqstm_prepare, ZEND_ACC_PUBLIC)
+       PHP_ME(pqstm, prepareAsync, ai_pqstm_prepare_async, ZEND_ACC_PUBLIC)
        {0}
 };
 
+PHP_MSHUTDOWN_FUNCTION(pqstm)
+{
+       zend_hash_destroy(&php_pqstm_object_prophandlers);
+       return SUCCESS;
+}
+
 PHP_MINIT_FUNCTION(pqstm)
 {
        zend_class_entry ce = {0};
@@ -359,6 +477,7 @@ PHP_MINIT_FUNCTION(pqstm)
        php_pqstm_object_handlers.write_property = php_pq_object_write_prop;
        php_pqstm_object_handlers.clone_obj = NULL;
        php_pqstm_object_handlers.get_property_ptr_ptr = NULL;
+       php_pqstm_object_handlers.get_gc = NULL;
        php_pqstm_object_handlers.get_properties = php_pq_object_properties;
        php_pqstm_object_handlers.get_debug_info = php_pq_object_debug_info;