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_driver.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 281 316 88.9 %
Date: 2015-09-02 Functions: 15 15 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2015 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             : #ifndef PDO_USE_MYSQLND
      35             : #include <mysqld_error.h>
      36             : #endif
      37             : #include "zend_exceptions.h"
      38             : 
      39             : #if defined(PDO_USE_MYSQLND)
      40             : #       define pdo_mysql_init(persistent) mysqlnd_init(MYSQLND_CLIENT_NO_FLAG, persistent)
      41             : #else
      42             : #       define pdo_mysql_init(persistent) mysql_init(NULL)
      43             : #endif
      44             : 
      45             : /* {{{ _pdo_mysql_error */
      46         741 : int _pdo_mysql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line)
      47             : {
      48         741 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
      49             :         pdo_error_type *pdo_err;
      50             :         pdo_mysql_error_info *einfo;
      51         741 :         pdo_mysql_stmt *S = NULL;
      52             : 
      53         741 :         PDO_DBG_ENTER("_pdo_mysql_error");
      54         741 :         PDO_DBG_INF_FMT("file=%s line=%d", file, line);
      55         741 :         if (stmt) {
      56          32 :                 S = (pdo_mysql_stmt*)stmt->driver_data;
      57          32 :                 pdo_err = &stmt->error_code;
      58          32 :                 einfo   = &S->einfo;
      59             :         } else {
      60         709 :                 pdo_err = &dbh->error_code;
      61         709 :                 einfo   = &H->einfo;
      62             :         }
      63             : 
      64         746 :         if (S && S->stmt) {
      65           5 :                 einfo->errcode = mysql_stmt_errno(S->stmt);
      66             :         } else {
      67         736 :                 einfo->errcode = mysql_errno(H->server);
      68             :         }
      69             : 
      70         741 :         einfo->file = file;
      71         741 :         einfo->line = line;
      72             : 
      73         741 :         if (einfo->errmsg) {
      74         436 :                 pefree(einfo->errmsg, dbh->is_persistent);
      75         436 :                 einfo->errmsg = NULL;
      76             :         }
      77             : 
      78         741 :         if (einfo->errcode) {
      79         738 :                 if (einfo->errcode == 2014) {
      80           8 :                         einfo->errmsg = pestrdup(
      81             :                                 "Cannot execute queries while other unbuffered queries are active.  "
      82             :                                 "Consider using PDOStatement::fetchAll().  Alternatively, if your code "
      83             :                                 "is only ever going to run against mysql, you may enable query "
      84             :                                 "buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.",
      85             :                                 dbh->is_persistent);
      86         730 :                 } else if (einfo->errcode == 2057) {
      87           0 :                         einfo->errmsg = pestrdup(
      88             :                                 "A stored procedure returning result sets of different size was called. "
      89             :                                 "This is not supported by libmysql",
      90             :                                 dbh->is_persistent);
      91             : 
      92             :                 } else {
      93         730 :                         einfo->errmsg = pestrdup(mysql_error(H->server), dbh->is_persistent);
      94             :                 }
      95             :         } else { /* no error */
      96           3 :                 strcpy(*pdo_err, PDO_ERR_NONE);
      97           3 :                 PDO_DBG_RETURN(0);
      98             :         }
      99             : 
     100         743 :         if (S && S->stmt) {
     101           5 :                 strcpy(*pdo_err, mysql_stmt_sqlstate(S->stmt));
     102             :         } else {
     103         733 :                 strcpy(*pdo_err, mysql_sqlstate(H->server));
     104             :         }
     105             : 
     106         738 :         if (!dbh->methods) {
     107          14 :                 PDO_DBG_INF("Throwing exception");
     108          14 :                 zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode, "SQLSTATE[%s] [%d] %s",
     109             :                                 *pdo_err, einfo->errcode, einfo->errmsg);
     110             :         }
     111             : 
     112         738 :         PDO_DBG_RETURN(einfo->errcode);
     113             : }
     114             : /* }}} */
     115             : 
     116             : /* {{{ pdo_mysql_fetch_error_func */
     117         829 : static int pdo_mysql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info)
     118             : {
     119         829 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     120         829 :         pdo_mysql_error_info *einfo = &H->einfo;
     121             : 
     122         829 :         PDO_DBG_ENTER("pdo_mysql_fetch_error_func");
     123         829 :         PDO_DBG_INF_FMT("dbh=%p stmt=%p", dbh, stmt);
     124         829 :         if (stmt) {
     125         761 :                 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     126         761 :                 einfo = &S->einfo;
     127             :         } else {
     128          68 :                 einfo = &H->einfo;
     129             :         }
     130             : 
     131         829 :         if (einfo->errcode) {
     132          97 :                 add_next_index_long(info, einfo->errcode);
     133          97 :                 add_next_index_string(info, einfo->errmsg);
     134             :         }
     135             : 
     136         829 :         PDO_DBG_RETURN(1);
     137             : }
     138             : /* }}} */
     139             : 
     140             : /* {{{ mysql_handle_closer */
     141         500 : static int mysql_handle_closer(pdo_dbh_t *dbh)
     142             : {
     143         500 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     144             : 
     145         500 :         PDO_DBG_ENTER("mysql_handle_closer");
     146         500 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     147         500 :         if (H) {
     148         500 :                 if (H->server) {
     149         500 :                         mysql_close(H->server);
     150             :                 }
     151         500 :                 if (H->einfo.errmsg) {
     152         270 :                         pefree(H->einfo.errmsg, dbh->is_persistent);
     153             :                 }
     154         500 :                 pefree(H, dbh->is_persistent);
     155         500 :                 dbh->driver_data = NULL;
     156             :         }
     157         500 :         PDO_DBG_RETURN(0);
     158             : }
     159             : /* }}} */
     160             : 
     161             : /* {{{ mysql_handle_preparer */
     162        1719 : static int mysql_handle_preparer(pdo_dbh_t *dbh, const char *sql, size_t sql_len, pdo_stmt_t *stmt, zval *driver_options)
     163             : {
     164        1719 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     165        1719 :         pdo_mysql_stmt *S = ecalloc(1, sizeof(pdo_mysql_stmt));
     166        1719 :         char *nsql = NULL;
     167        1719 :         size_t nsql_len = 0;
     168             :         int ret;
     169             :         int server_version;
     170             : 
     171        1719 :         PDO_DBG_ENTER("mysql_handle_preparer");
     172        1719 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     173        1719 :         PDO_DBG_INF_FMT("sql=%.*s", (int)sql_len, sql);
     174             : 
     175        1719 :         S->H = H;
     176        1719 :         stmt->driver_data = S;
     177        1719 :         stmt->methods = &mysql_stmt_methods;
     178             : 
     179        1719 :         if (H->emulate_prepare) {
     180         848 :                 goto end;
     181             :         }
     182             : 
     183         871 :         server_version = mysql_get_server_version(H->server);
     184         871 :         if (server_version < 40100) {
     185           0 :                 goto fallback;
     186             :         }
     187         871 :         stmt->supports_placeholders = PDO_PLACEHOLDER_POSITIONAL;
     188         871 :         ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len);
     189             : 
     190         871 :         if (ret == 1) {
     191             :                 /* query was rewritten */
     192         114 :                 sql = nsql;
     193         114 :                 sql_len = nsql_len;
     194         757 :         } else if (ret == -1) {
     195             :                 /* failed to parse */
     196           1 :                 strcpy(dbh->error_code, stmt->error_code);
     197           1 :                 PDO_DBG_RETURN(0);
     198             :         }
     199             : 
     200         870 :         if (!(S->stmt = mysql_stmt_init(H->server))) {
     201           0 :                 pdo_mysql_error(dbh);
     202           0 :                 if (nsql) {
     203           0 :                         efree(nsql);
     204             :                 }
     205           0 :                 PDO_DBG_RETURN(0);
     206             :         }
     207             : 
     208         870 :         if (mysql_stmt_prepare(S->stmt, sql, sql_len)) {
     209             :                 /* TODO: might need to pull statement specific info here? */
     210             :                 /* if the query isn't supported by the protocol, fallback to emulation */
     211          11 :                 if (mysql_errno(H->server) == 1295) {
     212           1 :                         if (nsql) {
     213           0 :                                 efree(nsql);
     214             :                         }
     215           1 :                         goto fallback;
     216             :                 }
     217          10 :                 pdo_mysql_error(dbh);
     218          10 :                 if (nsql) {
     219           1 :                         efree(nsql);
     220             :                 }
     221          10 :                 PDO_DBG_RETURN(0);
     222             :         }
     223         859 :         if (nsql) {
     224         113 :                 efree(nsql);
     225             :         }
     226             : 
     227         859 :         S->num_params = mysql_stmt_param_count(S->stmt);
     228             : 
     229         859 :         if (S->num_params) {
     230         380 :                 S->params_given = 0;
     231             : #if defined(PDO_USE_MYSQLND)
     232         380 :                 S->params = NULL;
     233             : #else
     234             :                 S->params = ecalloc(S->num_params, sizeof(MYSQL_BIND));
     235             :                 S->in_null = ecalloc(S->num_params, sizeof(my_bool));
     236             :                 S->in_length = ecalloc(S->num_params, sizeof(zend_ulong));
     237             : #endif
     238             :         }
     239         859 :         dbh->alloc_own_columns = 1;
     240             : 
     241         859 :         S->max_length = pdo_attr_lval(driver_options, PDO_ATTR_MAX_COLUMN_LEN, 0);
     242             : 
     243         859 :         PDO_DBG_RETURN(1);
     244             : 
     245             : fallback:
     246             : end:
     247         849 :         stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
     248             : 
     249         849 :         PDO_DBG_RETURN(1);
     250             : }
     251             : /* }}} */
     252             : 
     253             : /* {{{ mysql_handle_doer */
     254        1860 : static zend_long mysql_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
     255             : {
     256        1860 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     257        1860 :         PDO_DBG_ENTER("mysql_handle_doer");
     258        1860 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     259        1860 :         PDO_DBG_INF_FMT("sql=%.*s", (int)sql_len, sql);
     260             : 
     261        1860 :         if (mysql_real_query(H->server, sql, sql_len)) {
     262         682 :                 pdo_mysql_error(dbh);
     263         682 :                 PDO_DBG_RETURN(-1);
     264             :         } else {
     265        1178 :                 my_ulonglong c = mysql_affected_rows(H->server);
     266        1178 :                 if (c == (my_ulonglong) -1) {
     267           3 :                         pdo_mysql_error(dbh);
     268           3 :                         PDO_DBG_RETURN(H->einfo.errcode ? -1 : 0);
     269             :                 } else {
     270             : 
     271             :                         /* MULTI_QUERY support - eat up all unfetched result sets */
     272             :                         MYSQL_RES* result;
     273        2354 :                         while (mysql_more_results(H->server)) {
     274           4 :                                 if (mysql_next_result(H->server)) {
     275           0 :                                         PDO_DBG_RETURN(1);
     276             :                                 }
     277           4 :                                 result = mysql_store_result(H->server);
     278           4 :                                 if (result) {
     279           0 :                                         mysql_free_result(result);
     280             :                                 }
     281             :                         }
     282        1175 :                         PDO_DBG_RETURN((int)c);
     283             :                 }
     284             :         }
     285             : }
     286             : /* }}} */
     287             : 
     288             : /* {{{ pdo_mysql_last_insert_id */
     289          13 : static char *pdo_mysql_last_insert_id(pdo_dbh_t *dbh, const char *name, size_t *len)
     290             : {
     291          13 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     292          13 :         char *id = php_pdo_int64_to_str(mysql_insert_id(H->server));
     293          13 :         PDO_DBG_ENTER("pdo_mysql_last_insert_id");
     294          13 :         *len = strlen(id);
     295          13 :         PDO_DBG_RETURN(id);
     296             : }
     297             : /* }}} */
     298             : 
     299             : /* {{{ mysql_handle_quoter */
     300         338 : static int mysql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype )
     301             : {
     302         338 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     303         338 :         PDO_DBG_ENTER("mysql_handle_quoter");
     304         338 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     305         338 :         PDO_DBG_INF_FMT("unquoted=%.*s", (int)unquotedlen, unquoted);
     306         338 :         *quoted = safe_emalloc(2, unquotedlen, 3);
     307         338 :         *quotedlen = mysql_real_escape_string(H->server, *quoted + 1, unquoted, unquotedlen);
     308         338 :         (*quoted)[0] =(*quoted)[++*quotedlen] = '\'';
     309         338 :         (*quoted)[++*quotedlen] = '\0';
     310         338 :         PDO_DBG_INF_FMT("quoted=%.*s", (int)*quotedlen, *quoted);
     311         338 :         PDO_DBG_RETURN(1);
     312             : }
     313             : /* }}} */
     314             : 
     315             : /* {{{ mysql_handle_begin */
     316          21 : static int mysql_handle_begin(pdo_dbh_t *dbh)
     317             : {
     318          21 :         PDO_DBG_ENTER("mysql_handle_quoter");
     319          21 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     320          21 :         PDO_DBG_RETURN(0 <= mysql_handle_doer(dbh, ZEND_STRL("START TRANSACTION")));
     321             : }
     322             : /* }}} */
     323             : 
     324             : /* {{{ mysql_handle_commit */
     325          10 : static int mysql_handle_commit(pdo_dbh_t *dbh)
     326             : {
     327          10 :         PDO_DBG_ENTER("mysql_handle_commit");
     328          10 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     329             : #if MYSQL_VERSION_ID >= 40100 || defined(PDO_USE_MYSQLND)
     330          10 :         PDO_DBG_RETURN(0 <= mysql_commit(((pdo_mysql_db_handle *)dbh->driver_data)->server));
     331             : #else
     332             :         PDO_DBG_RETURN(0 <= mysql_handle_doer(dbh, ZEND_STRL("COMMIT")));
     333             : #endif
     334             : }
     335             : /* }}} */
     336             : 
     337             : /* {{{ mysql_handle_rollback */
     338          11 : static int mysql_handle_rollback(pdo_dbh_t *dbh)
     339             : {
     340          11 :         PDO_DBG_ENTER("mysql_handle_rollback");
     341          11 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     342             : #if MYSQL_VERSION_ID >= 40100 || defined(PDO_USE_MYSQLND)
     343          11 :         PDO_DBG_RETURN(0 <= mysql_rollback(((pdo_mysql_db_handle *)dbh->driver_data)->server));
     344             : #else
     345             :         PDO_DBG_RETURN(0 <= mysql_handle_doer(dbh, ZEND_STRL("ROLLBACK")));
     346             : #endif
     347             : }
     348             : /* }}} */
     349             : 
     350             : /* {{{ mysql_handle_autocommit */
     351           7 : static inline int mysql_handle_autocommit(pdo_dbh_t *dbh)
     352             : {
     353           7 :         PDO_DBG_ENTER("mysql_handle_autocommit");
     354           7 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     355           7 :         PDO_DBG_INF_FMT("dbh->autocommit=%d", dbh->auto_commit);
     356             : #if MYSQL_VERSION_ID >= 40100 || defined(PDO_USE_MYSQLND)
     357           7 :         PDO_DBG_RETURN(0 <= mysql_autocommit(((pdo_mysql_db_handle *)dbh->driver_data)->server, dbh->auto_commit));
     358             : #else
     359             :         if (dbh->auto_commit) {
     360             :                 PDO_DBG_RETURN(0 <= mysql_handle_doer(dbh, ZEND_STRL("SET AUTOCOMMIT=1")));
     361             :         } else {
     362             :                 PDO_DBG_RETURN(0 <= mysql_handle_doer(dbh, ZEND_STRL("SET AUTOCOMMIT=0")));
     363             :         }
     364             : #endif
     365             : }
     366             : /* }}} */
     367             : 
     368             : /* {{{ pdo_mysql_set_attribute */
     369         327 : static int pdo_mysql_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)
     370             : {
     371         327 :         PDO_DBG_ENTER("pdo_mysql_set_attribute");
     372         327 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     373         327 :         PDO_DBG_INF_FMT("attr=%l", attr);
     374         327 :         switch (attr) {
     375             :                 case PDO_ATTR_AUTOCOMMIT:
     376          10 :                         convert_to_boolean(val);
     377             :                         /* ignore if the new value equals the old one */
     378          20 :                         if (dbh->auto_commit ^ (Z_TYPE_P(val) == IS_TRUE)) {
     379           6 :                                 dbh->auto_commit = (Z_TYPE_P(val) == IS_TRUE);
     380           6 :                                 mysql_handle_autocommit(dbh);
     381             :                         }
     382          10 :                         PDO_DBG_RETURN(1);
     383             : 
     384             :                 case PDO_MYSQL_ATTR_USE_BUFFERED_QUERY:
     385          38 :                         convert_to_boolean(val);
     386             :                         /* ignore if the new value equals the old one */
     387          76 :                         ((pdo_mysql_db_handle *)dbh->driver_data)->buffered = (Z_TYPE_P(val) == IS_TRUE);
     388          38 :                         PDO_DBG_RETURN(1);
     389             :                 case PDO_MYSQL_ATTR_DIRECT_QUERY:
     390             :                 case PDO_ATTR_EMULATE_PREPARES:
     391         223 :                         convert_to_boolean(val);
     392             :                         /* ignore if the new value equals the old one */
     393         446 :                         ((pdo_mysql_db_handle *)dbh->driver_data)->emulate_prepare = (Z_TYPE_P(val) == IS_TRUE);
     394         223 :                         PDO_DBG_RETURN(1);
     395             :                 case PDO_ATTR_FETCH_TABLE_NAMES:
     396           2 :                         convert_to_boolean(val);
     397           4 :                         ((pdo_mysql_db_handle *)dbh->driver_data)->fetch_table_names = (Z_TYPE_P(val) == IS_TRUE);
     398           2 :                         PDO_DBG_RETURN(1);
     399             : #ifndef PDO_USE_MYSQLND
     400             :                 case PDO_MYSQL_ATTR_MAX_BUFFER_SIZE:
     401             :                         convert_to_long(val);
     402             :                         if (Z_LVAL_P(val) < 0) {
     403             :                                 /* TODO: Johannes, can we throw a warning here? */
     404             :                                 ((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size = 1024*1024;
     405             :                                 PDO_DBG_INF_FMT("Adjusting invalid buffer size to =%l", ((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size);
     406             :                         } else {
     407             :                                 ((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size = Z_LVAL_P(val);
     408             :                         }
     409             :                         PDO_DBG_RETURN(1);
     410             :                         break;
     411             : #endif
     412             : 
     413             :                 default:
     414          54 :                         PDO_DBG_RETURN(0);
     415             :         }
     416             : }
     417             : /* }}} */
     418             : 
     419             : /* {{{ pdo_mysql_get_attribute */
     420         112 : static int pdo_mysql_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
     421             : {
     422         112 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     423             : 
     424         112 :         PDO_DBG_ENTER("pdo_mysql_get_attribute");
     425         112 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     426         112 :         PDO_DBG_INF_FMT("attr=%l", attr);
     427         112 :         switch (attr) {
     428             :                 case PDO_ATTR_CLIENT_VERSION:
     429           8 :                         ZVAL_STRING(return_value, (char *)mysql_get_client_info());
     430           4 :                         break;
     431             : 
     432             :                 case PDO_ATTR_SERVER_VERSION:
     433          10 :                         ZVAL_STRING(return_value, (char *)mysql_get_server_info(H->server));
     434           5 :                         break;
     435             : 
     436             :                 case PDO_ATTR_CONNECTION_STATUS:
     437           6 :                         ZVAL_STRING(return_value, (char *)mysql_get_host_info(H->server));
     438           3 :                         break;
     439             :                 case PDO_ATTR_SERVER_INFO: {
     440             : #if defined(PDO_USE_MYSQLND)
     441             :                         zend_string *tmp;
     442             : 
     443           3 :                         if (mysqlnd_stat(H->server, &tmp) == PASS) {
     444           3 :                                 ZVAL_STR(return_value, tmp);
     445             : #else
     446             :                         char *tmp;
     447             :                         if ((tmp = (char *)mysql_stat(H->server))) {
     448             :                                 ZVAL_STRING(return_value, tmp);
     449             : #endif
     450             :                         } else {
     451           0 :                                 pdo_mysql_error(dbh);
     452           0 :                                 PDO_DBG_RETURN(-1);
     453             :                         }
     454             :                 }
     455           3 :                         break;
     456             :                 case PDO_ATTR_AUTOCOMMIT:
     457          19 :                         ZVAL_LONG(return_value, dbh->auto_commit);
     458          19 :                         break;
     459             : 
     460             :                 case PDO_MYSQL_ATTR_USE_BUFFERED_QUERY:
     461           6 :                         ZVAL_LONG(return_value, H->buffered);
     462           6 :                         break;
     463             : 
     464             :                 case PDO_ATTR_EMULATE_PREPARES:
     465             :                 case PDO_MYSQL_ATTR_DIRECT_QUERY:
     466          54 :                         ZVAL_LONG(return_value, H->emulate_prepare);
     467          54 :                         break;
     468             : 
     469             : #ifndef PDO_USE_MYSQLND
     470             :                 case PDO_MYSQL_ATTR_MAX_BUFFER_SIZE:
     471             :                         ZVAL_LONG(return_value, H->max_buffer_size);
     472             :                         break;
     473             : #endif
     474             : 
     475             :                 default:
     476          18 :                         PDO_DBG_RETURN(0);
     477             :         }
     478             : 
     479          94 :         PDO_DBG_RETURN(1);
     480             : }
     481             : /* }}} */
     482             : 
     483             : /* {{{ pdo_mysql_check_liveness */
     484          25 : static int pdo_mysql_check_liveness(pdo_dbh_t *dbh)
     485             : {
     486          25 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     487             : #if MYSQL_VERSION_ID <= 32230
     488             :         void (*handler) (int);
     489             :         unsigned int my_errno;
     490             : #endif
     491             : 
     492          25 :         PDO_DBG_ENTER("pdo_mysql_check_liveness");
     493          25 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     494             : 
     495             : #if MYSQL_VERSION_ID > 32230
     496          25 :         if (mysql_ping(H->server)) {
     497           1 :                 PDO_DBG_RETURN(FAILURE);
     498             :         }
     499             : #else /* no mysql_ping() */
     500             :         handler = signal(SIGPIPE, SIG_IGN);
     501             :         mysql_stat(H->server);
     502             :         switch (mysql_errno(H->server)) {
     503             :                 case CR_SERVER_GONE_ERROR:
     504             :                 case CR_SERVER_LOST:
     505             :                         signal(SIGPIPE, handler);
     506             :                         PDO_DBG_RETURN(FAILURE);
     507             :                 default:
     508             :                         break;
     509             :         }
     510             :         signal(SIGPIPE, handler);
     511             : #endif /* end mysql_ping() */
     512          24 :         PDO_DBG_RETURN(SUCCESS);
     513             : }
     514             : /* }}} */
     515             : 
     516             : /* {{{ mysql_methods */
     517             : static struct pdo_dbh_methods mysql_methods = {
     518             :         mysql_handle_closer,
     519             :         mysql_handle_preparer,
     520             :         mysql_handle_doer,
     521             :         mysql_handle_quoter,
     522             :         mysql_handle_begin,
     523             :         mysql_handle_commit,
     524             :         mysql_handle_rollback,
     525             :         pdo_mysql_set_attribute,
     526             :         pdo_mysql_last_insert_id,
     527             :         pdo_mysql_fetch_error_func,
     528             :         pdo_mysql_get_attribute,
     529             :         pdo_mysql_check_liveness
     530             : };
     531             : /* }}} */
     532             : 
     533             : #ifdef PHP_WIN32
     534             : # define PDO_DEFAULT_MYSQL_UNIX_ADDR    NULL
     535             : #else
     536             : # define PDO_DEFAULT_MYSQL_UNIX_ADDR    PDO_MYSQL_G(default_socket)
     537             : #endif
     538             : 
     539             : /* {{{ pdo_mysql_handle_factory */
     540         501 : static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
     541             : {
     542             :         pdo_mysql_db_handle *H;
     543         501 :         int i, ret = 0;
     544         501 :         char *host = NULL, *unix_socket = NULL;
     545         501 :         unsigned int port = 3306;
     546             :         char *dbname;
     547             :         struct pdo_data_src_parser vars[] = {
     548             :                 { "charset",  NULL,   0 },
     549             :                 { "dbname",   "",   0 },
     550             :                 { "host",   "localhost",    0 },
     551             :                 { "port",   "3306", 0 },
     552         501 :                 { "unix_socket",  PDO_DEFAULT_MYSQL_UNIX_ADDR,        0 },
     553         501 :         };
     554             :         int connect_opts = 0
     555             : #ifdef CLIENT_MULTI_RESULTS
     556             :                 |CLIENT_MULTI_RESULTS
     557             : #endif
     558         501 :                 ;
     559             : #if defined(PDO_USE_MYSQLND)
     560         501 :         size_t dbname_len = 0;
     561         501 :         size_t password_len = 0;
     562             : #endif
     563             : 
     564             : #ifdef CLIENT_MULTI_STATEMENTS
     565         501 :         if (!driver_options) {
     566         466 :                 connect_opts |= CLIENT_MULTI_STATEMENTS;
     567          35 :         } else if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MULTI_STATEMENTS, 1)) {
     568          32 :                 connect_opts |= CLIENT_MULTI_STATEMENTS;
     569             :         }
     570             : #endif
     571             : 
     572         501 :         PDO_DBG_ENTER("pdo_mysql_handle_factory");
     573         501 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     574             : #ifdef CLIENT_MULTI_RESULTS
     575         501 :         PDO_DBG_INF("multi results");
     576             : #endif
     577             : 
     578         501 :         php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 5);
     579             : 
     580         508 :         H = pecalloc(1, sizeof(pdo_mysql_db_handle), dbh->is_persistent);
     581             : 
     582         501 :         H->einfo.errcode = 0;
     583         501 :         H->einfo.errmsg = NULL;
     584             : 
     585             :         /* allocate an environment */
     586             : 
     587             :         /* handle for the server */
     588         501 :         if (!(H->server = pdo_mysql_init(dbh->is_persistent))) {
     589           0 :                 pdo_mysql_error(dbh);
     590           0 :                 goto cleanup;
     591             :         }
     592             : 
     593         501 :         dbh->driver_data = H;
     594             : 
     595             : #ifndef PDO_USE_MYSQLND
     596             :         H->max_buffer_size = 1024*1024;
     597             : #endif
     598             : 
     599         501 :         H->buffered = H->emulate_prepare = 1;
     600             : 
     601             :         /* handle MySQL options */
     602         501 :         if (driver_options) {
     603          35 :                 zend_long connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
     604          35 :                 zend_long local_infile = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_LOCAL_INFILE, 0);
     605          35 :                 zend_string *init_cmd = NULL;
     606             : #ifndef PDO_USE_MYSQLND
     607             :                 zend_string *default_file = NULL, *default_group = NULL;
     608             : #endif
     609          35 :                 zend_long compress = 0;
     610          35 :                 zend_string *ssl_key = NULL, *ssl_cert = NULL, *ssl_ca = NULL, *ssl_capath = NULL, *ssl_cipher = NULL;
     611          35 :                 H->buffered = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_USE_BUFFERED_QUERY, 1);
     612             : 
     613          35 :                 H->emulate_prepare = pdo_attr_lval(driver_options,
     614          35 :                         PDO_MYSQL_ATTR_DIRECT_QUERY, H->emulate_prepare);
     615          35 :                 H->emulate_prepare = pdo_attr_lval(driver_options,
     616          35 :                         PDO_ATTR_EMULATE_PREPARES, H->emulate_prepare);
     617             : 
     618             : #ifndef PDO_USE_MYSQLND
     619             :                 H->max_buffer_size = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MAX_BUFFER_SIZE, H->max_buffer_size);
     620             : #endif
     621             : 
     622          35 :                 if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_FOUND_ROWS, 0)) {
     623           1 :                         connect_opts |= CLIENT_FOUND_ROWS;
     624             :                 }
     625             : 
     626          35 :                 if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_IGNORE_SPACE, 0)) {
     627           0 :                         connect_opts |= CLIENT_IGNORE_SPACE;
     628             :                 }
     629             : 
     630          35 :                 if (mysql_options(H->server, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout)) {
     631           0 :                         pdo_mysql_error(dbh);
     632           0 :                         goto cleanup;
     633             :                 }
     634             : 
     635             : #if PHP_API_VERSION < 20100412
     636             :                 if ((PG(open_basedir) && PG(open_basedir)[0] != '\0') || PG(safe_mode))
     637             : #else
     638          35 :                 if (PG(open_basedir) && PG(open_basedir)[0] != '\0')
     639             : #endif
     640             :                 {
     641           0 :                         local_infile = 0;
     642             :                 }
     643             : #if defined(MYSQL_OPT_LOCAL_INFILE) || defined(PDO_USE_MYSQLND)
     644          35 :                 if (mysql_options(H->server, MYSQL_OPT_LOCAL_INFILE, (const char *)&local_infile)) {
     645           0 :                         pdo_mysql_error(dbh);
     646           0 :                         goto cleanup;
     647             :                 }
     648             : #endif
     649             : #ifdef MYSQL_OPT_RECONNECT
     650             :                 /* since 5.0.3, the default for this option is 0 if not specified.
     651             :                  * we want the old behaviour
     652             :                  * mysqlnd doesn't support reconnect, thus we don't have "|| defined(PDO_USE_MYSQLND)"
     653             :                 */
     654             :                 {
     655             :                         zend_long reconnect = 1;
     656             :                         mysql_options(H->server, MYSQL_OPT_RECONNECT, (const char*)&reconnect);
     657             :                 }
     658             : #endif
     659          35 :                 init_cmd = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_INIT_COMMAND, NULL);
     660          35 :                 if (init_cmd) {
     661           4 :                         if (mysql_options(H->server, MYSQL_INIT_COMMAND, (const char *)ZSTR_VAL(init_cmd))) {
     662             :                                 zend_string_release(init_cmd);
     663           0 :                                 pdo_mysql_error(dbh);
     664           0 :                                 goto cleanup;
     665             :                         }
     666             :                         zend_string_release(init_cmd);
     667             :                 }
     668             : #ifndef PDO_USE_MYSQLND
     669             :                 default_file = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_READ_DEFAULT_FILE, NULL);
     670             :                 if (default_file) {
     671             :                         if (mysql_options(H->server, MYSQL_READ_DEFAULT_FILE, (const char *)ZSTR_VAL(default_file))) {
     672             :                                 zend_string_release(default_file);
     673             :                                 pdo_mysql_error(dbh);
     674             :                                 goto cleanup;
     675             :                         }
     676             :                         zend_string_release(default_file);
     677             :                 }
     678             : 
     679             :                 default_group = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_READ_DEFAULT_GROUP, NULL);
     680             :                 if (default_group) {
     681             :                         if (mysql_options(H->server, MYSQL_READ_DEFAULT_GROUP, (const char *)ZSTR_VAL(default_group))) {
     682             :                                 zend_string_release(default_group);
     683             :                                 pdo_mysql_error(dbh);
     684             :                                 goto cleanup;
     685             :                         }
     686             :                         zend_string_release(default_group);
     687             :                 }
     688             : #endif
     689          35 :                 compress = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_COMPRESS, 0);
     690          35 :                 if (compress) {
     691           0 :                         if (mysql_options(H->server, MYSQL_OPT_COMPRESS, 0)) {
     692           0 :                                 pdo_mysql_error(dbh);
     693           0 :                                 goto cleanup;
     694             :                         }
     695             :                 }
     696             : 
     697          35 :                 ssl_key = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_KEY, NULL);
     698          35 :                 ssl_cert = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CERT, NULL);
     699          35 :                 ssl_ca = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CA, NULL);
     700          35 :                 ssl_capath = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CAPATH, NULL);
     701          35 :                 ssl_cipher = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CIPHER, NULL);
     702             : 
     703          35 :                 if (ssl_key || ssl_cert || ssl_ca || ssl_capath || ssl_cipher) {
     704           0 :                         mysql_ssl_set(H->server,
     705             :                                         ssl_key? ZSTR_VAL(ssl_key) : NULL,
     706             :                                         ssl_cert? ZSTR_VAL(ssl_cert) : NULL,
     707             :                                         ssl_ca? ZSTR_VAL(ssl_ca) : NULL,
     708             :                                         ssl_capath? ZSTR_VAL(ssl_capath) : NULL,
     709             :                                         ssl_cipher? ZSTR_VAL(ssl_cipher) : NULL);
     710           0 :                         if (ssl_key) {
     711             :                                 zend_string_release(ssl_key);
     712             :                         }
     713           0 :                         if (ssl_cert) {
     714             :                                 zend_string_release(ssl_cert);
     715             :                         }
     716           0 :                         if (ssl_ca) {
     717             :                                 zend_string_release(ssl_ca);
     718             :                         }
     719           0 :                         if (ssl_capath) {
     720             :                                 zend_string_release(ssl_capath);
     721             :                         }
     722           0 :                         if (ssl_cipher) {
     723             :                                 zend_string_release(ssl_cipher);
     724             :                         }
     725             :                 }
     726             : 
     727             : #if MYSQL_VERSION_ID > 50605 || defined(PDO_USE_MYSQLND)
     728             :                 {
     729          35 :                         zend_string *public_key = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SERVER_PUBLIC_KEY, NULL);
     730          35 :                         if (public_key) {
     731           0 :                                 if (mysql_options(H->server, MYSQL_SERVER_PUBLIC_KEY, ZSTR_VAL(public_key))) {
     732           0 :                                         pdo_mysql_error(dbh);
     733             :                                         zend_string_release(public_key);
     734           0 :                                         goto cleanup;
     735             :                                 }
     736             :                                 zend_string_release(public_key);
     737             :                         }
     738             :                 }
     739             : #endif
     740             :         }
     741             : 
     742             : #ifdef PDO_MYSQL_HAS_CHARSET
     743         501 :         if (vars[0].optval && mysql_options(H->server, MYSQL_SET_CHARSET_NAME, vars[0].optval)) {
     744           0 :                 pdo_mysql_error(dbh);
     745           0 :                 goto cleanup;
     746             :         }
     747             : #endif
     748             : 
     749         501 :         dbname = vars[1].optval;
     750         501 :         host = vars[2].optval;
     751         501 :         if(vars[3].optval) {
     752         501 :                 port = atoi(vars[3].optval);
     753             :         }
     754             : 
     755             : #ifdef PHP_WIN32
     756             :         if (vars[2].optval && !strcmp(".", vars[2].optval)) {
     757             : #else
     758         501 :         if (vars[2].optval && !strcmp("localhost", vars[2].optval)) {
     759             : #endif
     760         497 :                 unix_socket = vars[4].optval;
     761             :         }
     762             : 
     763             :         /* TODO: - Check zval cache + ZTS */
     764             : #ifdef PDO_USE_MYSQLND
     765         501 :         if (dbname) {
     766         501 :                 dbname_len = strlen(dbname);
     767             :         }
     768             : 
     769         501 :         if (dbh->password) {
     770         500 :                 password_len = strlen(dbh->password);
     771             :         }
     772             : 
     773         501 :         if (mysqlnd_connect(H->server, host, dbh->username, dbh->password, password_len, dbname, dbname_len,
     774             :                                                 port, unix_socket, connect_opts, MYSQLND_CLIENT_NO_FLAG) == NULL) {
     775             : #else
     776             :         if (mysql_real_connect(H->server, host, dbh->username, dbh->password, dbname, port, unix_socket, connect_opts) == NULL) {
     777             : #endif
     778          14 :                 pdo_mysql_error(dbh);
     779          14 :                 goto cleanup;
     780             :         }
     781             : 
     782         487 :         if (!dbh->auto_commit) {
     783           1 :                 mysql_handle_autocommit(dbh);
     784             :         }
     785             : 
     786         487 :         H->attached = 1;
     787             : 
     788         487 :         dbh->alloc_own_columns = 1;
     789         487 :         dbh->max_escaped_char_length = 2;
     790         487 :         dbh->methods = &mysql_methods;
     791             : 
     792         487 :         ret = 1;
     793             : 
     794             : cleanup:
     795        3006 :         for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
     796        2505 :                 if (vars[i].freeme) {
     797        1002 :                         efree(vars[i].optval);
     798             :                 }
     799             :         }
     800             : 
     801         501 :         dbh->methods = &mysql_methods;
     802             : 
     803         501 :         PDO_DBG_RETURN(ret);
     804             : }
     805             : /* }}} */
     806             : 
     807             : pdo_driver_t pdo_mysql_driver = {
     808             :         PDO_DRIVER_HEADER(mysql),
     809             :         pdo_mysql_handle_factory
     810             : };
     811             : 
     812             : /*
     813             :  * Local variables:
     814             :  * tab-width: 4
     815             :  * c-basic-offset: 4
     816             :  * End:
     817             :  * vim600: noet sw=4 ts=4 fdm=marker
     818             :  * vim<600: noet sw=4 ts=4
     819             :  */

Generated by: LCOV version 1.10

Generated at Wed, 02 Sep 2015 17:19:15 +0000 (47 hours ago)

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