2010-05-18 18:39:26 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 17:03:12 +08:00
|
|
|
| Copyright (c) The PHP Group |
|
2010-05-18 18:39:26 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.php.net/license/3_01.txt |
|
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Georg Richter <georg@php.net> |
|
|
|
|
| Andrey Hristov <andrey@php.net> |
|
|
|
|
| Ulf Wendel <uw@php.net> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include "php.h"
|
|
|
|
#include "php_ini.h"
|
|
|
|
#include "php_mysqli_structs.h"
|
2010-08-20 20:25:17 +08:00
|
|
|
#include "mysqli_priv.h"
|
2010-05-18 18:39:26 +08:00
|
|
|
#include "zend_interfaces.h"
|
|
|
|
|
|
|
|
|
2017-12-14 19:21:22 +08:00
|
|
|
extern const zend_object_iterator_funcs php_mysqli_result_iterator_funcs;
|
2010-05-18 18:39:26 +08:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
zend_object_iterator intern;
|
|
|
|
mysqli_object *result;
|
2014-05-11 14:53:18 +08:00
|
|
|
zval current_row;
|
2010-05-18 18:39:26 +08:00
|
|
|
my_longlong row_num;
|
|
|
|
} php_mysqli_result_iterator;
|
|
|
|
|
|
|
|
|
|
|
|
/* {{{ */
|
2014-12-14 06:06:14 +08:00
|
|
|
zend_object_iterator *php_mysqli_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
|
2010-05-18 18:39:26 +08:00
|
|
|
{
|
|
|
|
php_mysqli_result_iterator *iterator;
|
|
|
|
|
|
|
|
if (by_ref) {
|
|
|
|
zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
|
|
|
|
}
|
|
|
|
iterator = ecalloc(1, sizeof(php_mysqli_result_iterator));
|
2014-12-14 06:06:14 +08:00
|
|
|
zend_iterator_init(&iterator->intern);
|
2010-05-18 18:39:26 +08:00
|
|
|
|
2019-05-29 01:10:02 +08:00
|
|
|
Z_ADDREF_P(object);
|
|
|
|
ZVAL_OBJ(&iterator->intern.data, Z_OBJ_P(object));
|
2010-05-18 18:39:26 +08:00
|
|
|
iterator->intern.funcs = &php_mysqli_result_iterator_funcs;
|
2014-05-11 14:53:18 +08:00
|
|
|
iterator->result = Z_MYSQLI_P(object);
|
2010-05-18 18:39:26 +08:00
|
|
|
iterator->row_num = -1;
|
|
|
|
|
2014-05-11 14:53:18 +08:00
|
|
|
return &iterator->intern;
|
2010-05-18 18:39:26 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ */
|
2014-12-14 06:06:14 +08:00
|
|
|
static void php_mysqli_result_iterator_dtor(zend_object_iterator *iter)
|
2010-05-18 18:39:26 +08:00
|
|
|
{
|
2014-05-11 14:53:18 +08:00
|
|
|
php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*)iter;
|
2010-05-18 18:39:26 +08:00
|
|
|
|
2018-07-25 11:38:50 +08:00
|
|
|
/* cleanup handled in sxe_object_dtor as we don't always have an iterator wrapper */
|
2014-05-11 14:53:18 +08:00
|
|
|
zval_ptr_dtor(&iterator->intern.data);
|
|
|
|
zval_ptr_dtor(&iterator->current_row);
|
2010-05-18 18:39:26 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ */
|
2014-12-14 06:06:14 +08:00
|
|
|
static int php_mysqli_result_iterator_valid(zend_object_iterator *iter)
|
2010-05-18 18:39:26 +08:00
|
|
|
{
|
|
|
|
php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
|
|
|
|
|
2014-05-11 14:53:18 +08:00
|
|
|
return Z_TYPE(iterator->current_row) == IS_ARRAY ? SUCCESS : FAILURE;
|
2010-05-18 18:39:26 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ */
|
2014-12-14 06:06:14 +08:00
|
|
|
static zval *php_mysqli_result_iterator_current_data(zend_object_iterator *iter)
|
2010-05-18 18:39:26 +08:00
|
|
|
{
|
|
|
|
php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
|
|
|
|
|
2014-05-11 14:53:18 +08:00
|
|
|
return &iterator->current_row;
|
2010-05-18 18:39:26 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ */
|
2014-12-14 06:06:14 +08:00
|
|
|
static void php_mysqli_result_iterator_move_forward(zend_object_iterator *iter)
|
2010-05-18 18:39:26 +08:00
|
|
|
{
|
2010-10-04 18:02:58 +08:00
|
|
|
|
2010-05-18 18:39:26 +08:00
|
|
|
php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
|
|
|
|
mysqli_object *intern = iterator->result;
|
|
|
|
MYSQL_RES *result;
|
|
|
|
|
|
|
|
MYSQLI_FETCH_RESOURCE_BY_OBJ(result, MYSQL_RES *, intern, "mysqli_result", MYSQLI_STATUS_VALID);
|
2014-05-11 14:53:18 +08:00
|
|
|
|
|
|
|
zval_ptr_dtor(&iterator->current_row);
|
2014-12-14 06:06:14 +08:00
|
|
|
php_mysqli_fetch_into_hash_aux(&iterator->current_row, result, MYSQLI_ASSOC);
|
2014-05-11 14:53:18 +08:00
|
|
|
if (Z_TYPE(iterator->current_row) == IS_ARRAY) {
|
2010-05-18 18:39:26 +08:00
|
|
|
iterator->row_num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ */
|
2014-12-14 06:06:14 +08:00
|
|
|
static void php_mysqli_result_iterator_rewind(zend_object_iterator *iter)
|
2010-05-18 18:39:26 +08:00
|
|
|
{
|
|
|
|
php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
|
|
|
|
mysqli_object *intern = iterator->result;
|
|
|
|
MYSQL_RES *result;
|
|
|
|
|
|
|
|
MYSQLI_FETCH_RESOURCE_BY_OBJ(result, MYSQL_RES *, intern, "mysqli_result", MYSQLI_STATUS_VALID);
|
|
|
|
|
|
|
|
if (mysqli_result_is_unbuffered(result)) {
|
|
|
|
#if MYSQLI_USE_MYSQLND
|
2011-09-05 23:29:45 +08:00
|
|
|
if (result->unbuf->eof_reached) {
|
2010-05-18 18:39:26 +08:00
|
|
|
#else
|
|
|
|
if (result->eof) {
|
|
|
|
#endif
|
2014-12-14 06:06:14 +08:00
|
|
|
php_error_docref(NULL, E_WARNING, "Data fetched with MYSQLI_USE_RESULT can be iterated only once");
|
2010-05-18 18:39:26 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mysql_data_seek(result, 0);
|
|
|
|
}
|
|
|
|
iterator->row_num = -1;
|
2014-12-14 06:06:14 +08:00
|
|
|
php_mysqli_result_iterator_move_forward(iter);
|
2010-05-18 18:39:26 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2010-05-18 23:02:50 +08:00
|
|
|
/* {{{ php_mysqli_result_iterator_current_key */
|
2014-12-14 06:06:14 +08:00
|
|
|
static void php_mysqli_result_iterator_current_key(zend_object_iterator *iter, zval *key)
|
2010-05-18 18:39:26 +08:00
|
|
|
{
|
|
|
|
php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
ZVAL_LONG(key, iterator->row_num);
|
2010-05-18 18:39:26 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ php_mysqli_result_iterator_funcs */
|
2017-12-14 19:21:22 +08:00
|
|
|
const zend_object_iterator_funcs php_mysqli_result_iterator_funcs = {
|
2010-05-18 18:39:26 +08:00
|
|
|
php_mysqli_result_iterator_dtor,
|
|
|
|
php_mysqli_result_iterator_valid,
|
|
|
|
php_mysqli_result_iterator_current_data,
|
|
|
|
php_mysqli_result_iterator_current_key,
|
|
|
|
php_mysqli_result_iterator_move_forward,
|
|
|
|
php_mysqli_result_iterator_rewind,
|
2016-06-22 05:40:50 +08:00
|
|
|
NULL
|
2010-05-18 18:39:26 +08:00
|
|
|
};
|
|
|
|
/* }}} */
|