This source file includes following definitions.
- php_mysqli_result_get_iterator
- php_mysqli_result_iterator_dtor
- php_mysqli_result_iterator_valid
- php_mysqli_result_iterator_current_data
- php_mysqli_result_iterator_move_forward
- php_mysqli_result_iterator_rewind
- php_mysqli_result_iterator_current_key
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <signal.h>
28
29 #include "php.h"
30 #include "php_ini.h"
31 #include "php_mysqli_structs.h"
32 #include "mysqli_priv.h"
33 #include "zend_interfaces.h"
34
35
36 extern zend_object_iterator_funcs php_mysqli_result_iterator_funcs;
37
38 typedef struct {
39 zend_object_iterator intern;
40 mysqli_object *result;
41 zval current_row;
42 my_longlong row_num;
43 } php_mysqli_result_iterator;
44
45
46
47 zend_object_iterator *php_mysqli_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
48 {
49 php_mysqli_result_iterator *iterator;
50
51 if (by_ref) {
52 zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
53 }
54 iterator = ecalloc(1, sizeof(php_mysqli_result_iterator));
55 zend_iterator_init(&iterator->intern);
56
57 ZVAL_COPY(&iterator->intern.data, object);
58 iterator->intern.funcs = &php_mysqli_result_iterator_funcs;
59 iterator->result = Z_MYSQLI_P(object);
60 iterator->row_num = -1;
61
62 return &iterator->intern;
63 }
64
65
66
67 static void php_mysqli_result_iterator_dtor(zend_object_iterator *iter)
68 {
69 php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*)iter;
70
71
72 zval_ptr_dtor(&iterator->intern.data);
73 zval_ptr_dtor(&iterator->current_row);
74 }
75
76
77
78 static int php_mysqli_result_iterator_valid(zend_object_iterator *iter)
79 {
80 php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
81
82 return Z_TYPE(iterator->current_row) == IS_ARRAY ? SUCCESS : FAILURE;
83 }
84
85
86
87 static zval *php_mysqli_result_iterator_current_data(zend_object_iterator *iter)
88 {
89 php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
90
91 return &iterator->current_row;
92 }
93
94
95
96 static void php_mysqli_result_iterator_move_forward(zend_object_iterator *iter)
97 {
98
99 php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
100 mysqli_object *intern = iterator->result;
101 MYSQL_RES *result;
102
103 MYSQLI_FETCH_RESOURCE_BY_OBJ(result, MYSQL_RES *, intern, "mysqli_result", MYSQLI_STATUS_VALID);
104
105 zval_ptr_dtor(&iterator->current_row);
106 php_mysqli_fetch_into_hash_aux(&iterator->current_row, result, MYSQLI_ASSOC);
107 if (Z_TYPE(iterator->current_row) == IS_ARRAY) {
108 iterator->row_num++;
109 }
110 }
111
112
113
114 static void php_mysqli_result_iterator_rewind(zend_object_iterator *iter)
115 {
116 php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
117 mysqli_object *intern = iterator->result;
118 MYSQL_RES *result;
119
120 MYSQLI_FETCH_RESOURCE_BY_OBJ(result, MYSQL_RES *, intern, "mysqli_result", MYSQLI_STATUS_VALID);
121
122 if (mysqli_result_is_unbuffered(result)) {
123 #if MYSQLI_USE_MYSQLND
124 if (result->unbuf->eof_reached) {
125 #else
126 if (result->eof) {
127 #endif
128 php_error_docref(NULL, E_WARNING, "Data fetched with MYSQLI_USE_RESULT can be iterated only once");
129 return;
130 }
131 } else {
132 mysql_data_seek(result, 0);
133 }
134 iterator->row_num = -1;
135 php_mysqli_result_iterator_move_forward(iter);
136 }
137
138
139
140 static void php_mysqli_result_iterator_current_key(zend_object_iterator *iter, zval *key)
141 {
142 php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
143
144 ZVAL_LONG(key, iterator->row_num);
145 }
146
147
148
149 zend_object_iterator_funcs php_mysqli_result_iterator_funcs = {
150 php_mysqli_result_iterator_dtor,
151 php_mysqli_result_iterator_valid,
152 php_mysqli_result_iterator_current_data,
153 php_mysqli_result_iterator_current_key,
154 php_mysqli_result_iterator_move_forward,
155 php_mysqli_result_iterator_rewind,
156 };
157
158
159
160
161
162
163
164
165
166