X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-pq;a=blobdiff_plain;f=src%2Fphp_pqres.c;h=c050898bcc46965f0829854bd7974449cfe4ceab;hp=1ad5f1dd681aa7e636bc276b5a8a61c8dee04b67;hb=e7ce3fd45d17058f92ce219a34e560226cf72231;hpb=7d954192543192f2ef9b0db27f22b7807de31749 diff --git a/src/php_pqres.c b/src/php_pqres.c index 1ad5f1d..c050898 100644 --- a/src/php_pqres.c +++ b/src/php_pqres.c @@ -43,8 +43,8 @@ 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; - /* do not addref, because the iterator lives inside this object anyway */ + iter->zi.data = zend_object_store_get_object(object TSRMLS_CC); + zend_objects_store_add_ref(object TSRMLS_CC); zfetch_type = prop = zend_read_property(ce, object, ZEND_STRL("fetchType"), 0 TSRMLS_CC); if (Z_TYPE_P(zfetch_type) != IS_LONG) { @@ -67,18 +67,20 @@ static zend_object_iterator *php_pqres_iterator_init(zend_class_entry *ce, zval static void php_pqres_iterator_dtor(zend_object_iterator *i TSRMLS_DC) { php_pqres_iterator_t *iter = (php_pqres_iterator_t *) i; + php_pqres_object_t *obj = i->data; if (iter->current_val) { zval_ptr_dtor(&iter->current_val); iter->current_val = NULL; } + zend_objects_store_del_ref_by_handle_ex(obj->zv.handle, obj->zv.handlers TSRMLS_CC); efree(iter); } static STATUS php_pqres_iterator_valid(zend_object_iterator *i TSRMLS_DC) { php_pqres_iterator_t *iter = (php_pqres_iterator_t *) i; - php_pqres_object_t *obj = zend_object_store_get_object(iter->zi.data TSRMLS_CC); + php_pqres_object_t *obj = i->data; switch (PQresultStatus(obj->intern->res)) { case PGRES_TUPLES_OK: @@ -96,6 +98,9 @@ static STATUS php_pqres_iterator_valid(zend_object_iterator *i TSRMLS_DC) return SUCCESS; } +#define PHP_PQRES_JSON_OPTIONS(res) \ + (php_pqres_fetch_type(res) != PHP_PQRES_FETCH_OBJECT ? PHP_JSON_OBJECT_AS_ARRAY:0) + zval *php_pqres_typed_zval(php_pqres_t *res, char *val, size_t len, Oid typ TSRMLS_DC) { zval *zv, **zconv; @@ -165,28 +170,28 @@ zval *php_pqres_typed_zval(php_pqres_t *res, char *val, size_t len, Oid typ TSRM if (!(res->auto_convert & PHP_PQRES_CONV_DATETIME)) { goto noconversion; } - php_pqdt_from_string(val, len, "Y-m-d", zv TSRMLS_CC); + php_pqdt_from_string(zv, NULL, val, len, "Y-m-d", NULL TSRMLS_CC); break; case PHP_PQ_OID_ABSTIME: if (!(res->auto_convert & PHP_PQRES_CONV_DATETIME)) { goto noconversion; } - php_pqdt_from_string(val, len, "Y-m-d H:i:s", zv TSRMLS_CC); + php_pqdt_from_string(zv, NULL, val, len, "Y-m-d H:i:s", NULL TSRMLS_CC); break; case PHP_PQ_OID_TIMESTAMP: if (!(res->auto_convert & PHP_PQRES_CONV_DATETIME)) { goto noconversion; } - php_pqdt_from_string(val, len, "Y-m-d H:i:s.u", zv TSRMLS_CC); + php_pqdt_from_string(zv, NULL, val, len, "Y-m-d H:i:s.u", NULL TSRMLS_CC); break; case PHP_PQ_OID_TIMESTAMPTZ: if (!(res->auto_convert & PHP_PQRES_CONV_DATETIME)) { goto noconversion; } - php_pqdt_from_string(val, len, "Y-m-d H:i:s.uO", zv TSRMLS_CC); + php_pqdt_from_string(zv, NULL, val, len, "Y-m-d H:i:s.uO", NULL TSRMLS_CC); break; #if PHP_PQ_HAVE_PHP_JSON_H && defined(PHP_PQ_OID_JSON) @@ -197,7 +202,7 @@ zval *php_pqres_typed_zval(php_pqres_t *res, char *val, size_t len, Oid typ TSRM if (!(res->auto_convert & PHP_PQRES_CONV_JSON)) { goto noconversion; } - php_json_decode_ex(zv, val, len, 0, 512 /* PHP_JSON_DEFAULT_DEPTH */ TSRMLS_CC); + php_json_decode_ex(zv, val, len, PHP_PQRES_JSON_OPTIONS(res), 512 /* PHP_JSON_DEFAULT_DEPTH */ TSRMLS_CC); break; #endif @@ -302,7 +307,7 @@ zval *php_pqres_row_to_zval(PGresult *res, unsigned row, php_pqres_fetch_t fetch static void php_pqres_iterator_current(zend_object_iterator *i, zval ***data_ptr TSRMLS_DC) { php_pqres_iterator_t *iter = (php_pqres_iterator_t *) i; - php_pqres_object_t *obj = zend_object_store_get_object(iter->zi.data TSRMLS_CC); + php_pqres_object_t *obj = i->data; if (!iter->current_val) { iter->current_val = php_pqres_row_to_zval(obj->intern->res, iter->index, iter->fetch_type, NULL TSRMLS_CC); @@ -419,6 +424,11 @@ void php_pqres_init_instance_data(PGresult *res, php_pqconn_object_t *conn_obj, } } +php_pqres_fetch_t php_pqres_fetch_type(php_pqres_t *res) +{ + return res->iter ? res->iter->fetch_type : res->default_fetch_type; +} + static void php_pqres_object_free(void *o TSRMLS_DC) { php_pqres_object_t *obj = o; @@ -526,11 +536,7 @@ static void php_pqres_object_read_fetch_type(zval *object, void *o, zval *return { php_pqres_object_t *obj = o; - if (obj->intern->iter) { - RETVAL_LONG(obj->intern->iter->fetch_type); - } else { - RETVAL_LONG(obj->intern->default_fetch_type); - } + RETVAL_LONG(php_pqres_fetch_type(obj->intern)); } static void php_pqres_object_write_fetch_type(zval *object, void *o, zval *value TSRMLS_DC) @@ -783,7 +789,7 @@ static PHP_METHOD(pqres, fetchRow) { zval **row = NULL; if (fetch_type == -1) { - fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : obj->intern->default_fetch_type; + fetch_type = php_pqres_fetch_type(obj->intern); } zend_replace_error_handling(EH_THROW, exce(EX_RUNTIME), &zeh TSRMLS_CC); @@ -837,7 +843,7 @@ static PHP_METHOD(pqres, fetchCol) { zval **row = NULL; zend_replace_error_handling(EH_THROW, exce(EX_RUNTIME), &zeh TSRMLS_CC); - php_pqres_iteration(getThis(), obj, obj->intern->iter ? obj->intern->iter->fetch_type : 0, &row TSRMLS_CC); + php_pqres_iteration(getThis(), obj, php_pqres_fetch_type(obj->intern), &row TSRMLS_CC); if (row) { php_pqres_col_t col; @@ -979,7 +985,7 @@ static PHP_METHOD(pqres, map) { } if (fetch_type == -1) { - fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : obj->intern->default_fetch_type; + fetch_type = php_pqres_fetch_type(obj->intern); } if (keys) { @@ -1076,7 +1082,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 : obj->intern->default_fetch_type; + fetch_type = php_pqres_fetch_type(obj->intern); } array_init(return_value);