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: 276 307 89.9 %
Date: 2014-11-22 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-2014 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Author: George Schlossnagle <george@omniti.com>                      |
      16             :   |         Wez Furlong <wez@php.net>                                    |
      17             :   |         Johannes Schlueter <johannes@mysql.com>                      |
      18             :   +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : 
      23             : #ifdef HAVE_CONFIG_H
      24             : #include "config.h"
      25             : #endif
      26             : 
      27             : #include "php.h"
      28             : #include "php_ini.h"
      29             : #include "ext/standard/info.h"
      30             : #include "pdo/php_pdo.h"
      31             : #include "pdo/php_pdo_driver.h"
      32             : #include "php_pdo_mysql.h"
      33             : #include "php_pdo_mysql_int.h"
      34             : 
      35             : #ifdef PDO_USE_MYSQLND
      36             : #       define pdo_mysql_stmt_execute_prepared(stmt) pdo_mysql_stmt_execute_prepared_mysqlnd(stmt TSRMLS_CC)
      37             : #       define pdo_free_bound_result(res) zval_dtor(res.zv)
      38             : #       define pdo_mysql_stmt_close(stmt) mysqlnd_stmt_close(stmt, 0)
      39             : #else
      40             : #       define pdo_mysql_stmt_execute_prepared(stmt) pdo_mysql_stmt_execute_prepared_libmysql(stmt TSRMLS_CC)
      41             : #       define pdo_free_bound_result(res) efree(res.buffer)
      42             : #       define pdo_mysql_stmt_close(stmt) mysql_stmt_close(stmt)
      43             : #endif
      44             : 
      45             : 
      46             : 
      47        1702 : static int pdo_mysql_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
      48             : {
      49        1702 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
      50             : 
      51        1702 :         PDO_DBG_ENTER("pdo_mysql_stmt_dtor");
      52        1702 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
      53        1702 :         if (S->result) {
      54             :                 /* free the resource */
      55         564 :                 mysql_free_result(S->result);
      56         564 :                 S->result = NULL;
      57             :         }
      58        1702 :         if (S->einfo.errmsg) {
      59          29 :                 pefree(S->einfo.errmsg, stmt->dbh->is_persistent);
      60          29 :                 S->einfo.errmsg = NULL;
      61             :         }
      62        1702 :         if (S->stmt) {
      63         866 :                 pdo_mysql_stmt_close(S->stmt);
      64         866 :                 S->stmt = NULL;
      65             :         }
      66             : 
      67             : #ifndef PDO_USE_MYSQLND
      68             :         if (S->params) {
      69             :                 efree(S->params);
      70             :         }
      71             :         if (S->in_null) {
      72             :                 efree(S->in_null);
      73             :         }
      74             :         if (S->in_length) {
      75             :                 efree(S->in_length);
      76             :         }
      77             : 
      78             :         if (S->bound_result)
      79             :         {
      80             :                 int i;
      81             :                 for (i = 0; i < stmt->column_count; i++) {
      82             :                         pdo_free_bound_result(S->bound_result[i]);
      83             :                 }
      84             : 
      85             :                 efree(S->bound_result);
      86             :                 efree(S->out_null);
      87             :                 efree(S->out_length);
      88             :         }
      89             : #endif
      90             : 
      91             : 
      92        1702 :         if (S->H->server) {
      93        3406 :                 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        1702 :         if (!S->stmt && S->current_data) {
     108         200 :                 mnd_free(S->current_data);
     109             :         }
     110             : #endif /* PDO_USE_MYSQLND */
     111             : 
     112        1702 :         efree(S);
     113        1702 :         PDO_DBG_RETURN(1);
     114             : }
     115             : /* }}} */
     116             : 
     117         727 : static void pdo_mysql_stmt_set_row_count(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
     118             : {
     119             :         zend_long row_count;
     120         727 :         pdo_mysql_stmt *S = stmt->driver_data;
     121         727 :         row_count = (zend_long) mysql_stmt_affected_rows(S->stmt);
     122         727 :         if (row_count != (zend_long)-1) {
     123         713 :                 stmt->row_count = row_count;
     124             :         }
     125         727 : }
     126             : /* }}} */
     127             : 
     128         780 : static int pdo_mysql_fill_stmt_from_result(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
     129             : {
     130         780 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     131         780 :         pdo_mysql_db_handle *H = S->H;
     132             :         my_ulonglong row_count;
     133         780 :         PDO_DBG_ENTER("pdo_mysql_fill_stmt_from_result");
     134             : 
     135         780 :         row_count = mysql_affected_rows(H->server);
     136         780 :         if (row_count == (my_ulonglong)-1) {
     137             :                 /* we either have a query that returned a result set or an error occurred
     138             :                    lets see if we have access to a result set */
     139         515 :                 if (!H->buffered) {
     140          11 :                         S->result = mysql_use_result(H->server);
     141             :                 } else {
     142         504 :                         S->result = mysql_store_result(H->server);
     143             :                 }
     144         515 :                 if (NULL == S->result) {
     145           0 :                         pdo_mysql_error_stmt(stmt);
     146           0 :                         PDO_DBG_RETURN(0);
     147             :                 }
     148             : 
     149         515 :                 stmt->row_count = (zend_long) mysql_num_rows(S->result);
     150         515 :                 stmt->column_count = (int) mysql_num_fields(S->result);
     151         515 :                 S->fields = mysql_fetch_fields(S->result);
     152             :         } else {
     153             :                 /* this was a DML or DDL query (INSERT, UPDATE, DELETE, ... */
     154         265 :                 stmt->row_count = (zend_long) row_count;
     155             :         }
     156             : 
     157         780 :         PDO_DBG_RETURN(1);
     158             : }
     159             : /* }}} */
     160             : 
     161             : #ifndef PDO_USE_MYSQLND
     162             : static int pdo_mysql_stmt_execute_prepared_libmysql(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
     163             : {
     164             :         pdo_mysql_stmt *S = stmt->driver_data;
     165             :         pdo_mysql_db_handle *H = S->H;
     166             : 
     167             :         PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_libmysql");
     168             : 
     169             :         /* (re)bind the parameters */
     170             :         if (mysql_stmt_bind_param(S->stmt, S->params) || mysql_stmt_execute(S->stmt)) {
     171             :                 if (S->params) {
     172             :                         memset(S->params, 0, S->num_params * sizeof(MYSQL_BIND));
     173             :                 }
     174             :                 pdo_mysql_error_stmt(stmt);
     175             :                 if (mysql_stmt_errno(S->stmt) == 2057) {
     176             :                         /* CR_NEW_STMT_METADATA makes the statement unusable */
     177             :                         S->stmt = NULL;
     178             :                 }
     179             :                 PDO_DBG_RETURN(0);
     180             :         }
     181             : 
     182             :         if (!S->result) {
     183             :                 int i;
     184             : 
     185             :                 /* figure out the result set format, if any */
     186             :                 S->result = mysql_stmt_result_metadata(S->stmt);
     187             :                 if (S->result) {
     188             :                         int calc_max_length = H->buffered && S->max_length == 1;
     189             :                         S->fields = mysql_fetch_fields(S->result);
     190             :                         if (S->bound_result) {
     191             :                                 int i;
     192             :                                 for (i = 0; i < stmt->column_count; i++) {
     193             :                                         efree(S->bound_result[i].buffer);
     194             :                                 }
     195             :                                 efree(S->bound_result);
     196             :                                 efree(S->out_null);
     197             :                                 efree(S->out_length);
     198             :                         }
     199             : 
     200             :                         stmt->column_count = (int)mysql_num_fields(S->result);
     201             :                         S->bound_result = ecalloc(stmt->column_count, sizeof(MYSQL_BIND));
     202             :                         S->out_null = ecalloc(stmt->column_count, sizeof(my_bool));
     203             :                         S->out_length = ecalloc(stmt->column_count, sizeof(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 TSRMLS_CC);
     267             :         PDO_DBG_RETURN(1);
     268             : }
     269             : /* }}} */
     270             : #endif
     271             : 
     272             : #ifdef PDO_USE_MYSQLND
     273         732 : static int pdo_mysql_stmt_execute_prepared_mysqlnd(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
     274             : {
     275         732 :         pdo_mysql_stmt *S = stmt->driver_data;
     276         732 :         pdo_mysql_db_handle *H = S->H;
     277             :         int i;
     278             : 
     279         732 :         PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_mysqlnd");
     280             : 
     281         732 :         if (mysql_stmt_execute(S->stmt)) {
     282           5 :                 pdo_mysql_error_stmt(stmt);
     283           5 :                 PDO_DBG_RETURN(0);
     284             :         }
     285             : 
     286         727 :         if (S->result) {
     287             :                 /* TODO: add a test to check if we really have zvals here... */
     288          15 :                 mysql_free_result(S->result);
     289          15 :                 S->result = NULL;
     290             :         }
     291             : 
     292             :         /* for SHOW/DESCRIBE and others the column/field count is not available before execute */
     293         727 :         stmt->column_count = mysql_stmt_field_count(S->stmt);
     294        1614 :         for (i = 0; i < stmt->column_count; i++) {
     295         887 :                 mysqlnd_stmt_bind_one_result(S->stmt, i);
     296             :         }
     297             : 
     298         727 :         S->result = mysqlnd_stmt_result_metadata(S->stmt);
     299         727 :         if (S->result) {
     300         435 :                 S->fields = mysql_fetch_fields(S->result);
     301             :                 /* if buffered, pre-fetch all the data */
     302         435 :                 if (H->buffered) {
     303         421 :                         if (mysql_stmt_store_result(S->stmt)) {
     304           0 :                                 PDO_DBG_RETURN(0);
     305             :                         }
     306             :                 }
     307             :         }
     308             : 
     309         727 :         pdo_mysql_stmt_set_row_count(stmt TSRMLS_CC);
     310         727 :         PDO_DBG_RETURN(1);
     311             : }
     312             : /* }}} */
     313             : #endif
     314             : 
     315        1515 : static int pdo_mysql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
     316             : {
     317        1515 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     318        1515 :         pdo_mysql_db_handle *H = S->H;
     319        1515 :         PDO_DBG_ENTER("pdo_mysql_stmt_execute");
     320        1515 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     321             : 
     322        1515 :         if (S->stmt) {
     323         732 :                 PDO_DBG_RETURN(pdo_mysql_stmt_execute_prepared(stmt));
     324             :         }
     325             : 
     326             :         /* ensure that we free any previous unfetched results */
     327         783 :         if (S->result) {
     328          37 :                 mysql_free_result(S->result);
     329          37 :                 S->result = NULL;
     330             :         }
     331             : 
     332         783 :         if (mysql_real_query(H->server, stmt->active_query_string, stmt->active_query_stringlen) != 0) {
     333          25 :                 pdo_mysql_error_stmt(stmt);
     334          25 :                 PDO_DBG_RETURN(0);
     335             :         }
     336             : 
     337         758 :         PDO_DBG_RETURN(pdo_mysql_fill_stmt_from_result(stmt TSRMLS_CC));
     338             : }
     339             : /* }}} */
     340             : 
     341          67 : static int pdo_mysql_stmt_next_rowset(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
     342             : {
     343          67 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     344          67 :         pdo_mysql_db_handle *H = S->H;
     345             : #if PDO_USE_MYSQLND
     346             :         zend_long row_count;
     347             : #endif
     348          67 :         PDO_DBG_ENTER("pdo_mysql_stmt_next_rowset");
     349          67 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     350             : 
     351             : #if PDO_USE_MYSQLND
     352          67 :         if (!H->emulate_prepare) {
     353          20 :                 if (!mysqlnd_stmt_more_results(S->stmt)) {
     354           4 :                         PDO_DBG_RETURN(0);
     355             :                 }
     356          16 :                 if (mysqlnd_stmt_next_result(S->stmt)) {
     357           0 :                         PDO_DBG_RETURN(0);
     358             :                 }
     359             : 
     360          16 :                 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          14 :                         PDO_DBG_RETURN(0);
     368             :                 }
     369             : 
     370             :                 /* TODO - this code is stolen from execute() - see above */
     371           2 :                 if (S->result) {
     372           2 :                         mysql_free_result(S->result);
     373           2 :                         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           2 :                         stmt->column_count = mysql_stmt_field_count(S->stmt);
     380           6 :                         for (i = 0; i < stmt->column_count; i++) {
     381           4 :                                 mysqlnd_stmt_bind_one_result(S->stmt, i);
     382             :                         }
     383             :                 }
     384             : 
     385           2 :                 S->result = mysqlnd_stmt_result_metadata(S->stmt);
     386           2 :                 if (S->result) {
     387           2 :                         S->fields = mysql_fetch_fields(S->result);
     388             : 
     389             :                         /* if buffered, pre-fetch all the data */
     390           2 :                         if (H->buffered) {
     391           1 :                                 if (mysql_stmt_store_result(S->stmt)) {
     392           0 :                                         PDO_DBG_RETURN(1);
     393             :                                 }
     394             :                         }
     395             :                 }
     396           2 :                 row_count = (zend_long) mysql_stmt_affected_rows(S->stmt);
     397           2 :                 if (row_count != (zend_long)-1) {
     398           1 :                         stmt->row_count = row_count;
     399             :                 }
     400           2 :                 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 TSRMLS_CC));
     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 TSRMLS_CC));
     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       36397 : static int pdo_mysql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, enum pdo_param_event event_type TSRMLS_DC) /* {{{ */
     454             : {
     455             :         zval *parameter;
     456             : #ifndef PDO_USE_MYSQLND
     457             :         PDO_MYSQL_PARAM_BIND *b;
     458             : #endif
     459       36397 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     460             : 
     461       36397 :         PDO_DBG_ENTER("pdo_mysql_stmt_param_hook");
     462       36397 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     463       36397 :         PDO_DBG_INF_FMT("event = %s", pdo_param_event_names[event_type]);
     464       36397 :         if (S->stmt && param->is_param) {
     465        2428 :                 switch (event_type) {
     466             :                         case PDO_PARAM_EVT_ALLOC:
     467             :                                 /* sanity check parameter number range */
     468         576 :                                 if (param->paramno < 0 || param->paramno >= S->num_params) {
     469         100 :                                         strcpy(stmt->error_code, "HY093");
     470         100 :                                         PDO_DBG_RETURN(0);
     471             :                                 }
     472         476 :                                 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         476 :                                 PDO_DBG_RETURN(1);
     482             : 
     483             :                         case PDO_PARAM_EVT_EXEC_PRE:
     484         289 :                                 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         572 :                                 if (!Z_ISREF(param->parameter)) {
     492         206 :                                         parameter = &param->parameter;
     493             :                                 } else {
     494          80 :                                         parameter = Z_REFVAL(param->parameter);
     495             :                                 }
     496             : 
     497             : #if PDO_USE_MYSQLND
     498         572 :                                 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         282 :                                 switch (PDO_PARAM_TYPE(param->param_type)) {
     516             :                                         case PDO_PARAM_STMT:
     517           0 :                                                 PDO_DBG_RETURN(0);
     518             :                                         case PDO_PARAM_LOB:
     519           1 :                                                 PDO_DBG_INF("PDO_PARAM_LOB");
     520           2 :                                                 if (!Z_ISREF(param->parameter)) {
     521           0 :                                                         parameter = &param->parameter;
     522             :                                                 } else {
     523           1 :                                                         parameter = Z_REFVAL(param->parameter);
     524             :                                                 }
     525           1 :                                                 if (Z_TYPE_P(parameter) == IS_RESOURCE) {
     526             :                                                         php_stream *stm;
     527           1 :                                                         php_stream_from_zval_no_verify(stm, parameter);
     528           1 :                                                         if (stm) {
     529           1 :                                                                 ZVAL_STR(parameter, php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0));
     530             :                                                         } else {
     531           0 :                                                                 pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource" TSRMLS_CC);
     532           0 :                                                                 return 0;
     533             :                                                         }
     534             :                                                 }
     535             :                                                 /* fall through */
     536             : 
     537             :                                         default:
     538             :                                                 ;
     539             :                                 }
     540             : 
     541             : #if PDO_USE_MYSQLND
     542             :                                 /* Is it really correct to check the zval's type? - But well, that's what the old code below does, too */
     543         564 :                                 PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
     544         564 :                                 if (!Z_ISREF(param->parameter)) {
     545         204 :                                         parameter = &param->parameter;
     546             :                                 } else {
     547          78 :                                         parameter = Z_REFVAL(param->parameter);
     548             :                                 }
     549         282 :                                 switch (Z_TYPE_P(parameter)) {
     550             :                                         case IS_STRING:
     551         275 :                                                 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_VAR_STRING);
     552         275 :                                                 break;
     553             :                                         case IS_LONG:
     554             : #if SIZEOF_ZEND_LONG==8
     555           6 :                                                 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_LONGLONG);
     556             : #elif SIZEOF_ZEND_LONG==4
     557             :                                                 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_LONG);
     558             : #endif /* SIZEOF_LONG */
     559           6 :                                                 break;
     560             :                                         case IS_DOUBLE:
     561           0 :                                                 mysqlnd_stmt_bind_one_param(S->stmt, param->paramno, parameter, MYSQL_TYPE_DOUBLE);
     562           0 :                                                 break;
     563             :                                         default:
     564           1 :                                                 PDO_DBG_RETURN(0);
     565             :                                 }
     566             : 
     567         281 :                                 PDO_DBG_RETURN(1);
     568             : #else
     569             :                                 PDO_DBG_INF_FMT("param->parameter->type=%d", Z_TYPE(param->parameter));
     570             :                                 if (!Z_ISREF(param->parameter)) {
     571             :                                         parameter = &param->parameter;
     572             :                                 } else {
     573             :                                         parameter = Z_REFVAL(param->parameter);
     574             :                                 }
     575             :                                 switch (Z_TYPE_P(parameter)) {
     576             :                                         case IS_STRING:
     577             :                                                 b->buffer_type = MYSQL_TYPE_STRING;
     578             :                                                 b->buffer = Z_STRVAL_P(parameter);
     579             :                                                 b->buffer_length = Z_STRLEN_P(parameter);
     580             :                                                 *b->length = Z_STRLEN_P(parameter);
     581             :                                                 PDO_DBG_RETURN(1);
     582             : 
     583             :                                         case IS_LONG:
     584             :                                                 b->buffer_type = MYSQL_TYPE_LONG;
     585             :                                                 b->buffer = &Z_LVAL_P(parameter);
     586             :                                                 PDO_DBG_RETURN(1);
     587             : 
     588             :                                         case IS_DOUBLE:
     589             :                                                 b->buffer_type = MYSQL_TYPE_DOUBLE;
     590             :                                                 b->buffer = &Z_DVAL_P(parameter);
     591             :                                                 PDO_DBG_RETURN(1);
     592             : 
     593             :                                         default:
     594             :                                                 PDO_DBG_RETURN(0);
     595             :                                 }
     596             : #endif /* PDO_USE_MYSQLND */
     597             :                 case PDO_PARAM_EVT_FREE:
     598             :                 case PDO_PARAM_EVT_EXEC_POST:
     599             :                 case PDO_PARAM_EVT_FETCH_PRE:
     600             :                 case PDO_PARAM_EVT_FETCH_POST:
     601             :                 case PDO_PARAM_EVT_NORMALIZE:
     602             :                         /* do nothing */
     603             :                         break;
     604             :                 }
     605             :         }
     606             : 
     607       35532 :         PDO_DBG_RETURN(1);
     608             : }
     609             : /* }}} */
     610             : 
     611        1441 : static int pdo_mysql_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, zend_long offset TSRMLS_DC) /* {{{ */
     612             : {
     613        1441 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     614             : #if PDO_USE_MYSQLND
     615             :         zend_bool fetched_anything;
     616             : 
     617        1441 :         PDO_DBG_ENTER("pdo_mysql_stmt_fetch");
     618        1441 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     619        1441 :         if (S->stmt) {
     620         513 :                 if (FAIL == mysqlnd_stmt_fetch(S->stmt, &fetched_anything) || fetched_anything == FALSE) {
     621          66 :                         PDO_DBG_RETURN(0);
     622             :                 }
     623             : 
     624         447 :                 PDO_DBG_RETURN(1);
     625             :         }
     626             : #else
     627             :         int ret;
     628             : 
     629             :         if (S->stmt) {
     630             :                 ret = mysql_stmt_fetch(S->stmt);
     631             : 
     632             : #               ifdef MYSQL_DATA_TRUNCATED
     633             :                 if (ret == MYSQL_DATA_TRUNCATED) {
     634             :                         ret = 0;
     635             :                 }
     636             : #               endif
     637             : 
     638             :                 if (ret) {
     639             :                         if (ret != MYSQL_NO_DATA) {
     640             :                                 pdo_mysql_error_stmt(stmt);
     641             :                         }
     642             :                         PDO_DBG_RETURN(0);
     643             :                 }
     644             : 
     645             :                 PDO_DBG_RETURN(1);
     646             :         }
     647             : #endif /* PDO_USE_MYSQLND */
     648             : 
     649         928 :         if (!S->result) {
     650          13 :                 strcpy(stmt->error_code, "HY000");
     651          13 :                 PDO_DBG_RETURN(0);
     652             :         }
     653             : #if PDO_USE_MYSQLND
     654         915 :         if (!S->stmt && S->current_data) {
     655         535 :                 mnd_free(S->current_data);
     656             :         }
     657             : #endif /* PDO_USE_MYSQLND */
     658             : 
     659         915 :         if ((S->current_data = mysql_fetch_row(S->result)) == NULL) {
     660             : #if PDO_USE_MYSQLND
     661         180 :                 if (S->result->unbuf && !S->result->unbuf->eof_reached && mysql_errno(S->H->server)) {
     662             : #else
     663             :                 if (!S->result->eof && mysql_errno(S->H->server)) {
     664             : #endif
     665           0 :                         pdo_mysql_error_stmt(stmt);
     666             :                 }
     667         180 :                 PDO_DBG_RETURN(0);
     668             :         }
     669             : 
     670         735 :         S->current_lengths = mysql_fetch_lengths(S->result);
     671         735 :         PDO_DBG_RETURN(1);
     672             : }
     673             : /* }}} */
     674             : 
     675        1433 : static int pdo_mysql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC) /* {{{ */
     676             : {
     677        1433 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     678        1433 :         struct pdo_column_data *cols = stmt->columns;
     679             :         int i;
     680             : 
     681        1433 :         PDO_DBG_ENTER("pdo_mysql_stmt_describe");
     682        1433 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     683        1433 :         if (!S->result) {
     684           0 :                 PDO_DBG_RETURN(0);
     685             :         }
     686             : 
     687        1433 :         if (colno >= stmt->column_count) {
     688             :                 /* error invalid column */
     689           0 :                 PDO_DBG_RETURN(0);
     690             :         }
     691             : 
     692             :         /* fetch all on demand, this seems easiest
     693             :         ** if we've been here before bail out
     694             :         */
     695        1433 :         if (cols[0].name) {
     696         667 :                 PDO_DBG_RETURN(1);
     697             :         }
     698        2199 :         for (i = 0; i < stmt->column_count; i++) {
     699             :                 int namelen;
     700             : 
     701        1433 :                 if (S->H->fetch_table_names) {
     702           1 :                         namelen = spprintf(&cols[i].name, 0, "%s.%s", S->fields[i].table, S->fields[i].name);
     703           1 :                         cols[i].namelen = namelen;
     704             :                 } else {
     705        1432 :                         cols[i].namelen = S->fields[i].name_length;
     706        1432 :                         cols[i].name = estrndup(S->fields[i].name, S->fields[i].name_length);
     707             :                 }
     708             : 
     709        1433 :                 cols[i].precision = S->fields[i].decimals;
     710        1433 :                 cols[i].maxlen = S->fields[i].length;
     711             : 
     712             : #ifdef PDO_USE_MYSQLND
     713        1433 :                 if (S->stmt) {
     714         728 :                         cols[i].param_type = PDO_PARAM_ZVAL;
     715             :                 } else
     716             : #endif
     717             :                 {
     718         705 :                         cols[i].param_type = PDO_PARAM_STR;
     719             :                 }
     720             :         }
     721         766 :         PDO_DBG_RETURN(1);
     722             : }
     723             : /* }}} */
     724             : 
     725        2547 : static int pdo_mysql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, zend_ulong *len, int *caller_frees TSRMLS_DC) /* {{{ */
     726             : {
     727        2547 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     728             : 
     729        2547 :         PDO_DBG_ENTER("pdo_mysql_stmt_get_col");
     730        2547 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     731        2547 :         if (!S->result) {
     732           0 :                 PDO_DBG_RETURN(0);
     733             :         }
     734             : 
     735             :         /* With mysqlnd data is stored inside mysqlnd, not S->current_data */
     736        2547 :         if (!S->stmt) {
     737        1529 :                 if (S->current_data == NULL || !S->result) {
     738           0 :                         PDO_DBG_RETURN(0);
     739             :                 }
     740             :         }
     741             : 
     742        2547 :         if (colno >= stmt->column_count) {
     743             :                 /* error invalid column */
     744           0 :                 PDO_DBG_RETURN(0);
     745             :         }
     746             : #if PDO_USE_MYSQLND
     747        2547 :         if (S->stmt) {
     748        1018 :                 Z_TRY_ADDREF(S->stmt->data->result_bind[colno].zv);
     749        1018 :                 *ptr = (char*)&S->stmt->data->result_bind[colno].zv;
     750        1018 :                 *len = sizeof(zval);
     751        1018 :                 PDO_DBG_RETURN(1);
     752             :         }
     753             : #else
     754             :         if (S->stmt) {
     755             :                 if (S->out_null[colno]) {
     756             :                         *ptr = NULL;
     757             :                         *len = 0;
     758             :                         PDO_DBG_RETURN(1);
     759             :                 }
     760             :                 *ptr = S->bound_result[colno].buffer;
     761             :                 if (S->out_length[colno] > S->bound_result[colno].buffer_length) {
     762             :                         /* mysql lied about the column width */
     763             :                         strcpy(stmt->error_code, "01004"); /* truncated */
     764             :                         S->out_length[colno] = S->bound_result[colno].buffer_length;
     765             :                         *len = S->out_length[colno];
     766             :                         PDO_DBG_RETURN(0);
     767             :                 }
     768             :                 *len = S->out_length[colno];
     769             :                 PDO_DBG_RETURN(1);
     770             :         }
     771             : #endif
     772        1529 :         *ptr = S->current_data[colno];
     773        1529 :         *len = S->current_lengths[colno];
     774        1529 :         PDO_DBG_RETURN(1);
     775             : } /* }}} */
     776             : 
     777          70 : static char *type_to_name_native(int type) /* {{{ */
     778             : {
     779             : #define PDO_MYSQL_NATIVE_TYPE_NAME(x)   case FIELD_TYPE_##x: return #x;
     780             : 
     781          70 :     switch (type) {
     782           7 :         PDO_MYSQL_NATIVE_TYPE_NAME(STRING)
     783           5 :         PDO_MYSQL_NATIVE_TYPE_NAME(VAR_STRING)
     784             : #ifdef FIELD_TYPE_TINY
     785           3 :         PDO_MYSQL_NATIVE_TYPE_NAME(TINY)
     786             : #endif
     787             : #ifdef FIELD_TYPE_BIT
     788           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(BIT)
     789             : #endif
     790           2 :         PDO_MYSQL_NATIVE_TYPE_NAME(SHORT)
     791           8 :         PDO_MYSQL_NATIVE_TYPE_NAME(LONG)
     792           4 :         PDO_MYSQL_NATIVE_TYPE_NAME(LONGLONG)
     793           2 :         PDO_MYSQL_NATIVE_TYPE_NAME(INT24)
     794           4 :         PDO_MYSQL_NATIVE_TYPE_NAME(FLOAT)
     795           8 :         PDO_MYSQL_NATIVE_TYPE_NAME(DOUBLE)
     796           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(DECIMAL)
     797             : #ifdef FIELD_TYPE_NEWDECIMAL
     798           8 :         PDO_MYSQL_NATIVE_TYPE_NAME(NEWDECIMAL)
     799             : #endif
     800             : #ifdef FIELD_TYPE_GEOMETRY
     801           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(GEOMETRY)
     802             : #endif
     803           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(TIMESTAMP)
     804             : #ifdef FIELD_TYPE_YEAR
     805           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(YEAR)
     806             : #endif
     807           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(SET)
     808           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(ENUM)
     809           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(DATE)
     810             : #ifdef FIELD_TYPE_NEWDATE
     811           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(NEWDATE)
     812             : #endif
     813           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(TIME)
     814           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(DATETIME)
     815           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(TINY_BLOB)
     816           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(MEDIUM_BLOB)
     817           0 :         PDO_MYSQL_NATIVE_TYPE_NAME(LONG_BLOB)
     818          12 :         PDO_MYSQL_NATIVE_TYPE_NAME(BLOB)
     819           1 :         PDO_MYSQL_NATIVE_TYPE_NAME(NULL)
     820             :         default:
     821           0 :             return NULL;
     822             :     }
     823             : #undef PDO_MYSQL_NATIVE_TYPE_NAME
     824             : } /* }}} */
     825             : 
     826          73 : static int pdo_mysql_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value TSRMLS_DC) /* {{{ */
     827             : {
     828          73 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     829             :         const MYSQL_FIELD *F;
     830             :         zval flags;
     831             :         char *str;
     832             : 
     833          73 :         PDO_DBG_ENTER("pdo_mysql_stmt_col_meta");
     834          73 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     835          73 :         if (!S->result) {
     836           2 :                 PDO_DBG_RETURN(FAILURE);
     837             :         }
     838          71 :         if (colno >= stmt->column_count) {
     839             :                 /* error invalid column */
     840           1 :                 PDO_DBG_RETURN(FAILURE);
     841             :         }
     842             : 
     843          70 :         array_init(return_value);
     844          70 :         array_init(&flags);
     845             : 
     846          70 :         F = S->fields + colno;
     847             : 
     848          70 :         if (F->def) {
     849           0 :                 add_assoc_string(return_value, "mysql:def", F->def);
     850             :         }
     851          70 :         if (IS_NOT_NULL(F->flags)) {
     852           7 :                 add_next_index_string(&flags, "not_null");
     853             :         }
     854          70 :         if (IS_PRI_KEY(F->flags)) {
     855           3 :                 add_next_index_string(&flags, "primary_key");
     856             :         }
     857          70 :         if (F->flags & MULTIPLE_KEY_FLAG) {
     858           1 :                 add_next_index_string(&flags, "multiple_key");
     859             :         }
     860          70 :         if (F->flags & UNIQUE_KEY_FLAG) {
     861           1 :                 add_next_index_string(&flags, "unique_key");
     862             :         }
     863          70 :         if (IS_BLOB(F->flags)) {
     864          12 :                 add_next_index_string(&flags, "blob");
     865             :         }
     866          70 :         str = type_to_name_native(F->type);
     867          70 :         if (str) {
     868          70 :                 add_assoc_string(return_value, "native_type", str);
     869             :         }
     870             : 
     871             : #ifdef PDO_USE_MYSQLND
     872          70 :         switch (F->type) {
     873             :                 case MYSQL_TYPE_BIT:
     874             :                 case MYSQL_TYPE_YEAR:
     875             :                 case MYSQL_TYPE_TINY:
     876             :                 case MYSQL_TYPE_SHORT:
     877             :                 case MYSQL_TYPE_INT24:
     878             :                 case MYSQL_TYPE_LONG:
     879             : #if SIZEOF_LONG==8
     880             :                 case MYSQL_TYPE_LONGLONG:
     881             : #endif
     882          21 :                         add_assoc_long(return_value, "pdo_type", PDO_PARAM_INT);
     883          21 :                         break;
     884             :                 default:
     885          49 :                         add_assoc_long(return_value, "pdo_type", PDO_PARAM_STR);
     886             :                         break;
     887             :         }
     888             : #endif
     889             : 
     890          70 :         add_assoc_zval(return_value, "flags", &flags);
     891          70 :         add_assoc_string(return_value, "table", (char *) (F->table?F->table : ""));
     892             : 
     893          70 :         PDO_DBG_RETURN(SUCCESS);
     894             : } /* }}} */
     895             : 
     896         318 : static int pdo_mysql_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
     897             : {
     898         318 :         pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     899             : 
     900         318 :         PDO_DBG_ENTER("pdo_mysql_stmt_cursor_closer");
     901         318 :         PDO_DBG_INF_FMT("stmt=%p", S->stmt);
     902         318 :         if (S->result) {
     903         309 :                 mysql_free_result(S->result);
     904         309 :                 S->result = NULL;
     905             :         }
     906         318 :         if (S->stmt) {
     907             :                 int retval;
     908         211 :                 retval = mysql_stmt_free_result(S->stmt);
     909         211 :                 PDO_DBG_RETURN(retval ? 0 : 1);
     910             :         }
     911             : 
     912         215 :         while (mysql_more_results(S->H->server)) {
     913             :                 MYSQL_RES *res;
     914           1 :                 if (mysql_next_result(S->H->server) != 0) {
     915           0 :                         break;
     916             :                 }
     917           1 :                 res = mysql_store_result(S->H->server);
     918           1 :                 if (res) {
     919           0 :                         mysql_free_result(res);
     920             :                 }
     921             :         }
     922         107 :         PDO_DBG_RETURN(1);
     923             : }
     924             : /* }}} */
     925             : 
     926             : struct pdo_stmt_methods mysql_stmt_methods = {
     927             :         pdo_mysql_stmt_dtor,
     928             :         pdo_mysql_stmt_execute,
     929             :         pdo_mysql_stmt_fetch,
     930             :         pdo_mysql_stmt_describe,
     931             :         pdo_mysql_stmt_get_col,
     932             :         pdo_mysql_stmt_param_hook,
     933             :         NULL, /* set_attr */
     934             :         NULL, /* get_attr */
     935             :         pdo_mysql_stmt_col_meta,
     936             :         pdo_mysql_stmt_next_rowset,
     937             :         pdo_mysql_stmt_cursor_closer
     938             : };
     939             : 
     940             : /*
     941             :  * Local variables:
     942             :  * tab-width: 4
     943             :  * c-basic-offset: 4
     944             :  * End:
     945             :  * vim600: noet sw=4 ts=4 fdm=marker
     946             :  * vim<600: noet sw=4 ts=4
     947             :  */

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:21 +0000 (4 days ago)

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