Add deallocate() and prepare() to Statement
[m6w6/ext-pq] / src / php_pqconn.c
index 0527a7bb43ac85a133a749988fa6466408671c60..f2622d5184b73613019f15bca43f5b56a83c86f9 100644 (file)
@@ -301,6 +301,27 @@ static void php_pqconn_object_read_port(zval *object, void *o, zval *return_valu
        }
 }
 
        }
 }
 
+#if HAVE_PQCONNINFO
+static void php_pqconn_object_read_params(zval *object, void *o, zval *return_value TSRMLS_DC)
+{
+       php_pqconn_object_t *obj = o;
+       PQconninfoOption *ptr, *params = PQconninfo(obj->intern->conn);
+
+       array_init(return_value);
+
+       if (params) {
+               for (ptr = params; ptr->keyword; ++ptr) {
+                       if (ptr->val) {
+                               add_assoc_string(return_value, ptr->keyword, ptr->val, 1);
+                       } else {
+                               add_assoc_null(return_value, ptr->keyword);
+                       }
+               }
+               PQconninfoFree(params);
+       }
+}
+#endif
+
 static void php_pqconn_object_read_options(zval *object, void *o, zval *return_value TSRMLS_DC)
 {
        php_pqconn_object_t *obj = o;
 static void php_pqconn_object_read_options(zval *object, void *o, zval *return_value TSRMLS_DC)
 {
        php_pqconn_object_t *obj = o;
@@ -1112,8 +1133,10 @@ static PHP_METHOD(pqconn, execAsync) {
                        throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
                } else if (!PQsendQuery(obj->intern->conn, query_str)) {
                        throw_exce(EX_IO TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
                        throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
                } else if (!PQsendQuery(obj->intern->conn, query_str)) {
                        throw_exce(EX_IO TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
+#if HAVE_PQSETSINGLEROWMODE
                } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
                        throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
                } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
                        throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
+#endif
                } else {
                        php_pq_callback_recurse(&obj->intern->onevent, &resolver TSRMLS_CC);
                        obj->intern->poller = PQconsumeInput;
                } else {
                        php_pq_callback_recurse(&obj->intern->onevent, &resolver TSRMLS_CC);
                        obj->intern->poller = PQconsumeInput;
@@ -1201,8 +1224,10 @@ static PHP_METHOD(pqconn, execParamsAsync) {
 
                        if (!rc) {
                                throw_exce(EX_IO TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
 
                        if (!rc) {
                                throw_exce(EX_IO TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
+#if HAVE_PQSETSINGLEROWMODE
                        } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
                                throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
                        } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
                                throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
+#endif
                        } else {
                                php_pq_callback_recurse(&obj->intern->onevent, &resolver TSRMLS_CC);
                                obj->intern->poller = PQconsumeInput;
                        } else {
                                php_pq_callback_recurse(&obj->intern->onevent, &resolver TSRMLS_CC);
                                obj->intern->poller = PQconsumeInput;
@@ -1263,13 +1288,7 @@ static PHP_METHOD(pqconn, prepare) {
                        if (SUCCESS != php_pqconn_prepare(getThis(), obj, name_str, query_str, params TSRMLS_CC)) {
                                php_pq_params_free(&params);
                        } else {
                        if (SUCCESS != php_pqconn_prepare(getThis(), obj, name_str, query_str, params TSRMLS_CC)) {
                                php_pq_params_free(&params);
                        } else {
-                               php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
-
-                               php_pq_object_addref(obj TSRMLS_CC);
-                               stm->conn = obj;
-                               stm->name = estrdup(name_str);
-                               stm->params = params;
-                               ZEND_INIT_SYMTABLE(&stm->bound);
+                               php_pqstm_t *stm = php_pqstm_init(obj, name_str, query_str, params TSRMLS_CC);
 
                                return_value->type = IS_OBJECT;
                                return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
 
                                return_value->type = IS_OBJECT;
                                return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
@@ -1289,9 +1308,6 @@ STATUS php_pqconn_prepare_async(zval *object, php_pqconn_object_t *obj, const ch
        if (!PQsendPrepare(obj->intern->conn, name, query, params->type.count, params->type.oids)) {
                rv = FAILURE;
                throw_exce(EX_IO TSRMLS_CC, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj->intern->conn));
        if (!PQsendPrepare(obj->intern->conn, name, query, params->type.count, params->type.oids)) {
                rv = FAILURE;
                throw_exce(EX_IO TSRMLS_CC, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj->intern->conn));
-       } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
-               rv = FAILURE;
-               throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
        } else {
                rv = SUCCESS;
                obj->intern->poller = PQconsumeInput;
        } else {
                rv = SUCCESS;
                obj->intern->poller = PQconsumeInput;
@@ -1328,13 +1344,7 @@ static PHP_METHOD(pqconn, prepareAsync) {
                        if (SUCCESS != php_pqconn_prepare_async(getThis(), obj, name_str, query_str, params TSRMLS_CC)) {
                                php_pq_params_free(&params);
                        } else {
                        if (SUCCESS != php_pqconn_prepare_async(getThis(), obj, name_str, query_str, params TSRMLS_CC)) {
                                php_pq_params_free(&params);
                        } else {
-                               php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
-
-                               php_pq_object_addref(obj TSRMLS_CC);
-                               stm->conn = obj;
-                               stm->name = estrdup(name_str);
-                               stm->params = params;
-                               ZEND_INIT_SYMTABLE(&stm->bound);
+                               php_pqstm_t *stm = php_pqstm_init(obj, name_str, query_str, params TSRMLS_CC);
 
                                return_value->type = IS_OBJECT;
                                return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
 
                                return_value->type = IS_OBJECT;
                                return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
@@ -1419,9 +1429,6 @@ STATUS php_pqconn_declare_async(zval *object, php_pqconn_object_t *obj, const ch
        if (!PQsendQuery(obj->intern->conn, decl)) {
                rv = FAILURE;
                throw_exce(EX_IO TSRMLS_CC, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj->intern->conn));
        if (!PQsendQuery(obj->intern->conn, decl)) {
                rv = FAILURE;
                throw_exce(EX_IO TSRMLS_CC, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj->intern->conn));
-       } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
-               rv = FAILURE;
-               throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
        } else {
                rv = SUCCESS;
                obj->intern->poller = PQconsumeInput;
        } else {
                rv = SUCCESS;
                obj->intern->poller = PQconsumeInput;
@@ -1592,7 +1599,7 @@ STATUS php_pqconn_start_transaction(zval *zconn, php_pqconn_object_t *conn_obj,
        } else {
                PGresult *res;
                smart_str cmd = {0};
        } else {
                PGresult *res;
                smart_str cmd = {0};
-               const char *il = isolation_level(&isolation);
+               const char *il = php_pq_isolation_level(&isolation);
 
                smart_str_appends(&cmd, "START TRANSACTION ISOLATION LEVEL ");
                smart_str_appends(&cmd, il);
 
                smart_str_appends(&cmd, "START TRANSACTION ISOLATION LEVEL ");
                smart_str_appends(&cmd, il);
@@ -1631,7 +1638,7 @@ STATUS php_pqconn_start_transaction_async(zval *zconn, php_pqconn_object_t *conn
                throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
        } else {
                smart_str cmd = {0};
                throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
        } else {
                smart_str cmd = {0};
-               const char *il = isolation_level(&isolation);
+               const char *il = php_pq_isolation_level(&isolation);
 
                smart_str_appends(&cmd, "START TRANSACTION ISOLATION LEVEL ");
                smart_str_appends(&cmd, il);
 
                smart_str_appends(&cmd, "START TRANSACTION ISOLATION LEVEL ");
                smart_str_appends(&cmd, il);
@@ -1717,6 +1724,7 @@ static PHP_METHOD(pqconn, startTransactionAsync) {
 
                        php_pq_object_addref(obj TSRMLS_CC);
                        txn->conn = obj;
 
                        php_pq_object_addref(obj TSRMLS_CC);
                        txn->conn = obj;
+                       txn->open = 1;
                        txn->isolation = isolation;
                        txn->readonly = readonly;
                        txn->deferrable = deferrable;
                        txn->isolation = isolation;
                        txn->readonly = readonly;
                        txn->deferrable = deferrable;
@@ -1971,7 +1979,7 @@ PHP_MINIT_FUNCTION(pqconn)
        php_pqconn_object_handlers.get_properties = php_pq_object_properties;
        php_pqconn_object_handlers.get_debug_info = php_pq_object_debug_info;
 
        php_pqconn_object_handlers.get_properties = php_pq_object_properties;
        php_pqconn_object_handlers.get_debug_info = php_pq_object_debug_info;
 
-       zend_hash_init(&php_pqconn_object_prophandlers, 19, NULL, NULL, 1);
+       zend_hash_init(&php_pqconn_object_prophandlers, 20, NULL, NULL, 1);
 
        zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("status"), CONNECTION_BAD, ZEND_ACC_PUBLIC TSRMLS_CC);
        ph.read = php_pqconn_object_read_status;
 
        zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("status"), CONNECTION_BAD, ZEND_ACC_PUBLIC TSRMLS_CC);
        ph.read = php_pqconn_object_read_status;
@@ -2025,6 +2033,12 @@ PHP_MINIT_FUNCTION(pqconn)
        ph.read = php_pqconn_object_read_port;
        zend_hash_add(&php_pqconn_object_prophandlers, "port", sizeof("port"), (void *) &ph, sizeof(ph), NULL);
 
        ph.read = php_pqconn_object_read_port;
        zend_hash_add(&php_pqconn_object_prophandlers, "port", sizeof("port"), (void *) &ph, sizeof(ph), NULL);
 
+#if HAVE_PQCONNINFO
+       zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("params"), ZEND_ACC_PUBLIC TSRMLS_CC);
+       ph.read = php_pqconn_object_read_params;
+       zend_hash_add(&php_pqconn_object_prophandlers, "params", sizeof("params"), (void *) &ph, sizeof(ph), NULL);
+#endif
+
        zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("options"), ZEND_ACC_PUBLIC TSRMLS_CC);
        ph.read = php_pqconn_object_read_options;
        zend_hash_add(&php_pqconn_object_prophandlers, "options", sizeof("options"), (void *) &ph, sizeof(ph), NULL);
        zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("options"), ZEND_ACC_PUBLIC TSRMLS_CC);
        ph.read = php_pqconn_object_read_options;
        zend_hash_add(&php_pqconn_object_prophandlers, "options", sizeof("options"), (void *) &ph, sizeof(ph), NULL);