1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2006-2018 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: Andrey Hristov <andrey@php.net> |
16 | Ulf Wendel <uw@php.net> |
17 +----------------------------------------------------------------------+
18 */
19
20 #include "php.h"
21 #include "mysqlnd.h"
22 #include "mysqlnd_connection.h"
23 #include "mysqlnd_ps.h"
24 #include "mysqlnd_priv.h"
25 #include "mysqlnd_result.h"
26 #include "mysqlnd_wireprotocol.h"
27 #include "mysqlnd_debug.h"
28 #include "ext/standard/basic_functions.h"
29
30
31 /* {{{ php_mysqlnd_free_field_metadata */
32 static void
php_mysqlnd_free_field_metadata(MYSQLND_FIELD * meta)33 php_mysqlnd_free_field_metadata(MYSQLND_FIELD *meta)
34 {
35 if (meta) {
36 meta->root = NULL;
37 meta->def = NULL;
38 if (meta->sname) {
39 zend_string_release_ex(meta->sname, 0);
40 }
41 }
42 }
43 /* }}} */
44
45 /* {{{ mysqlnd_res_meta::read_metadata */
46 static enum_func_status
MYSQLND_METHOD(mysqlnd_res_meta,read_metadata)47 MYSQLND_METHOD(mysqlnd_res_meta, read_metadata)(MYSQLND_RES_METADATA * const meta, MYSQLND_CONN_DATA * conn, MYSQLND_RES * result)
48 {
49 unsigned int i = 0;
50 MYSQLND_PACKET_RES_FIELD field_packet;
51
52 DBG_ENTER("mysqlnd_res_meta::read_metadata");
53
54 conn->payload_decoder_factory->m.init_result_field_packet(&field_packet);
55 field_packet.memory_pool = result->memory_pool;
56 for (;i < meta->field_count; i++) {
57 zend_ulong idx;
58
59 /* We re-read metadata for PS */
60 ZEND_ASSERT(meta->fields[i].root == NULL);
61 meta->fields[i].root = NULL;
62
63 field_packet.metadata = &(meta->fields[i]);
64 if (FAIL == PACKET_READ(conn, &field_packet)) {
65 PACKET_FREE(&field_packet);
66 DBG_RETURN(FAIL);
67 }
68 if (field_packet.error_info.error_no) {
69 COPY_CLIENT_ERROR(conn->error_info, field_packet.error_info);
70 /* Return back from CONN_QUERY_SENT */
71 PACKET_FREE(&field_packet);
72 DBG_RETURN(FAIL);
73 }
74
75 if (mysqlnd_ps_fetch_functions[meta->fields[i].type].func == NULL) {
76 DBG_ERR_FMT("Unknown type %u sent by the server. Please send a report to the developers", meta->fields[i].type);
77 php_error_docref(NULL, E_WARNING, "Unknown type %u sent by the server. Please send a report to the developers", meta->fields[i].type);
78 PACKET_FREE(&field_packet);
79 DBG_RETURN(FAIL);
80 }
81
82 /* For BC we have to check whether the key is numeric and use it like this */
83 if ((meta->fields[i].is_numeric = ZEND_HANDLE_NUMERIC(field_packet.metadata->sname, idx))) {
84 meta->fields[i].num_key = idx;
85 }
86 }
87 PACKET_FREE(&field_packet);
88
89 DBG_RETURN(PASS);
90 }
91 /* }}} */
92
93
94 /* {{{ mysqlnd_res_meta::free */
95 static void
MYSQLND_METHOD(mysqlnd_res_meta,free)96 MYSQLND_METHOD(mysqlnd_res_meta, free)(MYSQLND_RES_METADATA * meta)
97 {
98 int i;
99 MYSQLND_FIELD *fields;
100 DBG_ENTER("mysqlnd_res_meta::free");
101
102 if ((fields = meta->fields)) {
103 DBG_INF("Freeing fields metadata");
104 i = meta->field_count;
105 while (i--) {
106 php_mysqlnd_free_field_metadata(fields++);
107 }
108 meta->fields = NULL;
109 }
110
111 DBG_INF("Freeing metadata structure");
112
113 DBG_VOID_RETURN;
114 }
115 /* }}} */
116
117
118 /* {{{ mysqlnd_res::clone_metadata */
119 static MYSQLND_RES_METADATA *
MYSQLND_METHOD(mysqlnd_res_meta,clone_metadata)120 MYSQLND_METHOD(mysqlnd_res_meta, clone_metadata)(MYSQLND_RES * result, const MYSQLND_RES_METADATA * const meta)
121 {
122 unsigned int i;
123 /* +1 is to have empty marker at the end */
124 MYSQLND_RES_METADATA * new_meta = NULL;
125 MYSQLND_FIELD * new_fields;
126 MYSQLND_FIELD * orig_fields = meta->fields;
127
128 DBG_ENTER("mysqlnd_res_meta::clone_metadata");
129
130 new_meta = result->memory_pool->get_chunk(result->memory_pool, sizeof(MYSQLND_RES_METADATA));
131 if (!new_meta) {
132 goto oom;
133 }
134 memset(new_meta, 0, sizeof(MYSQLND_RES_METADATA));
135 new_meta->m = meta->m;
136
137 new_fields = result->memory_pool->get_chunk(result->memory_pool, (meta->field_count + 1) * sizeof(MYSQLND_FIELD));
138 if (!new_fields) {
139 goto oom;
140 }
141 memset(new_fields, 0, (meta->field_count + 1) * sizeof(MYSQLND_FIELD));
142
143 /*
144 This will copy also the strings and the root, which we will have
145 to adjust in the loop
146 */
147 memcpy(new_fields, orig_fields, (meta->field_count) * sizeof(MYSQLND_FIELD));
148 for (i = 0; i < meta->field_count; i++) {
149 /* First copy the root, then field by field adjust the pointers */
150 new_fields[i].root = result->memory_pool->get_chunk(result->memory_pool, orig_fields[i].root_len);
151
152 if (!new_fields[i].root) {
153 goto oom;
154 }
155
156 memcpy(new_fields[i].root, orig_fields[i].root, new_fields[i].root_len);
157
158 if (orig_fields[i].sname) {
159 new_fields[i].sname = zend_string_copy(orig_fields[i].sname);
160 new_fields[i].name = ZSTR_VAL(new_fields[i].sname);
161 new_fields[i].name_length = ZSTR_LEN(new_fields[i].sname);
162 }
163
164 new_fields[i].is_numeric = orig_fields[i].is_numeric;
165 new_fields[i].num_key = orig_fields[i].num_key;
166
167 if (orig_fields[i].org_name && orig_fields[i].org_name != mysqlnd_empty_string) {
168 new_fields[i].org_name = new_fields[i].root +
169 (orig_fields[i].org_name - orig_fields[i].root);
170 }
171 if (orig_fields[i].table && orig_fields[i].table != mysqlnd_empty_string) {
172 new_fields[i].table = new_fields[i].root +
173 (orig_fields[i].table - orig_fields[i].root);
174 }
175 if (orig_fields[i].org_table && orig_fields[i].org_table != mysqlnd_empty_string) {
176 new_fields[i].org_table = new_fields[i].root +
177 (orig_fields[i].org_table - orig_fields[i].root);
178 }
179 if (orig_fields[i].db && orig_fields[i].db != mysqlnd_empty_string) {
180 new_fields[i].db = new_fields[i].root + (orig_fields[i].db - orig_fields[i].root);
181 }
182 if (orig_fields[i].catalog && orig_fields[i].catalog != mysqlnd_empty_string) {
183 new_fields[i].catalog = new_fields[i].root + (orig_fields[i].catalog - orig_fields[i].root);
184 }
185 /* def is not on the root, if allocated at all */
186 if (orig_fields[i].def) {
187 new_fields[i].def = result->memory_pool->get_chunk(result->memory_pool, orig_fields[i].def_length + 1);
188 if (!new_fields[i].def) {
189 goto oom;
190 }
191 /* copy the trailing \0 too */
192 memcpy(new_fields[i].def, orig_fields[i].def, orig_fields[i].def_length + 1);
193 }
194 }
195 new_meta->current_field = 0;
196 new_meta->field_count = meta->field_count;
197
198 new_meta->fields = new_fields;
199
200 DBG_RETURN(new_meta);
201 oom:
202 if (new_meta) {
203 new_meta->m->free_metadata(new_meta);
204 new_meta = NULL;
205 }
206 DBG_RETURN(NULL);
207 }
208 /* }}} */
209
210
211 /* {{{ mysqlnd_res_meta::fetch_field */
212 static const MYSQLND_FIELD *
MYSQLND_METHOD(mysqlnd_res_meta,fetch_field)213 MYSQLND_METHOD(mysqlnd_res_meta, fetch_field)(MYSQLND_RES_METADATA * const meta)
214 {
215 DBG_ENTER("mysqlnd_res_meta::fetch_field");
216 if (meta->current_field >= meta->field_count) {
217 DBG_INF("no more fields");
218 DBG_RETURN(NULL);
219 }
220 DBG_INF_FMT("name=%s max_length=%u",
221 meta->fields[meta->current_field].name? meta->fields[meta->current_field].name:"",
222 meta->fields[meta->current_field].max_length);
223 DBG_RETURN(&meta->fields[meta->current_field++]);
224 }
225 /* }}} */
226
227
228 /* {{{ mysqlnd_res_meta::fetch_field_direct */
229 static const MYSQLND_FIELD *
MYSQLND_METHOD(mysqlnd_res_meta,fetch_field_direct)230 MYSQLND_METHOD(mysqlnd_res_meta, fetch_field_direct)(const MYSQLND_RES_METADATA * const meta, const MYSQLND_FIELD_OFFSET fieldnr)
231 {
232 DBG_ENTER("mysqlnd_res_meta::fetch_field_direct");
233 DBG_INF_FMT("fieldnr=%u", fieldnr);
234 DBG_INF_FMT("name=%s max_length=%u",
235 meta->fields[meta->current_field].name? meta->fields[meta->current_field].name:"",
236 meta->fields[meta->current_field].max_length);
237 DBG_RETURN(&meta->fields[fieldnr]);
238 }
239 /* }}} */
240
241
242 /* {{{ mysqlnd_res_meta::fetch_fields */
243 static const MYSQLND_FIELD *
MYSQLND_METHOD(mysqlnd_res_meta,fetch_fields)244 MYSQLND_METHOD(mysqlnd_res_meta, fetch_fields)(MYSQLND_RES_METADATA * const meta)
245 {
246 DBG_ENTER("mysqlnd_res_meta::fetch_fields");
247 DBG_RETURN(meta->fields);
248 }
249 /* }}} */
250
251
252 /* {{{ mysqlnd_res_meta::field_tell */
253 static MYSQLND_FIELD_OFFSET
MYSQLND_METHOD(mysqlnd_res_meta,field_tell)254 MYSQLND_METHOD(mysqlnd_res_meta, field_tell)(const MYSQLND_RES_METADATA * const meta)
255 {
256 return meta->current_field;
257 }
258 /* }}} */
259
260
261 /* {{{ mysqlnd_res_meta::field_seek */
262 static MYSQLND_FIELD_OFFSET
MYSQLND_METHOD(mysqlnd_res_meta,field_seek)263 MYSQLND_METHOD(mysqlnd_res_meta, field_seek)(MYSQLND_RES_METADATA * const meta, const MYSQLND_FIELD_OFFSET field_offset)
264 {
265 MYSQLND_FIELD_OFFSET return_value = 0;
266 DBG_ENTER("mysqlnd_res_meta::fetch_fields");
267 return_value = meta->current_field;
268 meta->current_field = field_offset;
269 DBG_RETURN(return_value);
270 }
271 /* }}} */
272
273 static
274 MYSQLND_CLASS_METHODS_START(mysqlnd_res_meta)
275 MYSQLND_METHOD(mysqlnd_res_meta, fetch_field),
276 MYSQLND_METHOD(mysqlnd_res_meta, fetch_field_direct),
277 MYSQLND_METHOD(mysqlnd_res_meta, fetch_fields),
278 MYSQLND_METHOD(mysqlnd_res_meta, field_tell),
279 MYSQLND_METHOD(mysqlnd_res_meta, field_seek),
280 MYSQLND_METHOD(mysqlnd_res_meta, read_metadata),
281 MYSQLND_METHOD(mysqlnd_res_meta, clone_metadata),
282 MYSQLND_METHOD(mysqlnd_res_meta, free),
283 MYSQLND_CLASS_METHODS_END;
284
285
286 /* {{{ mysqlnd_result_meta_init */
287 PHPAPI MYSQLND_RES_METADATA *
mysqlnd_result_meta_init(MYSQLND_RES * result,unsigned int field_count)288 mysqlnd_result_meta_init(MYSQLND_RES *result, unsigned int field_count)
289 {
290 size_t alloc_size = sizeof(MYSQLND_RES_METADATA) + mysqlnd_plugin_count() * sizeof(void *);
291 MYSQLND_RES_METADATA *ret;
292 DBG_ENTER("mysqlnd_result_meta_init");
293
294 do {
295 ret = result->memory_pool->get_chunk(result->memory_pool, alloc_size);
296 memset(ret, 0, alloc_size);
297 ret->m = & mysqlnd_mysqlnd_res_meta_methods;
298
299 ret->field_count = field_count;
300 /* +1 is to have empty marker at the end */
301 alloc_size = (field_count + 1) * sizeof(MYSQLND_FIELD);
302 ret->fields = result->memory_pool->get_chunk(result->memory_pool, alloc_size);
303 memset(ret->fields, 0, alloc_size);
304 DBG_INF_FMT("meta=%p", ret);
305 DBG_RETURN(ret);
306 } while (0);
307 DBG_RETURN(NULL);
308 }
309 /* }}} */
310
311
312 /* {{{ mysqlnd_res_meta_get_methods */
313 PHPAPI struct st_mysqlnd_res_meta_methods *
mysqlnd_result_metadata_get_methods()314 mysqlnd_result_metadata_get_methods()
315 {
316 return &mysqlnd_mysqlnd_res_meta_methods;
317 }
318 /* }}} */
319
320
321 /* {{{ _mysqlnd_plugin_get_plugin_result_metadata_data */
322 PHPAPI void **
_mysqlnd_plugin_get_plugin_result_metadata_data(const MYSQLND_RES_METADATA * meta,unsigned int plugin_id)323 _mysqlnd_plugin_get_plugin_result_metadata_data(const MYSQLND_RES_METADATA * meta, unsigned int plugin_id)
324 {
325 DBG_ENTER("_mysqlnd_plugin_get_plugin_result_metadata_data");
326 DBG_INF_FMT("plugin_id=%u", plugin_id);
327 if (!meta || plugin_id >= mysqlnd_plugin_count()) {
328 return NULL;
329 }
330 DBG_RETURN((void *)((char *)meta + sizeof(MYSQLND_RES_METADATA) + plugin_id * sizeof(void *)));
331 }
332 /* }}} */
333
334 /*
335 * Local variables:
336 * tab-width: 4
337 * c-basic-offset: 4
338 * End:
339 * vim600: noet sw=4 ts=4 fdm=marker
340 * vim<600: noet sw=4 ts=4
341 */
342