PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/pdo_mysql - mysql_statement.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 273 303 90.1 %
Date: 2014-04-19 Functions: 13 13 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 20 Apr 2014 03:52:13 +0000 (27 hours ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.