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: 318 353 90.1 %
Date: 2018-12-06 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-2018 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Author: George Schlossnagle <george@omniti.com>                      |
      16             :   |         Wez Furlong <wez@php.net>                                    |
      17             :   |         Johannes Schlueter <johannes@mysql.com>                      |
      18             :   +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : 
      23             : #ifdef HAVE_CONFIG_H
      24             : #include "config.h"
      25             : #endif
      26             : 
      27             : #include "php.h"
      28             : #include "php_ini.h"
      29             : #include "ext/standard/info.h"
      30             : #include "pdo/php_pdo.h"
      31             : #include "pdo/php_pdo_driver.h"
      32             : #include "php_pdo_mysql.h"
      33             : #include "php_pdo_mysql_int.h"
      34             : #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         822 : int _pdo_mysql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line)
      47             : {
      48         822 :         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         822 :         pdo_mysql_stmt *S = NULL;
      52             : 
      53         822 :         PDO_DBG_ENTER("_pdo_mysql_error");
      54         822 :         PDO_DBG_INF_FMT("file=%s line=%d", file, line);
      55         822 :         if (stmt) {
      56          35 :                 S = (pdo_mysql_stmt*)stmt->driver_data;
      57          35 :                 pdo_err = &stmt->error_code;
      58          35 :                 einfo   = &S->einfo;
      59             :         } else {
      60         787 :                 pdo_err = &dbh->error_code;
      61         787 :                 einfo   = &H->einfo;
      62             :         }
      63             : 
      64         822 :         if (S && S->stmt) {
      65           5 :                 einfo->errcode = mysql_stmt_errno(S->stmt);
      66             :         } else {
      67         817 :                 einfo->errcode = mysql_errno(H->server);
      68             :         }
      69             : 
      70         822 :         einfo->file = file;
      71         822 :         einfo->line = line;
      72             : 
      73         822 :         if (einfo->errmsg) {
      74         488 :                 pefree(einfo->errmsg, dbh->is_persistent);
      75         488 :                 einfo->errmsg = NULL;
      76             :         }
      77             : 
      78         822 :         if (einfo->errcode) {
      79         819 :                 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         811 :                 } 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         811 :                         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         819 :         if (S && S->stmt) {
     101           5 :                 strcpy(*pdo_err, mysql_stmt_sqlstate(S->stmt));
     102             :         } else {
     103         814 :                 strcpy(*pdo_err, mysql_sqlstate(H->server));
     104             :         }
     105             : 
     106         819 :         if (!dbh->methods) {
     107          15 :                 PDO_DBG_INF("Throwing exception");
     108          15 :                 zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode, "SQLSTATE[%s] [%d] %s",
     109             :                                 *pdo_err, einfo->errcode, einfo->errmsg);
     110             :         }
     111             : 
     112         819 :         PDO_DBG_RETURN(einfo->errcode);
     113             : }
     114             : /* }}} */
     115             : 
     116             : /* {{{ pdo_mysql_fetch_error_func */
     117         828 : static int pdo_mysql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info)
     118             : {
     119         828 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     120         828 :         pdo_mysql_error_info *einfo = &H->einfo;
     121             : 
     122         828 :         PDO_DBG_ENTER("pdo_mysql_fetch_error_func");
     123         828 :         PDO_DBG_INF_FMT("dbh=%p stmt=%p", dbh, stmt);
     124         828 :         if (stmt) {
     125         767 :                 pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
     126         767 :                 einfo = &S->einfo;
     127             :         } else {
     128          61 :                 einfo = &H->einfo;
     129             :         }
     130             : 
     131         828 :         if (einfo->errcode) {
     132         107 :                 add_next_index_long(info, einfo->errcode);
     133         107 :                 add_next_index_string(info, einfo->errmsg);
     134             :         }
     135             : 
     136         828 :         PDO_DBG_RETURN(1);
     137             : }
     138             : /* }}} */
     139             : 
     140             : /* {{{ mysql_handle_closer */
     141         534 : static int mysql_handle_closer(pdo_dbh_t *dbh)
     142             : {
     143         534 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     144             : 
     145         534 :         PDO_DBG_ENTER("mysql_handle_closer");
     146         534 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     147         534 :         if (H) {
     148         534 :                 if (H->server) {
     149         534 :                         mysql_close(H->server);
     150             :                 }
     151         534 :                 if (H->einfo.errmsg) {
     152         297 :                         pefree(H->einfo.errmsg, dbh->is_persistent);
     153             :                 }
     154         534 :                 pefree(H, dbh->is_persistent);
     155         534 :                 dbh->driver_data = NULL;
     156             :         }
     157         534 :         PDO_DBG_RETURN(0);
     158             : }
     159             : /* }}} */
     160             : 
     161             : /* {{{ mysql_handle_preparer */
     162        1752 : 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        1752 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     165        1752 :         pdo_mysql_stmt *S = ecalloc(1, sizeof(pdo_mysql_stmt));
     166        1752 :         char *nsql = NULL;
     167        1752 :         size_t nsql_len = 0;
     168             :         int ret;
     169             :         int server_version;
     170             : 
     171        1752 :         PDO_DBG_ENTER("mysql_handle_preparer");
     172        1752 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     173        1752 :         PDO_DBG_INF_FMT("sql=%.*s", (int)sql_len, sql);
     174             : 
     175        1752 :         S->H = H;
     176        1752 :         stmt->driver_data = S;
     177        1752 :         stmt->methods = &mysql_stmt_methods;
     178             : 
     179        1752 :         if (H->emulate_prepare) {
     180         869 :                 goto end;
     181             :         }
     182             : 
     183         883 :         server_version = mysql_get_server_version(H->server);
     184         883 :         if (server_version < 40100) {
     185           0 :                 goto fallback;
     186             :         }
     187         883 :         stmt->supports_placeholders = PDO_PLACEHOLDER_POSITIONAL;
     188         883 :         ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len);
     189             : 
     190         883 :         if (ret == 1) {
     191             :                 /* query was rewritten */
     192         116 :                 sql = nsql;
     193         116 :                 sql_len = nsql_len;
     194         767 :         } 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         882 :         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         882 :         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         871 :         if (nsql) {
     224         115 :                 efree(nsql);
     225             :         }
     226             : 
     227         871 :         S->num_params = mysql_stmt_param_count(S->stmt);
     228             : 
     229         871 :         if (S->num_params) {
     230         384 :                 S->params_given = 0;
     231             : #if defined(PDO_USE_MYSQLND)
     232         384 :                 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         871 :         dbh->alloc_own_columns = 1;
     240             : 
     241         871 :         S->max_length = pdo_attr_lval(driver_options, PDO_ATTR_MAX_COLUMN_LEN, 0);
     242             : 
     243         871 :         PDO_DBG_RETURN(1);
     244             : 
     245         870 : fallback:
     246         870 : end:
     247         870 :         stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
     248             : 
     249         870 :         PDO_DBG_RETURN(1);
     250             : }
     251             : /* }}} */
     252             : 
     253             : /* {{{ mysql_handle_doer */
     254        1951 : static zend_long mysql_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
     255             : {
     256        1951 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     257        1951 :         PDO_DBG_ENTER("mysql_handle_doer");
     258        1951 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     259        1951 :         PDO_DBG_INF_FMT("sql=%.*s", (int)sql_len, sql);
     260             : 
     261        1951 :         if (mysql_real_query(H->server, sql, sql_len)) {
     262         759 :                 pdo_mysql_error(dbh);
     263         759 :                 PDO_DBG_RETURN(-1);
     264             :         } else {
     265        1192 :                 my_ulonglong c = mysql_affected_rows(H->server);
     266        1192 :                 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        2382 :                         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        1189 :                         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         366 : 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         366 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     303         366 :         zend_bool use_national_character_set = 0;
     304             : 
     305         366 :         if (H->assume_national_character_set_strings) {
     306           3 :                 use_national_character_set = 1;
     307             :         }
     308         366 :         if ((paramtype & PDO_PARAM_STR_NATL) == PDO_PARAM_STR_NATL) {
     309           2 :                 use_national_character_set = 1;
     310             :         }
     311         366 :         if ((paramtype & PDO_PARAM_STR_CHAR) == PDO_PARAM_STR_CHAR) {
     312           2 :                 use_national_character_set = 0;
     313             :         }
     314             : 
     315         366 :         PDO_DBG_ENTER("mysql_handle_quoter");
     316         366 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     317         366 :         PDO_DBG_INF_FMT("unquoted=%.*s", (int)unquotedlen, unquoted);
     318         366 :         *quoted = safe_emalloc(2, unquotedlen, 3 + (use_national_character_set ? 1 : 0));
     319             : 
     320         366 :         if (use_national_character_set) {
     321           3 :                 *quotedlen = mysql_real_escape_string(H->server, *quoted + 2, unquoted, unquotedlen);
     322           3 :                 (*quoted)[0] = 'N';
     323           3 :                 (*quoted)[1] = '\'';
     324             : 
     325           3 :                 ++*quotedlen; /* N prefix */
     326             :         } else {
     327         363 :                 *quotedlen = mysql_real_escape_string(H->server, *quoted + 1, unquoted, unquotedlen);
     328         363 :                 (*quoted)[0] = '\'';
     329             :         }
     330             : 
     331         366 :         (*quoted)[++*quotedlen] = '\'';
     332         366 :         (*quoted)[++*quotedlen] = '\0';
     333         366 :         PDO_DBG_INF_FMT("quoted=%.*s", (int)*quotedlen, *quoted);
     334         366 :         PDO_DBG_RETURN(1);
     335             : }
     336             : /* }}} */
     337             : 
     338             : /* {{{ mysql_handle_begin */
     339          21 : static int mysql_handle_begin(pdo_dbh_t *dbh)
     340             : {
     341          21 :         PDO_DBG_ENTER("mysql_handle_quoter");
     342          21 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     343          21 :         PDO_DBG_RETURN(0 <= mysql_handle_doer(dbh, ZEND_STRL("START TRANSACTION")));
     344             : }
     345             : /* }}} */
     346             : 
     347             : /* {{{ mysql_handle_commit */
     348          10 : static int mysql_handle_commit(pdo_dbh_t *dbh)
     349             : {
     350          10 :         PDO_DBG_ENTER("mysql_handle_commit");
     351          10 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     352             : #if MYSQL_VERSION_ID >= 40100 || defined(PDO_USE_MYSQLND)
     353          10 :         PDO_DBG_RETURN(0 == mysql_commit(((pdo_mysql_db_handle *)dbh->driver_data)->server));
     354             : #else
     355             :         PDO_DBG_RETURN(0 <= mysql_handle_doer(dbh, ZEND_STRL("COMMIT")));
     356             : #endif
     357             : }
     358             : /* }}} */
     359             : 
     360             : /* {{{ mysql_handle_rollback */
     361          11 : static int mysql_handle_rollback(pdo_dbh_t *dbh)
     362             : {
     363          11 :         PDO_DBG_ENTER("mysql_handle_rollback");
     364          11 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     365             : #if MYSQL_VERSION_ID >= 40100 || defined(PDO_USE_MYSQLND)
     366          11 :         PDO_DBG_RETURN(0 <= mysql_rollback(((pdo_mysql_db_handle *)dbh->driver_data)->server));
     367             : #else
     368             :         PDO_DBG_RETURN(0 <= mysql_handle_doer(dbh, ZEND_STRL("ROLLBACK")));
     369             : #endif
     370             : }
     371             : /* }}} */
     372             : 
     373             : /* {{{ mysql_handle_autocommit */
     374           7 : static inline int mysql_handle_autocommit(pdo_dbh_t *dbh)
     375             : {
     376           7 :         PDO_DBG_ENTER("mysql_handle_autocommit");
     377           7 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     378           7 :         PDO_DBG_INF_FMT("dbh->autocommit=%d", dbh->auto_commit);
     379             : #if MYSQL_VERSION_ID >= 40100 || defined(PDO_USE_MYSQLND)
     380           7 :         PDO_DBG_RETURN(0 <= mysql_autocommit(((pdo_mysql_db_handle *)dbh->driver_data)->server, dbh->auto_commit));
     381             : #else
     382             :         if (dbh->auto_commit) {
     383             :                 PDO_DBG_RETURN(0 <= mysql_handle_doer(dbh, ZEND_STRL("SET AUTOCOMMIT=1")));
     384             :         } else {
     385             :                 PDO_DBG_RETURN(0 <= mysql_handle_doer(dbh, ZEND_STRL("SET AUTOCOMMIT=0")));
     386             :         }
     387             : #endif
     388             : }
     389             : /* }}} */
     390             : 
     391             : /* {{{ pdo_mysql_set_attribute */
     392         338 : static int pdo_mysql_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)
     393             : {
     394         338 :         zend_long lval = zval_get_long(val);
     395         338 :         zend_bool bval = lval ? 1 : 0;
     396         338 :         PDO_DBG_ENTER("pdo_mysql_set_attribute");
     397         338 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     398         338 :         PDO_DBG_INF_FMT("attr=%l", attr);
     399         338 :         switch (attr) {
     400          10 :                 case PDO_ATTR_AUTOCOMMIT:
     401             :                         /* ignore if the new value equals the old one */
     402          10 :                         if (dbh->auto_commit ^ bval) {
     403           6 :                                 dbh->auto_commit = bval;
     404           6 :                                 mysql_handle_autocommit(dbh);
     405             :                         }
     406          10 :                         PDO_DBG_RETURN(1);
     407             : 
     408           1 :                 case PDO_ATTR_DEFAULT_STR_PARAM:
     409           1 :                         ((pdo_mysql_db_handle *)dbh->driver_data)->assume_national_character_set_strings = lval == PDO_PARAM_STR_NATL ? 1 : 0;
     410           1 :                         PDO_DBG_RETURN(1);
     411             : 
     412          38 :                 case PDO_MYSQL_ATTR_USE_BUFFERED_QUERY:
     413             :                         /* ignore if the new value equals the old one */
     414          38 :                         ((pdo_mysql_db_handle *)dbh->driver_data)->buffered = bval;
     415          38 :                         PDO_DBG_RETURN(1);
     416             : 
     417         230 :                 case PDO_MYSQL_ATTR_DIRECT_QUERY:
     418             :                 case PDO_ATTR_EMULATE_PREPARES:
     419             :                         /* ignore if the new value equals the old one */
     420         230 :                         ((pdo_mysql_db_handle *)dbh->driver_data)->emulate_prepare = bval;
     421         230 :                         PDO_DBG_RETURN(1);
     422             : 
     423           2 :                 case PDO_ATTR_FETCH_TABLE_NAMES:
     424           2 :                         ((pdo_mysql_db_handle *)dbh->driver_data)->fetch_table_names = bval;
     425           2 :                         PDO_DBG_RETURN(1);
     426             : 
     427             : #ifndef PDO_USE_MYSQLND
     428             :                 case PDO_MYSQL_ATTR_MAX_BUFFER_SIZE:
     429             :                         if (lval < 0) {
     430             :                                 /* TODO: Johannes, can we throw a warning here? */
     431             :                                 ((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size = 1024*1024;
     432             :                                 PDO_DBG_INF_FMT("Adjusting invalid buffer size to =%l", ((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size);
     433             :                         } else {
     434             :                                 ((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size = lval;
     435             :                         }
     436             :                         PDO_DBG_RETURN(1);
     437             :                         break;
     438             : #endif
     439             : 
     440          57 :                 default:
     441          57 :                         PDO_DBG_RETURN(0);
     442             :         }
     443             : }
     444             : /* }}} */
     445             : 
     446             : /* {{{ pdo_mysql_get_attribute */
     447         115 : static int pdo_mysql_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
     448             : {
     449         115 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     450             : 
     451         115 :         PDO_DBG_ENTER("pdo_mysql_get_attribute");
     452         115 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     453         115 :         PDO_DBG_INF_FMT("attr=%l", attr);
     454         115 :         switch (attr) {
     455           4 :                 case PDO_ATTR_CLIENT_VERSION:
     456           8 :                         ZVAL_STRING(return_value, (char *)mysql_get_client_info());
     457           4 :                         break;
     458             : 
     459           5 :                 case PDO_ATTR_SERVER_VERSION:
     460          10 :                         ZVAL_STRING(return_value, (char *)mysql_get_server_info(H->server));
     461           5 :                         break;
     462             : 
     463           3 :                 case PDO_ATTR_CONNECTION_STATUS:
     464           6 :                         ZVAL_STRING(return_value, (char *)mysql_get_host_info(H->server));
     465           3 :                         break;
     466           3 :                 case PDO_ATTR_SERVER_INFO: {
     467             : #if defined(PDO_USE_MYSQLND)
     468             :                         zend_string *tmp;
     469             : 
     470           3 :                         if (mysqlnd_stat(H->server, &tmp) == PASS) {
     471           3 :                                 ZVAL_STR(return_value, tmp);
     472             : #else
     473             :                         char *tmp;
     474             :                         if ((tmp = (char *)mysql_stat(H->server))) {
     475             :                                 ZVAL_STRING(return_value, tmp);
     476             : #endif
     477             :                         } else {
     478           0 :                                 pdo_mysql_error(dbh);
     479           0 :                                 PDO_DBG_RETURN(-1);
     480             :                         }
     481             :                 }
     482           3 :                         break;
     483             : 
     484          19 :                 case PDO_ATTR_AUTOCOMMIT:
     485          19 :                         ZVAL_LONG(return_value, dbh->auto_commit);
     486          19 :                         break;
     487             : 
     488           2 :                 case PDO_ATTR_DEFAULT_STR_PARAM:
     489           2 :                         ZVAL_LONG(return_value, H->assume_national_character_set_strings ? PDO_PARAM_STR_NATL : PDO_PARAM_STR_CHAR);
     490           2 :                         break;
     491             : 
     492           6 :                 case PDO_MYSQL_ATTR_USE_BUFFERED_QUERY:
     493           6 :                         ZVAL_LONG(return_value, H->buffered);
     494           6 :                         break;
     495             : 
     496          55 :                 case PDO_ATTR_EMULATE_PREPARES:
     497             :                 case PDO_MYSQL_ATTR_DIRECT_QUERY:
     498          55 :                         ZVAL_LONG(return_value, H->emulate_prepare);
     499          55 :                         break;
     500             : 
     501             : #ifndef PDO_USE_MYSQLND
     502             :                 case PDO_MYSQL_ATTR_MAX_BUFFER_SIZE:
     503             :                         ZVAL_LONG(return_value, H->max_buffer_size);
     504             :                         break;
     505             : #endif
     506             : 
     507          18 :                 default:
     508          18 :                         PDO_DBG_RETURN(0);
     509             :         }
     510             : 
     511          97 :         PDO_DBG_RETURN(1);
     512             : }
     513             : /* }}} */
     514             : 
     515             : /* {{{ pdo_mysql_check_liveness */
     516          26 : static int pdo_mysql_check_liveness(pdo_dbh_t *dbh)
     517             : {
     518          26 :         pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
     519             : #if MYSQL_VERSION_ID <= 32230
     520             :         void (*handler) (int);
     521             :         unsigned int my_errno;
     522             : #endif
     523             : 
     524          26 :         PDO_DBG_ENTER("pdo_mysql_check_liveness");
     525          26 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     526             : 
     527             : #if MYSQL_VERSION_ID > 32230
     528          26 :         if (mysql_ping(H->server)) {
     529           1 :                 PDO_DBG_RETURN(FAILURE);
     530             :         }
     531             : #else /* no mysql_ping() */
     532             :         handler = signal(SIGPIPE, SIG_IGN);
     533             :         mysql_stat(H->server);
     534             :         switch (mysql_errno(H->server)) {
     535             :                 case CR_SERVER_GONE_ERROR:
     536             :                 case CR_SERVER_LOST:
     537             :                         signal(SIGPIPE, handler);
     538             :                         PDO_DBG_RETURN(FAILURE);
     539             :                 default:
     540             :                         break;
     541             :         }
     542             :         signal(SIGPIPE, handler);
     543             : #endif /* end mysql_ping() */
     544          25 :         PDO_DBG_RETURN(SUCCESS);
     545             : }
     546             : /* }}} */
     547             : 
     548             : /* {{{ mysql_methods */
     549             : static struct pdo_dbh_methods mysql_methods = {
     550             :         mysql_handle_closer,
     551             :         mysql_handle_preparer,
     552             :         mysql_handle_doer,
     553             :         mysql_handle_quoter,
     554             :         mysql_handle_begin,
     555             :         mysql_handle_commit,
     556             :         mysql_handle_rollback,
     557             :         pdo_mysql_set_attribute,
     558             :         pdo_mysql_last_insert_id,
     559             :         pdo_mysql_fetch_error_func,
     560             :         pdo_mysql_get_attribute,
     561             :         pdo_mysql_check_liveness,
     562             :         NULL,
     563             :         NULL,
     564             :         NULL
     565             : };
     566             : /* }}} */
     567             : 
     568             : #ifdef PHP_WIN32
     569             : # define PDO_DEFAULT_MYSQL_UNIX_ADDR    NULL
     570             : #else
     571             : # define PDO_DEFAULT_MYSQL_UNIX_ADDR    PDO_MYSQL_G(default_socket)
     572             : #endif
     573             : 
     574             : /* {{{ pdo_mysql_handle_factory */
     575         534 : static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
     576             : {
     577             :         pdo_mysql_db_handle *H;
     578             :         size_t i;
     579         534 :         int ret = 0;
     580         534 :         char *host = NULL, *unix_socket = NULL;
     581         534 :         unsigned int port = 3306;
     582             :         char *dbname;
     583        1068 :         struct pdo_data_src_parser vars[] = {
     584             :                 { "charset",  NULL,   0 },
     585             :                 { "dbname",   "",   0 },
     586             :                 { "host",   "localhost",    0 },
     587             :                 { "port",   "3306", 0 },
     588         534 :                 { "unix_socket",  PDO_DEFAULT_MYSQL_UNIX_ADDR,        0 },
     589             :         };
     590         534 :         int connect_opts = 0
     591             : #ifdef CLIENT_MULTI_RESULTS
     592             :                 |CLIENT_MULTI_RESULTS
     593             : #endif
     594             :                 ;
     595             : #if defined(PDO_USE_MYSQLND)
     596         534 :         size_t dbname_len = 0;
     597         534 :         size_t password_len = 0;
     598             : #endif
     599             : 
     600             : #ifdef CLIENT_MULTI_STATEMENTS
     601         534 :         if (!driver_options) {
     602         496 :                 connect_opts |= CLIENT_MULTI_STATEMENTS;
     603          38 :         } else if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MULTI_STATEMENTS, 1)) {
     604          35 :                 connect_opts |= CLIENT_MULTI_STATEMENTS;
     605             :         }
     606             : #endif
     607             : 
     608         534 :         PDO_DBG_ENTER("pdo_mysql_handle_factory");
     609         534 :         PDO_DBG_INF_FMT("dbh=%p", dbh);
     610             : #ifdef CLIENT_MULTI_RESULTS
     611         534 :         PDO_DBG_INF("multi results");
     612             : #endif
     613             : 
     614         534 :         php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 5);
     615             : 
     616         534 :         H = pecalloc(1, sizeof(pdo_mysql_db_handle), dbh->is_persistent);
     617             : 
     618         534 :         H->einfo.errcode = 0;
     619         534 :         H->einfo.errmsg = NULL;
     620             : 
     621             :         /* allocate an environment */
     622             : 
     623             :         /* handle for the server */
     624         534 :         if (!(H->server = pdo_mysql_init(dbh->is_persistent))) {
     625           0 :                 pdo_mysql_error(dbh);
     626           0 :                 goto cleanup;
     627             :         }
     628             : 
     629         534 :         dbh->driver_data = H;
     630             : 
     631             : #ifndef PDO_USE_MYSQLND
     632             :         H->max_buffer_size = 1024*1024;
     633             : #endif
     634             : 
     635         534 :         H->assume_national_character_set_strings = 0;
     636         534 :         H->buffered = H->emulate_prepare = 1;
     637             : 
     638             :         /* handle MySQL options */
     639         534 :         if (driver_options) {
     640          38 :                 zend_long connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
     641          38 :                 zend_long local_infile = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_LOCAL_INFILE, 0);
     642          38 :                 zend_string *init_cmd = NULL;
     643             : #ifndef PDO_USE_MYSQLND
     644             :                 zend_string *default_file = NULL, *default_group = NULL;
     645             : #endif
     646          38 :                 zend_long compress = 0;
     647          38 :                 zend_string *ssl_key = NULL, *ssl_cert = NULL, *ssl_ca = NULL, *ssl_capath = NULL, *ssl_cipher = NULL;
     648          38 :                 H->buffered = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_USE_BUFFERED_QUERY, 1);
     649             : 
     650          38 :                 H->emulate_prepare = pdo_attr_lval(driver_options,
     651          38 :                         PDO_MYSQL_ATTR_DIRECT_QUERY, H->emulate_prepare);
     652          38 :                 H->emulate_prepare = pdo_attr_lval(driver_options,
     653          38 :                         PDO_ATTR_EMULATE_PREPARES, H->emulate_prepare);
     654             : 
     655          76 :                 H->assume_national_character_set_strings = pdo_attr_lval(driver_options,
     656          38 :                         PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL ? 1 : 0;
     657             : 
     658             : #ifndef PDO_USE_MYSQLND
     659             :                 H->max_buffer_size = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MAX_BUFFER_SIZE, H->max_buffer_size);
     660             : #endif
     661             : 
     662          38 :                 if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_FOUND_ROWS, 0)) {
     663           1 :                         connect_opts |= CLIENT_FOUND_ROWS;
     664             :                 }
     665             : 
     666          38 :                 if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_IGNORE_SPACE, 0)) {
     667           0 :                         connect_opts |= CLIENT_IGNORE_SPACE;
     668             :                 }
     669             : 
     670          38 :                 if (mysql_options(H->server, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout)) {
     671           0 :                         pdo_mysql_error(dbh);
     672           0 :                         goto cleanup;
     673             :                 }
     674             : 
     675             : #ifndef PDO_USE_MYSQLND
     676             :                 if (PG(open_basedir) && PG(open_basedir)[0] != '\0') {
     677             :                         local_infile = 0;
     678             :                 }
     679             : #endif
     680             : #if defined(MYSQL_OPT_LOCAL_INFILE) || defined(PDO_USE_MYSQLND)
     681          38 :                 if (mysql_options(H->server, MYSQL_OPT_LOCAL_INFILE, (const char *)&local_infile)) {
     682           0 :                         pdo_mysql_error(dbh);
     683           0 :                         goto cleanup;
     684             :                 }
     685             : #endif
     686             : #ifdef MYSQL_OPT_RECONNECT
     687             :                 /* since 5.0.3, the default for this option is 0 if not specified.
     688             :                  * we want the old behaviour
     689             :                  * mysqlnd doesn't support reconnect, thus we don't have "|| defined(PDO_USE_MYSQLND)"
     690             :                 */
     691             :                 {
     692             :                         zend_long reconnect = 1;
     693             :                         mysql_options(H->server, MYSQL_OPT_RECONNECT, (const char*)&reconnect);
     694             :                 }
     695             : #endif
     696          38 :                 init_cmd = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_INIT_COMMAND, NULL);
     697          38 :                 if (init_cmd) {
     698           5 :                         if (mysql_options(H->server, MYSQL_INIT_COMMAND, (const char *)ZSTR_VAL(init_cmd))) {
     699             :                                 zend_string_release(init_cmd);
     700           0 :                                 pdo_mysql_error(dbh);
     701           0 :                                 goto cleanup;
     702             :                         }
     703             :                         zend_string_release(init_cmd);
     704             :                 }
     705             : #ifndef PDO_USE_MYSQLND
     706             :                 default_file = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_READ_DEFAULT_FILE, NULL);
     707             :                 if (default_file) {
     708             :                         if (mysql_options(H->server, MYSQL_READ_DEFAULT_FILE, (const char *)ZSTR_VAL(default_file))) {
     709             :                                 zend_string_release(default_file);
     710             :                                 pdo_mysql_error(dbh);
     711             :                                 goto cleanup;
     712             :                         }
     713             :                         zend_string_release(default_file);
     714             :                 }
     715             : 
     716             :                 default_group = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_READ_DEFAULT_GROUP, NULL);
     717             :                 if (default_group) {
     718             :                         if (mysql_options(H->server, MYSQL_READ_DEFAULT_GROUP, (const char *)ZSTR_VAL(default_group))) {
     719             :                                 zend_string_release(default_group);
     720             :                                 pdo_mysql_error(dbh);
     721             :                                 goto cleanup;
     722             :                         }
     723             :                         zend_string_release(default_group);
     724             :                 }
     725             : #endif
     726          38 :                 compress = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_COMPRESS, 0);
     727          38 :                 if (compress) {
     728           0 :                         if (mysql_options(H->server, MYSQL_OPT_COMPRESS, 0)) {
     729           0 :                                 pdo_mysql_error(dbh);
     730           0 :                                 goto cleanup;
     731             :                         }
     732             :                 }
     733             : 
     734          38 :                 ssl_key = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_KEY, NULL);
     735          38 :                 ssl_cert = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CERT, NULL);
     736          38 :                 ssl_ca = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CA, NULL);
     737          38 :                 ssl_capath = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CAPATH, NULL);
     738          38 :                 ssl_cipher = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CIPHER, NULL);
     739             : 
     740          38 :                 if (ssl_key || ssl_cert || ssl_ca || ssl_capath || ssl_cipher) {
     741           0 :                         mysql_ssl_set(H->server,
     742             :                                         ssl_key? ZSTR_VAL(ssl_key) : NULL,
     743             :                                         ssl_cert? ZSTR_VAL(ssl_cert) : NULL,
     744             :                                         ssl_ca? ZSTR_VAL(ssl_ca) : NULL,
     745             :                                         ssl_capath? ZSTR_VAL(ssl_capath) : NULL,
     746             :                                         ssl_cipher? ZSTR_VAL(ssl_cipher) : NULL);
     747           0 :                         if (ssl_key) {
     748             :                                 zend_string_release(ssl_key);
     749             :                         }
     750           0 :                         if (ssl_cert) {
     751             :                                 zend_string_release(ssl_cert);
     752             :                         }
     753           0 :                         if (ssl_ca) {
     754             :                                 zend_string_release(ssl_ca);
     755             :                         }
     756           0 :                         if (ssl_capath) {
     757             :                                 zend_string_release(ssl_capath);
     758             :                         }
     759           0 :                         if (ssl_cipher) {
     760             :                                 zend_string_release(ssl_cipher);
     761             :                         }
     762             :                 }
     763             : 
     764             : #if MYSQL_VERSION_ID > 50605 || defined(PDO_USE_MYSQLND)
     765             :                 {
     766          38 :                         zend_string *public_key = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SERVER_PUBLIC_KEY, NULL);
     767          38 :                         if (public_key) {
     768           0 :                                 if (mysql_options(H->server, MYSQL_SERVER_PUBLIC_KEY, ZSTR_VAL(public_key))) {
     769           0 :                                         pdo_mysql_error(dbh);
     770             :                                         zend_string_release(public_key);
     771           0 :                                         goto cleanup;
     772             :                                 }
     773             :                                 zend_string_release(public_key);
     774             :                         }
     775             :                 }
     776             : #endif
     777             : 
     778             : #ifdef PDO_USE_MYSQLND
     779             :                 {
     780          38 :                         zend_long ssl_verify_cert = pdo_attr_lval(driver_options,
     781             :                                         PDO_MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, -1);
     782          38 :                         if (ssl_verify_cert != -1) {
     783           0 :                                 connect_opts |= ssl_verify_cert ?
     784             :                                         CLIENT_SSL_VERIFY_SERVER_CERT:
     785             :                                         CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
     786             :                         }
     787             :                 }
     788             : #endif
     789             :         }
     790             : 
     791             : #ifdef PDO_MYSQL_HAS_CHARSET
     792         534 :         if (vars[0].optval && mysql_options(H->server, MYSQL_SET_CHARSET_NAME, vars[0].optval)) {
     793           0 :                 pdo_mysql_error(dbh);
     794           0 :                 goto cleanup;
     795             :         }
     796             : #endif
     797             : 
     798         534 :         dbname = vars[1].optval;
     799         534 :         host = vars[2].optval;
     800         534 :         if(vars[3].optval) {
     801         534 :                 port = atoi(vars[3].optval);
     802             :         }
     803             : 
     804             : #ifdef PHP_WIN32
     805             :         if (vars[2].optval && !strcmp(".", vars[2].optval)) {
     806             : #else
     807         534 :         if (vars[2].optval && !strcmp("localhost", vars[2].optval)) {
     808             : #endif
     809         530 :                 unix_socket = vars[4].optval;
     810             :         }
     811             : 
     812             :         /* TODO: - Check zval cache + ZTS */
     813             : #ifdef PDO_USE_MYSQLND
     814         534 :         if (dbname) {
     815         534 :                 dbname_len = strlen(dbname);
     816             :         }
     817             : 
     818         534 :         if (dbh->password) {
     819         533 :                 password_len = strlen(dbh->password);
     820             :         }
     821             : 
     822         534 :         if (mysqlnd_connect(H->server, host, dbh->username, dbh->password, password_len, dbname, dbname_len,
     823             :                                                 port, unix_socket, connect_opts, MYSQLND_CLIENT_NO_FLAG) == NULL) {
     824             : #else
     825             :         if (mysql_real_connect(H->server, host, dbh->username, dbh->password, dbname, port, unix_socket, connect_opts) == NULL) {
     826             : #endif
     827          15 :                 pdo_mysql_error(dbh);
     828          15 :                 goto cleanup;
     829             :         }
     830             : 
     831         519 :         if (!dbh->auto_commit) {
     832           1 :                 mysql_handle_autocommit(dbh);
     833             :         }
     834             : 
     835         519 :         H->attached = 1;
     836             : 
     837         519 :         dbh->alloc_own_columns = 1;
     838         519 :         dbh->max_escaped_char_length = 2;
     839         519 :         dbh->methods = &mysql_methods;
     840             : 
     841         519 :         ret = 1;
     842             : 
     843         534 : cleanup:
     844        3204 :         for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
     845        2670 :                 if (vars[i].freeme) {
     846        1068 :                         efree(vars[i].optval);
     847             :                 }
     848             :         }
     849             : 
     850         534 :         dbh->methods = &mysql_methods;
     851             : 
     852         534 :         PDO_DBG_RETURN(ret);
     853             : }
     854             : /* }}} */
     855             : 
     856             : pdo_driver_t pdo_mysql_driver = {
     857             :         PDO_DRIVER_HEADER(mysql),
     858             :         pdo_mysql_handle_factory
     859             : };
     860             : 
     861             : /*
     862             :  * Local variables:
     863             :  * tab-width: 4
     864             :  * c-basic-offset: 4
     865             :  * End:
     866             :  * vim600: noet sw=4 ts=4 fdm=marker
     867             :  * vim<600: noet sw=4 ts=4
     868             :  */

Generated by: LCOV version 1.10

Generated at Thu, 06 Dec 2018 13:00:52 +0000 (5 days ago)

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