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: 291 327 89.0 %
Date: 2022-01-21 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-2018 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        1765 : static int pdo_mysql_stmt_dtor(pdo_stmt_t *stmt) /* {{{ */
      48             : {
      49        1765 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
      50             : 
      51        1765 :         PDO_DBG_ENTER("pdo_mysql_stmt_dtor");
      52        1765 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
      53        1765 :         if (S->result) {
      54             :                 /* free the resource */
      55         602 :                 mysql_free_result(S->result);
      56         602 :                 S->result = NULL;
      57             :         }
      58        1765 :         if (S->einfo.errmsg) {
      59          36 :                 pefree(S->einfo.errmsg, stmt->dbh->is_persistent);
      60          36 :                 S->einfo.errmsg = NULL;
      61             :         }
      62        1765 :         if (S->stmt) {
      63         894 :                 pdo_mysql_stmt_close(S->stmt);
      64         894 :                 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        3530 :         if (!Z_ISUNDEF(stmt->database_object_handle)
      92        1741 :                 && IS_OBJ_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE(stmt->database_object_handle)])
      93        1741 :                 && (!(GC_FLAGS(Z_OBJ(stmt->database_object_handle)) & IS_OBJ_FREE_CALLED))) {
      94        3482 :                 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        1765 :         if (!S->stmt && S->current_data) {
     109         206 :                 mnd_free(S->current_data);
     110             :         }
     111             : #endif /* PDO_USE_MYSQLND */
     112             : 
     113        1765 :         efree(S);
     114        1765 :         PDO_DBG_RETURN(1);
     115             : }
     116             : /* }}} */
     117             : 
     118         754 : static void pdo_mysql_stmt_set_row_count(pdo_stmt_t *stmt) /* {{{ */
     119             : {
     120             :         zend_long row_count;
     121         754 :         pdo_mysql_stmt *S = stmt->driver_data;
     122         754 :         row_count = (zend_long) mysql_stmt_affected_rows(S->stmt);
     123         754 :         if (row_count != (zend_long)-1) {
     124         737 :                 stmt->row_count = row_count;
     125             :         }
     126         754 : }
     127             : /* }}} */
     128             : 
     129         809 : static int pdo_mysql_fill_stmt_from_result(pdo_stmt_t *stmt) /* {{{ */
     130             : {
     131         809 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     132         809 :         pdo_mysql_db_handle *H = S->H;
     133             :         my_ulonglong row_count;
     134         809 :         PDO_DBG_ENTER("pdo_mysql_fill_stmt_from_result");
     135             : 
     136         809 :         row_count = mysql_affected_rows(H->server);
     137         809 :         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         537 :                 if (!H->buffered) {
     141          12 :                         S->result = mysql_use_result(H->server);
     142             :                 } else {
     143         525 :                         S->result = mysql_store_result(H->server);
     144             :                 }
     145         537 :                 if (NULL == S->result) {
     146           0 :                         pdo_mysql_error_stmt(stmt);
     147           0 :                         PDO_DBG_RETURN(0);
     148             :                 }
     149             : 
     150         537 :                 stmt->row_count = (zend_long) mysql_num_rows(S->result);
     151         537 :                 stmt->column_count = (int) mysql_num_fields(S->result);
     152         537 :                 S->fields = mysql_fetch_fields(S->result);
     153             :         } else {
     154             :                 /* this was a DML or DDL query (INSERT, UPDATE, DELETE, ... */
     155         272 :                 stmt->row_count = (zend_long) row_count;
     156             :         }
     157             : 
     158         809 :         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         761 : static int pdo_mysql_stmt_execute_prepared_mysqlnd(pdo_stmt_t *stmt) /* {{{ */
     275             : {
     276         761 :         pdo_mysql_stmt *S = stmt->driver_data;
     277         761 :         pdo_mysql_db_handle *H = S->H;
     278             :         int i;
     279             : 
     280         761 :         PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_mysqlnd");
     281             : 
     282         761 :         if (mysql_stmt_execute(S->stmt)) {
     283           7 :                 pdo_mysql_error_stmt(stmt);
     284           7 :                 PDO_DBG_RETURN(0);
     285             :         }
     286             : 
     287         754 :         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         754 :         stmt->column_count = mysql_stmt_field_count(S->stmt);
     295        1679 :         for (i = 0; i < stmt->column_count; i++) {
     296         925 :                 mysqlnd_stmt_bind_one_result(S->stmt, i);
     297             :         }
     298             : 
     299         754 :         S->result = mysqlnd_stmt_result_metadata(S->stmt);
     300         754 :         if (S->result) {
     301         455 :                 S->fields = mysql_fetch_fields(S->result);
     302             :                 /* if buffered, pre-fetch all the data */
     303         455 :                 if (H->buffered) {
     304         439 :                         if (mysql_stmt_store_result(S->stmt)) {
     305           0 :                                 PDO_DBG_RETURN(0);
     306             :                         }
     307             :                 }
     308             :         }
     309             : 
     310         754 :         pdo_mysql_stmt_set_row_count(stmt);
     311         754 :         PDO_DBG_RETURN(1);
     312             : }
     313             : /* }}} */
     314             : #endif
     315             : 
     316        1578 : static int pdo_mysql_stmt_execute(pdo_stmt_t *stmt) /* {{{ */
     317             : {
     318        1578 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     319        1578 :         pdo_mysql_db_handle *H = S->H;
     320        1578 :         PDO_DBG_ENTER("pdo_mysql_stmt_execute");
     321        1578 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     322             : 
     323        1578 :         if (S->stmt) {
     324         761 :                 PDO_DBG_RETURN(pdo_mysql_stmt_execute_prepared(stmt));
     325             :         }
     326             : 
     327             :         /* ensure that we free any previous unfetched results */
     328         817 :         if (S->result) {
     329          39 :                 mysql_free_result(S->result);
     330          39 :                 S->result = NULL;
     331             :         }
     332             : 
     333         817 :         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         787 :         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       36559 : 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       36559 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     461             : 
     462       36559 :         PDO_DBG_ENTER("pdo_mysql_stmt_param_hook");
     463       36559 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     464       36559 :         PDO_DBG_INF_FMT("event = %s", pdo_param_event_names[event_type]);
     465       36559 :         if (S->stmt && param->is_param) {
     466        2566 :                 switch (event_type) {
     467         604 :                         case PDO_PARAM_EVT_ALLOC:
     468             :                                 /* sanity check parameter number range */
     469         604 :                                 if (param->paramno < 0 || param->paramno >= S->num_params) {
     470         100 :                                         strcpy(stmt->error_code, "HY093");
     471         100 :                                         PDO_DBG_RETURN(0);
     472             :                                 }
     473         504 :                                 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         504 :                                 PDO_DBG_RETURN(1);
     483             : 
     484         317 :                         case PDO_PARAM_EVT_EXEC_PRE:
     485         317 :                                 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         628 :                                 if (!Z_ISREF(param->parameter)) {
     493         209 :                                         parameter = &param->parameter;
     494             :                                 } else {
     495         105 :                                         parameter = Z_REFVAL(param->parameter);
     496             :                                 }
     497             : 
     498             : #if PDO_USE_MYSQLND
     499         628 :                                 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL || (Z_TYPE_P(parameter) == IS_NULL)) {
     500           5 :                                         mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_NULL);
     501           5 :                                         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         309 :                                 switch (PDO_PARAM_TYPE(param->param_type)) {
     517           0 :                                         case PDO_PARAM_STMT:
     518           0 :                                                 PDO_DBG_RETURN(0);
     519           2 :                                         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         618 :                                 PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
     547         618 :                                 if (!Z_ISREF(param->parameter)) {
     548         207 :                                         parameter = &param->parameter;
     549             :                                 } else {
     550         102 :                                         parameter = Z_REFVAL(param->parameter);
     551             :                                 }
     552         309 :                                 switch (Z_TYPE_P(parameter)) {
     553         281 :                                         case IS_STRING:
     554         281 :                                                 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_VAR_STRING);
     555         281 :                                                 break;
     556          18 :                                         case IS_LONG:
     557             : #if SIZEOF_ZEND_LONG==8
     558          18 :                                                 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          18 :                                                 break;
     563          10 :                                         case IS_TRUE:
     564             :                                         case IS_FALSE:
     565          10 :                                                 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_TINY);
     566          10 :                                                 break;
     567           0 :                                         case IS_DOUBLE:
     568           0 :                                                 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_DOUBLE);
     569           0 :                                                 break;
     570           0 :                                         default:
     571           0 :                                                 PDO_DBG_RETURN(0);
     572             :                                 }
     573             : 
     574         309 :                                 PDO_DBG_RETURN(1);
     575             : #else
     576             :                                 PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
     577             :                                 if (!Z_ISREF(param->parameter)) {
     578             :                                         parameter = &param->parameter;
     579             :                                 } else {
     580             :                                         parameter = Z_REFVAL(param->parameter);
     581             :                                 }
     582             :                                 switch (Z_TYPE_P(parameter)) {
     583             :                                         case IS_STRING:
     584             :                                                 b->buffer_type = MYSQL_TYPE_STRING;
     585             :                                                 b->buffer = Z_STRVAL_P(parameter);
     586             :                                                 b->buffer_length = Z_STRLEN_P(parameter);
     587             :                                                 *b->length = Z_STRLEN_P(parameter);
     588             :                                                 PDO_DBG_RETURN(1);
     589             : 
     590             :                                         case IS_LONG:
     591             :                                                 b->buffer_type = MYSQL_TYPE_LONG;
     592             :                                                 b->buffer = &Z_LVAL_P(parameter);
     593             :                                                 PDO_DBG_RETURN(1);
     594             : 
     595             :                                         case IS_DOUBLE:
     596             :                                                 b->buffer_type = MYSQL_TYPE_DOUBLE;
     597             :                                                 b->buffer = &Z_DVAL_P(parameter);
     598             :                                                 PDO_DBG_RETURN(1);
     599             : 
     600             :                                         default:
     601             :                                                 PDO_DBG_RETURN(0);
     602             :                                 }
     603             : #endif /* PDO_USE_MYSQLND */
     604        1645 :                 case PDO_PARAM_EVT_FREE:
     605             :                 case PDO_PARAM_EVT_EXEC_POST:
     606             :                 case PDO_PARAM_EVT_FETCH_PRE:
     607             :                 case PDO_PARAM_EVT_FETCH_POST:
     608             :                 case PDO_PARAM_EVT_NORMALIZE:
     609             :                         /* do nothing */
     610        1645 :                         break;
     611             :                 }
     612             :         }
     613             : 
     614       35638 :         PDO_DBG_RETURN(1);
     615             : }
     616             : /* }}} */
     617             : 
     618        1527 : static int pdo_mysql_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, zend_long offset) /* {{{ */
     619             : {
     620        1527 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     621             : #if PDO_USE_MYSQLND
     622             :         zend_bool fetched_anything;
     623             : 
     624        1527 :         PDO_DBG_ENTER("pdo_mysql_stmt_fetch");
     625        1527 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     626        1527 :         if (S->stmt) {
     627         568 :                 if (FAIL == mysqlnd_stmt_fetch(S->stmt, &fetched_anything) || fetched_anything == FALSE) {
     628          81 :                         PDO_DBG_RETURN(0);
     629             :                 }
     630             : 
     631         487 :                 PDO_DBG_RETURN(1);
     632             :         }
     633             : #else
     634             :         int ret;
     635             : 
     636             :         if (S->stmt) {
     637             :                 ret = mysql_stmt_fetch(S->stmt);
     638             : 
     639             : #               ifdef MYSQL_DATA_TRUNCATED
     640             :                 if (ret == MYSQL_DATA_TRUNCATED) {
     641             :                         ret = 0;
     642             :                 }
     643             : #               endif
     644             : 
     645             :                 if (ret) {
     646             :                         if (ret != MYSQL_NO_DATA) {
     647             :                                 pdo_mysql_error_stmt(stmt);
     648             :                         }
     649             :                         PDO_DBG_RETURN(0);
     650             :                 }
     651             : 
     652             :                 PDO_DBG_RETURN(1);
     653             :         }
     654             : #endif /* PDO_USE_MYSQLND */
     655             : 
     656         959 :         if (!S->result) {
     657          13 :                 strcpy(stmt->error_code, "HY000");
     658          13 :                 PDO_DBG_RETURN(0);
     659             :         }
     660             : #if PDO_USE_MYSQLND
     661         946 :         if (!S->stmt && S->current_data) {
     662         551 :                 mnd_free(S->current_data);
     663             :         }
     664             : #endif /* PDO_USE_MYSQLND */
     665             : 
     666         946 :         if ((S->current_data = mysql_fetch_row(S->result)) == NULL) {
     667             : #if PDO_USE_MYSQLND
     668         189 :                 if (S->result->unbuf && !S->result->unbuf->eof_reached && mysql_errno(S->H->server)) {
     669             : #else
     670             :                 if (!S->result->eof && mysql_errno(S->H->server)) {
     671             : #endif
     672           0 :                         pdo_mysql_error_stmt(stmt);
     673             :                 }
     674         189 :                 PDO_DBG_RETURN(0);
     675             :         }
     676             : 
     677         757 :         S->current_lengths = mysql_fetch_lengths(S->result);
     678         757 :         PDO_DBG_RETURN(1);
     679             : }
     680             : /* }}} */
     681             : 
     682        1507 : static int pdo_mysql_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
     683             : {
     684        1507 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     685        1507 :         struct pdo_column_data *cols = stmt->columns;
     686             :         int i;
     687             : 
     688        1507 :         PDO_DBG_ENTER("pdo_mysql_stmt_describe");
     689        1507 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     690        1507 :         if (!S->result) {
     691           0 :                 PDO_DBG_RETURN(0);
     692             :         }
     693             : 
     694        1507 :         if (colno >= stmt->column_count) {
     695             :                 /* error invalid column */
     696           0 :                 PDO_DBG_RETURN(0);
     697             :         }
     698             : 
     699             :         /* fetch all on demand, this seems easiest
     700             :         ** if we've been here before bail out
     701             :         */
     702        1507 :         if (cols[0].name) {
     703         697 :                 PDO_DBG_RETURN(1);
     704             :         }
     705        2317 :         for (i = 0; i < stmt->column_count; i++) {
     706             : 
     707        1507 :                 if (S->H->fetch_table_names) {
     708           1 :                         cols[i].name = strpprintf(0, "%s.%s", S->fields[i].table, S->fields[i].name);
     709             :                 } else {
     710        3012 :                         cols[i].name = zend_string_init(S->fields[i].name, S->fields[i].name_length, 0);
     711             :                 }
     712             : 
     713        1507 :                 cols[i].precision = S->fields[i].decimals;
     714        1507 :                 cols[i].maxlen = S->fields[i].length;
     715             : 
     716             : #ifdef PDO_USE_MYSQLND
     717        1507 :                 if (S->stmt) {
     718         774 :                         cols[i].param_type = PDO_PARAM_ZVAL;
     719             :                 } else
     720             : #endif
     721             :                 {
     722         733 :                         cols[i].param_type = PDO_PARAM_STR;
     723             :                 }
     724             :         }
     725         810 :         PDO_DBG_RETURN(1);
     726             : }
     727             : /* }}} */
     728             : 
     729        2648 : static int pdo_mysql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, size_t *len, int *caller_frees) /* {{{ */
     730             : {
     731        2648 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     732             : 
     733        2648 :         PDO_DBG_ENTER("pdo_mysql_stmt_get_col");
     734        2648 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     735        2648 :         if (!S->result) {
     736           0 :                 PDO_DBG_RETURN(0);
     737             :         }
     738             : 
     739             :         /* With mysqlnd data is stored inside mysqlnd, not S->current_data */
     740        2648 :         if (!S->stmt) {
     741        1558 :                 if (S->current_data == NULL || !S->result) {
     742           0 :                         PDO_DBG_RETURN(0);
     743             :                 }
     744             :         }
     745             : 
     746        2648 :         if (colno >= stmt->column_count) {
     747             :                 /* error invalid column */
     748           0 :                 PDO_DBG_RETURN(0);
     749             :         }
     750             : #if PDO_USE_MYSQLND
     751        2648 :         if (S->stmt) {
     752        1090 :                 Z_TRY_ADDREF(S->stmt->data->result_bind[colno].zv);
     753        1090 :                 *ptr = (char*)&S->stmt->data->result_bind[colno].zv;
     754        1090 :                 *len = sizeof(zval);
     755        1090 :                 PDO_DBG_RETURN(1);
     756             :         }
     757             : #else
     758             :         if (S->stmt) {
     759             :                 if (S->out_null[colno]) {
     760             :                         *ptr = NULL;
     761             :                         *len = 0;
     762             :                         PDO_DBG_RETURN(1);
     763             :                 }
     764             :                 *ptr = S->bound_result[colno].buffer;
     765             :                 if (S->out_length[colno] > S->bound_result[colno].buffer_length) {
     766             :                         /* mysql lied about the column width */
     767             :                         strcpy(stmt->error_code, "01004"); /* truncated */
     768             :                         S->out_length[colno] = S->bound_result[colno].buffer_length;
     769             :                         *len = S->out_length[colno];
     770             :                         PDO_DBG_RETURN(0);
     771             :                 }
     772             :                 *len = S->out_length[colno];
     773             :                 PDO_DBG_RETURN(1);
     774             :         }
     775             : #endif
     776        1558 :         *ptr = S->current_data[colno];
     777        1558 :         *len = S->current_lengths[colno];
     778        1558 :         PDO_DBG_RETURN(1);
     779             : } /* }}} */
     780             : 
     781          70 : static char *type_to_name_native(int type) /* {{{ */
     782             : {
     783             : #define PDO_MYSQL_NATIVE_TYPE_NAME(x)   case FIELD_TYPE_##x: return #x;
     784             : 
     785          70 :     switch (type) {
     786           7 :         PDO_MYSQL_NATIVE_TYPE_NAME(STRING)
     787           5 :         PDO_MYSQL_NATIVE_TYPE_NAME(VAR_STRING)
     788             : #ifdef FIELD_TYPE_TINY
     789           3 :         PDO_MYSQL_NATIVE_TYPE_NAME(TINY)
     790             : #endif
     791             : #ifdef FIELD_TYPE_BIT
     792           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(BIT)
     793             : #endif
     794           2 :         PDO_MYSQL_NATIVE_TYPE_NAME(SHORT)
     795           8 :         PDO_MYSQL_NATIVE_TYPE_NAME(LONG)
     796           4 :         PDO_MYSQL_NATIVE_TYPE_NAME(LONGLONG)
     797           2 :         PDO_MYSQL_NATIVE_TYPE_NAME(INT24)
     798           4 :         PDO_MYSQL_NATIVE_TYPE_NAME(FLOAT)
     799           8 :         PDO_MYSQL_NATIVE_TYPE_NAME(DOUBLE)
     800           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(DECIMAL)
     801             : #ifdef FIELD_TYPE_NEWDECIMAL
     802           8 :         PDO_MYSQL_NATIVE_TYPE_NAME(NEWDECIMAL)
     803             : #endif
     804             : #ifdef FIELD_TYPE_GEOMETRY
     805           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(GEOMETRY)
     806             : #endif
     807           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(TIMESTAMP)
     808             : #ifdef FIELD_TYPE_YEAR
     809           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(YEAR)
     810             : #endif
     811           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(SET)
     812           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(ENUM)
     813           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(DATE)
     814             : #ifdef FIELD_TYPE_NEWDATE
     815           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(NEWDATE)
     816             : #endif
     817           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(TIME)
     818           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(DATETIME)
     819           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(TINY_BLOB)
     820           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(MEDIUM_BLOB)
     821           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(LONG_BLOB)
     822          12 :         PDO_MYSQL_NATIVE_TYPE_NAME(BLOB)
     823           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(NULL)
     824           0 :         default:
     825           0 :             return NULL;
     826             :     }
     827             : #undef PDO_MYSQL_NATIVE_TYPE_NAME
     828             : } /* }}} */
     829             : 
     830          73 : static int pdo_mysql_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value) /* {{{ */
     831             : {
     832          73 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     833             :         const MYSQL_FIELD *F;
     834             :         zval flags;
     835             :         char *str;
     836             : 
     837          73 :         PDO_DBG_ENTER("pdo_mysql_stmt_col_meta");
     838          73 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     839          73 :         if (!S->result) {
     840           2 :                 PDO_DBG_RETURN(FAILURE);
     841             :         }
     842          71 :         if (colno >= stmt->column_count) {
     843             :                 /* error invalid column */
     844           1 :                 PDO_DBG_RETURN(FAILURE);
     845             :         }
     846             : 
     847          70 :         array_init(return_value);
     848          70 :         array_init(&flags);
     849             : 
     850          70 :         F = S->fields + colno;
     851             : 
     852          70 :         if (F->def) {
     853           0 :                 add_assoc_string(return_value, "mysql:def", F->def);
     854             :         }
     855          70 :         if (IS_NOT_NULL(F->flags)) {
     856           7 :                 add_next_index_string(&flags, "not_null");
     857             :         }
     858          70 :         if (IS_PRI_KEY(F->flags)) {
     859           3 :                 add_next_index_string(&flags, "primary_key");
     860             :         }
     861          70 :         if (F->flags & MULTIPLE_KEY_FLAG) {
     862           1 :                 add_next_index_string(&flags, "multiple_key");
     863             :         }
     864          70 :         if (F->flags & UNIQUE_KEY_FLAG) {
     865           1 :                 add_next_index_string(&flags, "unique_key");
     866             :         }
     867          70 :         if (IS_BLOB(F->flags)) {
     868          12 :                 add_next_index_string(&flags, "blob");
     869             :         }
     870          70 :         str = type_to_name_native(F->type);
     871          70 :         if (str) {
     872          70 :                 add_assoc_string(return_value, "native_type", str);
     873             :         }
     874             : 
     875             : #ifdef PDO_USE_MYSQLND
     876          70 :         switch (F->type) {
     877          21 :                 case MYSQL_TYPE_BIT:
     878             :                 case MYSQL_TYPE_YEAR:
     879             :                 case MYSQL_TYPE_TINY:
     880             :                 case MYSQL_TYPE_SHORT:
     881             :                 case MYSQL_TYPE_INT24:
     882             :                 case MYSQL_TYPE_LONG:
     883             : #if SIZEOF_ZEND_LONG==8
     884             :                 case MYSQL_TYPE_LONGLONG:
     885             : #endif
     886          21 :                         add_assoc_long(return_value, "pdo_type", PDO_PARAM_INT);
     887          21 :                         break;
     888          49 :                 default:
     889          49 :                         add_assoc_long(return_value, "pdo_type", PDO_PARAM_STR);
     890          49 :                         break;
     891             :         }
     892             : #endif
     893             : 
     894          70 :         add_assoc_zval(return_value, "flags", &flags);
     895          70 :         add_assoc_string(return_value, "table", (char *) (F->table?F->table : ""));
     896             : 
     897          70 :         PDO_DBG_RETURN(SUCCESS);
     898             : } /* }}} */
     899             : 
     900         320 : static int pdo_mysql_stmt_cursor_closer(pdo_stmt_t *stmt) /* {{{ */
     901             : {
     902         320 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     903             : 
     904         320 :         PDO_DBG_ENTER("pdo_mysql_stmt_cursor_closer");
     905         320 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     906         320 :         if (S->result) {
     907         311 :                 mysql_free_result(S->result);
     908         311 :                 S->result = NULL;
     909             :         }
     910         320 :         if (S->stmt) {
     911             :                 int retval;
     912         211 :                 retval = mysql_stmt_free_result(S->stmt);
     913         211 :                 PDO_DBG_RETURN(retval ? 0 : 1);
     914             :         }
     915             : 
     916         221 :         while (mysql_more_results(S->H->server)) {
     917             :                 MYSQL_RES *res;
     918           3 :                 if (mysql_next_result(S->H->server) != 0) {
     919           0 :                         break;
     920             :                 }
     921           3 :                 res = mysql_store_result(S->H->server);
     922           3 :                 if (res) {
     923           0 :                         mysql_free_result(res);
     924             :                 }
     925             :         }
     926         109 :         PDO_DBG_RETURN(1);
     927             : }
     928             : /* }}} */
     929             : 
     930             : struct pdo_stmt_methods mysql_stmt_methods = {
     931             :         pdo_mysql_stmt_dtor,
     932             :         pdo_mysql_stmt_execute,
     933             :         pdo_mysql_stmt_fetch,
     934             :         pdo_mysql_stmt_describe,
     935             :         pdo_mysql_stmt_get_col,
     936             :         pdo_mysql_stmt_param_hook,
     937             :         NULL, /* set_attr */
     938             :         NULL, /* get_attr */
     939             :         pdo_mysql_stmt_col_meta,
     940             :         pdo_mysql_stmt_next_rowset,
     941             :         pdo_mysql_stmt_cursor_closer
     942             : };
     943             : 
     944             : /*
     945             :  * Local variables:
     946             :  * tab-width: 4
     947             :  * c-basic-offset: 4
     948             :  * End:
     949             :  * vim600: noet sw=4 ts=4 fdm=marker
     950             :  * vim<600: noet sw=4 ts=4
     951             :  */

Generated by: LCOV version 1.10

Generated at Fri, 21 Jan 2022 14:15:18 +0000 (29 hours ago)

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