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 #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 # define pdo_mysql_stmt_close(stmt) mysqlnd_stmt_close(stmt, 0)
37 #else
38 # define pdo_mysql_stmt_execute_prepared(stmt) pdo_mysql_stmt_execute_prepared_libmysql(stmt)
39 # define pdo_free_bound_result(res) efree(res.buffer)
40 # define pdo_mysql_stmt_close(stmt) mysql_stmt_close(stmt)
41 #endif
42
43
44
pdo_mysql_stmt_dtor(pdo_stmt_t * stmt)45 static int pdo_mysql_stmt_dtor(pdo_stmt_t *stmt) /* {{{ */
46 {
47 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
48
49 PDO_DBG_ENTER("pdo_mysql_stmt_dtor");
50 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
51 if (S->result) {
52 /* free the resource */
53 mysql_free_result(S->result);
54 S->result = NULL;
55 }
56 if (S->einfo.errmsg) {
57 pefree(S->einfo.errmsg, stmt->dbh->is_persistent);
58 S->einfo.errmsg = NULL;
59 }
60 if (S->stmt) {
61 pdo_mysql_stmt_close(S->stmt);
62 S->stmt = NULL;
63 }
64
65 #ifndef PDO_USE_MYSQLND
66 if (S->params) {
67 efree(S->params);
68 }
69 if (S->in_null) {
70 efree(S->in_null);
71 }
72 if (S->in_length) {
73 efree(S->in_length);
74 }
75
76 if (S->bound_result)
77 {
78 int i;
79 for (i = 0; i < stmt->column_count; i++) {
80 pdo_free_bound_result(S->bound_result[i]);
81 }
82
83 efree(S->bound_result);
84 efree(S->out_null);
85 efree(S->out_length);
86 }
87 #endif
88
89 if (!Z_ISUNDEF(stmt->database_object_handle)
90 && IS_OBJ_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE(stmt->database_object_handle)])
91 && (!(OBJ_FLAGS(Z_OBJ(stmt->database_object_handle)) & IS_OBJ_FREE_CALLED))) {
92 while (mysql_more_results(S->H->server)) {
93 MYSQL_RES *res;
94 if (mysql_next_result(S->H->server) != 0) {
95 break;
96 }
97
98 res = mysql_store_result(S->H->server);
99 if (res) {
100 mysql_free_result(res);
101 }
102 }
103 }
104
105 #if PDO_USE_MYSQLND
106 if (!S->stmt && S->current_data) {
107 mnd_free(S->current_data);
108 }
109 #endif /* PDO_USE_MYSQLND */
110
111 efree(S);
112 PDO_DBG_RETURN(1);
113 }
114 /* }}} */
115
pdo_mysql_stmt_set_row_count(pdo_stmt_t * stmt)116 static void pdo_mysql_stmt_set_row_count(pdo_stmt_t *stmt) /* {{{ */
117 {
118 zend_long row_count;
119 pdo_mysql_stmt *S = stmt->driver_data;
120 row_count = (zend_long) mysql_stmt_affected_rows(S->stmt);
121 if (row_count != (zend_long)-1) {
122 stmt->row_count = row_count;
123 }
124 }
125 /* }}} */
126
pdo_mysql_fill_stmt_from_result(pdo_stmt_t * stmt)127 static int pdo_mysql_fill_stmt_from_result(pdo_stmt_t *stmt) /* {{{ */
128 {
129 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
130 pdo_mysql_db_handle *H = S->H;
131 my_ulonglong row_count;
132 PDO_DBG_ENTER("pdo_mysql_fill_stmt_from_result");
133
134 row_count = mysql_affected_rows(H->server);
135 if (row_count == (my_ulonglong)-1) {
136 /* we either have a query that returned a result set or an error occurred
137 lets see if we have access to a result set */
138 if (!H->buffered) {
139 S->result = mysql_use_result(H->server);
140 } else {
141 S->result = mysql_store_result(H->server);
142 }
143 if (NULL == S->result) {
144 pdo_mysql_error_stmt(stmt);
145 PDO_DBG_RETURN(0);
146 }
147
148 stmt->row_count = (zend_long) mysql_num_rows(S->result);
149 stmt->column_count = (int) mysql_num_fields(S->result);
150 S->fields = mysql_fetch_fields(S->result);
151 } else {
152 /* this was a DML or DDL query (INSERT, UPDATE, DELETE, ... */
153 stmt->row_count = (zend_long) row_count;
154 }
155
156 PDO_DBG_RETURN(1);
157 }
158 /* }}} */
159
160 #ifndef PDO_USE_MYSQLND
pdo_mysql_stmt_execute_prepared_libmysql(pdo_stmt_t * stmt)161 static int pdo_mysql_stmt_execute_prepared_libmysql(pdo_stmt_t *stmt) /* {{{ */
162 {
163 pdo_mysql_stmt *S = stmt->driver_data;
164 pdo_mysql_db_handle *H = S->H;
165
166 PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_libmysql");
167
168 /* (re)bind the parameters */
169 if (mysql_stmt_bind_param(S->stmt, S->params) || mysql_stmt_execute(S->stmt)) {
170 if (S->params) {
171 memset(S->params, 0, S->num_params * sizeof(MYSQL_BIND));
172 }
173 pdo_mysql_error_stmt(stmt);
174 if (mysql_stmt_errno(S->stmt) == 2057) {
175 /* CR_NEW_STMT_METADATA makes the statement unusable */
176 S->stmt = NULL;
177 }
178 PDO_DBG_RETURN(0);
179 }
180
181 if (!S->result) {
182 int i;
183
184 /* figure out the result set format, if any */
185 S->result = mysql_stmt_result_metadata(S->stmt);
186 if (S->result) {
187 int calc_max_length = H->buffered && S->max_length == 1;
188 S->fields = mysql_fetch_fields(S->result);
189 if (S->bound_result) {
190 int i;
191 for (i = 0; i < stmt->column_count; i++) {
192 efree(S->bound_result[i].buffer);
193 }
194 efree(S->bound_result);
195 efree(S->out_null);
196 efree(S->out_length);
197 }
198
199 stmt->column_count = (int)mysql_num_fields(S->result);
200 S->bound_result = ecalloc(stmt->column_count, sizeof(MYSQL_BIND));
201 S->out_null = ecalloc(stmt->column_count, sizeof(my_bool));
202 S->out_length = ecalloc(stmt->column_count, sizeof(zend_ulong));
203
204 /* summon memory to hold the row */
205 for (i = 0; i < stmt->column_count; i++) {
206 if (calc_max_length && S->fields[i].type == FIELD_TYPE_BLOB) {
207 my_bool on = 1;
208 mysql_stmt_attr_set(S->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &on);
209 calc_max_length = 0;
210 }
211 switch (S->fields[i].type) {
212 case FIELD_TYPE_INT24:
213 S->bound_result[i].buffer_length = MAX_MEDIUMINT_WIDTH + 1;
214 break;
215 case FIELD_TYPE_LONG:
216 S->bound_result[i].buffer_length = MAX_INT_WIDTH + 1;
217 break;
218 case FIELD_TYPE_LONGLONG:
219 S->bound_result[i].buffer_length = MAX_BIGINT_WIDTH + 1;
220 break;
221 case FIELD_TYPE_TINY:
222 S->bound_result[i].buffer_length = MAX_TINYINT_WIDTH + 1;
223 break;
224 case FIELD_TYPE_SHORT:
225 S->bound_result[i].buffer_length = MAX_SMALLINT_WIDTH + 1;
226 break;
227 default:
228 S->bound_result[i].buffer_length =
229 S->fields[i].max_length? S->fields[i].max_length:
230 S->fields[i].length;
231 /* work-around for longtext and alike */
232 if (S->bound_result[i].buffer_length > H->max_buffer_size) {
233 S->bound_result[i].buffer_length = H->max_buffer_size;
234 }
235 }
236
237 /* there are cases where the length reported by mysql is too short.
238 * eg: when describing a table that contains an enum column. Since
239 * we have no way of knowing the true length either, we'll bump up
240 * our buffer size to a reasonable size, just in case */
241 if (S->fields[i].max_length == 0 && S->bound_result[i].buffer_length < 128 && MYSQL_TYPE_VAR_STRING) {
242 S->bound_result[i].buffer_length = 128;
243 }
244
245 S->out_length[i] = 0;
246
247 S->bound_result[i].buffer = emalloc(S->bound_result[i].buffer_length);
248 S->bound_result[i].is_null = &S->out_null[i];
249 S->bound_result[i].length = &S->out_length[i];
250 S->bound_result[i].buffer_type = MYSQL_TYPE_STRING;
251 }
252
253 if (mysql_stmt_bind_result(S->stmt, S->bound_result)) {
254 pdo_mysql_error_stmt(stmt);
255 PDO_DBG_RETURN(0);
256 }
257
258 /* if buffered, pre-fetch all the data */
259 if (H->buffered) {
260 mysql_stmt_store_result(S->stmt);
261 }
262 }
263 }
264
265 pdo_mysql_stmt_set_row_count(stmt);
266 PDO_DBG_RETURN(1);
267 }
268 /* }}} */
269 #endif
270
271 #ifdef PDO_USE_MYSQLND
pdo_mysql_stmt_execute_prepared_mysqlnd(pdo_stmt_t * stmt)272 static int pdo_mysql_stmt_execute_prepared_mysqlnd(pdo_stmt_t *stmt) /* {{{ */
273 {
274 pdo_mysql_stmt *S = stmt->driver_data;
275 pdo_mysql_db_handle *H = S->H;
276 int i;
277
278 PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_mysqlnd");
279
280 if (mysql_stmt_execute(S->stmt)) {
281 pdo_mysql_error_stmt(stmt);
282 PDO_DBG_RETURN(0);
283 }
284
285 if (S->result) {
286 /* TODO: add a test to check if we really have zvals here... */
287 mysql_free_result(S->result);
288 S->result = NULL;
289 }
290
291 /* for SHOW/DESCRIBE and others the column/field count is not available before execute */
292 stmt->column_count = mysql_stmt_field_count(S->stmt);
293 for (i = 0; i < stmt->column_count; i++) {
294 mysqlnd_stmt_bind_one_result(S->stmt, i);
295 }
296
297 S->result = mysqlnd_stmt_result_metadata(S->stmt);
298 if (S->result) {
299 S->fields = mysql_fetch_fields(S->result);
300 /* if buffered, pre-fetch all the data */
301 if (H->buffered) {
302 if (mysql_stmt_store_result(S->stmt)) {
303 PDO_DBG_RETURN(0);
304 }
305 }
306 }
307
308 pdo_mysql_stmt_set_row_count(stmt);
309 PDO_DBG_RETURN(1);
310 }
311 /* }}} */
312 #endif
313
pdo_mysql_stmt_execute(pdo_stmt_t * stmt)314 static int pdo_mysql_stmt_execute(pdo_stmt_t *stmt) /* {{{ */
315 {
316 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
317 pdo_mysql_db_handle *H = S->H;
318 PDO_DBG_ENTER("pdo_mysql_stmt_execute");
319 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
320
321 if (S->stmt) {
322 PDO_DBG_RETURN(pdo_mysql_stmt_execute_prepared(stmt));
323 }
324
325 /* ensure that we free any previous unfetched results */
326 if (S->result) {
327 mysql_free_result(S->result);
328 S->result = NULL;
329 }
330
331 if (mysql_real_query(H->server, stmt->active_query_string, stmt->active_query_stringlen) != 0) {
332 pdo_mysql_error_stmt(stmt);
333 PDO_DBG_RETURN(0);
334 }
335
336 PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt));
337 }
338 /* }}} */
339
pdo_mysql_stmt_next_rowset(pdo_stmt_t * stmt)340 static int pdo_mysql_stmt_next_rowset(pdo_stmt_t *stmt) /* {{{ */
341 {
342 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
343 pdo_mysql_db_handle *H = S->H;
344 #if PDO_USE_MYSQLND
345 zend_long row_count;
346 #endif
347 PDO_DBG_ENTER("pdo_mysql_stmt_next_rowset");
348 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
349
350 #if PDO_USE_MYSQLND
351 if (!H->emulate_prepare) {
352 if (!mysqlnd_stmt_more_results(S->stmt)) {
353 PDO_DBG_RETURN(0);
354 }
355 if (mysqlnd_stmt_next_result(S->stmt)) {
356 PDO_DBG_RETURN(0);
357 }
358
359 if (!mysqlnd_stmt_more_results(S->stmt)) {
360 /*
361 MySQL gives us n + 1 result sets for
362 CALL proc() and n result sets returned by the proc itself.
363 Result set n + 1 is about the procedure call itself.
364 As the PDO emulation does not return it, we skip it as well
365 */
366 PDO_DBG_RETURN(0);
367 }
368
369 /* TODO - this code is stolen from execute() - see above */
370 if (S->result) {
371 mysql_free_result(S->result);
372 S->result = NULL;
373 }
374 {
375 /* for SHOW/DESCRIBE and others the column/field count is not available before execute */
376 int i;
377
378 stmt->column_count = mysql_stmt_field_count(S->stmt);
379 for (i = 0; i < stmt->column_count; i++) {
380 mysqlnd_stmt_bind_one_result(S->stmt, i);
381 }
382 }
383
384 S->result = mysqlnd_stmt_result_metadata(S->stmt);
385 if (S->result) {
386 S->fields = mysql_fetch_fields(S->result);
387
388 /* if buffered, pre-fetch all the data */
389 if (H->buffered) {
390 if (mysql_stmt_store_result(S->stmt)) {
391 PDO_DBG_RETURN(1);
392 }
393 }
394 }
395 row_count = (zend_long) mysql_stmt_affected_rows(S->stmt);
396 if (row_count != (zend_long)-1) {
397 stmt->row_count = row_count;
398 }
399 PDO_DBG_RETURN(1);
400 }
401 #endif
402
403 /* ensure that we free any previous unfetched results */
404 #ifndef PDO_USE_MYSQLND
405 if (S->stmt) {
406 if (S->result) {
407 stmt->column_count = (int)mysql_num_fields(S->result);
408 }
409 mysql_stmt_free_result(S->stmt);
410 }
411 #endif
412 if (S->result) {
413 mysql_free_result(S->result);
414 S->result = NULL;
415 }
416
417 if (!mysql_more_results(H->server)) {
418 /* No more results */
419 PDO_DBG_RETURN(0);
420 }
421 #if PDO_USE_MYSQLND
422 if (mysql_next_result(H->server) == FAIL) {
423 pdo_mysql_error_stmt(stmt);
424 PDO_DBG_RETURN(0);
425 } else {
426 PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt));
427 }
428 #else
429 if (mysql_next_result(H->server) > 0) {
430 pdo_mysql_error_stmt(stmt);
431 PDO_DBG_RETURN(0);
432 } else {
433 PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt));
434 }
435 #endif
436 }
437 /* }}} */
438
439
440 static const char * const pdo_param_event_names[] =
441 {
442 "PDO_PARAM_EVT_ALLOC",
443 "PDO_PARAM_EVT_FREE",
444 "PDO_PARAM_EVT_EXEC_PRE",
445 "PDO_PARAM_EVT_EXEC_POST",
446 "PDO_PARAM_EVT_FETCH_PRE",
447 "PDO_PARAM_EVT_FETCH_POST",
448 "PDO_PARAM_EVT_NORMALIZE",
449 };
450
451
pdo_mysql_stmt_param_hook(pdo_stmt_t * stmt,struct pdo_bound_param_data * param,enum pdo_param_event event_type)452 static int pdo_mysql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, enum pdo_param_event event_type) /* {{{ */
453 {
454 zval *parameter;
455 #ifndef PDO_USE_MYSQLND
456 PDO_MYSQL_PARAM_BIND *b;
457 #endif
458 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
459
460 PDO_DBG_ENTER("pdo_mysql_stmt_param_hook");
461 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
462 PDO_DBG_INF_FMT("event = %s", pdo_param_event_names[event_type]);
463 if (S->stmt && param->is_param) {
464 switch (event_type) {
465 case PDO_PARAM_EVT_ALLOC:
466 /* sanity check parameter number range */
467 if (param->paramno < 0 || param->paramno >= S->num_params) {
468 strcpy(stmt->error_code, "HY093");
469 PDO_DBG_RETURN(0);
470 }
471 S->params_given++;
472
473 #ifndef PDO_USE_MYSQLND
474 b = &S->params[param->paramno];
475 param->driver_data = b;
476 b->is_null = &S->in_null[param->paramno];
477 b->length = &S->in_length[param->paramno];
478 /* recall how many parameters have been provided */
479 #endif
480 PDO_DBG_RETURN(1);
481
482 case PDO_PARAM_EVT_EXEC_PRE:
483 if (S->params_given < (unsigned int) S->num_params) {
484 /* too few parameter bound */
485 PDO_DBG_ERR("too few parameters bound");
486 strcpy(stmt->error_code, "HY093");
487 PDO_DBG_RETURN(0);
488 }
489
490 if (!Z_ISREF(param->parameter)) {
491 parameter = ¶m->parameter;
492 } else {
493 parameter = Z_REFVAL(param->parameter);
494 }
495
496 #if PDO_USE_MYSQLND
497 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL || (Z_TYPE_P(parameter) == IS_NULL)) {
498 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_NULL);
499 PDO_DBG_RETURN(1);
500 }
501 #else
502 b = (PDO_MYSQL_PARAM_BIND*)param->driver_data;
503 *b->is_null = 0;
504 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL || Z_TYPE_P(parameter) == IS_NULL) {
505 *b->is_null = 1;
506 b->buffer_type = MYSQL_TYPE_STRING;
507 b->buffer = NULL;
508 b->buffer_length = 0;
509 *b->length = 0;
510 PDO_DBG_RETURN(1);
511 }
512 #endif /* PDO_USE_MYSQLND */
513
514 switch (PDO_PARAM_TYPE(param->param_type)) {
515 case PDO_PARAM_STMT:
516 PDO_DBG_RETURN(0);
517 case PDO_PARAM_LOB:
518 PDO_DBG_INF("PDO_PARAM_LOB");
519 if (!Z_ISREF(param->parameter)) {
520 parameter = ¶m->parameter;
521 } else {
522 parameter = Z_REFVAL(param->parameter);
523 }
524 if (Z_TYPE_P(parameter) == IS_RESOURCE) {
525 php_stream *stm = NULL;
526 php_stream_from_zval_no_verify(stm, parameter);
527 if (stm) {
528 zend_string *mem = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
529 zval_ptr_dtor(parameter);
530 ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC());
531 } else {
532 pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource");
533 return 0;
534 }
535 }
536 /* fall through */
537
538 default:
539 ;
540 }
541
542 #if PDO_USE_MYSQLND
543 /* Is it really correct to check the zval's type? - But well, that's what the old code below does, too */
544 PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
545 if (!Z_ISREF(param->parameter)) {
546 parameter = ¶m->parameter;
547 } else {
548 parameter = Z_REFVAL(param->parameter);
549 }
550 switch (Z_TYPE_P(parameter)) {
551 case IS_STRING:
552 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_VAR_STRING);
553 break;
554 case IS_LONG:
555 #if SIZEOF_ZEND_LONG==8
556 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_LONGLONG);
557 #elif SIZEOF_ZEND_LONG==4
558 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_LONG);
559 #endif /* SIZEOF_LONG */
560 break;
561 case IS_TRUE:
562 case IS_FALSE:
563 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_TINY);
564 break;
565 case IS_DOUBLE:
566 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_DOUBLE);
567 break;
568 default:
569 PDO_DBG_RETURN(0);
570 }
571
572 PDO_DBG_RETURN(1);
573 #else
574 PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
575 if (!Z_ISREF(param->parameter)) {
576 parameter = ¶m->parameter;
577 } else {
578 parameter = Z_REFVAL(param->parameter);
579 }
580 switch (Z_TYPE_P(parameter)) {
581 case IS_STRING:
582 b->buffer_type = MYSQL_TYPE_STRING;
583 b->buffer = Z_STRVAL_P(parameter);
584 b->buffer_length = Z_STRLEN_P(parameter);
585 *b->length = Z_STRLEN_P(parameter);
586 PDO_DBG_RETURN(1);
587
588 case IS_LONG:
589 b->buffer_type = MYSQL_TYPE_LONG;
590 b->buffer = &Z_LVAL_P(parameter);
591 PDO_DBG_RETURN(1);
592
593 case IS_DOUBLE:
594 b->buffer_type = MYSQL_TYPE_DOUBLE;
595 b->buffer = &Z_DVAL_P(parameter);
596 PDO_DBG_RETURN(1);
597
598 default:
599 PDO_DBG_RETURN(0);
600 }
601 #endif /* PDO_USE_MYSQLND */
602 case PDO_PARAM_EVT_FREE:
603 case PDO_PARAM_EVT_EXEC_POST:
604 case PDO_PARAM_EVT_FETCH_PRE:
605 case PDO_PARAM_EVT_FETCH_POST:
606 case PDO_PARAM_EVT_NORMALIZE:
607 /* do nothing */
608 break;
609 }
610 }
611
612 PDO_DBG_RETURN(1);
613 }
614 /* }}} */
615
pdo_mysql_stmt_fetch(pdo_stmt_t * stmt,enum pdo_fetch_orientation ori,zend_long offset)616 static int pdo_mysql_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, zend_long offset) /* {{{ */
617 {
618 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
619 #if PDO_USE_MYSQLND
620 zend_bool fetched_anything;
621
622 PDO_DBG_ENTER("pdo_mysql_stmt_fetch");
623 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
624 if (S->stmt) {
625 if (FAIL == mysqlnd_stmt_fetch(S->stmt, &fetched_anything) || fetched_anything == FALSE) {
626 PDO_DBG_RETURN(0);
627 }
628
629 PDO_DBG_RETURN(1);
630 }
631 #else
632 int ret;
633
634 if (S->stmt) {
635 ret = mysql_stmt_fetch(S->stmt);
636
637 # ifdef MYSQL_DATA_TRUNCATED
638 if (ret == MYSQL_DATA_TRUNCATED) {
639 ret = 0;
640 }
641 # endif
642
643 if (ret) {
644 if (ret != MYSQL_NO_DATA) {
645 pdo_mysql_error_stmt(stmt);
646 }
647 PDO_DBG_RETURN(0);
648 }
649
650 PDO_DBG_RETURN(1);
651 }
652 #endif /* PDO_USE_MYSQLND */
653
654 if (!S->result) {
655 strcpy(stmt->error_code, "HY000");
656 PDO_DBG_RETURN(0);
657 }
658 #if PDO_USE_MYSQLND
659 if (!S->stmt && S->current_data) {
660 mnd_free(S->current_data);
661 }
662 #endif /* PDO_USE_MYSQLND */
663
664 if ((S->current_data = mysql_fetch_row(S->result)) == NULL) {
665 #if PDO_USE_MYSQLND
666 if (S->result->unbuf && !S->result->unbuf->eof_reached && mysql_errno(S->H->server)) {
667 #else
668 if (!S->result->eof && mysql_errno(S->H->server)) {
669 #endif
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
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
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
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
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
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
942 /*
943 * Local variables:
944 * tab-width: 4
945 * c-basic-offset: 4
946 * End:
947 * vim600: noet sw=4 ts=4 fdm=marker
948 * vim<600: noet sw=4 ts=4
949 */
950