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