xref: /PHP-7.4/ext/pdo_mysql/mysql_statement.c (revision f3d58778)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 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   | Author: George Schlossnagle <george@omniti.com>                      |
16   |         Wez Furlong <wez@php.net>                                    |
17   |         Johannes Schlueter <johannes@mysql.com>                      |
18   +----------------------------------------------------------------------+
19 */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "php.h"
26 #include "php_ini.h"
27 #include "ext/standard/info.h"
28 #include "pdo/php_pdo.h"
29 #include "pdo/php_pdo_driver.h"
30 #include "php_pdo_mysql.h"
31 #include "php_pdo_mysql_int.h"
32 
33 #ifdef PDO_USE_MYSQLND
34 #	define pdo_mysql_stmt_execute_prepared(stmt) pdo_mysql_stmt_execute_prepared_mysqlnd(stmt)
35 #	define pdo_free_bound_result(res) zval_ptr_dtor(res.zv)
36 #else
37 #	define pdo_mysql_stmt_execute_prepared(stmt) pdo_mysql_stmt_execute_prepared_libmysql(stmt)
38 #	define pdo_free_bound_result(res) efree(res.buffer)
39 #endif
40 
41 
42 
pdo_mysql_stmt_dtor(pdo_stmt_t * stmt)43 static int pdo_mysql_stmt_dtor(pdo_stmt_t *stmt) /* {{{ */
44 {
45 	pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
46 
47 	PDO_DBG_ENTER("pdo_mysql_stmt_dtor");
48 	PDO_DBG_INF_FMT("stmt=%p", S->stmt);
49 	if (S->result) {
50 		/* free the resource */
51 		mysql_free_result(S->result);
52 		S->result = NULL;
53 	}
54 	if (S->einfo.errmsg) {
55 		pefree(S->einfo.errmsg, stmt->dbh->is_persistent);
56 		S->einfo.errmsg = NULL;
57 	}
58 	if (S->stmt) {
59 		mysql_stmt_close(S->stmt);
60 		S->stmt = NULL;
61 	}
62 
63 #ifndef PDO_USE_MYSQLND
64 	if (S->params) {
65 		efree(S->params);
66 	}
67 	if (S->in_null) {
68 		efree(S->in_null);
69 	}
70 	if (S->in_length) {
71 		efree(S->in_length);
72 	}
73 
74 	if (S->bound_result)
75 	{
76 		int i;
77 		for (i = 0; i < stmt->column_count; i++) {
78 			pdo_free_bound_result(S->bound_result[i]);
79 		}
80 
81 		efree(S->bound_result);
82 		efree(S->out_null);
83 		efree(S->out_length);
84 	}
85 #endif
86 
87 	if (!Z_ISUNDEF(stmt->database_object_handle)
88 		&& IS_OBJ_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE(stmt->database_object_handle)])
89 		&& (!(OBJ_FLAGS(Z_OBJ(stmt->database_object_handle)) & IS_OBJ_FREE_CALLED))) {
90 		while (mysql_more_results(S->H->server)) {
91 			MYSQL_RES *res;
92 			if (mysql_next_result(S->H->server) != 0) {
93 				break;
94 			}
95 
96 			res = mysql_store_result(S->H->server);
97 			if (res) {
98 				mysql_free_result(res);
99 			}
100 		}
101 	}
102 
103 #if PDO_USE_MYSQLND
104 	if (!S->stmt && S->current_data) {
105 		mnd_free(S->current_data);
106 	}
107 #endif /* PDO_USE_MYSQLND */
108 
109 	efree(S);
110 	PDO_DBG_RETURN(1);
111 }
112 /* }}} */
113 
pdo_mysql_stmt_set_row_count(pdo_stmt_t * stmt)114 static void pdo_mysql_stmt_set_row_count(pdo_stmt_t *stmt) /* {{{ */
115 {
116 	zend_long row_count;
117 	pdo_mysql_stmt *S = stmt->driver_data;
118 	row_count = (zend_long) mysql_stmt_affected_rows(S->stmt);
119 	if (row_count != (zend_long)-1) {
120 		stmt->row_count = row_count;
121 	}
122 }
123 /* }}} */
124 
pdo_mysql_fill_stmt_from_result(pdo_stmt_t * stmt)125 static int pdo_mysql_fill_stmt_from_result(pdo_stmt_t *stmt) /* {{{ */
126 {
127 	pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
128 	pdo_mysql_db_handle *H = S->H;
129 	my_ulonglong row_count;
130 	PDO_DBG_ENTER("pdo_mysql_fill_stmt_from_result");
131 
132 	row_count = mysql_affected_rows(H->server);
133 	if (row_count == (my_ulonglong)-1) {
134 		/* we either have a query that returned a result set or an error occurred
135 		   lets see if we have access to a result set */
136 		if (!H->buffered) {
137 			S->result = mysql_use_result(H->server);
138 		} else {
139 			S->result = mysql_store_result(H->server);
140 		}
141 		if (NULL == S->result) {
142 			pdo_mysql_error_stmt(stmt);
143 			PDO_DBG_RETURN(0);
144 		}
145 
146 		stmt->row_count = (zend_long) mysql_num_rows(S->result);
147 		stmt->column_count = (int) mysql_num_fields(S->result);
148 		S->fields = mysql_fetch_fields(S->result);
149 	} else {
150 		/* this was a DML or DDL query (INSERT, UPDATE, DELETE, ... */
151 		stmt->row_count = (zend_long) row_count;
152 	}
153 
154 	PDO_DBG_RETURN(1);
155 }
156 /* }}} */
157 
158 #ifndef PDO_USE_MYSQLND
pdo_mysql_stmt_execute_prepared_libmysql(pdo_stmt_t * stmt)159 static int pdo_mysql_stmt_execute_prepared_libmysql(pdo_stmt_t *stmt) /* {{{ */
160 {
161 	pdo_mysql_stmt *S = stmt->driver_data;
162 	pdo_mysql_db_handle *H = S->H;
163 
164 	PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_libmysql");
165 
166 	/* (re)bind the parameters */
167 	if (mysql_stmt_bind_param(S->stmt, S->params) || mysql_stmt_execute(S->stmt)) {
168 		if (S->params) {
169 			memset(S->params, 0, S->num_params * sizeof(MYSQL_BIND));
170 		}
171 		pdo_mysql_error_stmt(stmt);
172 		if (mysql_stmt_errno(S->stmt) == 2057) {
173 			/* CR_NEW_STMT_METADATA makes the statement unusable */
174 			S->stmt = NULL;
175 		}
176 		PDO_DBG_RETURN(0);
177 	}
178 
179 	if (!S->result) {
180 		int i;
181 
182 		/* figure out the result set format, if any */
183 		S->result = mysql_stmt_result_metadata(S->stmt);
184 		if (S->result) {
185 			int calc_max_length = H->buffered && S->max_length == 1;
186 			S->fields = mysql_fetch_fields(S->result);
187 			if (S->bound_result) {
188 				int i;
189 				for (i = 0; i < stmt->column_count; i++) {
190 					efree(S->bound_result[i].buffer);
191 				}
192 				efree(S->bound_result);
193 				efree(S->out_null);
194 				efree(S->out_length);
195 			}
196 
197 			stmt->column_count = (int)mysql_num_fields(S->result);
198 			S->bound_result = ecalloc(stmt->column_count, sizeof(MYSQL_BIND));
199 			S->out_null = ecalloc(stmt->column_count, sizeof(my_bool));
200 			S->out_length = ecalloc(stmt->column_count, sizeof(zend_ulong));
201 
202 			/* summon memory to hold the row */
203 			for (i = 0; i < stmt->column_count; i++) {
204 				if (calc_max_length && S->fields[i].type == FIELD_TYPE_BLOB) {
205 					my_bool on = 1;
206 					mysql_stmt_attr_set(S->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &on);
207 					calc_max_length = 0;
208 				}
209 				switch (S->fields[i].type) {
210 					case FIELD_TYPE_INT24:
211 						S->bound_result[i].buffer_length = MAX_MEDIUMINT_WIDTH + 1;
212 						break;
213 					case FIELD_TYPE_LONG:
214 						S->bound_result[i].buffer_length = MAX_INT_WIDTH + 1;
215 						break;
216 					case FIELD_TYPE_LONGLONG:
217 						S->bound_result[i].buffer_length = MAX_BIGINT_WIDTH + 1;
218 						break;
219 					case FIELD_TYPE_TINY:
220 						S->bound_result[i].buffer_length = MAX_TINYINT_WIDTH + 1;
221 						break;
222 					case FIELD_TYPE_SHORT:
223 						S->bound_result[i].buffer_length = MAX_SMALLINT_WIDTH + 1;
224 						break;
225 					default:
226 						S->bound_result[i].buffer_length =
227 							S->fields[i].max_length? S->fields[i].max_length:
228 							S->fields[i].length;
229 						/* work-around for longtext and alike */
230 						if (S->bound_result[i].buffer_length > H->max_buffer_size) {
231 							S->bound_result[i].buffer_length = H->max_buffer_size;
232 						}
233 				}
234 
235 				/* there are cases where the length reported by mysql is too short.
236 				 * eg: when describing a table that contains an enum column. Since
237 				 * we have no way of knowing the true length either, we'll bump up
238 				 * our buffer size to a reasonable size, just in case */
239 				if (S->fields[i].max_length == 0 && S->bound_result[i].buffer_length < 128 && MYSQL_TYPE_VAR_STRING) {
240 					S->bound_result[i].buffer_length = 128;
241 				}
242 
243 				S->out_length[i] = 0;
244 
245 				S->bound_result[i].buffer = emalloc(S->bound_result[i].buffer_length);
246 				S->bound_result[i].is_null = &S->out_null[i];
247 				S->bound_result[i].length = &S->out_length[i];
248 				S->bound_result[i].buffer_type = MYSQL_TYPE_STRING;
249 			}
250 
251 			if (mysql_stmt_bind_result(S->stmt, S->bound_result)) {
252 				pdo_mysql_error_stmt(stmt);
253 				PDO_DBG_RETURN(0);
254 			}
255 
256 			/* if buffered, pre-fetch all the data */
257 			if (H->buffered) {
258 				if (mysql_stmt_store_result(S->stmt)) {
259 					pdo_mysql_error_stmt(stmt);
260 					PDO_DBG_RETURN(0);
261 				}
262 			}
263 		}
264 	}
265 
266 	pdo_mysql_stmt_set_row_count(stmt);
267 	PDO_DBG_RETURN(1);
268 }
269 /* }}} */
270 #endif
271 
272 #ifdef PDO_USE_MYSQLND
pdo_mysql_stmt_execute_prepared_mysqlnd(pdo_stmt_t * stmt)273 static int pdo_mysql_stmt_execute_prepared_mysqlnd(pdo_stmt_t *stmt) /* {{{ */
274 {
275 	pdo_mysql_stmt *S = stmt->driver_data;
276 	pdo_mysql_db_handle *H = S->H;
277 	int i;
278 
279 	PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_mysqlnd");
280 
281 	if (mysql_stmt_execute(S->stmt)) {
282 		pdo_mysql_error_stmt(stmt);
283 		PDO_DBG_RETURN(0);
284 	}
285 
286 	if (S->result) {
287 		/* TODO: add a test to check if we really have zvals here... */
288 		mysql_free_result(S->result);
289 		S->result = NULL;
290 	}
291 
292 	/* for SHOW/DESCRIBE and others the column/field count is not available before execute */
293 	stmt->column_count = mysql_stmt_field_count(S->stmt);
294 	for (i = 0; i < stmt->column_count; i++) {
295 		mysqlnd_stmt_bind_one_result(S->stmt, i);
296 	}
297 
298 	S->result = mysqlnd_stmt_result_metadata(S->stmt);
299 	if (S->result) {
300 		S->fields = mysql_fetch_fields(S->result);
301 		/* if buffered, pre-fetch all the data */
302 		if (H->buffered) {
303 			if (mysql_stmt_store_result(S->stmt)) {
304 				pdo_mysql_error_stmt(stmt);
305 				PDO_DBG_RETURN(0);
306 			}
307 		}
308 	}
309 
310 	pdo_mysql_stmt_set_row_count(stmt);
311 	PDO_DBG_RETURN(1);
312 }
313 /* }}} */
314 #endif
315 
pdo_mysql_stmt_execute(pdo_stmt_t * stmt)316 static int pdo_mysql_stmt_execute(pdo_stmt_t *stmt) /* {{{ */
317 {
318 	pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
319 	pdo_mysql_db_handle *H = S->H;
320 	PDO_DBG_ENTER("pdo_mysql_stmt_execute");
321 	PDO_DBG_INF_FMT("stmt=%p", S->stmt);
322 
323 	if (S->stmt) {
324 		PDO_DBG_RETURN(pdo_mysql_stmt_execute_prepared(stmt));
325 	}
326 
327 	/* ensure that we free any previous unfetched results */
328 	if (S->result) {
329 		mysql_free_result(S->result);
330 		S->result = NULL;
331 	}
332 
333 	if (mysql_real_query(H->server, stmt->active_query_string, stmt->active_query_stringlen) != 0) {
334 		pdo_mysql_error_stmt(stmt);
335 		PDO_DBG_RETURN(0);
336 	}
337 
338 	PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt));
339 }
340 /* }}} */
341 
pdo_mysql_stmt_next_rowset(pdo_stmt_t * stmt)342 static int pdo_mysql_stmt_next_rowset(pdo_stmt_t *stmt) /* {{{ */
343 {
344 	pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
345 	pdo_mysql_db_handle *H = S->H;
346 #if PDO_USE_MYSQLND
347 	zend_long row_count;
348 #endif
349 	PDO_DBG_ENTER("pdo_mysql_stmt_next_rowset");
350 	PDO_DBG_INF_FMT("stmt=%p", S->stmt);
351 
352 #if PDO_USE_MYSQLND
353 	if (S->stmt) {
354 		if (!mysqlnd_stmt_more_results(S->stmt)) {
355 			PDO_DBG_RETURN(0);
356 		}
357 		if (mysqlnd_stmt_next_result(S->stmt)) {
358 			pdo_mysql_error_stmt(stmt);
359 			PDO_DBG_RETURN(0);
360 		}
361 
362 		if (!mysqlnd_stmt_more_results(S->stmt)) {
363 			/*
364 			MySQL gives us n + 1 result sets for
365 			CALL proc() and n result sets returned by the proc itself.
366 			Result set n + 1 is about the procedure call itself.
367 			As the PDO emulation does not return it, we skip it as well
368 			*/
369 			PDO_DBG_RETURN(0);
370 		}
371 
372 		/* TODO - this code is stolen from execute() - see above */
373 		if (S->result) {
374 			mysql_free_result(S->result);
375 			S->result = NULL;
376 		}
377 		{
378 			/* for SHOW/DESCRIBE and others the column/field count is not available before execute */
379 			int i;
380 
381 			stmt->column_count = mysql_stmt_field_count(S->stmt);
382 			for (i = 0; i < stmt->column_count; i++) {
383 				mysqlnd_stmt_bind_one_result(S->stmt, i);
384 			}
385 		}
386 
387 		S->result = mysqlnd_stmt_result_metadata(S->stmt);
388 		if (S->result) {
389 			S->fields = mysql_fetch_fields(S->result);
390 
391 			/* if buffered, pre-fetch all the data */
392 			if (H->buffered) {
393 				if (mysql_stmt_store_result(S->stmt)) {
394 					pdo_mysql_error_stmt(stmt);
395 					PDO_DBG_RETURN(0);
396 				}
397 			}
398 		}
399 		row_count = (zend_long) mysql_stmt_affected_rows(S->stmt);
400 		if (row_count != (zend_long)-1) {
401 			stmt->row_count = row_count;
402 		}
403 		PDO_DBG_RETURN(1);
404 	}
405 #endif
406 
407 /* ensure that we free any previous unfetched results */
408 #ifndef PDO_USE_MYSQLND
409 	if (S->stmt) {
410 		if (S->result) {
411 			stmt->column_count = (int)mysql_num_fields(S->result);
412 		}
413 		mysql_stmt_free_result(S->stmt);
414 	}
415 #endif
416 	if (S->result) {
417 		mysql_free_result(S->result);
418 		S->result = NULL;
419 	}
420 
421 	if (!mysql_more_results(H->server)) {
422 		/* No more results */
423 		PDO_DBG_RETURN(0);
424 	}
425 #if PDO_USE_MYSQLND
426 	if (mysql_next_result(H->server) == FAIL) {
427 		pdo_mysql_error_stmt(stmt);
428 		PDO_DBG_RETURN(0);
429 	} else {
430 		PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt));
431 	}
432 #else
433 	if (mysql_next_result(H->server) > 0) {
434 		pdo_mysql_error_stmt(stmt);
435 		PDO_DBG_RETURN(0);
436 	} else {
437 		PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt));
438 	}
439 #endif
440 }
441 /* }}} */
442 
443 
444 static const char * const pdo_param_event_names[] =
445 {
446 	"PDO_PARAM_EVT_ALLOC",
447 	"PDO_PARAM_EVT_FREE",
448 	"PDO_PARAM_EVT_EXEC_PRE",
449 	"PDO_PARAM_EVT_EXEC_POST",
450 	"PDO_PARAM_EVT_FETCH_PRE",
451 	"PDO_PARAM_EVT_FETCH_POST",
452 	"PDO_PARAM_EVT_NORMALIZE",
453 };
454 
455 
pdo_mysql_stmt_param_hook(pdo_stmt_t * stmt,struct pdo_bound_param_data * param,enum pdo_param_event event_type)456 static int pdo_mysql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, enum pdo_param_event event_type) /* {{{ */
457 {
458 	zval *parameter;
459 #ifndef PDO_USE_MYSQLND
460 	PDO_MYSQL_PARAM_BIND *b;
461 #endif
462 	pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
463 
464 	PDO_DBG_ENTER("pdo_mysql_stmt_param_hook");
465 	PDO_DBG_INF_FMT("stmt=%p", S->stmt);
466 	PDO_DBG_INF_FMT("event = %s", pdo_param_event_names[event_type]);
467 	if (S->stmt && param->is_param) {
468 		switch (event_type) {
469 			case PDO_PARAM_EVT_ALLOC:
470 				/* sanity check parameter number range */
471 				if (param->paramno < 0 || param->paramno >= S->num_params) {
472 					strcpy(stmt->error_code, "HY093");
473 					PDO_DBG_RETURN(0);
474 				}
475 				S->params_given++;
476 
477 #ifndef PDO_USE_MYSQLND
478 				b = &S->params[param->paramno];
479 				param->driver_data = b;
480 				b->is_null = &S->in_null[param->paramno];
481 				b->length = &S->in_length[param->paramno];
482 				/* recall how many parameters have been provided */
483 #endif
484 				PDO_DBG_RETURN(1);
485 
486 			case PDO_PARAM_EVT_EXEC_PRE:
487 				if (S->params_given < (unsigned int) S->num_params) {
488 					/* too few parameter bound */
489 					PDO_DBG_ERR("too few parameters bound");
490 					strcpy(stmt->error_code, "HY093");
491 					PDO_DBG_RETURN(0);
492 				}
493 
494 				if (!Z_ISREF(param->parameter)) {
495 					parameter = &param->parameter;
496 				} else {
497 					parameter = Z_REFVAL(param->parameter);
498 				}
499 
500 #if PDO_USE_MYSQLND
501 				if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL || (Z_TYPE_P(parameter) == IS_NULL)) {
502 					mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_NULL);
503 					PDO_DBG_RETURN(1);
504 				}
505 #else
506 				b = (PDO_MYSQL_PARAM_BIND*)param->driver_data;
507 				*b->is_null = 0;
508 				if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL || Z_TYPE_P(parameter) == IS_NULL) {
509 					*b->is_null = 1;
510 					b->buffer_type = MYSQL_TYPE_STRING;
511 					b->buffer = NULL;
512 					b->buffer_length = 0;
513 					*b->length = 0;
514 					PDO_DBG_RETURN(1);
515 				}
516 #endif /* PDO_USE_MYSQLND */
517 
518 				switch (PDO_PARAM_TYPE(param->param_type)) {
519 					case PDO_PARAM_STMT:
520 						PDO_DBG_RETURN(0);
521 					case PDO_PARAM_LOB:
522 						PDO_DBG_INF("PDO_PARAM_LOB");
523 						if (!Z_ISREF(param->parameter)) {
524 							parameter = &param->parameter;
525 						} else {
526 							parameter = Z_REFVAL(param->parameter);
527 						}
528 						if (Z_TYPE_P(parameter) == IS_RESOURCE) {
529 							php_stream *stm = NULL;
530 							php_stream_from_zval_no_verify(stm, parameter);
531 							if (stm) {
532 								zend_string *mem = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
533 								zval_ptr_dtor(parameter);
534 								ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC());
535 							} else {
536 								pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource");
537 								return 0;
538 							}
539 						}
540 						/* fall through */
541 
542 					default:
543 						;
544 				}
545 
546 #if PDO_USE_MYSQLND
547 				/* Is it really correct to check the zval's type? - But well, that's what the old code below does, too */
548 				PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
549 				if (!Z_ISREF(param->parameter)) {
550 					parameter = &param->parameter;
551 				} else {
552 					parameter = Z_REFVAL(param->parameter);
553 				}
554 				switch (Z_TYPE_P(parameter)) {
555 					case IS_STRING:
556 						mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_VAR_STRING);
557 						break;
558 					case IS_LONG:
559 #if SIZEOF_ZEND_LONG==8
560 						mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_LONGLONG);
561 #elif SIZEOF_ZEND_LONG==4
562 						mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_LONG);
563 #endif /* SIZEOF_LONG */
564 						break;
565 					case IS_TRUE:
566 					case IS_FALSE:
567 						mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_TINY);
568 						break;
569 					case IS_DOUBLE:
570 						mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_DOUBLE);
571 						break;
572 					default:
573 						PDO_DBG_RETURN(0);
574 				}
575 
576 				PDO_DBG_RETURN(1);
577 #else
578 				PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
579 				if (!Z_ISREF(param->parameter)) {
580 					parameter = &param->parameter;
581 				} else {
582 					parameter = Z_REFVAL(param->parameter);
583 				}
584 				switch (Z_TYPE_P(parameter)) {
585 					case IS_STRING:
586 						b->buffer_type = MYSQL_TYPE_STRING;
587 						b->buffer = Z_STRVAL_P(parameter);
588 						b->buffer_length = Z_STRLEN_P(parameter);
589 						*b->length = Z_STRLEN_P(parameter);
590 						PDO_DBG_RETURN(1);
591 
592 					case IS_LONG:
593 						b->buffer_type = MYSQL_TYPE_LONG;
594 						b->buffer = &Z_LVAL_P(parameter);
595 						PDO_DBG_RETURN(1);
596 
597 					case IS_DOUBLE:
598 						b->buffer_type = MYSQL_TYPE_DOUBLE;
599 						b->buffer = &Z_DVAL_P(parameter);
600 						PDO_DBG_RETURN(1);
601 
602 					default:
603 						PDO_DBG_RETURN(0);
604 				}
605 #endif /* PDO_USE_MYSQLND */
606 		case PDO_PARAM_EVT_FREE:
607 		case PDO_PARAM_EVT_EXEC_POST:
608 		case PDO_PARAM_EVT_FETCH_PRE:
609 		case PDO_PARAM_EVT_FETCH_POST:
610 		case PDO_PARAM_EVT_NORMALIZE:
611 			/* do nothing */
612 			break;
613 		}
614 	}
615 
616 	PDO_DBG_RETURN(1);
617 }
618 /* }}} */
619 
pdo_mysql_stmt_fetch(pdo_stmt_t * stmt,enum pdo_fetch_orientation ori,zend_long offset)620 static int pdo_mysql_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, zend_long offset) /* {{{ */
621 {
622 	pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
623 
624 	if (!S->result) {
625 		PDO_DBG_RETURN(0);
626 	}
627 
628 #ifdef PDO_USE_MYSQLND
629 	zend_bool fetched_anything;
630 
631 	PDO_DBG_ENTER("pdo_mysql_stmt_fetch");
632 	PDO_DBG_INF_FMT("stmt=%p", S->stmt);
633 	if (S->stmt) {
634 		if (FAIL == mysqlnd_stmt_fetch(S->stmt, &fetched_anything) || fetched_anything == FALSE) {
635 			pdo_mysql_error_stmt(stmt);
636 			PDO_DBG_RETURN(0);
637 		}
638 
639 		PDO_DBG_RETURN(1);
640 	}
641 
642 	if (!S->stmt && S->current_data) {
643 		mnd_free(S->current_data);
644 	}
645 #else
646 	int ret;
647 
648 	if (S->stmt) {
649 		ret = mysql_stmt_fetch(S->stmt);
650 
651 #		ifdef MYSQL_DATA_TRUNCATED
652 		if (ret == MYSQL_DATA_TRUNCATED) {
653 			ret = 0;
654 		}
655 #		endif
656 
657 		if (ret) {
658 			if (ret != MYSQL_NO_DATA) {
659 				pdo_mysql_error_stmt(stmt);
660 			}
661 			PDO_DBG_RETURN(0);
662 		}
663 
664 		PDO_DBG_RETURN(1);
665 	}
666 #endif /* PDO_USE_MYSQLND */
667 
668 	if ((S->current_data = mysql_fetch_row(S->result)) == NULL) {
669 		if (!S->H->buffered && mysql_errno(S->H->server)) {
670 			pdo_mysql_error_stmt(stmt);
671 		}
672 		PDO_DBG_RETURN(0);
673 	}
674 
675 	S->current_lengths = mysql_fetch_lengths(S->result);
676 	PDO_DBG_RETURN(1);
677 }
678 /* }}} */
679 
pdo_mysql_stmt_describe(pdo_stmt_t * stmt,int colno)680 static int pdo_mysql_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
681 {
682 	pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
683 	struct pdo_column_data *cols = stmt->columns;
684 	int i;
685 
686 	PDO_DBG_ENTER("pdo_mysql_stmt_describe");
687 	PDO_DBG_INF_FMT("stmt=%p", S->stmt);
688 	if (!S->result) {
689 		PDO_DBG_RETURN(0);
690 	}
691 
692 	if (colno >= stmt->column_count) {
693 		/* error invalid column */
694 		PDO_DBG_RETURN(0);
695 	}
696 
697 	/* fetch all on demand, this seems easiest
698 	** if we've been here before bail out
699 	*/
700 	if (cols[0].name) {
701 		PDO_DBG_RETURN(1);
702 	}
703 	for (i = 0; i < stmt->column_count; i++) {
704 
705 		if (S->H->fetch_table_names) {
706 			cols[i].name = strpprintf(0, "%s.%s", S->fields[i].table, S->fields[i].name);
707 		} else {
708 			cols[i].name = zend_string_init(S->fields[i].name, S->fields[i].name_length, 0);
709 		}
710 
711 		cols[i].precision = S->fields[i].decimals;
712 		cols[i].maxlen = S->fields[i].length;
713 
714 #ifdef PDO_USE_MYSQLND
715 		if (S->stmt) {
716 			cols[i].param_type = PDO_PARAM_ZVAL;
717 		} else
718 #endif
719 		{
720 			cols[i].param_type = PDO_PARAM_STR;
721 		}
722 	}
723 	PDO_DBG_RETURN(1);
724 }
725 /* }}} */
726 
pdo_mysql_stmt_get_col(pdo_stmt_t * stmt,int colno,char ** ptr,size_t * len,int * caller_frees)727 static int pdo_mysql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, size_t *len, int *caller_frees) /* {{{ */
728 {
729 	pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
730 
731 	PDO_DBG_ENTER("pdo_mysql_stmt_get_col");
732 	PDO_DBG_INF_FMT("stmt=%p", S->stmt);
733 	if (!S->result) {
734 		PDO_DBG_RETURN(0);
735 	}
736 
737 	/* With mysqlnd data is stored inside mysqlnd, not S->current_data */
738 	if (!S->stmt) {
739 		if (S->current_data == NULL || !S->result) {
740 			PDO_DBG_RETURN(0);
741 		}
742 	}
743 
744 	if (colno >= stmt->column_count) {
745 		/* error invalid column */
746 		PDO_DBG_RETURN(0);
747 	}
748 #if PDO_USE_MYSQLND
749 	if (S->stmt) {
750 		Z_TRY_ADDREF(S->stmt->data->result_bind[colno].zv);
751 		*ptr = (char*)&S->stmt->data->result_bind[colno].zv;
752 		*len = sizeof(zval);
753 		PDO_DBG_RETURN(1);
754 	}
755 #else
756 	if (S->stmt) {
757 		if (S->out_null[colno]) {
758 			*ptr = NULL;
759 			*len = 0;
760 			PDO_DBG_RETURN(1);
761 		}
762 		*ptr = S->bound_result[colno].buffer;
763 		if (S->out_length[colno] > S->bound_result[colno].buffer_length) {
764 			/* mysql lied about the column width */
765 			strcpy(stmt->error_code, "01004"); /* truncated */
766 			S->out_length[colno] = S->bound_result[colno].buffer_length;
767 			*len = S->out_length[colno];
768 			PDO_DBG_RETURN(0);
769 		}
770 		*len = S->out_length[colno];
771 		PDO_DBG_RETURN(1);
772 	}
773 #endif
774 	*ptr = S->current_data[colno];
775 	*len = S->current_lengths[colno];
776 	PDO_DBG_RETURN(1);
777 } /* }}} */
778 
type_to_name_native(int type)779 static char *type_to_name_native(int type) /* {{{ */
780 {
781 #define PDO_MYSQL_NATIVE_TYPE_NAME(x)	case FIELD_TYPE_##x: return #x;
782 
783     switch (type) {
784         PDO_MYSQL_NATIVE_TYPE_NAME(STRING)
785         PDO_MYSQL_NATIVE_TYPE_NAME(VAR_STRING)
786 #ifdef FIELD_TYPE_TINY
787         PDO_MYSQL_NATIVE_TYPE_NAME(TINY)
788 #endif
789 #ifdef FIELD_TYPE_BIT
790         PDO_MYSQL_NATIVE_TYPE_NAME(BIT)
791 #endif
792         PDO_MYSQL_NATIVE_TYPE_NAME(SHORT)
793         PDO_MYSQL_NATIVE_TYPE_NAME(LONG)
794         PDO_MYSQL_NATIVE_TYPE_NAME(LONGLONG)
795         PDO_MYSQL_NATIVE_TYPE_NAME(INT24)
796         PDO_MYSQL_NATIVE_TYPE_NAME(FLOAT)
797         PDO_MYSQL_NATIVE_TYPE_NAME(DOUBLE)
798         PDO_MYSQL_NATIVE_TYPE_NAME(DECIMAL)
799 #ifdef FIELD_TYPE_NEWDECIMAL
800         PDO_MYSQL_NATIVE_TYPE_NAME(NEWDECIMAL)
801 #endif
802 #ifdef FIELD_TYPE_GEOMETRY
803         PDO_MYSQL_NATIVE_TYPE_NAME(GEOMETRY)
804 #endif
805         PDO_MYSQL_NATIVE_TYPE_NAME(TIMESTAMP)
806 #ifdef FIELD_TYPE_YEAR
807         PDO_MYSQL_NATIVE_TYPE_NAME(YEAR)
808 #endif
809         PDO_MYSQL_NATIVE_TYPE_NAME(SET)
810         PDO_MYSQL_NATIVE_TYPE_NAME(ENUM)
811         PDO_MYSQL_NATIVE_TYPE_NAME(DATE)
812 #ifdef FIELD_TYPE_NEWDATE
813         PDO_MYSQL_NATIVE_TYPE_NAME(NEWDATE)
814 #endif
815         PDO_MYSQL_NATIVE_TYPE_NAME(TIME)
816         PDO_MYSQL_NATIVE_TYPE_NAME(DATETIME)
817         PDO_MYSQL_NATIVE_TYPE_NAME(TINY_BLOB)
818         PDO_MYSQL_NATIVE_TYPE_NAME(MEDIUM_BLOB)
819         PDO_MYSQL_NATIVE_TYPE_NAME(LONG_BLOB)
820         PDO_MYSQL_NATIVE_TYPE_NAME(BLOB)
821         PDO_MYSQL_NATIVE_TYPE_NAME(NULL)
822         default:
823             return NULL;
824     }
825 #undef PDO_MYSQL_NATIVE_TYPE_NAME
826 } /* }}} */
827 
pdo_mysql_stmt_col_meta(pdo_stmt_t * stmt,zend_long colno,zval * return_value)828 static int pdo_mysql_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value) /* {{{ */
829 {
830 	pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
831 	const MYSQL_FIELD *F;
832 	zval flags;
833 	char *str;
834 
835 	PDO_DBG_ENTER("pdo_mysql_stmt_col_meta");
836 	PDO_DBG_INF_FMT("stmt=%p", S->stmt);
837 	if (!S->result) {
838 		PDO_DBG_RETURN(FAILURE);
839 	}
840 	if (colno >= stmt->column_count) {
841 		/* error invalid column */
842 		PDO_DBG_RETURN(FAILURE);
843 	}
844 
845 	array_init(return_value);
846 	array_init(&flags);
847 
848 	F = S->fields + colno;
849 
850 	if (F->def) {
851 		add_assoc_string(return_value, "mysql:def", F->def);
852 	}
853 	if (IS_NOT_NULL(F->flags)) {
854 		add_next_index_string(&flags, "not_null");
855 	}
856 	if (IS_PRI_KEY(F->flags)) {
857 		add_next_index_string(&flags, "primary_key");
858 	}
859 	if (F->flags & MULTIPLE_KEY_FLAG) {
860 		add_next_index_string(&flags, "multiple_key");
861 	}
862 	if (F->flags & UNIQUE_KEY_FLAG) {
863 		add_next_index_string(&flags, "unique_key");
864 	}
865 	if (IS_BLOB(F->flags)) {
866 		add_next_index_string(&flags, "blob");
867 	}
868 	str = type_to_name_native(F->type);
869 	if (str) {
870 		add_assoc_string(return_value, "native_type", str);
871 	}
872 
873 #ifdef PDO_USE_MYSQLND
874 	switch (F->type) {
875 		case MYSQL_TYPE_BIT:
876 		case MYSQL_TYPE_YEAR:
877 		case MYSQL_TYPE_TINY:
878 		case MYSQL_TYPE_SHORT:
879 		case MYSQL_TYPE_INT24:
880 		case MYSQL_TYPE_LONG:
881 #if SIZEOF_ZEND_LONG==8
882 		case MYSQL_TYPE_LONGLONG:
883 #endif
884 			add_assoc_long(return_value, "pdo_type", PDO_PARAM_INT);
885 			break;
886 		default:
887 			add_assoc_long(return_value, "pdo_type", PDO_PARAM_STR);
888 			break;
889 	}
890 #endif
891 
892 	add_assoc_zval(return_value, "flags", &flags);
893 	add_assoc_string(return_value, "table", (char *) (F->table?F->table : ""));
894 
895 	PDO_DBG_RETURN(SUCCESS);
896 } /* }}} */
897 
pdo_mysql_stmt_cursor_closer(pdo_stmt_t * stmt)898 static int pdo_mysql_stmt_cursor_closer(pdo_stmt_t *stmt) /* {{{ */
899 {
900 	pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
901 
902 	PDO_DBG_ENTER("pdo_mysql_stmt_cursor_closer");
903 	PDO_DBG_INF_FMT("stmt=%p", S->stmt);
904 	if (S->result) {
905 		mysql_free_result(S->result);
906 		S->result = NULL;
907 	}
908 	if (S->stmt) {
909 		int retval;
910 		retval = mysql_stmt_free_result(S->stmt);
911 		PDO_DBG_RETURN(retval ? 0 : 1);
912 	}
913 
914 	while (mysql_more_results(S->H->server)) {
915 		MYSQL_RES *res;
916 		if (mysql_next_result(S->H->server) != 0) {
917 			break;
918 		}
919 		res = mysql_store_result(S->H->server);
920 		if (res) {
921 			mysql_free_result(res);
922 		}
923 	}
924 	PDO_DBG_RETURN(1);
925 }
926 /* }}} */
927 
928 const struct pdo_stmt_methods mysql_stmt_methods = {
929 	pdo_mysql_stmt_dtor,
930 	pdo_mysql_stmt_execute,
931 	pdo_mysql_stmt_fetch,
932 	pdo_mysql_stmt_describe,
933 	pdo_mysql_stmt_get_col,
934 	pdo_mysql_stmt_param_hook,
935 	NULL, /* set_attr */
936 	NULL, /* get_attr */
937 	pdo_mysql_stmt_col_meta,
938 	pdo_mysql_stmt_next_rowset,
939 	pdo_mysql_stmt_cursor_closer
940 };
941