- Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathmysqli_result_iterator.c
153 lines (125 loc) · 4.66 KB
/
mysqli_result_iterator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| 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: |
| https://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> |
+----------------------------------------------------------------------+
*/
#ifdefHAVE_CONFIG_H
#include<config.h>
#endif
#include<signal.h>
#include"php.h"
#include"php_mysqli_structs.h"
#include"mysqli_priv.h"
#include"zend_interfaces.h"
externconstzend_object_iterator_funcsphp_mysqli_result_iterator_funcs;
typedefstruct {
zend_object_iteratorintern;
mysqli_object*result;
zvalcurrent_row;
my_longlongrow_num;
} php_mysqli_result_iterator;
/* {{{ */
zend_object_iterator*php_mysqli_result_get_iterator(zend_class_entry*ce, zval*object, intby_ref)
{
php_mysqli_result_iterator*iterator;
if (by_ref) {
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
returnNULL;
}
iterator=ecalloc(1, sizeof(php_mysqli_result_iterator));
zend_iterator_init(&iterator->intern);
Z_ADDREF_P(object);
ZVAL_OBJ(&iterator->intern.data, Z_OBJ_P(object));
iterator->intern.funcs=&php_mysqli_result_iterator_funcs;
iterator->result=Z_MYSQLI_P(object);
iterator->row_num=-1;
return&iterator->intern;
}
/* }}} */
/* {{{ */
staticvoidphp_mysqli_result_iterator_dtor(zend_object_iterator*iter)
{
php_mysqli_result_iterator*iterator= (php_mysqli_result_iterator*)iter;
/* cleanup handled in sxe_object_dtor as we don't always have an iterator wrapper */
zval_ptr_dtor(&iterator->intern.data);
zval_ptr_dtor(&iterator->current_row);
}
/* }}} */
/* {{{ */
staticzend_resultphp_mysqli_result_iterator_valid(zend_object_iterator*iter)
{
php_mysqli_result_iterator*iterator= (php_mysqli_result_iterator*) iter;
returnZ_TYPE(iterator->current_row) ==IS_ARRAY ? SUCCESS : FAILURE;
}
/* }}} */
/* {{{ */
staticzval*php_mysqli_result_iterator_current_data(zend_object_iterator*iter)
{
php_mysqli_result_iterator*iterator= (php_mysqli_result_iterator*) iter;
return&iterator->current_row;
}
/* }}} */
/* {{{ */
staticvoidphp_mysqli_result_iterator_move_forward(zend_object_iterator*iter)
{
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);
zval_ptr_dtor(&iterator->current_row);
php_mysqli_fetch_into_hash_aux(&iterator->current_row, result, MYSQLI_ASSOC);
if (Z_TYPE(iterator->current_row) ==IS_ARRAY) {
iterator->row_num++;
}
}
/* }}} */
/* {{{ */
staticvoidphp_mysqli_result_iterator_rewind(zend_object_iterator*iter)
{
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 (result->unbuf->eof_reached) {
zend_error(E_WARNING, "Data fetched with MYSQLI_USE_RESULT can be iterated only once");
return;
}
} else {
mysql_data_seek(result, 0);
}
iterator->row_num=-1;
php_mysqli_result_iterator_move_forward(iter);
}
/* }}} */
/* {{{ php_mysqli_result_iterator_current_key */
staticvoidphp_mysqli_result_iterator_current_key(zend_object_iterator*iter, zval*key)
{
php_mysqli_result_iterator*iterator= (php_mysqli_result_iterator*) iter;
ZVAL_LONG(key, iterator->row_num);
}
/* }}} */
/* {{{ php_mysqli_result_iterator_funcs */
constzend_object_iterator_funcsphp_mysqli_result_iterator_funcs= {
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,
NULL,
NULL, /* get_gc */
};
/* }}} */