/[svn]/php/php-src/trunk/ext/pdo_mysql/mysql_statement.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 301877 - (show annotations)
Thu Aug 5 13:29:44 2010 UTC (13 years, 11 months ago) by johannes
File MIME type: text/x-c
File size: 24824 byte(s)
- Drop (broken) support for libmysql 3.23 and 4.0 from pdo_mysql, see bug #51259

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

Properties

Name Value
cvs2svn:cvs-rev 1.48.2.14.2.6.2.5
svn:eol-style native
svn:executable *
svn:keywords Id Rev Revision Date LastChangedDate LastChangedRevision Author LastChangedBy HeadURL URL
svn:mime-type text/x-c