1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2017 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Georg Richter <georg@php.net> |
16 | Andrey Hristov <andrey@php.net> |
17 | Ulf Wendel <uw@php.net> |
18 +----------------------------------------------------------------------+
19
20 $Id: mysqli.c 299335 2010-05-13 11:05:09Z andrey $
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 /* {{{ */
php_mysqli_result_get_iterator(zend_class_entry * ce,zval * object,int by_ref)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 /* {{{ */
php_mysqli_result_iterator_dtor(zend_object_iterator * iter)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 /* cleanup handled in sxe_object_dtor as we dont always have an iterator wrapper */
72 zval_ptr_dtor(&iterator->intern.data);
73 zval_ptr_dtor(&iterator->current_row);
74 }
75 /* }}} */
76
77 /* {{{ */
php_mysqli_result_iterator_valid(zend_object_iterator * iter)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 /* {{{ */
php_mysqli_result_iterator_current_data(zend_object_iterator * iter)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 /* {{{ */
php_mysqli_result_iterator_move_forward(zend_object_iterator * iter)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 /* {{{ */
php_mysqli_result_iterator_rewind(zend_object_iterator * iter)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 /* {{{ php_mysqli_result_iterator_current_key */
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 /* {{{ php_mysqli_result_iterator_funcs */
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 * Local variables:
161 * tab-width: 4
162 * c-basic-offset: 4
163 * End:
164 * vim600: noet sw=4 ts=4 fdm=marker
165 * vim<600: noet sw=4 ts=4
166 */
167