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 = ¶m->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 = ¶m->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 = ¶m->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_DOUBLE:
564 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_DOUBLE);
565 break;
566 default:
567 PDO_DBG_RETURN(0);
568 }
569
570 PDO_DBG_RETURN(1);
571 #else
572 PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
573 if (!Z_ISREF(param->parameter)) {
574 parameter = ¶m->parameter;
575 } else {
576 parameter = Z_REFVAL(param->parameter);
577 }
578 switch (Z_TYPE_P(parameter)) {
579 case IS_STRING:
580 b->buffer_type = MYSQL_TYPE_STRING;
581 b->buffer = Z_STRVAL_P(parameter);
582 b->buffer_length = Z_STRLEN_P(parameter);
583 *b->length = Z_STRLEN_P(parameter);
584 PDO_DBG_RETURN(1);
585
586 case IS_LONG:
587 b->buffer_type = MYSQL_TYPE_LONG;
588 b->buffer = &Z_LVAL_P(parameter);
589 PDO_DBG_RETURN(1);
590
591 case IS_DOUBLE:
592 b->buffer_type = MYSQL_TYPE_DOUBLE;
593 b->buffer = &Z_DVAL_P(parameter);
594 PDO_DBG_RETURN(1);
595
596 default:
597 PDO_DBG_RETURN(0);
598 }
599 #endif /* PDO_USE_MYSQLND */
600 case PDO_PARAM_EVT_FREE:
601 case PDO_PARAM_EVT_EXEC_POST:
602 case PDO_PARAM_EVT_FETCH_PRE:
603 case PDO_PARAM_EVT_FETCH_POST:
604 case PDO_PARAM_EVT_NORMALIZE:
605 /* do nothing */
606 break;
607 }
608 }
609
610 PDO_DBG_RETURN(1);
611 }
612 /* }}} */
613
pdo_mysql_stmt_fetch(pdo_stmt_t * stmt,enum pdo_fetch_orientation ori,zend_long offset)614 static int pdo_mysql_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, zend_long offset) /* {{{ */
615 {
616 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
617 #if PDO_USE_MYSQLND
618 zend_bool fetched_anything;
619
620 PDO_DBG_ENTER("pdo_mysql_stmt_fetch");
621 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
622 if (S->stmt) {
623 if (FAIL == mysqlnd_stmt_fetch(S->stmt, &fetched_anything) || fetched_anything == FALSE) {
624 PDO_DBG_RETURN(0);
625 }
626
627 PDO_DBG_RETURN(1);
628 }
629 #else
630 int ret;
631
632 if (S->stmt) {
633 ret = mysql_stmt_fetch(S->stmt);
634
635 # ifdef MYSQL_DATA_TRUNCATED
636 if (ret == MYSQL_DATA_TRUNCATED) {
637 ret = 0;
638 }
639 # endif
640
641 if (ret) {
642 if (ret != MYSQL_NO_DATA) {
643 pdo_mysql_error_stmt(stmt);
644 }
645 PDO_DBG_RETURN(0);
646 }
647
648 PDO_DBG_RETURN(1);
649 }
650 #endif /* PDO_USE_MYSQLND */
651
652 if (!S->result) {
653 strcpy(stmt->error_code, "HY000");
654 PDO_DBG_RETURN(0);
655 }
656 #if PDO_USE_MYSQLND
657 if (!S->stmt && S->current_data) {
658 mnd_free(S->current_data);
659 }
660 #endif /* PDO_USE_MYSQLND */
661
662 if ((S->current_data = mysql_fetch_row(S->result)) == NULL) {
663 #if PDO_USE_MYSQLND
664 if (S->result->unbuf && !S->result->unbuf->eof_reached && mysql_errno(S->H->server)) {
665 #else
666 if (!S->result->eof && mysql_errno(S->H->server)) {
667 #endif
668 pdo_mysql_error_stmt(stmt);
669 }
670 PDO_DBG_RETURN(0);
671 }
672
673 S->current_lengths = mysql_fetch_lengths(S->result);
674 PDO_DBG_RETURN(1);
675 }
676 /* }}} */
677
678 static int pdo_mysql_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
679 {
680 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
681 struct pdo_column_data *cols = stmt->columns;
682 int i;
683
684 PDO_DBG_ENTER("pdo_mysql_stmt_describe");
685 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
686 if (!S->result) {
687 PDO_DBG_RETURN(0);
688 }
689
690 if (colno >= stmt->column_count) {
691 /* error invalid column */
692 PDO_DBG_RETURN(0);
693 }
694
695 /* fetch all on demand, this seems easiest
696 ** if we've been here before bail out
697 */
698 if (cols[0].name) {
699 PDO_DBG_RETURN(1);
700 }
701 for (i = 0; i < stmt->column_count; i++) {
702
703 if (S->H->fetch_table_names) {
704 cols[i].name = strpprintf(0, "%s.%s", S->fields[i].table, S->fields[i].name);
705 } else {
706 cols[i].name = zend_string_init(S->fields[i].name, S->fields[i].name_length, 0);
707 }
708
709 cols[i].precision = S->fields[i].decimals;
710 cols[i].maxlen = S->fields[i].length;
711
712 #ifdef PDO_USE_MYSQLND
713 if (S->stmt) {
714 cols[i].param_type = PDO_PARAM_ZVAL;
715 } else
716 #endif
717 {
718 cols[i].param_type = PDO_PARAM_STR;
719 }
720 }
721 PDO_DBG_RETURN(1);
722 }
723 /* }}} */
724
725 static int pdo_mysql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, size_t *len, int *caller_frees) /* {{{ */
726 {
727 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
728
729 PDO_DBG_ENTER("pdo_mysql_stmt_get_col");
730 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
731 if (!S->result) {
732 PDO_DBG_RETURN(0);
733 }
734
735 /* With mysqlnd data is stored inside mysqlnd, not S->current_data */
736 if (!S->stmt) {
737 if (S->current_data == NULL || !S->result) {
738 PDO_DBG_RETURN(0);
739 }
740 }
741
742 if (colno >= stmt->column_count) {
743 /* error invalid column */
744 PDO_DBG_RETURN(0);
745 }
746 #if PDO_USE_MYSQLND
747 if (S->stmt) {
748 Z_TRY_ADDREF(S->stmt->data->result_bind[colno].zv);
749 *ptr = (char*)&S->stmt->data->result_bind[colno].zv;
750 *len = sizeof(zval);
751 PDO_DBG_RETURN(1);
752 }
753 #else
754 if (S->stmt) {
755 if (S->out_null[colno]) {
756 *ptr = NULL;
757 *len = 0;
758 PDO_DBG_RETURN(1);
759 }
760 *ptr = S->bound_result[colno].buffer;
761 if (S->out_length[colno] > S->bound_result[colno].buffer_length) {
762 /* mysql lied about the column width */
763 strcpy(stmt->error_code, "01004"); /* truncated */
764 S->out_length[colno] = S->bound_result[colno].buffer_length;
765 *len = S->out_length[colno];
766 PDO_DBG_RETURN(0);
767 }
768 *len = S->out_length[colno];
769 PDO_DBG_RETURN(1);
770 }
771 #endif
772 *ptr = S->current_data[colno];
773 *len = S->current_lengths[colno];
774 PDO_DBG_RETURN(1);
775 } /* }}} */
776
777 static char *type_to_name_native(int type) /* {{{ */
778 {
779 #define PDO_MYSQL_NATIVE_TYPE_NAME(x) case FIELD_TYPE_##x: return #x;
780
781 switch (type) {
782 PDO_MYSQL_NATIVE_TYPE_NAME(STRING)
783 PDO_MYSQL_NATIVE_TYPE_NAME(VAR_STRING)
784 #ifdef FIELD_TYPE_TINY
785 PDO_MYSQL_NATIVE_TYPE_NAME(TINY)
786 #endif
787 #ifdef FIELD_TYPE_BIT
788 PDO_MYSQL_NATIVE_TYPE_NAME(BIT)
789 #endif
790 PDO_MYSQL_NATIVE_TYPE_NAME(SHORT)
791 PDO_MYSQL_NATIVE_TYPE_NAME(LONG)
792 PDO_MYSQL_NATIVE_TYPE_NAME(LONGLONG)
793 PDO_MYSQL_NATIVE_TYPE_NAME(INT24)
794 PDO_MYSQL_NATIVE_TYPE_NAME(FLOAT)
795 PDO_MYSQL_NATIVE_TYPE_NAME(DOUBLE)
796 PDO_MYSQL_NATIVE_TYPE_NAME(DECIMAL)
797 #ifdef FIELD_TYPE_NEWDECIMAL
798 PDO_MYSQL_NATIVE_TYPE_NAME(NEWDECIMAL)
799 #endif
800 #ifdef FIELD_TYPE_GEOMETRY
801 PDO_MYSQL_NATIVE_TYPE_NAME(GEOMETRY)
802 #endif
803 PDO_MYSQL_NATIVE_TYPE_NAME(TIMESTAMP)
804 #ifdef FIELD_TYPE_YEAR
805 PDO_MYSQL_NATIVE_TYPE_NAME(YEAR)
806 #endif
807 PDO_MYSQL_NATIVE_TYPE_NAME(SET)
808 PDO_MYSQL_NATIVE_TYPE_NAME(ENUM)
809 PDO_MYSQL_NATIVE_TYPE_NAME(DATE)
810 #ifdef FIELD_TYPE_NEWDATE
811 PDO_MYSQL_NATIVE_TYPE_NAME(NEWDATE)
812 #endif
813 PDO_MYSQL_NATIVE_TYPE_NAME(TIME)
814 PDO_MYSQL_NATIVE_TYPE_NAME(DATETIME)
815 PDO_MYSQL_NATIVE_TYPE_NAME(TINY_BLOB)
816 PDO_MYSQL_NATIVE_TYPE_NAME(MEDIUM_BLOB)
817 PDO_MYSQL_NATIVE_TYPE_NAME(LONG_BLOB)
818 PDO_MYSQL_NATIVE_TYPE_NAME(BLOB)
819 PDO_MYSQL_NATIVE_TYPE_NAME(NULL)
820 default:
821 return NULL;
822 }
823 #undef PDO_MYSQL_NATIVE_TYPE_NAME
824 } /* }}} */
825
826 static int pdo_mysql_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value) /* {{{ */
827 {
828 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
829 const MYSQL_FIELD *F;
830 zval flags;
831 char *str;
832
833 PDO_DBG_ENTER("pdo_mysql_stmt_col_meta");
834 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
835 if (!S->result) {
836 PDO_DBG_RETURN(FAILURE);
837 }
838 if (colno >= stmt->column_count) {
839 /* error invalid column */
840 PDO_DBG_RETURN(FAILURE);
841 }
842
843 array_init(return_value);
844 array_init(&flags);
845
846 F = S->fields + colno;
847
848 if (F->def) {
849 add_assoc_string(return_value, "mysql:def", F->def);
850 }
851 if (IS_NOT_NULL(F->flags)) {
852 add_next_index_string(&flags, "not_null");
853 }
854 if (IS_PRI_KEY(F->flags)) {
855 add_next_index_string(&flags, "primary_key");
856 }
857 if (F->flags & MULTIPLE_KEY_FLAG) {
858 add_next_index_string(&flags, "multiple_key");
859 }
860 if (F->flags & UNIQUE_KEY_FLAG) {
861 add_next_index_string(&flags, "unique_key");
862 }
863 if (IS_BLOB(F->flags)) {
864 add_next_index_string(&flags, "blob");
865 }
866 str = type_to_name_native(F->type);
867 if (str) {
868 add_assoc_string(return_value, "native_type", str);
869 }
870
871 #ifdef PDO_USE_MYSQLND
872 switch (F->type) {
873 case MYSQL_TYPE_BIT:
874 case MYSQL_TYPE_YEAR:
875 case MYSQL_TYPE_TINY:
876 case MYSQL_TYPE_SHORT:
877 case MYSQL_TYPE_INT24:
878 case MYSQL_TYPE_LONG:
879 #if SIZEOF_LONG==8
880 case MYSQL_TYPE_LONGLONG:
881 #endif
882 add_assoc_long(return_value, "pdo_type", PDO_PARAM_INT);
883 break;
884 default:
885 add_assoc_long(return_value, "pdo_type", PDO_PARAM_STR);
886 break;
887 }
888 #endif
889
890 add_assoc_zval(return_value, "flags", &flags);
891 add_assoc_string(return_value, "table", (char *) (F->table?F->table : ""));
892
893 PDO_DBG_RETURN(SUCCESS);
894 } /* }}} */
895
896 static int pdo_mysql_stmt_cursor_closer(pdo_stmt_t *stmt) /* {{{ */
897 {
898 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
899
900 PDO_DBG_ENTER("pdo_mysql_stmt_cursor_closer");
901 PDO_DBG_INF_FMT("stmt=%p", S->stmt);
902 if (S->result) {
903 mysql_free_result(S->result);
904 S->result = NULL;
905 }
906 if (S->stmt) {
907 int retval;
908 retval = mysql_stmt_free_result(S->stmt);
909 PDO_DBG_RETURN(retval ? 0 : 1);
910 }
911
912 while (mysql_more_results(S->H->server)) {
913 MYSQL_RES *res;
914 if (mysql_next_result(S->H->server) != 0) {
915 break;
916 }
917 res = mysql_store_result(S->H->server);
918 if (res) {
919 mysql_free_result(res);
920 }
921 }
922 PDO_DBG_RETURN(1);
923 }
924 /* }}} */
925
926 struct pdo_stmt_methods mysql_stmt_methods = {
927 pdo_mysql_stmt_dtor,
928 pdo_mysql_stmt_execute,
929 pdo_mysql_stmt_fetch,
930 pdo_mysql_stmt_describe,
931 pdo_mysql_stmt_get_col,
932 pdo_mysql_stmt_param_hook,
933 NULL, /* set_attr */
934 NULL, /* get_attr */
935 pdo_mysql_stmt_col_meta,
936 pdo_mysql_stmt_next_rowset,
937 pdo_mysql_stmt_cursor_closer
938 };
939
940 /*
941 * Local variables:
942 * tab-width: 4
943 * c-basic-offset: 4
944 * End:
945 * vim600: noet sw=4 ts=4 fdm=marker
946 * vim<600: noet sw=4 ts=4
947 */
948