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