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: 272 301 90.4 %
Date: 2014-04-18 Functions: 13 13 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Fri, 18 Apr 2014 07:01:32 +0000 (6 days ago)

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