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