add default propoerties to connection
[m6w6/ext-pq] / src / php_pqres.c
index addeab3c93af153d6735a7d7b42d5caf55c73891..b365f3d2e36a1379f40fad1745775550bf4631a2 100644 (file)
@@ -38,7 +38,7 @@ static zend_object_iterator *php_pqres_iterator_init(zend_class_entry *ce, zval
        iter = ecalloc(1, sizeof(*iter));
        iter->zi.funcs = &php_pqres_iterator_funcs;
        iter->zi.data = object;
-       Z_ADDREF_P(object);
+       /* do not addref, because the iterator lives inside this object anyway */
 
        zfetch_type = prop = zend_read_property(ce, object, ZEND_STRL("fetchType"), 0 TSRMLS_CC);
        if (Z_TYPE_P(zfetch_type) != IS_LONG) {
@@ -66,7 +66,6 @@ static void php_pqres_iterator_dtor(zend_object_iterator *i TSRMLS_DC)
                zval_ptr_dtor(&iter->current_val);
                iter->current_val = NULL;
        }
-       zval_ptr_dtor((zval **) &iter->zi.data);
        efree(iter);
 }
 
@@ -273,8 +272,10 @@ void php_pqres_init_instance_data(PGresult *res, php_pqconn_object_t *conn_obj,
        zend_hash_init(&r->bound, 0, 0, ZVAL_PTR_DTOR, 0);
        zend_hash_init(&r->converters, 0, 0, ZVAL_PTR_DTOR, 0);
        zend_hash_copy(&r->converters, &conn_obj->intern->converters, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
-       php_pqres_create_object_ex(php_pqres_class_entry, r, &obj TSRMLS_CC);
 
+       r->default_fetch_type = conn_obj->intern->default_fetch_type;
+
+       php_pqres_create_object_ex(php_pqres_class_entry, r, &obj TSRMLS_CC);
        PQresultSetInstanceData(res, php_pqconn_event, obj);
 
        if (ptr) {
@@ -392,7 +393,7 @@ static void php_pqres_object_read_fetch_type(zval *object, void *o, zval *return
        if (obj->intern->iter) {
                RETVAL_LONG(obj->intern->iter->fetch_type);
        } else {
-               RETVAL_LONG(PHP_PQRES_FETCH_ARRAY);
+               RETVAL_LONG(obj->intern->default_fetch_type);
        }
 }
 
@@ -601,7 +602,7 @@ static PHP_METHOD(pqres, fetchRow) {
                        zval **row = NULL;
 
                        if (fetch_type == -1) {
-                                fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : PHP_PQRES_FETCH_ARRAY;
+                                fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : obj->intern->default_fetch_type;
                        }
 
                        zend_replace_error_handling(EH_THROW, exce(EX_RUNTIME), &zeh TSRMLS_CC);
@@ -670,7 +671,7 @@ static PHP_METHOD(pqres, fetchCol) {
                                                RETVAL_FALSE;
                                        } else {
                                                zval_dtor(zref);
-                                               ZVAL_ZVAL(zref, *zres, 1, 1);
+                                               ZVAL_ZVAL(zref, *zres, 1, 0);
                                                RETVAL_TRUE;
                                        }
                                }
@@ -761,7 +762,7 @@ static PHP_METHOD(pqres, map) {
                        }
 
                        if (fetch_type == -1) {
-                               fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : PHP_PQRES_FETCH_ARRAY;
+                               fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : obj->intern->default_fetch_type;
                        }
 
                        if (keys) {
@@ -858,7 +859,7 @@ static PHP_METHOD(pqres, fetchAll) {
                        int r, rows = PQntuples(obj->intern->res);
 
                        if (fetch_type == -1) {
-                                fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : PHP_PQRES_FETCH_ARRAY;
+                                fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : obj->intern->default_fetch_type;
                        }
 
                        array_init(return_value);
@@ -883,6 +884,25 @@ static PHP_METHOD(pqres, count) {
        }
 }
 
+ZEND_BEGIN_ARG_INFO_EX(ai_pqres_desc, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqres, desc) {
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               if (!obj->intern) {
+                       throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Result not initialized");
+               } else {
+                       int p, params;
+
+                       array_init(return_value);
+                       for (p = 0, params = PQnparams(obj->intern->res); p < params; ++p) {
+                               add_next_index_long(return_value, PQparamtype(obj->intern->res, p));
+                       }
+               }
+       }
+}
+
 static zend_function_entry php_pqres_methods[] = {
        PHP_ME(pqres, bind, ai_pqres_bind, ZEND_ACC_PUBLIC)
        PHP_ME(pqres, fetchBound, ai_pqres_fetch_bound, ZEND_ACC_PUBLIC)
@@ -891,6 +911,7 @@ static zend_function_entry php_pqres_methods[] = {
        PHP_ME(pqres, fetchAll, ai_pqres_fetch_all, ZEND_ACC_PUBLIC)
        PHP_ME(pqres, count, ai_pqres_count, ZEND_ACC_PUBLIC)
        PHP_ME(pqres, map, ai_pqres_map, ZEND_ACC_PUBLIC)
+       PHP_ME(pqres, desc, ai_pqres_desc, ZEND_ACC_PUBLIC)
        {0}
 };