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