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: 279 310 90.0 %
Date: 2016-09-18 Functions: 13 13 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 18 Sep 2016 08:20:12 +0000 (8 days ago)

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