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: 278 309 90.0 %
Date: 2016-07-19 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        1731 : static int pdo_mysql_stmt_dtor(pdo_stmt_t *stmt) /* {{{ */
      48             : {
      49        1731 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
      50             : 
      51        1731 :         PDO_DBG_ENTER("pdo_mysql_stmt_dtor");
      52        1731 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
      53        1731 :         if (S->result) {
      54             :                 /* free the resource */
      55         583 :                 mysql_free_result(S->result);
      56         583 :                 S->result = NULL;
      57             :         }
      58        1731 :         if (S->einfo.errmsg) {
      59          32 :                 pefree(S->einfo.errmsg, stmt->dbh->is_persistent);
      60          32 :                 S->einfo.errmsg = NULL;
      61             :         }
      62        1731 :         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        3457 :         if (IS_OBJ_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE(stmt->database_object_handle)])
      92        1726 :                 && (!(GC_FLAGS(Z_OBJ(stmt->database_object_handle)) & IS_OBJ_FREE_CALLED))) {
      93        3452 :                 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        1731 :         if (!S->stmt && S->current_data) {
     108         201 :                 mnd_free(S->current_data);
     109             :         }
     110             : #endif /* PDO_USE_MYSQLND */
     111             : 
     112        1731 :         efree(S);
     113        1731 :         PDO_DBG_RETURN(1);
     114             : }
     115             : /* }}} */
     116             : 
     117         739 : static void pdo_mysql_stmt_set_row_count(pdo_stmt_t *stmt) /* {{{ */
     118             : {
     119             :         zend_long row_count;
     120         739 :         pdo_mysql_stmt *S = stmt->driver_data;
     121         739 :         row_count = (zend_long) mysql_stmt_affected_rows(S->stmt);
     122         739 :         if (row_count != (zend_long)-1) {
     123         723 :                 stmt->row_count = row_count;
     124             :         }
     125         739 : }
     126             : /* }}} */
     127             : 
     128         790 : static int pdo_mysql_fill_stmt_from_result(pdo_stmt_t *stmt) /* {{{ */
     129             : {
     130         790 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     131         790 :         pdo_mysql_db_handle *H = S->H;
     132             :         my_ulonglong row_count;
     133         790 :         PDO_DBG_ENTER("pdo_mysql_fill_stmt_from_result");
     134             : 
     135         790 :         row_count = mysql_affected_rows(H->server);
     136         790 :         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         525 :                 if (!H->buffered) {
     140          12 :                         S->result = mysql_use_result(H->server);
     141             :                 } else {
     142         513 :                         S->result = mysql_store_result(H->server);
     143             :                 }
     144         525 :                 if (NULL == S->result) {
     145           0 :                         pdo_mysql_error_stmt(stmt);
     146           0 :                         PDO_DBG_RETURN(0);
     147             :                 }
     148             : 
     149         525 :                 stmt->row_count = (zend_long) mysql_num_rows(S->result);
     150         525 :                 stmt->column_count = (int) mysql_num_fields(S->result);
     151         525 :                 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 = (zend_long) row_count;
     155             :         }
     156             : 
     157         790 :         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) /* {{{ */
     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(zend_ulong));
     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);
     267             :         PDO_DBG_RETURN(1);
     268             : }
     269             : /* }}} */
     270             : #endif
     271             : 
     272             : #ifdef PDO_USE_MYSQLND
     273         744 : static int pdo_mysql_stmt_execute_prepared_mysqlnd(pdo_stmt_t *stmt) /* {{{ */
     274             : {
     275         744 :         pdo_mysql_stmt *S = stmt->driver_data;
     276         744 :         pdo_mysql_db_handle *H = S->H;
     277             :         int i;
     278             : 
     279         744 :         PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_mysqlnd");
     280             : 
     281         744 :         if (mysql_stmt_execute(S->stmt)) {
     282           5 :                 pdo_mysql_error_stmt(stmt);
     283           5 :                 PDO_DBG_RETURN(0);
     284             :         }
     285             : 
     286         739 :         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         739 :         stmt->column_count = mysql_stmt_field_count(S->stmt);
     294        1637 :         for (i = 0; i < stmt->column_count; i++) {
     295         898 :                 mysqlnd_stmt_bind_one_result(S->stmt, i);
     296             :         }
     297             : 
     298         739 :         S->result = mysqlnd_stmt_result_metadata(S->stmt);
     299         739 :         if (S->result) {
     300         446 :                 S->fields = mysql_fetch_fields(S->result);
     301             :                 /* if buffered, pre-fetch all the data */
     302         446 :                 if (H->buffered) {
     303         430 :                         if (mysql_stmt_store_result(S->stmt)) {
     304           0 :                                 PDO_DBG_RETURN(0);
     305             :                         }
     306             :                 }
     307             :         }
     308             : 
     309         739 :         pdo_mysql_stmt_set_row_count(stmt);
     310         739 :         PDO_DBG_RETURN(1);
     311             : }
     312             : /* }}} */
     313             : #endif
     314             : 
     315        1540 : static int pdo_mysql_stmt_execute(pdo_stmt_t *stmt) /* {{{ */
     316             : {
     317        1540 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     318        1540 :         pdo_mysql_db_handle *H = S->H;
     319        1540 :         PDO_DBG_ENTER("pdo_mysql_stmt_execute");
     320        1540 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     321             : 
     322        1540 :         if (S->stmt) {
     323         744 :                 PDO_DBG_RETURN(pdo_mysql_stmt_execute_prepared(stmt));
     324             :         }
     325             : 
     326             :         /* ensure that we free any previous unfetched results */
     327         796 :         if (S->result) {
     328          37 :                 mysql_free_result(S->result);
     329          37 :                 S->result = NULL;
     330             :         }
     331             : 
     332         796 :         if (mysql_real_query(H->server, stmt->active_query_string, stmt->active_query_stringlen) != 0) {
     333          28 :                 pdo_mysql_error_stmt(stmt);
     334          28 :                 PDO_DBG_RETURN(0);
     335             :         }
     336             : 
     337         768 :         PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt));
     338             : }
     339             : /* }}} */
     340             : 
     341          79 : static int pdo_mysql_stmt_next_rowset(pdo_stmt_t *stmt) /* {{{ */
     342             : {
     343          79 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     344          79 :         pdo_mysql_db_handle *H = S->H;
     345             : #if PDO_USE_MYSQLND
     346             :         zend_long row_count;
     347             : #endif
     348          79 :         PDO_DBG_ENTER("pdo_mysql_stmt_next_rowset");
     349          79 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     350             : 
     351             : #if PDO_USE_MYSQLND
     352          79 :         if (!H->emulate_prepare) {
     353          32 :                 if (!mysqlnd_stmt_more_results(S->stmt)) {
     354           8 :                         PDO_DBG_RETURN(0);
     355             :                 }
     356          24 :                 if (mysqlnd_stmt_next_result(S->stmt)) {
     357           0 :                         PDO_DBG_RETURN(0);
     358             :                 }
     359             : 
     360          24 :                 if (!mysqlnd_stmt_more_results(S->stmt)) {
     361             :                         /*
     362             :                         MySQL gives us n + 1 result sets for
     363             :                         CALL proc() and n result sets returned by the proc itself.
     364             :                         Result set n + 1 is about the procedure call itself.
     365             :                         As the PDO emulation does not return it, we skip it as well
     366             :                         */
     367          18 :                         PDO_DBG_RETURN(0);
     368             :                 }
     369             : 
     370             :                 /* TODO - this code is stolen from execute() - see above */
     371           6 :                 if (S->result) {
     372           6 :                         mysql_free_result(S->result);
     373           6 :                         S->result = NULL;
     374             :                 }
     375             :                 {
     376             :                         /* for SHOW/DESCRIBE and others the column/field count is not available before execute */
     377             :                         int i;
     378             : 
     379           6 :                         stmt->column_count = mysql_stmt_field_count(S->stmt);
     380          18 :                         for (i = 0; i < stmt->column_count; i++) {
     381          12 :                                 mysqlnd_stmt_bind_one_result(S->stmt, i);
     382             :                         }
     383             :                 }
     384             : 
     385           6 :                 S->result = mysqlnd_stmt_result_metadata(S->stmt);
     386           6 :                 if (S->result) {
     387           6 :                         S->fields = mysql_fetch_fields(S->result);
     388             : 
     389             :                         /* if buffered, pre-fetch all the data */
     390           6 :                         if (H->buffered) {
     391           3 :                                 if (mysql_stmt_store_result(S->stmt)) {
     392           0 :                                         PDO_DBG_RETURN(1);
     393             :                                 }
     394             :                         }
     395             :                 }
     396           6 :                 row_count = (zend_long) mysql_stmt_affected_rows(S->stmt);
     397           6 :                 if (row_count != (zend_long)-1) {
     398           3 :                         stmt->row_count = row_count;
     399             :                 }
     400           6 :                 PDO_DBG_RETURN(1);
     401             :         }
     402             : #endif
     403             : 
     404             : /* ensure that we free any previous unfetched results */
     405             : #ifndef PDO_USE_MYSQLND
     406             :         if (S->stmt) {
     407             :                 if (S->result) {
     408             :                         stmt->column_count = (int)mysql_num_fields(S->result);
     409             :                 }
     410             :                 mysql_stmt_free_result(S->stmt);
     411             :         }
     412             : #endif
     413          47 :         if (S->result) {
     414          25 :                 mysql_free_result(S->result);
     415          25 :                 S->result = NULL;
     416             :         }
     417             : 
     418          47 :         if (!mysql_more_results(H->server)) {
     419             :                 /* No more results */
     420          25 :                 PDO_DBG_RETURN(0);
     421             :         }
     422             : #if PDO_USE_MYSQLND
     423          22 :         if (mysql_next_result(H->server) == FAIL) {
     424           0 :                 pdo_mysql_error_stmt(stmt);
     425           0 :                 PDO_DBG_RETURN(0);
     426             :         } else {
     427          22 :                 PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt));
     428             :         }
     429             : #else
     430             :         if (mysql_next_result(H->server) > 0) {
     431             :                 pdo_mysql_error_stmt(stmt);
     432             :                 PDO_DBG_RETURN(0);
     433             :         } else {
     434             :                 PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt));
     435             :         }
     436             : #endif
     437             : }
     438             : /* }}} */
     439             : 
     440             : 
     441             : static const char * const pdo_param_event_names[] =
     442             : {
     443             :         "PDO_PARAM_EVT_ALLOC",
     444             :         "PDO_PARAM_EVT_FREE",
     445             :         "PDO_PARAM_EVT_EXEC_PRE",
     446             :         "PDO_PARAM_EVT_EXEC_POST",
     447             :         "PDO_PARAM_EVT_FETCH_PRE",
     448             :         "PDO_PARAM_EVT_FETCH_POST",
     449             :         "PDO_PARAM_EVT_NORMALIZE",
     450             : };
     451             : 
     452             : 
     453       36416 : static int pdo_mysql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, enum pdo_param_event event_type) /* {{{ */
     454             : {
     455             :         zval *parameter;
     456             : #ifndef PDO_USE_MYSQLND
     457             :         PDO_MYSQL_PARAM_BIND *b;
     458             : #endif
     459       36416 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     460             : 
     461       36416 :         PDO_DBG_ENTER("pdo_mysql_stmt_param_hook");
     462       36416 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     463       36416 :         PDO_DBG_INF_FMT("event = %s", pdo_param_event_names[event_type]);
     464       36416 :         if (S->stmt && param->is_param) {
     465        2447 :                 switch (event_type) {
     466             :                         case PDO_PARAM_EVT_ALLOC:
     467             :                                 /* sanity check parameter number range */
     468         579 :                                 if (param->paramno < 0 || param->paramno >= S->num_params) {
     469         100 :                                         strcpy(stmt->error_code, "HY093");
     470         100 :                                         PDO_DBG_RETURN(0);
     471             :                                 }
     472         479 :                                 S->params_given++;
     473             : 
     474             : #ifndef PDO_USE_MYSQLND
     475             :                                 b = &S->params[param->paramno];
     476             :                                 param->driver_data = b;
     477             :                                 b->is_null = &S->in_null[param->paramno];
     478             :                                 b->length = &S->in_length[param->paramno];
     479             :                                 /* recall how many parameters have been provided */
     480             : #endif
     481         479 :                                 PDO_DBG_RETURN(1);
     482             : 
     483             :                         case PDO_PARAM_EVT_EXEC_PRE:
     484         292 :                                 if (S->params_given < (unsigned int) S->num_params) {
     485             :                                         /* too few parameter bound */
     486           3 :                                         PDO_DBG_ERR("too few parameters bound");
     487           3 :                                         strcpy(stmt->error_code, "HY093");
     488           3 :                                         PDO_DBG_RETURN(0);
     489             :                                 }
     490             : 
     491         578 :                                 if (!Z_ISREF(param->parameter)) {
     492         208 :                                         parameter = &param->parameter;
     493             :                                 } else {
     494          81 :                                         parameter = Z_REFVAL(param->parameter);
     495             :                                 }
     496             : 
     497             : #if PDO_USE_MYSQLND
     498         578 :                                 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL || (Z_TYPE_P(parameter) == IS_NULL)) {
     499           4 :                                         mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_NULL);
     500           4 :                                         PDO_DBG_RETURN(1);
     501             :                                 }
     502             : #else
     503             :                                 b = (PDO_MYSQL_PARAM_BIND*)param->driver_data;
     504             :                                 *b->is_null = 0;
     505             :                                 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL || Z_TYPE_P(parameter) == IS_NULL) {
     506             :                                         *b->is_null = 1;
     507             :                                         b->buffer_type = MYSQL_TYPE_STRING;
     508             :                                         b->buffer = NULL;
     509             :                                         b->buffer_length = 0;
     510             :                                         *b->length = 0;
     511             :                                         PDO_DBG_RETURN(1);
     512             :                                 }
     513             : #endif /* PDO_USE_MYSQLND */
     514             : 
     515         285 :                                 switch (PDO_PARAM_TYPE(param->param_type)) {
     516             :                                         case PDO_PARAM_STMT:
     517           0 :                                                 PDO_DBG_RETURN(0);
     518             :                                         case PDO_PARAM_LOB:
     519           2 :                                                 PDO_DBG_INF("PDO_PARAM_LOB");
     520           4 :                                                 if (!Z_ISREF(param->parameter)) {
     521           0 :                                                         parameter = &param->parameter;
     522             :                                                 } else {
     523           2 :                                                         parameter = Z_REFVAL(param->parameter);
     524             :                                                 }
     525           2 :                                                 if (Z_TYPE_P(parameter) == IS_RESOURCE) {
     526           2 :                                                         php_stream *stm = NULL;
     527           2 :                                                         php_stream_from_zval_no_verify(stm, parameter);
     528           2 :                                                         if (stm) {
     529           2 :                                                                 zend_string *mem = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
     530           2 :                                                                 zval_ptr_dtor(parameter);
     531           2 :                                                                 ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC());
     532             :                                                         } else {
     533           0 :                                                                 pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource");
     534           0 :                                                                 return 0;
     535             :                                                         }
     536             :                                                 }
     537             :                                                 /* fall through */
     538             : 
     539             :                                         default:
     540             :                                                 ;
     541             :                                 }
     542             : 
     543             : #if PDO_USE_MYSQLND
     544             :                                 /* Is it really correct to check the zval's type? - But well, that's what the old code below does, too */
     545         570 :                                 PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
     546         570 :                                 if (!Z_ISREF(param->parameter)) {
     547         206 :                                         parameter = &param->parameter;
     548             :                                 } else {
     549          79 :                                         parameter = Z_REFVAL(param->parameter);
     550             :                                 }
     551         285 :                                 switch (Z_TYPE_P(parameter)) {
     552             :                                         case IS_STRING:
     553         278 :                                                 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_VAR_STRING);
     554         278 :                                                 break;
     555             :                                         case IS_LONG:
     556             : #if SIZEOF_ZEND_LONG==8
     557           6 :                                                 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_LONGLONG);
     558             : #elif SIZEOF_ZEND_LONG==4
     559             :                                                 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_LONG);
     560             : #endif /* SIZEOF_LONG */
     561           6 :                                                 break;
     562             :                                         case IS_DOUBLE:
     563           0 :                                                 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_DOUBLE);
     564           0 :                                                 break;
     565             :                                         default:
     566           1 :                                                 PDO_DBG_RETURN(0);
     567             :                                 }
     568             : 
     569         284 :                                 PDO_DBG_RETURN(1);
     570             : #else
     571             :                                 PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
     572             :                                 if (!Z_ISREF(param->parameter)) {
     573             :                                         parameter = &param->parameter;
     574             :                                 } else {
     575             :                                         parameter = Z_REFVAL(param->parameter);
     576             :                                 }
     577             :                                 switch (Z_TYPE_P(parameter)) {
     578             :                                         case IS_STRING:
     579             :                                                 b->buffer_type = MYSQL_TYPE_STRING;
     580             :                                                 b->buffer = Z_STRVAL_P(parameter);
     581             :                                                 b->buffer_length = Z_STRLEN_P(parameter);
     582             :                                                 *b->length = Z_STRLEN_P(parameter);
     583             :                                                 PDO_DBG_RETURN(1);
     584             : 
     585             :                                         case IS_LONG:
     586             :                                                 b->buffer_type = MYSQL_TYPE_LONG;
     587             :                                                 b->buffer = &Z_LVAL_P(parameter);
     588             :                                                 PDO_DBG_RETURN(1);
     589             : 
     590             :                                         case IS_DOUBLE:
     591             :                                                 b->buffer_type = MYSQL_TYPE_DOUBLE;
     592             :                                                 b->buffer = &Z_DVAL_P(parameter);
     593             :                                                 PDO_DBG_RETURN(1);
     594             : 
     595             :                                         default:
     596             :                                                 PDO_DBG_RETURN(0);
     597             :                                 }
     598             : #endif /* PDO_USE_MYSQLND */
     599             :                 case PDO_PARAM_EVT_FREE:
     600             :                 case PDO_PARAM_EVT_EXEC_POST:
     601             :                 case PDO_PARAM_EVT_FETCH_PRE:
     602             :                 case PDO_PARAM_EVT_FETCH_POST:
     603             :                 case PDO_PARAM_EVT_NORMALIZE:
     604             :                         /* do nothing */
     605             :                         break;
     606             :                 }
     607             :         }
     608             : 
     609       35545 :         PDO_DBG_RETURN(1);
     610             : }
     611             : /* }}} */
     612             : 
     613        1497 : static int pdo_mysql_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, zend_long offset) /* {{{ */
     614             : {
     615        1497 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     616             : #if PDO_USE_MYSQLND
     617             :         zend_bool fetched_anything;
     618             : 
     619        1497 :         PDO_DBG_ENTER("pdo_mysql_stmt_fetch");
     620        1497 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     621        1497 :         if (S->stmt) {
     622         552 :                 if (FAIL == mysqlnd_stmt_fetch(S->stmt, &fetched_anything) || fetched_anything == FALSE) {
     623          74 :                         PDO_DBG_RETURN(0);
     624             :                 }
     625             : 
     626         478 :                 PDO_DBG_RETURN(1);
     627             :         }
     628             : #else
     629             :         int ret;
     630             : 
     631             :         if (S->stmt) {
     632             :                 ret = mysql_stmt_fetch(S->stmt);
     633             : 
     634             : #               ifdef MYSQL_DATA_TRUNCATED
     635             :                 if (ret == MYSQL_DATA_TRUNCATED) {
     636             :                         ret = 0;
     637             :                 }
     638             : #               endif
     639             : 
     640             :                 if (ret) {
     641             :                         if (ret != MYSQL_NO_DATA) {
     642             :                                 pdo_mysql_error_stmt(stmt);
     643             :                         }
     644             :                         PDO_DBG_RETURN(0);
     645             :                 }
     646             : 
     647             :                 PDO_DBG_RETURN(1);
     648             :         }
     649             : #endif /* PDO_USE_MYSQLND */
     650             : 
     651         945 :         if (!S->result) {
     652          13 :                 strcpy(stmt->error_code, "HY000");
     653          13 :                 PDO_DBG_RETURN(0);
     654             :         }
     655             : #if PDO_USE_MYSQLND
     656         932 :         if (!S->stmt && S->current_data) {
     657         544 :                 mnd_free(S->current_data);
     658             :         }
     659             : #endif /* PDO_USE_MYSQLND */
     660             : 
     661         932 :         if ((S->current_data = mysql_fetch_row(S->result)) == NULL) {
     662             : #if PDO_USE_MYSQLND
     663         187 :                 if (S->result->unbuf && !S->result->unbuf->eof_reached && mysql_errno(S->H->server)) {
     664             : #else
     665             :                 if (!S->result->eof && mysql_errno(S->H->server)) {
     666             : #endif
     667           0 :                         pdo_mysql_error_stmt(stmt);
     668             :                 }
     669         187 :                 PDO_DBG_RETURN(0);
     670             :         }
     671             : 
     672         745 :         S->current_lengths = mysql_fetch_lengths(S->result);
     673         745 :         PDO_DBG_RETURN(1);
     674             : }
     675             : /* }}} */
     676             : 
     677        1463 : static int pdo_mysql_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
     678             : {
     679        1463 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     680        1463 :         struct pdo_column_data *cols = stmt->columns;
     681             :         int i;
     682             : 
     683        1463 :         PDO_DBG_ENTER("pdo_mysql_stmt_describe");
     684        1463 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     685        1463 :         if (!S->result) {
     686           0 :                 PDO_DBG_RETURN(0);
     687             :         }
     688             : 
     689        1463 :         if (colno >= stmt->column_count) {
     690             :                 /* error invalid column */
     691           0 :                 PDO_DBG_RETURN(0);
     692             :         }
     693             : 
     694             :         /* fetch all on demand, this seems easiest
     695             :         ** if we've been here before bail out
     696             :         */
     697        1463 :         if (cols[0].name) {
     698         672 :                 PDO_DBG_RETURN(1);
     699             :         }
     700        2254 :         for (i = 0; i < stmt->column_count; i++) {
     701             : 
     702        1463 :                 if (S->H->fetch_table_names) {
     703           1 :                         cols[i].name = strpprintf(0, "%s.%s", S->fields[i].table, S->fields[i].name);
     704             :                 } else {
     705        2924 :                         cols[i].name = zend_string_init(S->fields[i].name, S->fields[i].name_length, 0);
     706             :                 }
     707             : 
     708        1463 :                 cols[i].precision = S->fields[i].decimals;
     709        1463 :                 cols[i].maxlen = S->fields[i].length;
     710             : 
     711             : #ifdef PDO_USE_MYSQLND
     712        1463 :                 if (S->stmt) {
     713         747 :                         cols[i].param_type = PDO_PARAM_ZVAL;
     714             :                 } else
     715             : #endif
     716             :                 {
     717         716 :                         cols[i].param_type = PDO_PARAM_STR;
     718             :                 }
     719             :         }
     720         791 :         PDO_DBG_RETURN(1);
     721             : }
     722             : /* }}} */
     723             : 
     724        2602 : static int pdo_mysql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, size_t *len, int *caller_frees) /* {{{ */
     725             : {
     726        2602 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     727             : 
     728        2602 :         PDO_DBG_ENTER("pdo_mysql_stmt_get_col");
     729        2602 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     730        2602 :         if (!S->result) {
     731           0 :                 PDO_DBG_RETURN(0);
     732             :         }
     733             : 
     734             :         /* With mysqlnd data is stored inside mysqlnd, not S->current_data */
     735        2602 :         if (!S->stmt) {
     736        1541 :                 if (S->current_data == NULL || !S->result) {
     737           0 :                         PDO_DBG_RETURN(0);
     738             :                 }
     739             :         }
     740             : 
     741        2602 :         if (colno >= stmt->column_count) {
     742             :                 /* error invalid column */
     743           0 :                 PDO_DBG_RETURN(0);
     744             :         }
     745             : #if PDO_USE_MYSQLND
     746        2602 :         if (S->stmt) {
     747        1061 :                 Z_TRY_ADDREF(S->stmt->data->result_bind[colno].zv);
     748        1061 :                 *ptr = (char*)&S->stmt->data->result_bind[colno].zv;
     749        1061 :                 *len = sizeof(zval);
     750        1061 :                 PDO_DBG_RETURN(1);
     751             :         }
     752             : #else
     753             :         if (S->stmt) {
     754             :                 if (S->out_null[colno]) {
     755             :                         *ptr = NULL;
     756             :                         *len = 0;
     757             :                         PDO_DBG_RETURN(1);
     758             :                 }
     759             :                 *ptr = S->bound_result[colno].buffer;
     760             :                 if (S->out_length[colno] > S->bound_result[colno].buffer_length) {
     761             :                         /* mysql lied about the column width */
     762             :                         strcpy(stmt->error_code, "01004"); /* truncated */
     763             :                         S->out_length[colno] = S->bound_result[colno].buffer_length;
     764             :                         *len = S->out_length[colno];
     765             :                         PDO_DBG_RETURN(0);
     766             :                 }
     767             :                 *len = S->out_length[colno];
     768             :                 PDO_DBG_RETURN(1);
     769             :         }
     770             : #endif
     771        1541 :         *ptr = S->current_data[colno];
     772        1541 :         *len = S->current_lengths[colno];
     773        1541 :         PDO_DBG_RETURN(1);
     774             : } /* }}} */
     775             : 
     776          70 : static char *type_to_name_native(int type) /* {{{ */
     777             : {
     778             : #define PDO_MYSQL_NATIVE_TYPE_NAME(x)   case FIELD_TYPE_##x: return #x;
     779             : 
     780          70 :     switch (type) {
     781           7 :         PDO_MYSQL_NATIVE_TYPE_NAME(STRING)
     782           5 :         PDO_MYSQL_NATIVE_TYPE_NAME(VAR_STRING)
     783             : #ifdef FIELD_TYPE_TINY
     784           3 :         PDO_MYSQL_NATIVE_TYPE_NAME(TINY)
     785             : #endif
     786             : #ifdef FIELD_TYPE_BIT
     787           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(BIT)
     788             : #endif
     789           2 :         PDO_MYSQL_NATIVE_TYPE_NAME(SHORT)
     790           8 :         PDO_MYSQL_NATIVE_TYPE_NAME(LONG)
     791           4 :         PDO_MYSQL_NATIVE_TYPE_NAME(LONGLONG)
     792           2 :         PDO_MYSQL_NATIVE_TYPE_NAME(INT24)
     793           4 :         PDO_MYSQL_NATIVE_TYPE_NAME(FLOAT)
     794           8 :         PDO_MYSQL_NATIVE_TYPE_NAME(DOUBLE)
     795           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(DECIMAL)
     796             : #ifdef FIELD_TYPE_NEWDECIMAL
     797           8 :         PDO_MYSQL_NATIVE_TYPE_NAME(NEWDECIMAL)
     798             : #endif
     799             : #ifdef FIELD_TYPE_GEOMETRY
     800           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(GEOMETRY)
     801             : #endif
     802           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(TIMESTAMP)
     803             : #ifdef FIELD_TYPE_YEAR
     804           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(YEAR)
     805             : #endif
     806           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(SET)
     807           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(ENUM)
     808           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(DATE)
     809             : #ifdef FIELD_TYPE_NEWDATE
     810           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(NEWDATE)
     811             : #endif
     812           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(TIME)
     813           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(DATETIME)
     814           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(TINY_BLOB)
     815           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(MEDIUM_BLOB)
     816           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(LONG_BLOB)
     817          12 :         PDO_MYSQL_NATIVE_TYPE_NAME(BLOB)
     818           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(NULL)
     819             :         default:
     820           0 :             return NULL;
     821             :     }
     822             : #undef PDO_MYSQL_NATIVE_TYPE_NAME
     823             : } /* }}} */
     824             : 
     825          73 : static int pdo_mysql_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value) /* {{{ */
     826             : {
     827          73 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     828             :         const MYSQL_FIELD *F;
     829             :         zval flags;
     830             :         char *str;
     831             : 
     832          73 :         PDO_DBG_ENTER("pdo_mysql_stmt_col_meta");
     833          73 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     834          73 :         if (!S->result) {
     835           2 :                 PDO_DBG_RETURN(FAILURE);
     836             :         }
     837          71 :         if (colno >= stmt->column_count) {
     838             :                 /* error invalid column */
     839           1 :                 PDO_DBG_RETURN(FAILURE);
     840             :         }
     841             : 
     842          70 :         array_init(return_value);
     843          70 :         array_init(&flags);
     844             : 
     845          70 :         F = S->fields + colno;
     846             : 
     847          70 :         if (F->def) {
     848           0 :                 add_assoc_string(return_value, "mysql:def", F->def);
     849             :         }
     850          70 :         if (IS_NOT_NULL(F->flags)) {
     851           7 :                 add_next_index_string(&flags, "not_null");
     852             :         }
     853          70 :         if (IS_PRI_KEY(F->flags)) {
     854           3 :                 add_next_index_string(&flags, "primary_key");
     855             :         }
     856          70 :         if (F->flags & MULTIPLE_KEY_FLAG) {
     857           1 :                 add_next_index_string(&flags, "multiple_key");
     858             :         }
     859          70 :         if (F->flags & UNIQUE_KEY_FLAG) {
     860           1 :                 add_next_index_string(&flags, "unique_key");
     861             :         }
     862          70 :         if (IS_BLOB(F->flags)) {
     863          12 :                 add_next_index_string(&flags, "blob");
     864             :         }
     865          70 :         str = type_to_name_native(F->type);
     866          70 :         if (str) {
     867          70 :                 add_assoc_string(return_value, "native_type", str);
     868             :         }
     869             : 
     870             : #ifdef PDO_USE_MYSQLND
     871          70 :         switch (F->type) {
     872             :                 case MYSQL_TYPE_BIT:
     873             :                 case MYSQL_TYPE_YEAR:
     874             :                 case MYSQL_TYPE_TINY:
     875             :                 case MYSQL_TYPE_SHORT:
     876             :                 case MYSQL_TYPE_INT24:
     877             :                 case MYSQL_TYPE_LONG:
     878             : #if SIZEOF_LONG==8
     879             :                 case MYSQL_TYPE_LONGLONG:
     880             : #endif
     881          21 :                         add_assoc_long(return_value, "pdo_type", PDO_PARAM_INT);
     882          21 :                         break;
     883             :                 default:
     884          49 :                         add_assoc_long(return_value, "pdo_type", PDO_PARAM_STR);
     885             :                         break;
     886             :         }
     887             : #endif
     888             : 
     889          70 :         add_assoc_zval(return_value, "flags", &flags);
     890          70 :         add_assoc_string(return_value, "table", (char *) (F->table?F->table : ""));
     891             : 
     892          70 :         PDO_DBG_RETURN(SUCCESS);
     893             : } /* }}} */
     894             : 
     895         320 : static int pdo_mysql_stmt_cursor_closer(pdo_stmt_t *stmt) /* {{{ */
     896             : {
     897         320 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     898             : 
     899         320 :         PDO_DBG_ENTER("pdo_mysql_stmt_cursor_closer");
     900         320 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     901         320 :         if (S->result) {
     902         311 :                 mysql_free_result(S->result);
     903         311 :                 S->result = NULL;
     904             :         }
     905         320 :         if (S->stmt) {
     906             :                 int retval;
     907         211 :                 retval = mysql_stmt_free_result(S->stmt);
     908         211 :                 PDO_DBG_RETURN(retval ? 0 : 1);
     909             :         }
     910             : 
     911         221 :         while (mysql_more_results(S->H->server)) {
     912             :                 MYSQL_RES *res;
     913           3 :                 if (mysql_next_result(S->H->server) != 0) {
     914           0 :                         break;
     915             :                 }
     916           3 :                 res = mysql_store_result(S->H->server);
     917           3 :                 if (res) {
     918           0 :                         mysql_free_result(res);
     919             :                 }
     920             :         }
     921         109 :         PDO_DBG_RETURN(1);
     922             : }
     923             : /* }}} */
     924             : 
     925             : struct pdo_stmt_methods mysql_stmt_methods = {
     926             :         pdo_mysql_stmt_dtor,
     927             :         pdo_mysql_stmt_execute,
     928             :         pdo_mysql_stmt_fetch,
     929             :         pdo_mysql_stmt_describe,
     930             :         pdo_mysql_stmt_get_col,
     931             :         pdo_mysql_stmt_param_hook,
     932             :         NULL, /* set_attr */
     933             :         NULL, /* get_attr */
     934             :         pdo_mysql_stmt_col_meta,
     935             :         pdo_mysql_stmt_next_rowset,
     936             :         pdo_mysql_stmt_cursor_closer
     937             : };
     938             : 
     939             : /*
     940             :  * Local variables:
     941             :  * tab-width: 4
     942             :  * c-basic-offset: 4
     943             :  * End:
     944             :  * vim600: noet sw=4 ts=4 fdm=marker
     945             :  * vim<600: noet sw=4 ts=4
     946             :  */

Generated by: LCOV version 1.10

Generated at Wed, 20 Jul 2016 02:56:24 +0000 (3 days ago)

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