Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: eaad585a2e
Fetching contributors…

Cannot retrieve contributors at this time

executable file 925 lines (817 sloc) 24.824 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: George Schlossnagle <george@omniti.com> |
| Wez Furlong <wez@php.net> |
| Johannes Schlueter <johannes@mysql.com> |
+----------------------------------------------------------------------+
*/

/* $Id$ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "pdo/php_pdo.h"
#include "pdo/php_pdo_driver.h"
#include "php_pdo_mysql.h"
#include "php_pdo_mysql_int.h"

#ifdef PDO_USE_MYSQLND
# define pdo_mysql_stmt_execute_prepared(stmt) pdo_mysql_stmt_execute_prepared_mysqlnd(stmt TSRMLS_CC)
# define pdo_free_bound_result(res) zval_dtor(res.zv)
# define pdo_mysql_stmt_close(stmt) mysqlnd_stmt_close(stmt, 0)
#else
# define pdo_mysql_stmt_execute_prepared(stmt) pdo_mysql_stmt_execute_prepared_libmysql(stmt TSRMLS_CC)
# define pdo_free_bound_result(res) efree(res.buffer)
# define pdo_mysql_stmt_close(stmt) mysql_stmt_close(stmt)
#endif



static int pdo_mysql_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
{
pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;

PDO_DBG_ENTER("pdo_mysql_stmt_dtor");
PDO_DBG_INF_FMT("stmt=%p", S->stmt);
if (S->result) {
/* free the resource */
mysql_free_result(S->result);
S->result = NULL;
}
if (S->einfo.errmsg) {
pefree(S->einfo.errmsg, stmt->dbh->is_persistent);
S->einfo.errmsg = NULL;
}
if (S->stmt) {
pdo_mysql_stmt_close(S->stmt);
S->stmt = NULL;
}

#ifndef PDO_USE_MYSQLND
if (S->params) {
efree(S->params);
}
if (S->in_null) {
efree(S->in_null);
}
if (S->in_length) {
efree(S->in_length);
}

if (S->bound_result)
{
int i;
for (i = 0; i < stmt->column_count; i++) {
pdo_free_bound_result(S->bound_result[i]);
}

efree(S->bound_result);
efree(S->out_null);
efree(S->out_length);
}
#endif


if (S->H->server) {
while (mysql_more_results(S->H->server)) {
MYSQL_RES *res;
if (mysql_next_result(S->H->server) != 0) {
break;
}

res = mysql_store_result(S->H->server);
if (res) {
mysql_free_result(res);
}
}
}

#if PDO_USE_MYSQLND
if (!S->stmt && S->current_data) {
mnd_free(S->current_data);
}
#endif /* PDO_USE_MYSQLND */

efree(S);
PDO_DBG_RETURN(1);
}
/* }}} */

static void pdo_mysql_stmt_set_row_count(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
{
long row_count;
pdo_mysql_stmt *S = stmt->driver_data;
row_count = (long) mysql_stmt_affected_rows(S->stmt);
if (row_count != (long)-1) {
stmt->row_count = row_count;
}
}
/* }}} */

#ifndef PDO_USE_MYSQLND
static int pdo_mysql_stmt_execute_prepared_libmysql(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
{
pdo_mysql_stmt *S = stmt->driver_data;
pdo_mysql_db_handle *H = S->H;

PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_libmysql");

/* (re)bind the parameters */
if (mysql_stmt_bind_param(S->stmt, S->params) || mysql_stmt_execute(S->stmt)) {
if (S->params) {
efree(S->params);
S->params = 0;
}
pdo_mysql_error_stmt(stmt);
if (mysql_stmt_errno(S->stmt) == 2057) {
/* CR_NEW_STMT_METADATA makes the statement unusable */
S->stmt = NULL;
}
PDO_DBG_RETURN(0);
}

if (!S->result) {
int i;

/* figure out the result set format, if any */
S->result = mysql_stmt_result_metadata(S->stmt);
if (S->result) {
int calc_max_length = H->buffered && S->max_length == 1;
S->fields = mysql_fetch_fields(S->result);
if (S->bound_result) {
int i;
for (i = 0; i < stmt->column_count; i++) {
efree(S->bound_result[i].buffer);
}
efree(S->bound_result);
efree(S->out_null);
efree(S->out_length);
}

stmt->column_count = (int)mysql_num_fields(S->result);
S->bound_result = ecalloc(stmt->column_count, sizeof(MYSQL_BIND));
S->out_null = ecalloc(stmt->column_count, sizeof(my_bool));
S->out_length = ecalloc(stmt->column_count, sizeof(unsigned long));

/* summon memory to hold the row */
for (i = 0; i < stmt->column_count; i++) {
if (calc_max_length && S->fields[i].type == FIELD_TYPE_BLOB) {
my_bool on = 1;
mysql_stmt_attr_set(S->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &on);
calc_max_length = 0;
}
switch (S->fields[i].type) {
case FIELD_TYPE_INT24:
S->bound_result[i].buffer_length = MAX_MEDIUMINT_WIDTH + 1;
break;
case FIELD_TYPE_LONG:
S->bound_result[i].buffer_length = MAX_INT_WIDTH + 1;
break;
case FIELD_TYPE_LONGLONG:
S->bound_result[i].buffer_length = MAX_BIGINT_WIDTH + 1;
break;
case FIELD_TYPE_TINY:
S->bound_result[i].buffer_length = MAX_TINYINT_WIDTH + 1;
break;
case FIELD_TYPE_SHORT:
S->bound_result[i].buffer_length = MAX_SMALLINT_WIDTH + 1;
break;
default:
S->bound_result[i].buffer_length =
S->fields[i].max_length? S->fields[i].max_length:
S->fields[i].length;
/* work-around for longtext and alike */
if (S->bound_result[i].buffer_length > H->max_buffer_size) {
S->bound_result[i].buffer_length = H->max_buffer_size;
}
}

/* there are cases where the length reported by mysql is too short.
* eg: when describing a table that contains an enum column. Since
* we have no way of knowing the true length either, we'll bump up
* our buffer size to a reasonable size, just in case */
if (S->fields[i].max_length == 0 && S->bound_result[i].buffer_length < 128 && MYSQL_TYPE_VAR_STRING) {
S->bound_result[i].buffer_length = 128;
}

S->out_length[i] = 0;

S->bound_result[i].buffer = emalloc(S->bound_result[i].buffer_length);
S->bound_result[i].is_null = &S->out_null[i];
S->bound_result[i].length = &S->out_length[i];
S->bound_result[i].buffer_type = MYSQL_TYPE_STRING;
}

if (mysql_stmt_bind_result(S->stmt, S->bound_result)) {
pdo_mysql_error_stmt(stmt);
PDO_DBG_RETURN(0);
}

/* if buffered, pre-fetch all the data */
if (H->buffered) {
mysql_stmt_store_result(S->stmt);
}
}
}

pdo_mysql_stmt_set_row_count(stmt TSRMLS_CC);
PDO_DBG_RETURN(1);
}
/* }}} */
#endif

#ifdef PDO_USE_MYSQLND
static int pdo_mysql_stmt_execute_prepared_mysqlnd(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
{
pdo_mysql_stmt *S = stmt->driver_data;
pdo_mysql_db_handle *H = S->H;
int i;

PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_mysqlnd");

if (mysql_stmt_execute(S->stmt)) {
pdo_mysql_error_stmt(stmt);
PDO_DBG_RETURN(0);
}

if (S->result) {
/* TODO: add a test to check if we really have zvals here... */
mysql_free_result(S->result);
S->result = NULL;
}

/* for SHOW/DESCRIBE and others the column/field count is not available before execute */
stmt->column_count = mysql_stmt_field_count(S->stmt);
for (i = 0; i < stmt->column_count; i++) {
mysqlnd_stmt_bind_one_result(S->stmt, i);
}

S->result = mysqlnd_stmt_result_metadata(S->stmt);
if (S->result) {
S->fields = mysql_fetch_fields(S->result);
/* if buffered, pre-fetch all the data */
if (H->buffered) {
if (mysql_stmt_store_result(S->stmt)) {
PDO_DBG_RETURN(0);
}
}
}

pdo_mysql_stmt_set_row_count(stmt TSRMLS_CC);
PDO_DBG_RETURN(1);
}
/* }}} */
#endif

static int pdo_mysql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
{
pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
pdo_mysql_db_handle *H = S->H;
my_ulonglong row_count;
PDO_DBG_ENTER("pdo_mysql_stmt_execute");
PDO_DBG_INF_FMT("stmt=%p", S->stmt);

if (S->stmt) {
PDO_DBG_RETURN(pdo_mysql_stmt_execute_prepared(stmt));
}

/* ensure that we free any previous unfetched results */
if (S->result) {
mysql_free_result(S->result);
S->result = NULL;
}

if (mysql_real_query(H->server, stmt->active_query_string, stmt->active_query_stringlen) != 0) {
pdo_mysql_error_stmt(stmt);
PDO_DBG_RETURN(0);
}

row_count = mysql_affected_rows(H->server);
if (row_count == (my_ulonglong)-1) {
/* we either have a query that returned a result set or an error occured
lets see if we have access to a result set */
if (!H->buffered) {
S->result = mysql_use_result(H->server);
} else {
S->result = mysql_store_result(H->server);
}
if (NULL == S->result) {
pdo_mysql_error_stmt(stmt);
PDO_DBG_RETURN(0);
}

stmt->row_count = (long) mysql_num_rows(S->result);
stmt->column_count = (int) mysql_num_fields(S->result);
S->fields = mysql_fetch_fields(S->result);

} else {
/* this was a DML or DDL query (INSERT, UPDATE, DELETE, ... */
stmt->row_count = (long) row_count;
}

PDO_DBG_RETURN(1);
}
/* {{{ */

static int pdo_mysql_stmt_next_rowset(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
{
pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
pdo_mysql_db_handle *H = S->H;
long row_count;
int ret;
PDO_DBG_ENTER("pdo_mysql_stmt_next_rowset");
PDO_DBG_INF_FMT("stmt=%p", S->stmt);

#if PDO_USE_MYSQLND
if (!H->emulate_prepare) {
if (!mysqlnd_stmt_more_results(S->stmt)) {
PDO_DBG_RETURN(0);
}
if (mysqlnd_stmt_next_result(S->stmt)) {
PDO_DBG_RETURN(0);
}

if (!mysqlnd_stmt_more_results(S->stmt)) {
/*
MySQL gives us n + 1 result sets for
CALL proc() and n result sets returned by the proc itself.
Result set n + 1 is about the procedure call itself.
As the PDO emulation does not return it, we skip it as well
*/
PDO_DBG_RETURN(0);
}

/* TODO - this code is stolen from execute() - see above */
if (S->result) {
mysql_free_result(S->result);
S->result = NULL;
}
{
/* for SHOW/DESCRIBE and others the column/field count is not available before execute */
int i;

stmt->column_count = mysql_stmt_field_count(S->stmt);
for (i = 0; i < stmt->column_count; i++) {
mysqlnd_stmt_bind_one_result(S->stmt, i);
}
}

S->result = mysqlnd_stmt_result_metadata(S->stmt);
if (S->result) {
S->fields = mysql_fetch_fields(S->result);

/* if buffered, pre-fetch all the data */
if (H->buffered) {
if (mysql_stmt_store_result(S->stmt)) {
PDO_DBG_RETURN(1);
}
}
}
row_count = (long) mysql_stmt_affected_rows(S->stmt);
if (row_count != (long)-1) {
stmt->row_count = row_count;
}
PDO_DBG_RETURN(1);
}
#endif

/* ensure that we free any previous unfetched results */
#ifndef PDO_USE_MYSQLND
if (S->stmt) {
stmt->column_count = (int)mysql_num_fields(S->result);
mysql_stmt_free_result(S->stmt);
}
#endif
if (S->result) {
mysql_free_result(S->result);
S->result = NULL;
}

ret = mysql_next_result(H->server);

if (ret > 0) {
pdo_mysql_error_stmt(stmt);
PDO_DBG_RETURN(0);
} else if (ret < 0) {
/* No more results */
PDO_DBG_RETURN(0);
} else {
if (!H->buffered) {
S->result = mysql_use_result(H->server);
row_count = 0;
} else {
S->result = mysql_store_result(H->server);
if ((long)-1 == (row_count = (long) mysql_affected_rows(H->server))) {
pdo_mysql_error_stmt(stmt);
PDO_DBG_RETURN(0);
}
}

if (NULL == S->result) {
PDO_DBG_RETURN(0);
}

stmt->row_count = row_count;
stmt->column_count = (int) mysql_num_fields(S->result);
S->fields = mysql_fetch_fields(S->result);
PDO_DBG_RETURN(1);
}
}
/* }}} */


static const char * const pdo_param_event_names[] =
{
"PDO_PARAM_EVT_ALLOC",
"PDO_PARAM_EVT_FREE",
"PDO_PARAM_EVT_EXEC_PRE",
"PDO_PARAM_EVT_EXEC_POST",
"PDO_PARAM_EVT_FETCH_PRE",
"PDO_PARAM_EVT_FETCH_POST",
"PDO_PARAM_EVT_NORMALIZE",
};


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) /* {{{ */
{
#ifndef PDO_USE_MYSQLND
PDO_MYSQL_PARAM_BIND *b;
#endif
pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;

PDO_DBG_ENTER("pdo_mysql_stmt_param_hook");
PDO_DBG_INF_FMT("stmt=%p", S->stmt);
PDO_DBG_INF_FMT("event = %s", pdo_param_event_names[event_type]);
if (S->stmt && param->is_param) {
switch (event_type) {
case PDO_PARAM_EVT_ALLOC:
/* sanity check parameter number range */
if (param->paramno < 0 || param->paramno >= S->num_params) {
strcpy(stmt->error_code, "HY093");
PDO_DBG_RETURN(0);
}
S->params_given++;

#ifndef PDO_USE_MYSQLND
b = &S->params[param->paramno];
param->driver_data = b;
b->is_null = &S->in_null[param->paramno];
b->length = &S->in_length[param->paramno];
/* recall how many parameters have been provided */
#endif
PDO_DBG_RETURN(1);

case PDO_PARAM_EVT_EXEC_PRE:
if (S->params_given < (unsigned int) S->num_params) {
/* too few parameter bound */
PDO_DBG_ERR("too few parameters bound");
strcpy(stmt->error_code, "HY093");
PDO_DBG_RETURN(0);
}

#if PDO_USE_MYSQLND
if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
Z_TYPE_P(param->parameter) == IS_NULL) {
mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_NULL);
PDO_DBG_RETURN(1);
}
#else
b = (PDO_MYSQL_PARAM_BIND*)param->driver_data;
*b->is_null = 0;
if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
Z_TYPE_P(param->parameter) == IS_NULL) {
*b->is_null = 1;
b->buffer_type = MYSQL_TYPE_STRING;
b->buffer = NULL;
b->buffer_length = 0;
*b->length = 0;
PDO_DBG_RETURN(1);
}
#endif /* PDO_USE_MYSQLND */

switch (PDO_PARAM_TYPE(param->param_type)) {
case PDO_PARAM_STMT:
PDO_DBG_RETURN(0);
case PDO_PARAM_LOB:
PDO_DBG_INF("PDO_PARAM_LOB");
if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
php_stream *stm;
php_stream_from_zval_no_verify(stm, &param->parameter);
if (stm) {
SEPARATE_ZVAL_IF_NOT_REF(&param->parameter);
Z_TYPE_P(param->parameter) = IS_STRING;
Z_STRLEN_P(param->parameter) = php_stream_copy_to_mem(stm,
&Z_STRVAL_P(param->parameter), PHP_STREAM_COPY_ALL, 0);
} else {
pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource" TSRMLS_CC);
return 0;
}
}
/* fall through */

default:
;
}
#if PDO_USE_MYSQLND
/* Is it really correct to check the zval's type? - But well, that's what the old code below does, too */
PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE_P(param->parameter));
switch (Z_TYPE_P(param->parameter)) {
case IS_STRING:
mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_VAR_STRING);
break;
case IS_LONG:
#if SIZEOF_LONG==8
mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_LONGLONG);
#elif SIZEOF_LONG==4
mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_LONG);
#endif /* SIZEOF_LONG */
break;
case IS_DOUBLE:
mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, param->parameter, MYSQL_TYPE_DOUBLE);
break;
default:
PDO_DBG_RETURN(0);
}

PDO_DBG_RETURN(1);
#else
PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE_P(param->parameter));
switch (Z_TYPE_P(param->parameter)) {
case IS_STRING:
b->buffer_type = MYSQL_TYPE_STRING;
b->buffer = Z_STRVAL_P(param->parameter);
b->buffer_length = Z_STRLEN_P(param->parameter);
*b->length = Z_STRLEN_P(param->parameter);
PDO_DBG_RETURN(1);

case IS_LONG:
b->buffer_type = MYSQL_TYPE_LONG;
b->buffer = &Z_LVAL_P(param->parameter);
PDO_DBG_RETURN(1);

case IS_DOUBLE:
b->buffer_type = MYSQL_TYPE_DOUBLE;
b->buffer = &Z_DVAL_P(param->parameter);
PDO_DBG_RETURN(1);

default:
PDO_DBG_RETURN(0);
}
#endif /* PDO_USE_MYSQLND */
case PDO_PARAM_EVT_FREE:
case PDO_PARAM_EVT_EXEC_POST:
case PDO_PARAM_EVT_FETCH_PRE:
case PDO_PARAM_EVT_FETCH_POST:
case PDO_PARAM_EVT_NORMALIZE:
/* do nothing */
break;
}
}

PDO_DBG_RETURN(1);
}
/* }}} */

static int pdo_mysql_stmt_fetch(pdo_stmt_t *stmt,
enum pdo_fetch_orientation ori, long offset TSRMLS_DC) /* {{{ */
{
pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
#if PDO_USE_MYSQLND
zend_bool fetched_anything;

PDO_DBG_ENTER("pdo_mysql_stmt_fetch");
PDO_DBG_INF_FMT("stmt=%p", S->stmt);
if (S->stmt) {
if (FAIL == mysqlnd_stmt_fetch(S->stmt, &fetched_anything) || fetched_anything == FALSE) {
PDO_DBG_RETURN(0);
}

PDO_DBG_RETURN(1);
}
#else
int ret;

if (S->stmt) {
ret = mysql_stmt_fetch(S->stmt);

# ifdef MYSQL_DATA_TRUNCATED
if (ret == MYSQL_DATA_TRUNCATED) {
ret = 0;
}
# endif

if (ret) {
if (ret != MYSQL_NO_DATA) {
pdo_mysql_error_stmt(stmt);
}
PDO_DBG_RETURN(0);
}

PDO_DBG_RETURN(1);
}
#endif /* PDO_USE_MYSQLND */

if (!S->result) {
strcpy(stmt->error_code, "HY000");
PDO_DBG_RETURN(0);
}
#if PDO_USE_MYSQLND
if (!S->stmt && S->current_data) {
mnd_free(S->current_data);
}
#endif /* PDO_USE_MYSQLND */

if ((S->current_data = mysql_fetch_row(S->result)) == NULL) {
if (mysql_errno(S->H->server)) {
pdo_mysql_error_stmt(stmt);
}
PDO_DBG_RETURN(0);
}

S->current_lengths = mysql_fetch_lengths(S->result);
PDO_DBG_RETURN(1);
}
/* }}} */

static int pdo_mysql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC) /* {{{ */
{
pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
struct pdo_column_data *cols = stmt->columns;
int i;

PDO_DBG_ENTER("pdo_mysql_stmt_describe");
PDO_DBG_INF_FMT("stmt=%p", S->stmt);
if (!S->result) {
PDO_DBG_RETURN(0);
}

if (colno >= stmt->column_count) {
/* error invalid column */
PDO_DBG_RETURN(0);
}

/* fetch all on demand, this seems easiest
** if we've been here before bail out
*/
if (cols[0].name) {
PDO_DBG_RETURN(1);
}
for (i = 0; i < stmt->column_count; i++) {
int namelen;

if (S->H->fetch_table_names) {
namelen = spprintf(&cols[i].name, 0, "%s.%s", S->fields[i].table, S->fields[i].name);
cols[i].namelen = namelen;
} else {
namelen = strlen(S->fields[i].name);
cols[i].namelen = namelen;
cols[i].name = estrndup(S->fields[i].name, namelen);
}

cols[i].precision = S->fields[i].decimals;
cols[i].maxlen = S->fields[i].length;
#ifdef PDO_USE_MYSQLND
if (S->stmt) {
cols[i].param_type = PDO_PARAM_ZVAL;
} else
#endif
{
cols[i].param_type = PDO_PARAM_STR;
}
}
PDO_DBG_RETURN(1);
}
/* }}} */

static int pdo_mysql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC) /* {{{ */
{
pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;

PDO_DBG_ENTER("pdo_mysql_stmt_get_col");
PDO_DBG_INF_FMT("stmt=%p", S->stmt);
if (!S->result) {
PDO_DBG_RETURN(0);
}

/* With mysqlnd data is stored inside mysqlnd, not S->current_data */
if (!S->stmt) {
if (S->current_data == NULL || !S->result) {
PDO_DBG_RETURN(0);
}
}

if (colno >= stmt->column_count) {
/* error invalid column */
PDO_DBG_RETURN(0);
}
#if PDO_USE_MYSQLND
if (S->stmt) {
Z_ADDREF_P(S->stmt->data->result_bind[colno].zv);
*ptr = (char*)&S->stmt->data->result_bind[colno].zv;
*len = sizeof(zval);
PDO_DBG_RETURN(1);
}
#else
if (S->stmt) {
if (S->out_null[colno]) {
*ptr = NULL;
*len = 0;
PDO_DBG_RETURN(1);
}
*ptr = S->bound_result[colno].buffer;
if (S->out_length[colno] > S->bound_result[colno].buffer_length) {
/* mysql lied about the column width */
strcpy(stmt->error_code, "01004"); /* truncated */
S->out_length[colno] = S->bound_result[colno].buffer_length;
*len = S->out_length[colno];
PDO_DBG_RETURN(0);
}
*len = S->out_length[colno];
PDO_DBG_RETURN(1);
}
#endif
*ptr = S->current_data[colno];
*len = S->current_lengths[colno];
PDO_DBG_RETURN(1);
} /* }}} */

static char *type_to_name_native(int type) /* }}} */
{
#define PDO_MYSQL_NATIVE_TYPE_NAME(x) case FIELD_TYPE_##x: return #x;

    switch (type) {
        PDO_MYSQL_NATIVE_TYPE_NAME(STRING)
        PDO_MYSQL_NATIVE_TYPE_NAME(VAR_STRING)
#ifdef MYSQL_HAS_TINY
        PDO_MYSQL_NATIVE_TYPE_NAME(TINY)
#endif
        PDO_MYSQL_NATIVE_TYPE_NAME(SHORT)
        PDO_MYSQL_NATIVE_TYPE_NAME(LONG)
        PDO_MYSQL_NATIVE_TYPE_NAME(LONGLONG)
        PDO_MYSQL_NATIVE_TYPE_NAME(INT24)
        PDO_MYSQL_NATIVE_TYPE_NAME(FLOAT)
        PDO_MYSQL_NATIVE_TYPE_NAME(DOUBLE)
        PDO_MYSQL_NATIVE_TYPE_NAME(DECIMAL)
#ifdef FIELD_TYPE_NEWDECIMAL
        PDO_MYSQL_NATIVE_TYPE_NAME(NEWDECIMAL)
#endif
#ifdef FIELD_TYPE_GEOMETRY
        PDO_MYSQL_NATIVE_TYPE_NAME(GEOMETRY)
#endif
        PDO_MYSQL_NATIVE_TYPE_NAME(TIMESTAMP)
#ifdef MYSQL_HAS_YEAR
        PDO_MYSQL_NATIVE_TYPE_NAME(YEAR)
#endif
        PDO_MYSQL_NATIVE_TYPE_NAME(SET)
        PDO_MYSQL_NATIVE_TYPE_NAME(ENUM)
        PDO_MYSQL_NATIVE_TYPE_NAME(DATE)
#ifdef FIELD_TYPE_NEWDATE
        PDO_MYSQL_NATIVE_TYPE_NAME(NEWDATE)
#endif
        PDO_MYSQL_NATIVE_TYPE_NAME(TIME)
        PDO_MYSQL_NATIVE_TYPE_NAME(DATETIME)
        PDO_MYSQL_NATIVE_TYPE_NAME(TINY_BLOB)
        PDO_MYSQL_NATIVE_TYPE_NAME(MEDIUM_BLOB)
        PDO_MYSQL_NATIVE_TYPE_NAME(LONG_BLOB)
        PDO_MYSQL_NATIVE_TYPE_NAME(BLOB)
        PDO_MYSQL_NATIVE_TYPE_NAME(NULL)
        default:
            return NULL;
    }
#undef PDO_MYSQL_NATIVE_TYPE_NAME
} /* }}} */

static int pdo_mysql_stmt_col_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC) /* {{{ */
{
pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
const MYSQL_FIELD *F;
zval *flags;
char *str;

PDO_DBG_ENTER("pdo_mysql_stmt_col_meta");
PDO_DBG_INF_FMT("stmt=%p", S->stmt);
if (!S->result) {
PDO_DBG_RETURN(FAILURE);
}
if (colno >= stmt->column_count) {
/* error invalid column */
PDO_DBG_RETURN(FAILURE);
}

array_init(return_value);
MAKE_STD_ZVAL(flags);
array_init(flags);

F = S->fields + colno;

if (F->def) {
add_assoc_string(return_value, "mysql:def", F->def, 1);
}
if (IS_NOT_NULL(F->flags)) {
add_next_index_string(flags, "not_null", 1);
}
if (IS_PRI_KEY(F->flags)) {
add_next_index_string(flags, "primary_key", 1);
}
if (F->flags & MULTIPLE_KEY_FLAG) {
add_next_index_string(flags, "multiple_key", 1);
}
if (F->flags & UNIQUE_KEY_FLAG) {
add_next_index_string(flags, "unique_key", 1);
}
if (IS_BLOB(F->flags)) {
add_next_index_string(flags, "blob", 1);
}
str = type_to_name_native(F->type);
if (str) {
add_assoc_string(return_value, "native_type", str, 1);
}

#ifdef PDO_USE_MYSQLND
switch (F->type) {
case MYSQL_TYPE_BIT:
case MYSQL_TYPE_YEAR:
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG:
#if SIZEOF_LONG==8
case MYSQL_TYPE_LONGLONG:
#endif
add_assoc_long(return_value, "pdo_type", PDO_PARAM_INT);
break;
default:
add_assoc_long(return_value, "pdo_type", PDO_PARAM_STR);
break;
}
#endif

add_assoc_zval(return_value, "flags", flags);
add_assoc_string(return_value, "table",(F->table?F->table:""), 1);
PDO_DBG_RETURN(SUCCESS);
} /* }}} */

static int pdo_mysql_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
{
pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;

PDO_DBG_ENTER("pdo_mysql_stmt_cursor_closer");
PDO_DBG_INF_FMT("stmt=%p", S->stmt);
if (S->result) {
mysql_free_result(S->result);
S->result = NULL;
}
if (S->stmt) {
int retval;
retval = mysql_stmt_free_result(S->stmt);
PDO_DBG_RETURN(retval ? 0 : 1);
}

while (mysql_more_results(S->H->server)) {
MYSQL_RES *res;
if (mysql_next_result(S->H->server) != 0) {
break;
}
res = mysql_store_result(S->H->server);
if (res) {
mysql_free_result(res);
}
}
PDO_DBG_RETURN(1);
}
/* }}} */

struct pdo_stmt_methods mysql_stmt_methods = {
pdo_mysql_stmt_dtor,
pdo_mysql_stmt_execute,
pdo_mysql_stmt_fetch,
pdo_mysql_stmt_describe,
pdo_mysql_stmt_get_col,
pdo_mysql_stmt_param_hook,
NULL, /* set_attr */
NULL, /* get_attr */
pdo_mysql_stmt_col_meta,
pdo_mysql_stmt_next_rowset,
pdo_mysql_stmt_cursor_closer
};

/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
Something went wrong with that request. Please try again.