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/mysqlnd - mysqlnd.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 1132 1402 80.7 %
Date: 2014-11-22 Functions: 81 88 92.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 2006-2014 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Authors: Andrey Hristov <andrey@mysql.com>                           |
      16             :   |          Ulf Wendel <uwendel@mysql.com>                              |
      17             :   |          Georg Richter <georg@mysql.com>                             |
      18             :   +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : #include "php.h"
      23             : #include "mysqlnd.h"
      24             : #include "mysqlnd_wireprotocol.h"
      25             : #include "mysqlnd_priv.h"
      26             : #include "mysqlnd_result.h"
      27             : #include "mysqlnd_statistics.h"
      28             : #include "mysqlnd_charset.h"
      29             : #include "mysqlnd_debug.h"
      30             : #include "zend_smart_str.h"
      31             : 
      32             : /*
      33             :   TODO :
      34             :   - Don't bind so tightly the metadata with the result set. This means
      35             :         that the metadata reading should not expect a MYSQLND_RES pointer, it
      36             :         does not need it, but return a pointer to the metadata (MYSQLND_FIELD *).
      37             :         For normal statements we will then just assign it to a member of
      38             :         MYSQLND_RES. For PS statements, it will stay as part of the statement
      39             :         (MYSQLND_STMT) between prepare and execute. At execute the new metadata
      40             :         will be sent by the server, so we will discard the old one and then
      41             :         finally attach it to the result set. This will make the code more clean,
      42             :         as a prepared statement won't have anymore stmt->result != NULL, as it
      43             :         is now, just to have where to store the metadata.
      44             : 
      45             :   - Change mysqlnd_simple_command to accept a heap dynamic array of MYSQLND_STRING
      46             :         terminated by a string with ptr being NULL. Thus, multi-part messages can be
      47             :         sent to the network like writev() and this can save at least for
      48             :         mysqlnd_stmt_send_long_data() new malloc. This change will probably make the
      49             :         code in few other places cleaner.
      50             : */
      51             : 
      52             : extern MYSQLND_CHARSET *mysqlnd_charsets;
      53             : 
      54             : 
      55             : 
      56             : PHPAPI const char * const mysqlnd_old_passwd  = "mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. "
      57             : "Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD('your_existing_password'). This will "
      58             : "store a new, and more secure, hash value in mysql.user. If this user is used in other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords "
      59             : "flag from your my.cnf file";
      60             : 
      61             : PHPAPI const char * const mysqlnd_server_gone = "MySQL server has gone away";
      62             : PHPAPI const char * const mysqlnd_out_of_sync = "Commands out of sync; you can't run this command now";
      63             : PHPAPI const char * const mysqlnd_out_of_memory = "Out of memory";
      64             : 
      65             : PHPAPI MYSQLND_STATS *mysqlnd_global_stats = NULL;
      66             : 
      67             : 
      68             : /* {{{ mysqlnd_conn_data::free_options */
      69             : static void
      70        1899 : MYSQLND_METHOD(mysqlnd_conn_data, free_options)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
      71             : {
      72        1899 :         zend_bool pers = conn->persistent;
      73             : 
      74        1899 :         if (conn->options->charset_name) {
      75           5 :                 mnd_pefree(conn->options->charset_name, pers);
      76           5 :                 conn->options->charset_name = NULL;
      77             :         }
      78        1899 :         if (conn->options->auth_protocol) {
      79        1826 :                 mnd_pefree(conn->options->auth_protocol, pers);
      80        1826 :                 conn->options->auth_protocol = NULL;
      81             :         }
      82        1899 :         if (conn->options->num_commands) {
      83             :                 unsigned int i;
      84          24 :                 for (i = 0; i < conn->options->num_commands; i++) {
      85             :                         /* allocated with pestrdup */
      86          13 :                         mnd_pefree(conn->options->init_commands[i], pers);
      87             :                 }
      88          11 :                 mnd_pefree(conn->options->init_commands, pers);
      89          11 :                 conn->options->init_commands = NULL;
      90             :         }
      91        1899 :         if (conn->options->cfg_file) {
      92           0 :                 mnd_pefree(conn->options->cfg_file, pers);
      93           0 :                 conn->options->cfg_file = NULL;
      94             :         }
      95        1899 :         if (conn->options->cfg_section) {
      96           0 :                 mnd_pefree(conn->options->cfg_section, pers);
      97           0 :                 conn->options->cfg_section = NULL;
      98             :         }
      99        1899 :         if (conn->options->connect_attr) {
     100        1871 :                 zend_hash_destroy(conn->options->connect_attr);
     101        1871 :                 mnd_pefree(conn->options->connect_attr, pers);
     102        1871 :                 conn->options->connect_attr = NULL;
     103             :         }
     104        1899 : }
     105             : /* }}} */
     106             : 
     107             : 
     108             : /* {{{ mysqlnd_conn_data::free_contents */
     109             : static void
     110        1952 : MYSQLND_METHOD(mysqlnd_conn_data, free_contents)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
     111             : {
     112        1952 :         zend_bool pers = conn->persistent;
     113             : 
     114        1952 :         DBG_ENTER("mysqlnd_conn_data::free_contents");
     115             : 
     116        1952 :         if (conn->current_result) {
     117           4 :                 conn->current_result->m.free_result(conn->current_result, TRUE TSRMLS_CC);
     118           4 :                 conn->current_result = NULL;
     119             :         }
     120             : 
     121        1952 :         if (conn->net) {
     122        1952 :                 conn->net->data->m.free_contents(conn->net TSRMLS_CC);
     123             :         }
     124             : 
     125        1952 :         DBG_INF("Freeing memory of members");
     126             : 
     127        1952 :         if (conn->host) {
     128           2 :                 mnd_pefree(conn->host, pers);
     129           2 :                 conn->host = NULL;
     130             :         }
     131        1952 :         if (conn->user) {
     132        1827 :                 mnd_pefree(conn->user, pers);
     133        1827 :                 conn->user = NULL;
     134             :         }
     135        1952 :         if (conn->passwd) {
     136        1827 :                 mnd_pefree(conn->passwd, pers);
     137        1827 :                 conn->passwd = NULL;
     138             :         }
     139        1952 :         if (conn->connect_or_select_db) {
     140        1827 :                 mnd_pefree(conn->connect_or_select_db, pers);
     141        1827 :                 conn->connect_or_select_db = NULL;
     142             :         }
     143        1952 :         if (conn->unix_socket) {
     144        1825 :                 mnd_pefree(conn->unix_socket, pers);
     145        1825 :                 conn->unix_socket = NULL;
     146             :         }
     147        1952 :         DBG_INF_FMT("scheme=%s", conn->scheme);
     148        1952 :         if (conn->scheme) {
     149        1876 :                 mnd_pefree(conn->scheme, pers);
     150        1876 :                 conn->scheme = NULL;
     151             :         }
     152        1952 :         if (conn->server_version) {
     153        1862 :                 mnd_pefree(conn->server_version, pers);
     154        1862 :                 conn->server_version = NULL;
     155             :         }
     156        1952 :         if (conn->host_info) {
     157        1827 :                 mnd_pefree(conn->host_info, pers);
     158        1827 :                 conn->host_info = NULL;
     159             :         }
     160        1952 :         if (conn->auth_plugin_data) {
     161        1859 :                 mnd_pefree(conn->auth_plugin_data, pers);
     162        1859 :                 conn->auth_plugin_data = NULL;
     163             :         }
     164        1952 :         if (conn->last_message) {
     165          65 :                 mnd_pefree(conn->last_message, pers);
     166          65 :                 conn->last_message = NULL;
     167             :         }
     168        1952 :         if (conn->error_info->error_list) {
     169        1902 :                 zend_llist_clean(conn->error_info->error_list);
     170        1902 :                 mnd_pefree(conn->error_info->error_list, pers);
     171        1902 :                 conn->error_info->error_list = NULL;
     172             :         }
     173        1952 :         conn->charset = NULL;
     174        1952 :         conn->greet_charset = NULL;
     175             : 
     176        1952 :         DBG_VOID_RETURN;
     177             : }
     178             : /* }}} */
     179             : 
     180             : 
     181             : /* {{{ mysqlnd_conn_data::dtor */
     182             : static void
     183        1899 : MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
     184             : {
     185        1899 :         DBG_ENTER("mysqlnd_conn_data::dtor");
     186        1899 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
     187             : 
     188        1899 :         conn->m->free_contents(conn TSRMLS_CC);
     189        1899 :         conn->m->free_options(conn TSRMLS_CC);
     190             : 
     191        1899 :         if (conn->net) {
     192        1899 :                 mysqlnd_net_free(conn->net, conn->stats, conn->error_info TSRMLS_CC);
     193        1899 :                 conn->net = NULL;
     194             :         }
     195             : 
     196        1899 :         if (conn->protocol) {
     197        1899 :                 mysqlnd_protocol_free(conn->protocol TSRMLS_CC);
     198        1899 :                 conn->protocol = NULL;
     199             :         }
     200             : 
     201        1899 :         if (conn->stats) {
     202        1899 :                 mysqlnd_stats_end(conn->stats);
     203             :         }
     204             : 
     205        1899 :         mnd_pefree(conn, conn->persistent);
     206             : 
     207        1899 :         DBG_VOID_RETURN;
     208             : }
     209             : /* }}} */
     210             : 
     211             : 
     212             : /* {{{ mysqlnd_conn_data::simple_command_handle_response */
     213             : static enum_func_status
     214         434 : MYSQLND_METHOD(mysqlnd_conn_data, simple_command_handle_response)(MYSQLND_CONN_DATA * conn, enum mysqlnd_packet_type ok_packet,
     215             :                                                                                                                          zend_bool silent, enum php_mysqlnd_server_command command,
     216             :                                                                                                                          zend_bool ignore_upsert_status TSRMLS_DC)
     217             : {
     218         434 :         enum_func_status ret = FAIL;
     219             : 
     220         434 :         DBG_ENTER("mysqlnd_conn_data::simple_command_handle_response");
     221         434 :         DBG_INF_FMT("silent=%u packet=%u command=%s", silent, ok_packet, mysqlnd_command_to_text[command]);
     222             : 
     223         434 :         switch (ok_packet) {
     224             :                 case PROT_OK_PACKET:{
     225         299 :                         MYSQLND_PACKET_OK * ok_response = conn->protocol->m.get_ok_packet(conn->protocol, FALSE TSRMLS_CC);
     226         299 :                         if (!ok_response) {
     227           0 :                                 SET_OOM_ERROR(*conn->error_info);
     228           0 :                                 break;
     229             :                         }
     230         299 :                         if (FAIL == (ret = PACKET_READ(ok_response, conn))) {
     231           0 :                                 if (!silent) {
     232           0 :                                         DBG_ERR_FMT("Error while reading %s's OK packet", mysqlnd_command_to_text[command]);
     233           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading %s's OK packet. PID=%u",
     234             :                                                                          mysqlnd_command_to_text[command], getpid());
     235             :                                 }
     236             :                         } else {
     237         299 :                                 DBG_INF_FMT("OK from server");
     238         299 :                                 if (0xFF == ok_response->field_count) {
     239             :                                         /* The server signalled error. Set the error */
     240          10 :                                         SET_CLIENT_ERROR(*conn->error_info, ok_response->error_no, ok_response->sqlstate, ok_response->error);
     241          10 :                                         ret = FAIL;
     242             :                                         /*
     243             :                                           Cover a protocol design error: error packet does not
     244             :                                           contain the server status. Therefore, the client has no way
     245             :                                           to find out whether there are more result sets of
     246             :                                           a multiple-result-set statement pending. Luckily, in 5.0 an
     247             :                                           error always aborts execution of a statement, wherever it is
     248             :                                           a multi-statement or a stored procedure, so it should be
     249             :                                           safe to unconditionally turn off the flag here.
     250             :                                         */
     251          10 :                                         conn->upsert_status->server_status &= ~SERVER_MORE_RESULTS_EXISTS;
     252          10 :                                         SET_ERROR_AFF_ROWS(conn);
     253             :                                 } else {
     254         289 :                                         SET_NEW_MESSAGE(conn->last_message, conn->last_message_len,
     255             :                                                                         ok_response->message, ok_response->message_len,
     256             :                                                                         conn->persistent);
     257             : 
     258         289 :                                         if (!ignore_upsert_status) {
     259          11 :                                                 memset(conn->upsert_status, 0, sizeof(*conn->upsert_status));
     260          11 :                                                 conn->upsert_status->warning_count = ok_response->warning_count;
     261          11 :                                                 conn->upsert_status->server_status = ok_response->server_status;
     262          11 :                                                 conn->upsert_status->affected_rows = ok_response->affected_rows;
     263          11 :                                                 conn->upsert_status->last_insert_id = ok_response->last_insert_id;
     264             :                                         }
     265             :                                 }
     266             :                         }
     267         299 :                         PACKET_FREE(ok_response);
     268         299 :                         break;
     269             :                 }
     270             :                 case PROT_EOF_PACKET:{
     271         135 :                         MYSQLND_PACKET_EOF * ok_response = conn->protocol->m.get_eof_packet(conn->protocol, FALSE TSRMLS_CC);
     272         135 :                         if (!ok_response) {
     273           0 :                                 SET_OOM_ERROR(*conn->error_info);
     274           0 :                                 break;
     275             :                         }
     276         135 :                         if (FAIL == (ret = PACKET_READ(ok_response, conn))) {
     277           0 :                                 SET_CLIENT_ERROR(*conn->error_info, CR_MALFORMED_PACKET, UNKNOWN_SQLSTATE,
     278             :                                                                  "Malformed packet");
     279           0 :                                 if (!silent) {
     280           0 :                                         DBG_ERR_FMT("Error while reading %s's EOF packet", mysqlnd_command_to_text[command]);
     281           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading %s's EOF packet. PID=%d",
     282             :                                                                          mysqlnd_command_to_text[command], getpid());
     283             :                                 }
     284         135 :                         } else if (0xFF == ok_response->field_count) {
     285             :                                 /* The server signalled error. Set the error */
     286           2 :                                 SET_CLIENT_ERROR(*conn->error_info, ok_response->error_no, ok_response->sqlstate, ok_response->error);
     287           2 :                                 SET_ERROR_AFF_ROWS(conn);
     288         133 :                         } else if (0xFE != ok_response->field_count) {
     289           0 :                                 SET_CLIENT_ERROR(*conn->error_info, CR_MALFORMED_PACKET, UNKNOWN_SQLSTATE, "Malformed packet");
     290           0 :                                 if (!silent) {
     291           0 :                                         DBG_ERR_FMT("EOF packet expected, field count wasn't 0xFE but 0x%2X", ok_response->field_count);
     292           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "EOF packet expected, field count wasn't 0xFE but 0x%2X",
     293           0 :                                                                         ok_response->field_count);
     294             :                                 }
     295             :                         } else {
     296         133 :                                 DBG_INF_FMT("OK from server");
     297             :                         }
     298         135 :                         PACKET_FREE(ok_response);
     299         135 :                         break;
     300             :                 }
     301             :                 default:
     302           0 :                         SET_CLIENT_ERROR(*conn->error_info, CR_MALFORMED_PACKET, UNKNOWN_SQLSTATE, "Malformed packet");
     303           0 :                         php_error_docref(NULL TSRMLS_CC, E_ERROR, "Wrong response packet %u passed to the function", ok_packet);
     304             :                         break;
     305             :         }
     306         434 :         DBG_INF(ret == PASS ? "PASS":"FAIL");
     307         434 :         DBG_RETURN(ret);
     308             : }
     309             : /* }}} */
     310             : 
     311             : 
     312             : /* {{{ mysqlnd_conn_data::simple_command_send_request */
     313             : static enum_func_status
     314       39156 : MYSQLND_METHOD(mysqlnd_conn_data, simple_command_send_request)(MYSQLND_CONN_DATA * conn, enum php_mysqlnd_server_command command,
     315             :                            const zend_uchar * const arg, size_t arg_len, zend_bool silent, zend_bool ignore_upsert_status TSRMLS_DC)
     316             : {
     317       39156 :         enum_func_status ret = PASS;
     318             :         MYSQLND_PACKET_COMMAND * cmd_packet;
     319             : 
     320       39156 :         DBG_ENTER("mysqlnd_conn_data::simple_command_send_request");
     321       39156 :         DBG_INF_FMT("command=%s silent=%u", mysqlnd_command_to_text[command], silent);
     322       39156 :         DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
     323       39156 :         DBG_INF_FMT("sending %u bytes", arg_len + 1); /* + 1 is for the command */
     324             : 
     325       39156 :         switch (CONN_GET_STATE(conn)) {
     326             :                 case CONN_READY:
     327       39124 :                         break;
     328             :                 case CONN_QUIT_SENT:
     329          12 :                         SET_CLIENT_ERROR(*conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
     330          12 :                         DBG_ERR("Server is gone");
     331          12 :                         DBG_RETURN(FAIL);
     332             :                 default:
     333          20 :                         SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
     334          20 :                         DBG_ERR_FMT("Command out of sync. State=%u", CONN_GET_STATE(conn));
     335          20 :                         DBG_RETURN(FAIL);
     336             :         }
     337             : 
     338       39124 :         SET_ERROR_AFF_ROWS(conn);
     339       39124 :         SET_EMPTY_ERROR(*conn->error_info);
     340             : 
     341       39124 :         cmd_packet = conn->protocol->m.get_command_packet(conn->protocol, FALSE TSRMLS_CC);
     342       39124 :         if (!cmd_packet) {
     343           0 :                 SET_OOM_ERROR(*conn->error_info);
     344           0 :                 DBG_RETURN(FAIL);
     345             :         }
     346             : 
     347       39124 :         cmd_packet->command = command;
     348       39124 :         if (arg && arg_len) {
     349       37258 :                 cmd_packet->argument = arg;
     350       37258 :                 cmd_packet->arg_len  = arg_len;
     351             :         }
     352             : 
     353       39124 :         MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_COM_QUIT + command - 1 /* because of COM_SLEEP */ );
     354             : 
     355       39124 :         if (! PACKET_WRITE(cmd_packet, conn)) {
     356          12 :                 if (!silent) {
     357           3 :                         DBG_ERR_FMT("Error while sending %s packet", mysqlnd_command_to_text[command]);
     358           3 :                         php_error(E_WARNING, "Error while sending %s packet. PID=%d", mysqlnd_command_to_text[command], getpid());
     359             :                 }
     360          12 :                 CONN_SET_STATE(conn, CONN_QUIT_SENT);
     361          12 :                 conn->m->send_close(conn TSRMLS_CC);
     362          12 :                 DBG_ERR("Server is gone");
     363          12 :                 ret = FAIL;
     364             :         }
     365       39124 :         PACKET_FREE(cmd_packet);
     366       39124 :         DBG_RETURN(ret);
     367             : }
     368             : /* }}} */
     369             : 
     370             : 
     371             : /* {{{ mysqlnd_conn_data::simple_command */
     372             : static enum_func_status
     373       39156 : MYSQLND_METHOD(mysqlnd_conn_data, simple_command)(MYSQLND_CONN_DATA * conn, enum php_mysqlnd_server_command command,
     374             :                            const zend_uchar * const arg, size_t arg_len, enum mysqlnd_packet_type ok_packet, zend_bool silent,
     375             :                            zend_bool ignore_upsert_status TSRMLS_DC)
     376             : {
     377             :         enum_func_status ret;
     378       39156 :         DBG_ENTER("mysqlnd_conn_data::simple_command");
     379             : 
     380       39156 :         ret = conn->m->simple_command_send_request(conn, command, arg, arg_len, silent, ignore_upsert_status TSRMLS_CC);
     381       39156 :         if (PASS == ret && ok_packet != PROT_LAST) {
     382         423 :                 ret = conn->m->simple_command_handle_response(conn, ok_packet, silent, command, ignore_upsert_status TSRMLS_CC);
     383             :         }
     384             : 
     385       39156 :         DBG_INF(ret == PASS ? "PASS":"FAIL");
     386       39156 :         DBG_RETURN(ret);
     387             : }
     388             : /* }}} */
     389             : 
     390             : 
     391             : /* {{{ mysqlnd_conn_data::set_server_option */
     392             : static enum_func_status
     393         134 : MYSQLND_METHOD(mysqlnd_conn_data, set_server_option)(MYSQLND_CONN_DATA * const conn, enum_mysqlnd_server_option option TSRMLS_DC)
     394             : {
     395         134 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, set_server_option);
     396             :         zend_uchar buffer[2];
     397         134 :         enum_func_status ret = FAIL;
     398         134 :         DBG_ENTER("mysqlnd_conn_data::set_server_option");
     399         134 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
     400             : 
     401         134 :                 int2store(buffer, (unsigned int) option);
     402         134 :                 ret = conn->m->simple_command(conn, COM_SET_OPTION, buffer, sizeof(buffer), PROT_EOF_PACKET, FALSE, TRUE TSRMLS_CC);
     403             :         
     404         134 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
     405             :         }
     406         134 :         DBG_RETURN(ret);
     407             : }
     408             : /* }}} */
     409             : 
     410             : 
     411             : /* {{{ mysqlnd_conn_data::restart_psession */
     412             : static enum_func_status
     413          55 : MYSQLND_METHOD(mysqlnd_conn_data, restart_psession)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
     414             : {
     415          55 :         DBG_ENTER("mysqlnd_conn_data::restart_psession");
     416          55 :         MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_REUSED);
     417             :         /* Free here what should not be seen by the next script */
     418          55 :         if (conn->last_message) {
     419           0 :                 mnd_pefree(conn->last_message, conn->persistent);
     420           0 :                 conn->last_message = NULL;
     421             :         }
     422          55 :         DBG_RETURN(PASS);
     423             : }
     424             : /* }}} */
     425             : 
     426             : 
     427             : /* {{{ mysqlnd_conn_data::end_psession */
     428             : static enum_func_status
     429         100 : MYSQLND_METHOD(mysqlnd_conn_data, end_psession)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
     430             : {
     431         100 :         DBG_ENTER("mysqlnd_conn_data::end_psession");
     432         100 :         DBG_RETURN(PASS);
     433             : }
     434             : /* }}} */
     435             : 
     436             : 
     437             : /* {{{ mysqlnd_switch_to_ssl_if_needed */
     438             : static enum_func_status
     439        1861 : mysqlnd_switch_to_ssl_if_needed(
     440             :                         MYSQLND_CONN_DATA * conn,
     441             :                         const MYSQLND_PACKET_GREET * const greet_packet,
     442             :                         const MYSQLND_OPTIONS * const options,
     443             :                         zend_ulong mysql_flags
     444             :                         TSRMLS_DC
     445             :                 )
     446             : {
     447        1861 :         enum_func_status ret = FAIL;
     448             :         const MYSQLND_CHARSET * charset;
     449             :         MYSQLND_PACKET_AUTH * auth_packet;
     450        1861 :         DBG_ENTER("mysqlnd_switch_to_ssl_if_needed");
     451        1861 :         DBG_INF_FMT("client_capability_flags=%lu", mysql_flags);
     452        1861 :         DBG_INF_FMT("CLIENT_LONG_PASSWORD= %d", mysql_flags & CLIENT_LONG_PASSWORD? 1:0);
     453        1861 :         DBG_INF_FMT("CLIENT_FOUND_ROWS=            %d", mysql_flags & CLIENT_FOUND_ROWS? 1:0);
     454        1861 :         DBG_INF_FMT("CLIENT_LONG_FLAG=             %d", mysql_flags & CLIENT_LONG_FLAG? 1:0);
     455        1861 :         DBG_INF_FMT("CLIENT_NO_SCHEMA=             %d", mysql_flags & CLIENT_NO_SCHEMA? 1:0);
     456        1861 :         DBG_INF_FMT("CLIENT_COMPRESS=              %d", mysql_flags & CLIENT_COMPRESS? 1:0);
     457        1861 :         DBG_INF_FMT("CLIENT_ODBC=                  %d", mysql_flags & CLIENT_ODBC? 1:0);
     458        1861 :         DBG_INF_FMT("CLIENT_LOCAL_FILES=   %d", mysql_flags & CLIENT_LOCAL_FILES? 1:0);
     459        1861 :         DBG_INF_FMT("CLIENT_IGNORE_SPACE=  %d", mysql_flags & CLIENT_IGNORE_SPACE? 1:0);
     460        1861 :         DBG_INF_FMT("CLIENT_PROTOCOL_41=   %d", mysql_flags & CLIENT_PROTOCOL_41? 1:0);
     461        1861 :         DBG_INF_FMT("CLIENT_INTERACTIVE=   %d", mysql_flags & CLIENT_INTERACTIVE? 1:0);
     462        1861 :         DBG_INF_FMT("CLIENT_SSL=                   %d", mysql_flags & CLIENT_SSL? 1:0);
     463        1861 :         DBG_INF_FMT("CLIENT_IGNORE_SIGPIPE=        %d", mysql_flags & CLIENT_IGNORE_SIGPIPE? 1:0);
     464        1861 :         DBG_INF_FMT("CLIENT_TRANSACTIONS=  %d", mysql_flags & CLIENT_TRANSACTIONS? 1:0);
     465        1861 :         DBG_INF_FMT("CLIENT_RESERVED=              %d", mysql_flags & CLIENT_RESERVED? 1:0);
     466        1861 :         DBG_INF_FMT("CLIENT_SECURE_CONNECTION=%d", mysql_flags & CLIENT_SECURE_CONNECTION? 1:0);
     467        1861 :         DBG_INF_FMT("CLIENT_MULTI_STATEMENTS=%d", mysql_flags & CLIENT_MULTI_STATEMENTS? 1:0);
     468        1861 :         DBG_INF_FMT("CLIENT_MULTI_RESULTS= %d", mysql_flags & CLIENT_MULTI_RESULTS? 1:0);
     469        1861 :         DBG_INF_FMT("CLIENT_PS_MULTI_RESULTS=%d", mysql_flags & CLIENT_PS_MULTI_RESULTS? 1:0);
     470        1861 :         DBG_INF_FMT("CLIENT_CONNECT_ATTRS= %d", mysql_flags & CLIENT_PLUGIN_AUTH? 1:0);
     471        1861 :         DBG_INF_FMT("CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA=        %d", mysql_flags & CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA? 1:0);
     472        1861 :         DBG_INF_FMT("CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS=  %d", mysql_flags & CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS? 1:0);
     473        1861 :         DBG_INF_FMT("CLIENT_SESSION_TRACK=         %d", mysql_flags & CLIENT_SESSION_TRACK? 1:0);
     474        1861 :         DBG_INF_FMT("CLIENT_SSL_VERIFY_SERVER_CERT=        %d", mysql_flags & CLIENT_SSL_VERIFY_SERVER_CERT? 1:0);
     475        1861 :         DBG_INF_FMT("CLIENT_REMEMBER_OPTIONS=              %d", mysql_flags & CLIENT_REMEMBER_OPTIONS? 1:0);
     476             : 
     477        1861 :         auth_packet = conn->protocol->m.get_auth_packet(conn->protocol, FALSE TSRMLS_CC);
     478        1861 :         if (!auth_packet) {
     479           0 :                 SET_OOM_ERROR(*conn->error_info);
     480           0 :                 goto end;
     481             :         }
     482        1861 :         auth_packet->client_flags = mysql_flags;
     483        1861 :         auth_packet->max_packet_size = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
     484             : 
     485        1865 :         if (options->charset_name && (charset = mysqlnd_find_charset_name(options->charset_name))) {
     486           4 :                 auth_packet->charset_no      = charset->nr;
     487             :         } else {
     488        1857 :                 auth_packet->charset_no      = greet_packet->charset_no;
     489             :         }
     490             : 
     491             : #ifdef MYSQLND_SSL_SUPPORTED
     492        1861 :         if ((greet_packet->server_capabilities & CLIENT_SSL) && (mysql_flags & CLIENT_SSL)) {
     493           3 :                 zend_bool verify = mysql_flags & CLIENT_SSL_VERIFY_SERVER_CERT? TRUE:FALSE;
     494           3 :                 DBG_INF("Switching to SSL");
     495           3 :                 if (!PACKET_WRITE(auth_packet, conn)) {
     496           0 :                         CONN_SET_STATE(conn, CONN_QUIT_SENT);
     497           0 :                         conn->m->send_close(conn TSRMLS_CC);
     498           0 :                         SET_CLIENT_ERROR(*conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
     499           0 :                         goto end;
     500             :                 }
     501             : 
     502           3 :                 conn->net->data->m.set_client_option(conn->net, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (const char *) &verify TSRMLS_CC);
     503             : 
     504           3 :                 if (FAIL == conn->net->data->m.enable_ssl(conn->net TSRMLS_CC)) {
     505           3 :                         goto end;
     506             :                 }
     507             :         }
     508             : #endif
     509        1858 :         ret = PASS;
     510             : end:
     511        1861 :         PACKET_FREE(auth_packet);
     512        1861 :         DBG_RETURN(ret);
     513             : }
     514             : /* }}} */
     515             : 
     516             : 
     517             : /* {{{ mysqlnd_conn_data::fetch_auth_plugin_by_name */
     518             : static struct st_mysqlnd_authentication_plugin *
     519        1933 : MYSQLND_METHOD(mysqlnd_conn_data, fetch_auth_plugin_by_name)(const char * const requested_protocol TSRMLS_DC)
     520             : {
     521             :         struct st_mysqlnd_authentication_plugin * auth_plugin;
     522        1933 :         char * plugin_name = NULL;
     523        1933 :         DBG_ENTER("mysqlnd_conn_data::fetch_auth_plugin_by_name");
     524             : 
     525        1933 :         mnd_sprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);
     526        1933 :         DBG_INF_FMT("looking for %s auth plugin", plugin_name);
     527        1933 :         auth_plugin = mysqlnd_plugin_find(plugin_name);
     528        1933 :         mnd_sprintf_free(plugin_name);
     529             : 
     530        1933 :         DBG_RETURN(auth_plugin);
     531             : }
     532             : /* }}} */
     533             : 
     534             : 
     535             : /* {{{ mysqlnd_run_authentication */
     536             : static enum_func_status
     537        1933 : mysqlnd_run_authentication(
     538             :                         MYSQLND_CONN_DATA * conn,
     539             :                         const char * const user,
     540             :                         const char * const passwd,
     541             :                         const size_t passwd_len,
     542             :                         const char * const db,
     543             :                         const size_t db_len,
     544             :                         const zend_uchar * const auth_plugin_data,
     545             :                         const size_t auth_plugin_data_len,
     546             :                         const char * const auth_protocol,
     547             :                         unsigned int charset_no,
     548             :                         const MYSQLND_OPTIONS * const options,
     549             :                         zend_ulong mysql_flags,
     550             :                         zend_bool silent,
     551             :                         zend_bool is_change_user
     552             :                         TSRMLS_DC)
     553             : {
     554        1933 :         enum_func_status ret = FAIL;
     555        1933 :         zend_bool first_call = TRUE;
     556             : 
     557        1933 :         char * switch_to_auth_protocol = NULL;
     558        1933 :         size_t switch_to_auth_protocol_len = 0;
     559        1933 :         char * requested_protocol = NULL;
     560             :         zend_uchar * plugin_data;
     561             :         size_t plugin_data_len;
     562             : 
     563        1933 :         DBG_ENTER("mysqlnd_run_authentication");
     564             : 
     565        1933 :         plugin_data_len = auth_plugin_data_len;
     566        1933 :         plugin_data = mnd_emalloc(plugin_data_len + 1);
     567        1933 :         if (!plugin_data) {
     568           0 :                 goto end;
     569             :         }
     570        1933 :         memcpy(plugin_data, auth_plugin_data, plugin_data_len);
     571        1933 :         plugin_data[plugin_data_len] = '\0';
     572             : 
     573        1933 :         requested_protocol = mnd_pestrdup(auth_protocol? auth_protocol : MYSQLND_DEFAULT_AUTH_PROTOCOL, FALSE);
     574        1933 :         if (!requested_protocol) {
     575           0 :                 goto end;
     576             :         }
     577             : 
     578             :         do {
     579        1933 :                 struct st_mysqlnd_authentication_plugin * auth_plugin = conn->m->fetch_auth_plugin_by_name(requested_protocol TSRMLS_CC);
     580             : 
     581        1933 :                 if (!auth_plugin) {
     582           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "The server requested authentication method unknown to the client [%s]", requested_protocol);
     583           0 :                         SET_CLIENT_ERROR(*conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "The server requested authentication method unknown to the client");
     584           0 :                         goto end;
     585             :                 }
     586        1933 :                 DBG_INF("plugin found");
     587             : 
     588             :                 {
     589        1933 :                         zend_uchar * switch_to_auth_protocol_data = NULL;
     590        1933 :                         size_t switch_to_auth_protocol_data_len = 0;
     591        1933 :                         zend_uchar * scrambled_data = NULL;
     592        1933 :                         size_t scrambled_data_len = 0;
     593             : 
     594        1933 :                         switch_to_auth_protocol = NULL;
     595        1933 :                         switch_to_auth_protocol_len = 0;
     596             : 
     597        1933 :                         if (conn->auth_plugin_data) {
     598          75 :                                 mnd_pefree(conn->auth_plugin_data, conn->persistent);
     599          75 :                                 conn->auth_plugin_data = NULL;
     600             :                         }
     601        1933 :                         conn->auth_plugin_data_len = plugin_data_len;
     602        1933 :                         conn->auth_plugin_data = mnd_pemalloc(conn->auth_plugin_data_len, conn->persistent);
     603        1933 :                         if (!conn->auth_plugin_data) {
     604           0 :                                 SET_OOM_ERROR(*conn->error_info);
     605           0 :                                 goto end;
     606             :                         }
     607        1933 :                         memcpy(conn->auth_plugin_data, plugin_data, plugin_data_len);
     608             : 
     609        1933 :                         DBG_INF_FMT("salt(%d)=[%.*s]", plugin_data_len, plugin_data_len, plugin_data);
     610             :                         /* The data should be allocated with malloc() */
     611        1933 :                         scrambled_data =
     612        3866 :                                 auth_plugin->methods.get_auth_data(NULL, &scrambled_data_len, conn, user, passwd, passwd_len,
     613        1933 :                                                                                                    plugin_data, plugin_data_len, options, &conn->net->data->options, mysql_flags TSRMLS_CC);
     614        1933 :                         if (conn->error_info->error_no) {
     615           0 :                                 goto end;       
     616             :                         }
     617        1933 :                         if (FALSE == is_change_user) {
     618        1858 :                                 ret = mysqlnd_auth_handshake(conn, user, passwd, passwd_len, db, db_len, options, mysql_flags,
     619             :                                                                                         charset_no,
     620             :                                                                                         first_call,
     621             :                                                                                         requested_protocol,
     622             :                                                                                         scrambled_data, scrambled_data_len,
     623             :                                                                                         &switch_to_auth_protocol, &switch_to_auth_protocol_len,
     624             :                                                                                         &switch_to_auth_protocol_data, &switch_to_auth_protocol_data_len
     625             :                                                                                         TSRMLS_CC);
     626             :                         } else {
     627          75 :                                 ret = mysqlnd_auth_change_user(conn, user, strlen(user), passwd, passwd_len, db, db_len, silent,
     628             :                                                                                            first_call,
     629             :                                                                                            requested_protocol,
     630             :                                                                                            scrambled_data, scrambled_data_len,
     631             :                                                                                            &switch_to_auth_protocol, &switch_to_auth_protocol_len,
     632             :                                                                                            &switch_to_auth_protocol_data, &switch_to_auth_protocol_data_len
     633             :                                                                                            TSRMLS_CC);                          
     634             :                         }
     635        1933 :                         first_call = FALSE;
     636        1933 :                         free(scrambled_data);
     637             : 
     638        1933 :                         DBG_INF_FMT("switch_to_auth_protocol=%s", switch_to_auth_protocol? switch_to_auth_protocol:"n/a");
     639        1933 :                         if (requested_protocol && switch_to_auth_protocol) {
     640           0 :                                 mnd_efree(requested_protocol);
     641           0 :                                 requested_protocol = switch_to_auth_protocol;
     642             :                         }
     643             : 
     644        1933 :                         if (plugin_data) {
     645        1933 :                                 mnd_efree(plugin_data);
     646             :                         }
     647        1933 :                         plugin_data_len = switch_to_auth_protocol_data_len;
     648        1933 :                         plugin_data = switch_to_auth_protocol_data;
     649             :                 }
     650        1933 :                 DBG_INF_FMT("conn->error_info->error_no = %d", conn->error_info->error_no);
     651        1933 :         } while (ret == FAIL && conn->error_info->error_no == 0 && switch_to_auth_protocol != NULL);
     652             :                 
     653        1933 :         if (ret == PASS) {
     654        1886 :                 DBG_INF_FMT("saving requested_protocol=%s", requested_protocol);
     655        1886 :                 conn->m->set_client_option(conn, MYSQLND_OPT_AUTH_PROTOCOL, requested_protocol TSRMLS_CC);
     656             :         }
     657             : end:
     658        1933 :         if (plugin_data) {
     659           0 :                 mnd_efree(plugin_data);
     660             :         }
     661        1933 :         if (requested_protocol) {
     662        1933 :                 mnd_efree(requested_protocol);
     663             :         }
     664             : 
     665        1933 :         DBG_RETURN(ret);
     666             : }
     667             : /* }}} */
     668             : 
     669             : 
     670             : /* {{{ mysqlnd_connect_run_authentication */
     671             : static enum_func_status
     672        1861 : mysqlnd_connect_run_authentication(
     673             :                         MYSQLND_CONN_DATA * conn,
     674             :                         const char * const user,
     675             :                         const char * const passwd,
     676             :                         const char * const db,
     677             :                         size_t db_len,
     678             :                         size_t passwd_len,
     679             :                         const MYSQLND_PACKET_GREET * const greet_packet,
     680             :                         const MYSQLND_OPTIONS * const options,
     681             :                         zend_ulong mysql_flags
     682             :                         TSRMLS_DC)
     683             : {
     684        1861 :         enum_func_status ret = FAIL;
     685        1861 :         DBG_ENTER("mysqlnd_connect_run_authentication");
     686             : 
     687        1861 :         ret = mysqlnd_switch_to_ssl_if_needed(conn, greet_packet, options, mysql_flags TSRMLS_CC);
     688        1861 :         if (PASS == ret) {
     689        5574 :                 ret = mysqlnd_run_authentication(conn, user, passwd, passwd_len, db, db_len,
     690        3716 :                                                                                  greet_packet->auth_plugin_data, greet_packet->auth_plugin_data_len, greet_packet->auth_protocol,
     691        1858 :                                                                                  greet_packet->charset_no, options, mysql_flags, FALSE /*silent*/, FALSE/*is_change*/ TSRMLS_CC);
     692             :         }
     693        1861 :         DBG_RETURN(ret);
     694             : }
     695             : /* }}} */
     696             : 
     697             : 
     698             : /* {{{ mysqlnd_conn_data::execute_init_commands */
     699             : static enum_func_status
     700        1826 : MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
     701             : {
     702        1826 :         enum_func_status ret = PASS;
     703             : 
     704        1826 :         DBG_ENTER("mysqlnd_conn_data::execute_init_commands");
     705        1826 :         if (conn->options->init_commands) {
     706           9 :                 unsigned int current_command = 0;
     707          15 :                 for (; current_command < conn->options->num_commands; ++current_command) {
     708           9 :                         const char * const command = conn->options->init_commands[current_command];
     709           9 :                         if (command) {
     710           9 :                                 MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_EXECUTED_COUNT);
     711           9 :                                 if (PASS != conn->m->query(conn, command, strlen(command) TSRMLS_CC)) {
     712           3 :                                         MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_FAILED_COUNT);
     713           3 :                                         ret = FAIL;
     714           3 :                                         break;
     715             :                                 }
     716           6 :                                 if (conn->last_query_type == QUERY_SELECT) {
     717           1 :                                         MYSQLND_RES * result = conn->m->use_result(conn, 0 TSRMLS_CC);
     718           1 :                                         if (result) {
     719           1 :                                                 result->m.free_result(result, TRUE TSRMLS_CC);
     720             :                                         }
     721             :                                 }
     722             :                         }
     723             :                 }
     724             :         }
     725        1826 :         DBG_RETURN(ret);
     726             : }
     727             : /* }}} */
     728             : 
     729             : 
     730             : /* {{{ mysqlnd_conn_data::get_updated_connect_flags */
     731             : static unsigned int
     732        1875 : MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags)(MYSQLND_CONN_DATA * conn, unsigned int mysql_flags TSRMLS_DC)
     733             : {
     734        1875 :         MYSQLND_NET * net = conn->net;
     735             : 
     736        1875 :         DBG_ENTER("mysqlnd_conn_data::get_updated_connect_flags");
     737             :         /* we allow load data local infile by default */
     738        1875 :         mysql_flags |= MYSQLND_CAPABILITIES;
     739             : 
     740        1875 :         mysql_flags |= conn->options->flags; /* use the flags from set_client_option() */
     741             : 
     742        1875 :         if (PG(open_basedir) && strlen(PG(open_basedir))) {
     743           3 :                 mysql_flags ^= CLIENT_LOCAL_FILES;
     744             :         }
     745             : 
     746             : #ifndef MYSQLND_COMPRESSION_ENABLED
     747             :         if (mysql_flags & CLIENT_COMPRESS) {
     748             :                 mysql_flags &= ~CLIENT_COMPRESS;
     749             :         }
     750             : #else
     751        1875 :         if (net && net->data->options.flags & MYSQLND_NET_FLAG_USE_COMPRESSION) {
     752           0 :                 mysql_flags |= CLIENT_COMPRESS;
     753             :         }
     754             : #endif
     755             : #ifndef MYSQLND_SSL_SUPPORTED
     756             :         if (mysql_flags & CLIENT_SSL) {
     757             :                 mysql_flags &= ~CLIENT_SSL;
     758             :         }
     759             : #else
     760        7497 :         if (net && (net->data->options.ssl_key || net->data->options.ssl_cert ||
     761        5622 :                 net->data->options.ssl_ca || net->data->options.ssl_capath || net->data->options.ssl_cipher))
     762             :         {
     763           3 :                 mysql_flags |= CLIENT_SSL;
     764             :         }
     765             : #endif
     766             : 
     767        1875 :         DBG_RETURN(mysql_flags);
     768             : }
     769             : /* }}} */
     770             : 
     771             : 
     772             : /* {{{ mysqlnd_conn_data::connect_handshake */
     773             : static enum_func_status
     774        1875 : MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake)(MYSQLND_CONN_DATA * conn,
     775             :                                                 const char * const host, const char * const user,
     776             :                                                 const char * const passwd, const unsigned int passwd_len,
     777             :                                                 const char * const db, const unsigned int db_len,
     778             :                                                 const unsigned int mysql_flags TSRMLS_DC)
     779             : {
     780             :         MYSQLND_PACKET_GREET * greet_packet;
     781        1875 :         MYSQLND_NET * net = conn->net;
     782             : 
     783        1875 :         DBG_ENTER("mysqlnd_conn_data::connect_handshake");
     784             : 
     785        1875 :         greet_packet = conn->protocol->m.get_greet_packet(conn->protocol, FALSE TSRMLS_CC);
     786        1875 :         if (!greet_packet) {
     787           0 :                 SET_OOM_ERROR(*conn->error_info);
     788           0 :                 DBG_RETURN(FAIL); /* OOM */
     789             :         }
     790             : 
     791        1875 :         if (FAIL == net->data->m.connect_ex(conn->net, conn->scheme, conn->scheme_len, conn->persistent,
     792             :                                                                                 conn->stats, conn->error_info TSRMLS_CC))
     793             :         {
     794          14 :                 goto err;
     795             :         }
     796             : 
     797        1861 :         DBG_INF_FMT("stream=%p", net->data->m.get_stream(net TSRMLS_CC));
     798             : 
     799        1861 :         if (FAIL == PACKET_READ(greet_packet, conn)) {
     800           0 :                 DBG_ERR("Error while reading greeting packet");
     801           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading greeting packet. PID=%d", getpid());
     802           0 :                 goto err;
     803        1861 :         } else if (greet_packet->error_no) {
     804           0 :                 DBG_ERR_FMT("errorno=%u error=%s", greet_packet->error_no, greet_packet->error);
     805           0 :                 SET_CLIENT_ERROR(*conn->error_info, greet_packet->error_no, greet_packet->sqlstate, greet_packet->error);
     806           0 :                 goto err;
     807        1861 :         } else if (greet_packet->pre41) {
     808           0 :                 DBG_ERR_FMT("Connecting to 3.22, 3.23 & 4.0 is not supported. Server is %-.32s", greet_packet->server_version);
     809           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connecting to 3.22, 3.23 & 4.0 "
     810             :                                                 " is not supported. Server is %-.32s", greet_packet->server_version);
     811           0 :                 SET_CLIENT_ERROR(*conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
     812             :                                                  "Connecting to 3.22, 3.23 & 4.0 servers is not supported");
     813           0 :                 goto err;
     814             :         }
     815             : 
     816        1861 :         conn->thread_id                      = greet_packet->thread_id;
     817        1861 :         conn->protocol_version       = greet_packet->protocol_version;
     818        1861 :         conn->server_version = mnd_pestrdup(greet_packet->server_version, conn->persistent);
     819             : 
     820        1861 :         conn->greet_charset = mysqlnd_find_charset_nr(greet_packet->charset_no);
     821        1861 :         if (!conn->greet_charset) {
     822           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING,
     823           0 :                         "Server sent charset (%d) unknown to the client. Please, report to the developers", greet_packet->charset_no);
     824           0 :                 SET_CLIENT_ERROR(*conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
     825             :                         "Server sent charset unknown to the client. Please, report to the developers");
     826           0 :                 goto err;
     827             :         }
     828             : 
     829        1861 :         conn->client_flag                    = mysql_flags;
     830        1861 :         conn->server_capabilities    = greet_packet->server_capabilities;
     831             : 
     832        3722 :         if (FAIL == mysqlnd_connect_run_authentication(conn, user, passwd, db, db_len, (size_t) passwd_len,
     833        1861 :                                                                                                    greet_packet, conn->options, mysql_flags TSRMLS_CC))
     834             :         {
     835          35 :                 goto err;
     836             :         }
     837        1826 :         memset(conn->upsert_status, 0, sizeof(*conn->upsert_status));
     838        1826 :         conn->upsert_status->warning_count = 0;
     839        1826 :         conn->upsert_status->server_status = greet_packet->server_status;
     840        1826 :         conn->upsert_status->affected_rows = 0;
     841             : 
     842        1826 :         PACKET_FREE(greet_packet);
     843        1826 :         DBG_RETURN(PASS);
     844             : err:
     845          49 :         conn->client_flag = 0;
     846          49 :         conn->server_capabilities = 0;
     847          49 :         PACKET_FREE(greet_packet);
     848          49 :         DBG_RETURN(FAIL);
     849             : }
     850             : /* }}} */
     851             : 
     852             : 
     853             : /* {{{ mysqlnd_conn_data::connect */
     854             : static enum_func_status
     855        1875 : MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
     856             :                                                  const char *host, const char *user,
     857             :                                                  const char *passwd, unsigned int passwd_len,
     858             :                                                  const char *db, unsigned int db_len,
     859             :                                                  unsigned int port,
     860             :                                                  const char *socket_or_pipe,
     861             :                                                  unsigned int mysql_flags
     862             :                                                  TSRMLS_DC)
     863             : {
     864        1875 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, connect);
     865             :         size_t host_len;
     866        1875 :         zend_bool unix_socket = FALSE;
     867        1875 :         zend_bool named_pipe = FALSE;
     868        1875 :         zend_bool reconnect = FALSE;
     869        1875 :         zend_bool saved_compression = FALSE;
     870        1875 :         zend_bool local_tx_started = FALSE;
     871        1875 :         MYSQLND_NET * net = conn->net;
     872             : 
     873        1875 :         DBG_ENTER("mysqlnd_conn_data::connect");
     874        1875 :         DBG_INF_FMT("conn=%p", conn);
     875             : 
     876        1875 :         if (PASS != conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
     877           0 :                 goto err;
     878             :         }
     879        1875 :         local_tx_started = TRUE;
     880             : 
     881        1875 :         SET_EMPTY_ERROR(*conn->error_info);
     882        1875 :         SET_ERROR_AFF_ROWS(conn);
     883             : 
     884        7500 :         DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u persistent=%u state=%u",
     885             :                                 host?host:"", user?user:"", db?db:"", port, mysql_flags,
     886        5625 :                                 conn? conn->persistent:0, conn? CONN_GET_STATE(conn):-1);
     887             : 
     888        1876 :         if (CONN_GET_STATE(conn) > CONN_ALLOCED && CONN_GET_STATE(conn) ) {
     889           1 :                 DBG_INF("Connecting on a connected handle.");
     890             : 
     891           1 :                 if (CONN_GET_STATE(conn) < CONN_QUIT_SENT) {
     892           0 :                         MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CLOSE_IMPLICIT);
     893           0 :                         reconnect = TRUE;
     894           0 :                         conn->m->send_close(conn TSRMLS_CC);
     895             :                 }
     896             : 
     897           1 :                 conn->m->free_contents(conn TSRMLS_CC);
     898           1 :                 MYSQLND_DEC_CONN_STATISTIC(conn->stats, STAT_OPENED_CONNECTIONS);
     899           1 :                 if (conn->persistent) {
     900           1 :                         MYSQLND_DEC_CONN_STATISTIC(conn->stats, STAT_OPENED_PERSISTENT_CONNECTIONS);
     901             :                 }
     902             :                 /* Now reconnect using the same handle */
     903           1 :                 if (net->data->compressed) {
     904             :                         /*
     905             :                           we need to save the state. As we will re-connect, net->compressed should be off, or
     906             :                           we will look for a compression header as part of the greet message, but there will
     907             :                           be none.
     908             :                         */
     909           0 :                         saved_compression = TRUE;
     910           0 :                         net->data->compressed = FALSE;
     911             :                 }
     912           1 :                 if (net->data->ssl) {
     913           0 :                         net->data->ssl = FALSE;
     914             :                 }
     915             :         } else {
     916        1874 :                 unsigned int max_allowed_size = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
     917        1874 :                 conn->m->set_client_option(conn, MYSQLND_OPT_MAX_ALLOWED_PACKET, (char *)&max_allowed_size TSRMLS_CC);
     918             :         }
     919             : 
     920        1875 :         if (!host || !host[0]) {
     921          12 :                 host = "localhost";
     922             :         }
     923        1875 :         if (!user) {
     924           8 :                 DBG_INF_FMT("no user given, using empty string");
     925           8 :                 user = "";
     926             :         }
     927        1875 :         if (!passwd) {
     928          10 :                 DBG_INF_FMT("no password given, using empty string");
     929          10 :                 passwd = "";
     930          10 :                 passwd_len = 0;
     931             :         }
     932        1875 :         if (!db) {
     933         230 :                 DBG_INF_FMT("no db given, using empty string");
     934         230 :                 db = "";
     935         230 :                 db_len = 0;
     936             :         } else {
     937        1645 :                 mysql_flags |= CLIENT_CONNECT_WITH_DB;  
     938             :         }
     939             : 
     940        1875 :         host_len = strlen(host);
     941             :         {
     942        1875 :                 char * transport = NULL;
     943             :                 int transport_len;
     944             : #ifndef PHP_WIN32
     945        3739 :                 if (host_len == sizeof("localhost") - 1 && !strncasecmp(host, "localhost", host_len)) {
     946        1864 :                         DBG_INF_FMT("socket=%s", socket_or_pipe? socket_or_pipe:"n/a");
     947        1864 :                         if (!socket_or_pipe) {
     948           0 :                                 socket_or_pipe = "/tmp/mysql.sock";
     949             :                         }
     950        1864 :                         transport_len = mnd_sprintf(&transport, 0, "unix://%s", socket_or_pipe);
     951        1864 :                         unix_socket = TRUE;
     952             : #else
     953             :                 if (host_len == sizeof(".") - 1 && host[0] == '.') {
     954             :                         /* named pipe in socket */
     955             :                         if (!socket_or_pipe) {
     956             :                                 socket_or_pipe = "\\\\.\\pipe\\MySQL";
     957             :                         }
     958             :                         transport_len = mnd_sprintf(&transport, 0, "pipe://%s", socket_or_pipe);
     959             :                         named_pipe = TRUE;
     960             : #endif
     961             :                 } else {
     962          11 :                         if (!port) {
     963           1 :                                 port = 3306;
     964             :                         }
     965          11 :                         transport_len = mnd_sprintf(&transport, 0, "tcp://%s:%u", host, port);
     966             :                 }
     967        1875 :                 if (!transport) {
     968           0 :                         SET_OOM_ERROR(*conn->error_info);
     969           0 :                         goto err; /* OOM */
     970             :                 }
     971        1875 :                 DBG_INF_FMT("transport=%s conn->scheme=%s", transport, conn->scheme);
     972        1875 :                 conn->scheme = mnd_pestrndup(transport, transport_len, conn->persistent);
     973        1875 :                 conn->scheme_len = transport_len;
     974        1875 :                 mnd_sprintf_free(transport);
     975        1875 :                 transport = NULL;
     976        1875 :                 if (!conn->scheme) {
     977           0 :                         goto err; /* OOM */
     978             :                 }
     979             :         }
     980             : 
     981        1875 :         mysql_flags = conn->m->get_updated_connect_flags(conn, mysql_flags TSRMLS_CC);
     982             : 
     983        1875 :         if (FAIL == conn->m->connect_handshake(conn, host, user, passwd, passwd_len, db, db_len, mysql_flags TSRMLS_CC)) {
     984          49 :                 goto err;
     985             :         }
     986             : 
     987             :         {
     988        1826 :                 CONN_SET_STATE(conn, CONN_READY);
     989             : 
     990        1826 :                 if (saved_compression) {
     991           0 :                         net->data->compressed = TRUE;
     992             :                 }
     993             :                 /*
     994             :                   If a connect on a existing handle is performed and mysql_flags is
     995             :                   passed which doesn't CLIENT_COMPRESS, then we need to overwrite the value
     996             :                   which we set based on saved_compression.
     997             :                 */
     998        1826 :                 net->data->compressed = mysql_flags & CLIENT_COMPRESS? TRUE:FALSE;
     999             : 
    1000        1826 :                 conn->user_len                       = strlen(user);
    1001        1826 :                 conn->user                           = mnd_pestrndup(user, conn->user_len, conn->persistent);
    1002        1826 :                 conn->passwd                 = mnd_pestrndup(passwd, passwd_len, conn->persistent);
    1003        1826 :                 conn->passwd_len             = passwd_len;
    1004        1826 :                 conn->port                           = port;
    1005        1826 :                 conn->connect_or_select_db = mnd_pestrndup(db, db_len, conn->persistent);
    1006        1826 :                 conn->connect_or_select_db_len = db_len;
    1007             : 
    1008        1826 :                 if (!conn->user || !conn->passwd || !conn->connect_or_select_db) {
    1009           0 :                         SET_OOM_ERROR(*conn->error_info);
    1010           0 :                         goto err; /* OOM */
    1011             :                 }
    1012             : 
    1013        1828 :                 if (!unix_socket && !named_pipe) {
    1014           2 :                         conn->host = mnd_pestrndup(host, host_len, conn->persistent);
    1015           2 :                         if (!conn->host) {
    1016           0 :                                 SET_OOM_ERROR(*conn->error_info);
    1017           0 :                                 goto err; /* OOM */
    1018             :                         }
    1019           2 :                         conn->host_len = host_len;
    1020             :                         {
    1021             :                                 char *p;
    1022           2 :                                 mnd_sprintf(&p, 0, "%s via TCP/IP", conn->host);
    1023           2 :                                 if (!p) {
    1024           0 :                                         SET_OOM_ERROR(*conn->error_info);
    1025           0 :                                         goto err; /* OOM */
    1026             :                                 }
    1027           2 :                                 conn->host_info = mnd_pestrdup(p, conn->persistent);
    1028           2 :                                 mnd_sprintf_free(p);
    1029           2 :                                 if (!conn->host_info) {
    1030           0 :                                         SET_OOM_ERROR(*conn->error_info);
    1031           0 :                                         goto err; /* OOM */
    1032             :                                 }
    1033             :                         }
    1034             :                 } else {
    1035        1824 :                         conn->unix_socket = mnd_pestrdup(socket_or_pipe, conn->persistent);
    1036        1824 :                         if (unix_socket) {
    1037        1824 :                                 conn->host_info = mnd_pestrdup("Localhost via UNIX socket", conn->persistent);
    1038           0 :                         } else if (named_pipe) {
    1039             :                                 char *p;
    1040           0 :                                 mnd_sprintf(&p, 0, "%s via named pipe", conn->unix_socket);
    1041           0 :                                 if (!p) {
    1042           0 :                                         SET_OOM_ERROR(*conn->error_info);
    1043           0 :                                         goto err; /* OOM */
    1044             :                                 }
    1045           0 :                                 conn->host_info =  mnd_pestrdup(p, conn->persistent);
    1046           0 :                                 mnd_sprintf_free(p);
    1047           0 :                                 if (!conn->host_info) {
    1048           0 :                                         SET_OOM_ERROR(*conn->error_info);
    1049           0 :                                         goto err; /* OOM */
    1050             :                                 }
    1051             :                         } else {
    1052           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Impossible. Should be either socket or a pipe. Report a bug!");
    1053             :                         }
    1054        1824 :                         if (!conn->unix_socket || !conn->host_info) {
    1055           0 :                                 SET_OOM_ERROR(*conn->error_info);
    1056           0 :                                 goto err; /* OOM */
    1057             :                         }
    1058        1824 :                         conn->unix_socket_len = strlen(conn->unix_socket);
    1059             :                 }
    1060        1826 :                 conn->max_packet_size        = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
    1061             :                 /* todo: check if charset is available */
    1062             : 
    1063        1826 :                 SET_EMPTY_ERROR(*conn->error_info);
    1064             : 
    1065        1826 :                 mysqlnd_local_infile_default(conn);
    1066             : 
    1067        1826 :                 if (FAIL == conn->m->execute_init_commands(conn TSRMLS_CC)) {
    1068           3 :                         goto err;
    1069             :                 }
    1070             : 
    1071        1823 :                 MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_CONNECT_SUCCESS, 1, STAT_OPENED_CONNECTIONS, 1);
    1072        1823 :                 if (reconnect) {
    1073           0 :                         MYSQLND_INC_GLOBAL_STATISTIC(STAT_RECONNECT);
    1074             :                 }
    1075        1823 :                 if (conn->persistent) {
    1076        1122 :                         MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_PCONNECT_SUCCESS, 1, STAT_OPENED_PERSISTENT_CONNECTIONS, 1);
    1077             :                 }
    1078             : 
    1079        1823 :                 DBG_INF_FMT("connection_id=%llu", conn->thread_id);
    1080             : 
    1081        1823 :                 conn->m->local_tx_end(conn, this_func, PASS TSRMLS_CC);
    1082        1823 :                 DBG_RETURN(PASS);
    1083             :         }
    1084             : err:
    1085             : 
    1086          52 :         DBG_ERR_FMT("[%u] %.128s (trying to connect via %s)", conn->error_info->error_no, conn->error_info->error, conn->scheme);
    1087          52 :         if (!conn->error_info->error_no) {
    1088           3 :                 SET_CLIENT_ERROR(*conn->error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, conn->error_info->error? conn->error_info->error:"Unknown error");
    1089           9 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "[%u] %.128s (trying to connect via %s)",
    1090           6 :                                                  conn->error_info->error_no, conn->error_info->error, conn->scheme);
    1091             :         }
    1092             : 
    1093          52 :         conn->m->free_contents(conn TSRMLS_CC);
    1094          52 :         MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_FAILURE);
    1095          52 :         if (TRUE == local_tx_started) {
    1096          52 :                 conn->m->local_tx_end(conn, this_func, FAIL TSRMLS_CC);
    1097             :         }
    1098             : 
    1099          52 :         DBG_RETURN(FAIL);
    1100             : }
    1101             : /* }}} */
    1102             : 
    1103             : 
    1104             : /* {{{ mysqlnd_conn::connect */
    1105             : static enum_func_status
    1106        1875 : MYSQLND_METHOD(mysqlnd_conn, connect)(MYSQLND * conn_handle,
    1107             :                                                  const char * host, const char * user,
    1108             :                                                  const char * passwd, unsigned int passwd_len,
    1109             :                                                  const char * db, unsigned int db_len,
    1110             :                                                  unsigned int port,
    1111             :                                                  const char * socket_or_pipe,
    1112             :                                                  unsigned int mysql_flags
    1113             :                                                  TSRMLS_DC)
    1114             : {
    1115        1875 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, connect);
    1116        1875 :         enum_func_status ret = FAIL;
    1117        1875 :         MYSQLND_CONN_DATA * conn = conn_handle->data;
    1118             : 
    1119        1875 :         DBG_ENTER("mysqlnd_conn::connect");
    1120             : 
    1121        1875 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1122        1875 :                 mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_client_name", "mysqlnd");
    1123        1875 :                 ret = conn->m->connect(conn, host, user, passwd, passwd_len, db, db_len, port, socket_or_pipe, mysql_flags TSRMLS_CC);
    1124             : 
    1125        1875 :                 conn->m->local_tx_end(conn, this_func, FAIL TSRMLS_CC);
    1126             :         }
    1127        1875 :         DBG_RETURN(ret);
    1128             : }
    1129             : /* }}} */
    1130             : 
    1131             : 
    1132             : /* {{{ mysqlnd_connect */
    1133        1875 : PHPAPI MYSQLND * mysqlnd_connect(MYSQLND * conn_handle,
    1134             :                                                  const char * host, const char * user,
    1135             :                                                  const char * passwd, unsigned int passwd_len,
    1136             :                                                  const char * db, unsigned int db_len,
    1137             :                                                  unsigned int port,
    1138             :                                                  const char * socket_or_pipe,
    1139             :                                                  unsigned int mysql_flags,
    1140             :                                                  unsigned int client_api_flags
    1141             :                                                  TSRMLS_DC)
    1142             : {
    1143        1875 :         enum_func_status ret = FAIL;
    1144        1875 :         zend_bool self_alloced = FALSE;
    1145             : 
    1146        1875 :         DBG_ENTER("mysqlnd_connect");
    1147        1875 :         DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u", host?host:"", user?user:"", db?db:"", port, mysql_flags);
    1148             : 
    1149        1875 :         if (!conn_handle) {
    1150           0 :                 self_alloced = TRUE;
    1151           0 :                 if (!(conn_handle = mysqlnd_init(client_api_flags, FALSE))) {
    1152             :                         /* OOM */
    1153           0 :                         DBG_RETURN(NULL);
    1154             :                 }
    1155             :         }
    1156             : 
    1157        1875 :         ret = conn_handle->m->connect(conn_handle, host, user, passwd, passwd_len, db, db_len, port, socket_or_pipe, mysql_flags TSRMLS_CC);
    1158             : 
    1159        1875 :         if (ret == FAIL) {
    1160          52 :                 if (self_alloced) {
    1161             :                         /*
    1162             :                           We have alloced, thus there are no references to this
    1163             :                           object - we are free to kill it!
    1164             :                         */
    1165           0 :                         conn_handle->m->dtor(conn_handle TSRMLS_CC);
    1166             :                 }
    1167          52 :                 DBG_RETURN(NULL);
    1168             :         }
    1169        1823 :         DBG_RETURN(conn_handle);
    1170             : }
    1171             : /* }}} */
    1172             : 
    1173             : 
    1174             : /* {{{ mysqlnd_conn_data::query */
    1175             : /*
    1176             :   If conn->error_info->error_no is not zero, then we had an error.
    1177             :   Still the result from the query is PASS
    1178             : */
    1179             : static enum_func_status
    1180       23416 : MYSQLND_METHOD(mysqlnd_conn_data, query)(MYSQLND_CONN_DATA * conn, const char * query, unsigned int query_len TSRMLS_DC)
    1181             : {
    1182       23416 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, query);
    1183       23416 :         enum_func_status ret = FAIL;
    1184       23416 :         DBG_ENTER("mysqlnd_conn_data::query");
    1185       23416 :         DBG_INF_FMT("conn=%p conn=%llu query=%s", conn, conn->thread_id, query);
    1186             : 
    1187       23416 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1188       46817 :                 if (PASS == conn->m->send_query(conn, query, query_len TSRMLS_CC) &&
    1189       23401 :                         PASS == conn->m->reap_query(conn TSRMLS_CC))
    1190             :                 {
    1191       22626 :                         ret = PASS;
    1192       22626 :                         if (conn->last_query_type == QUERY_UPSERT && conn->upsert_status->affected_rows) {
    1193       15795 :                                 MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, conn->upsert_status->affected_rows);
    1194             :                         }
    1195             :                 }
    1196       23416 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1197             :         }
    1198       23416 :         DBG_RETURN(ret);
    1199             : }
    1200             : /* }}} */
    1201             : 
    1202             : 
    1203             : /* {{{ mysqlnd_conn_data::send_query */
    1204             : static enum_func_status
    1205       23445 : MYSQLND_METHOD(mysqlnd_conn_data, send_query)(MYSQLND_CONN_DATA * conn, const char * query, unsigned int query_len TSRMLS_DC)
    1206             : {
    1207       23445 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, send_query);
    1208       23445 :         enum_func_status ret = FAIL;
    1209       23445 :         DBG_ENTER("mysqlnd_conn_data::send_query");
    1210       23445 :         DBG_INF_FMT("conn=%llu query=%s", conn->thread_id, query);
    1211       23445 :         DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
    1212             : 
    1213       23445 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1214       23445 :                 ret = conn->m->simple_command(conn, COM_QUERY, (zend_uchar *) query, query_len,
    1215             :                                                                                          PROT_LAST /* we will handle the OK packet*/,
    1216             :                                                                                          FALSE, FALSE TSRMLS_CC);
    1217       23445 :                 if (PASS == ret) {
    1218       23428 :                         CONN_SET_STATE(conn, CONN_QUERY_SENT);
    1219             :                 }
    1220       23445 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1221             :         }
    1222       23445 :         DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
    1223       23445 :         DBG_RETURN(ret);
    1224             : }
    1225             : /* }}} */
    1226             : 
    1227             : 
    1228             : /* {{{ mysqlnd_conn_data::reap_query */
    1229             : static enum_func_status
    1230       23427 : MYSQLND_METHOD(mysqlnd_conn_data, reap_query)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
    1231             : {
    1232       23427 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, reap_query);
    1233       23427 :         enum_mysqlnd_connection_state state = CONN_GET_STATE(conn);
    1234       23427 :         enum_func_status ret = FAIL;
    1235       23427 :         DBG_ENTER("mysqlnd_conn_data::reap_query");
    1236       23427 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    1237             : 
    1238       23427 :         DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
    1239       23427 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1240       23427 :                 if (state <= CONN_READY || state == CONN_QUIT_SENT) {
    1241           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection not opened, clear or has been closed");
    1242           0 :                         DBG_ERR_FMT("Connection not opened, clear or has been closed. State=%u", state);
    1243           0 :                         DBG_RETURN(ret);
    1244             :                 }
    1245       23427 :                 ret = conn->m->query_read_result_set_header(conn, NULL TSRMLS_CC);
    1246             : 
    1247       23427 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1248             :         }
    1249       23427 :         DBG_INF_FMT("conn->server_status=%u", conn->upsert_status->server_status);
    1250       23427 :         DBG_RETURN(ret);
    1251             : }
    1252             : /* }}} */
    1253             : 
    1254             : 
    1255             : #include "php_network.h"
    1256             : 
    1257             : /* {{{ mysqlnd_stream_array_to_fd_set */
    1258          33 : MYSQLND ** mysqlnd_stream_array_check_for_readiness(MYSQLND ** conn_array TSRMLS_DC)
    1259             : {
    1260          33 :         int cnt = 0;
    1261          33 :         MYSQLND **p = conn_array, **p_p;
    1262          33 :         MYSQLND **ret = NULL;
    1263             : 
    1264         127 :         while (*p) {
    1265          61 :                 if (CONN_GET_STATE((*p)->data) <= CONN_READY || CONN_GET_STATE((*p)->data) == CONN_QUIT_SENT) {
    1266          20 :                         cnt++;
    1267             :                 }
    1268          61 :                 p++;
    1269             :         }
    1270          33 :         if (cnt) {
    1271          18 :                 MYSQLND **ret_p = ret = ecalloc(cnt + 1, sizeof(MYSQLND *));
    1272          18 :                 p_p = p = conn_array;
    1273          65 :                 while (*p) {
    1274          49 :                         if (CONN_GET_STATE((*p)->data) <= CONN_READY || CONN_GET_STATE((*p)->data) == CONN_QUIT_SENT) {
    1275          20 :                                 *ret_p = *p;
    1276          20 :                                 *p = NULL;
    1277          20 :                                 ret_p++;
    1278             :                         } else {
    1279           9 :                                 *p_p = *p;
    1280           9 :                                 p_p++;
    1281             :                         }
    1282          29 :                         p++;
    1283             :                 }
    1284          18 :                 *ret_p = NULL;
    1285             :         }
    1286          33 :         return ret;
    1287             : }
    1288             : /* }}} */
    1289             : 
    1290             : 
    1291             : /* {{{ mysqlnd_stream_array_to_fd_set */
    1292          65 : static int mysqlnd_stream_array_to_fd_set(MYSQLND ** conn_array, fd_set * fds, php_socket_t * max_fd TSRMLS_DC)
    1293             : {
    1294             :         php_socket_t this_fd;
    1295          65 :         php_stream *stream = NULL;
    1296          65 :         unsigned int cnt = 0;
    1297          65 :         MYSQLND **p = conn_array;
    1298          65 :         DBG_ENTER("mysqlnd_stream_array_to_fd_set");
    1299             : 
    1300         238 :         while (*p) {
    1301             :                 /* get the fd.
    1302             :                  * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
    1303             :                  * when casting.  It is only used here so that the buffered data warning
    1304             :                  * is not displayed.
    1305             :                  * */
    1306         108 :                 stream = (*p)->data->net->data->m.get_stream((*p)->data->net TSRMLS_CC);
    1307         108 :                 DBG_INF_FMT("conn=%llu stream=%p", (*p)->data->thread_id, stream);
    1308         322 :                 if (stream != NULL && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL,
    1309         214 :                                                                                 (void*)&this_fd, 1) && ZEND_VALID_SOCKET(this_fd)) {
    1310             : 
    1311         107 :                         PHP_SAFE_FD_SET(this_fd, fds);
    1312             : 
    1313         107 :                         if (this_fd > *max_fd) {
    1314          49 :                                 *max_fd = this_fd;
    1315             :                         }
    1316         107 :                         cnt++;
    1317             :                 }
    1318         108 :                 p++;
    1319             :         }
    1320          65 :         DBG_RETURN(cnt ? 1 : 0);
    1321             : }
    1322             : /* }}} */
    1323             : 
    1324             : 
    1325             : /* {{{ mysqlnd_stream_array_from_fd_set */
    1326          58 : static int mysqlnd_stream_array_from_fd_set(MYSQLND ** conn_array, fd_set * fds TSRMLS_DC)
    1327             : {
    1328             :         php_socket_t this_fd;
    1329          58 :         php_stream *stream = NULL;
    1330          58 :         int ret = 0;
    1331          58 :         zend_bool disproportion = FALSE;
    1332          58 :         MYSQLND **fwd = conn_array, **bckwd = conn_array;
    1333          58 :         DBG_ENTER("mysqlnd_stream_array_from_fd_set");
    1334             : 
    1335         223 :         while (*fwd) {
    1336         107 :                 stream = (*fwd)->data->net->data->m.get_stream((*fwd)->data->net TSRMLS_CC);
    1337         107 :                 DBG_INF_FMT("conn=%llu stream=%p", (*fwd)->data->thread_id, stream);
    1338         321 :                 if (stream != NULL && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL,
    1339         214 :                                                                                 (void*)&this_fd, 1) && ZEND_VALID_SOCKET(this_fd)) {
    1340         107 :                         if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
    1341          31 :                                 if (disproportion) {
    1342           0 :                                         *bckwd = *fwd;
    1343             :                                 }
    1344          31 :                                 bckwd++;
    1345          31 :                                 fwd++;
    1346          31 :                                 ret++;
    1347          31 :                                 continue;
    1348             :                         }
    1349             :                 }
    1350          76 :                 disproportion = TRUE;
    1351          76 :                 fwd++;
    1352             :         }
    1353          58 :         *bckwd = NULL;/* NULL-terminate the list */
    1354             : 
    1355          58 :         DBG_RETURN(ret);
    1356             : }
    1357             : /* }}} */
    1358             : 
    1359             : 
    1360             : #ifndef PHP_WIN32
    1361             : #define php_select(m, r, w, e, t)       select(m, r, w, e, t)
    1362             : #else
    1363             : #include "win32/select.h"
    1364             : #endif
    1365             : 
    1366             : 
    1367             : /* {{{ _mysqlnd_poll */
    1368             : PHPAPI enum_func_status
    1369          33 : _mysqlnd_poll(MYSQLND **r_array, MYSQLND **e_array, MYSQLND ***dont_poll, long sec, long usec, int * desc_num TSRMLS_DC)
    1370             : {
    1371             :         struct timeval  tv;
    1372          33 :         struct timeval *tv_p = NULL;
    1373             :         fd_set                  rfds, wfds, efds;
    1374          33 :         php_socket_t    max_fd = 0;
    1375          33 :         int                             retval, sets = 0;
    1376          33 :         int                             set_count, max_set_count = 0;
    1377             : 
    1378          33 :         DBG_ENTER("_mysqlnd_poll");
    1379          33 :         if (sec < 0 || usec < 0) {
    1380           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Negative values passed for sec and/or usec");
    1381           0 :                 DBG_RETURN(FAIL);
    1382             :         }
    1383             : 
    1384          33 :         FD_ZERO(&rfds);
    1385          33 :         FD_ZERO(&wfds);
    1386          33 :         FD_ZERO(&efds);
    1387             : 
    1388          33 :         if (r_array != NULL) {
    1389          33 :                 *dont_poll = mysqlnd_stream_array_check_for_readiness(r_array TSRMLS_CC);
    1390          33 :                 set_count = mysqlnd_stream_array_to_fd_set(r_array, &rfds, &max_fd TSRMLS_CC);
    1391          33 :                 if (set_count > max_set_count) {
    1392          22 :                         max_set_count = set_count;
    1393             :                 }
    1394          33 :                 sets += set_count;
    1395             :         }
    1396             : 
    1397          33 :         if (e_array != NULL) {
    1398          32 :                 set_count = mysqlnd_stream_array_to_fd_set(e_array, &efds, &max_fd TSRMLS_CC);
    1399          32 :                 if (set_count > max_set_count) {
    1400           7 :                         max_set_count = set_count;
    1401             :                 }
    1402          32 :                 sets += set_count;
    1403             :         }
    1404             : 
    1405          33 :         if (!sets) {
    1406           4 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, *dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
    1407           4 :                 DBG_ERR_FMT(*dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
    1408           4 :                 DBG_RETURN(FAIL);
    1409             :         }
    1410             : 
    1411          29 :         PHP_SAFE_MAX_FD(max_fd, max_set_count);
    1412             : 
    1413             :         /* Solaris + BSD do not like microsecond values which are >= 1 sec */
    1414          29 :         if (usec > 999999) {
    1415           0 :                 tv.tv_sec = sec + (usec / 1000000);
    1416           0 :                 tv.tv_usec = usec % 1000000;
    1417             :         } else {
    1418          29 :                 tv.tv_sec = sec;
    1419          29 :                 tv.tv_usec = usec;
    1420             :         }
    1421             : 
    1422          29 :         tv_p = &tv;
    1423             : 
    1424          29 :         retval = php_select(max_fd + 1, &rfds, &wfds, &efds, tv_p);
    1425             : 
    1426          29 :         if (retval == -1) {
    1427           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to select [%d]: %s (max_fd=%d)",
    1428             :                                                 errno, strerror(errno), max_fd);
    1429           0 :                 DBG_RETURN(FAIL);
    1430             :         }
    1431             : 
    1432          29 :         if (r_array != NULL) {
    1433          29 :                 mysqlnd_stream_array_from_fd_set(r_array, &rfds TSRMLS_CC);
    1434             :         }
    1435          29 :         if (e_array != NULL) {
    1436          29 :                 mysqlnd_stream_array_from_fd_set(e_array, &efds TSRMLS_CC);
    1437             :         }
    1438             : 
    1439          29 :         *desc_num = retval;
    1440          29 :         DBG_RETURN(PASS);
    1441             : }
    1442             : /* }}} */
    1443             : 
    1444             : 
    1445             : /*
    1446             :   COM_FIELD_LIST is special, different from a SHOW FIELDS FROM :
    1447             :   - There is no result set header - status from the command, which
    1448             :     impacts us to allocate big chunk of memory for reading the metadata.
    1449             :   - The EOF packet is consumed by the metadata packet reader.
    1450             : */
    1451             : 
    1452             : /* {{{ mysqlnd_conn_data::list_fields */
    1453             : MYSQLND_RES *
    1454           4 : MYSQLND_METHOD(mysqlnd_conn_data, list_fields)(MYSQLND_CONN_DATA * conn, const char *table, const char *achtung_wild TSRMLS_DC)
    1455             : {
    1456           4 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, list_fields);
    1457             :         /* db + \0 + wild + \0 (for wild) */
    1458             :         zend_uchar buff[MYSQLND_MAX_ALLOWED_DB_LEN * 2 + 1 + 1], *p;
    1459             :         size_t table_len, wild_len;
    1460           4 :         MYSQLND_RES * result = NULL;
    1461           4 :         DBG_ENTER("mysqlnd_conn_data::list_fields");
    1462           4 :         DBG_INF_FMT("conn=%llu table=%s wild=%s", conn->thread_id, table? table:"",achtung_wild? achtung_wild:"");
    1463             : 
    1464           4 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1465             :                 do {
    1466           4 :                         p = buff;
    1467           4 :                         if (table && (table_len = strlen(table))) {
    1468           4 :                                 size_t to_copy = MIN(table_len, MYSQLND_MAX_ALLOWED_DB_LEN);
    1469           4 :                                 memcpy(p, table, to_copy);
    1470           4 :                                 p += to_copy;
    1471           4 :                                 *p++ = '\0';
    1472             :                         }
    1473             : 
    1474           4 :                         if (achtung_wild && (wild_len = strlen(achtung_wild))) {
    1475           0 :                                 size_t to_copy = MIN(wild_len, MYSQLND_MAX_ALLOWED_DB_LEN);
    1476           0 :                                 memcpy(p, achtung_wild, to_copy);
    1477           0 :                                 p += to_copy;
    1478           0 :                                 *p++ = '\0';
    1479             :                         }
    1480             : 
    1481           4 :                         if (PASS != conn->m->simple_command(conn, COM_FIELD_LIST, buff, p - buff,
    1482             :                                                                                            PROT_LAST /* we will handle the OK packet*/,
    1483             :                                                                                            FALSE, TRUE TSRMLS_CC)) {
    1484           0 :                                 conn->m->local_tx_end(conn, 0, FAIL TSRMLS_CC);
    1485           0 :                                 break;
    1486             :                         }
    1487             : 
    1488             :                         /*
    1489             :                            Prepare for the worst case.
    1490             :                            MyISAM goes to 2500 BIT columns, double it for safety.
    1491             :                         */
    1492           4 :                         result = conn->m->result_init(5000, conn->persistent TSRMLS_CC);
    1493           4 :                         if (!result) {
    1494           0 :                                 break;
    1495             :                         }
    1496             : 
    1497           4 :                         if (FAIL == result->m.read_result_metadata(result, conn TSRMLS_CC)) {
    1498           1 :                                 DBG_ERR("Error occurred while reading metadata");
    1499           1 :                                 result->m.free_result(result, TRUE TSRMLS_CC);
    1500           1 :                                 result = NULL;
    1501           1 :                                 break;
    1502             :                         }
    1503             : 
    1504           3 :                         result->type = MYSQLND_RES_NORMAL;
    1505           3 :                         result->unbuf = mysqlnd_result_unbuffered_init(result->field_count, FALSE, result->persistent TSRMLS_CC);
    1506           3 :                         if (!result->unbuf) {
    1507             :                                 /* OOM */
    1508           0 :                                 SET_OOM_ERROR(*conn->error_info);
    1509           0 :                                 result->m.free_result(result, TRUE TSRMLS_CC);
    1510           0 :                                 result = NULL;
    1511           0 :                                 break;
    1512             :                         }
    1513           3 :                         result->unbuf->eof_reached = TRUE;
    1514             :                 } while (0);
    1515           4 :                 conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS TSRMLS_CC);
    1516             :         }
    1517             : 
    1518           4 :         DBG_RETURN(result);
    1519             : }
    1520             : /* }}} */
    1521             : 
    1522             : 
    1523             : /* {{{ mysqlnd_conn_data::list_method */
    1524             : MYSQLND_RES *
    1525           8 : MYSQLND_METHOD(mysqlnd_conn_data, list_method)(MYSQLND_CONN_DATA * conn, const char * query, const char *achtung_wild, char *par1 TSRMLS_DC)
    1526             : {
    1527           8 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, list_method);
    1528           8 :         char * show_query = NULL;
    1529             :         size_t show_query_len;
    1530           8 :         MYSQLND_RES * result = NULL;
    1531             : 
    1532           8 :         DBG_ENTER("mysqlnd_conn_data::list_method");
    1533           8 :         DBG_INF_FMT("conn=%llu query=%s wild=%u", conn->thread_id, query, achtung_wild);
    1534             : 
    1535           8 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1536           8 :                 if (par1) {
    1537           0 :                         if (achtung_wild) {
    1538           0 :                                 show_query_len = mnd_sprintf(&show_query, 0, query, par1, achtung_wild);
    1539             :                         } else {
    1540           0 :                                 show_query_len = mnd_sprintf(&show_query, 0, query, par1);
    1541             :                         }
    1542             :                 } else {
    1543           8 :                         if (achtung_wild) {
    1544           0 :                                 show_query_len = mnd_sprintf(&show_query, 0, query, achtung_wild);
    1545             :                         } else {
    1546           8 :                                 show_query_len = strlen(show_query = (char *)query);
    1547             :                         }
    1548             :                 }
    1549             : 
    1550           8 :                 if (PASS == conn->m->query(conn, show_query, show_query_len TSRMLS_CC)) {
    1551           8 :                         result = conn->m->store_result(conn, MYSQLND_STORE_NO_COPY TSRMLS_CC);
    1552             :                 }
    1553           8 :                 if (show_query != query) {
    1554           0 :                         mnd_sprintf_free(show_query);
    1555             :                 }
    1556           8 :                 conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS TSRMLS_CC);
    1557             :         }
    1558           8 :         DBG_RETURN(result);
    1559             : }
    1560             : /* }}} */
    1561             : 
    1562             : 
    1563             : /* {{{ mysqlnd_conn_data::errno */
    1564             : static unsigned int
    1565        2080 : MYSQLND_METHOD(mysqlnd_conn_data, errno)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1566             : {
    1567        2080 :         return conn->error_info->error_no;
    1568             : }
    1569             : /* }}} */
    1570             : 
    1571             : 
    1572             : /* {{{ mysqlnd_conn_data::error */
    1573             : static const char *
    1574        2019 : MYSQLND_METHOD(mysqlnd_conn_data, error)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1575             : {
    1576        2019 :         return conn->error_info->error;
    1577             : }
    1578             : /* }}} */
    1579             : 
    1580             : 
    1581             : /* {{{ mysqlnd_conn_data::sqlstate */
    1582             : static const char *
    1583         765 : MYSQLND_METHOD(mysqlnd_conn_data, sqlstate)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1584             : {
    1585         765 :         return conn->error_info->sqlstate[0] ? conn->error_info->sqlstate:MYSQLND_SQLSTATE_NULL;
    1586             : }
    1587             : /* }}} */
    1588             : 
    1589             : 
    1590             : /* {{{ mysqlnd_old_escape_string */
    1591             : PHPAPI  zend_ulong
    1592           9 : mysqlnd_old_escape_string(char * newstr, const char * escapestr, size_t escapestr_len TSRMLS_DC)
    1593             : {
    1594           9 :         DBG_ENTER("mysqlnd_old_escape_string");
    1595           9 :         DBG_RETURN(mysqlnd_cset_escape_slashes(mysqlnd_find_charset_name("latin1"), newstr, escapestr, escapestr_len TSRMLS_CC));
    1596             : }
    1597             : /* }}} */
    1598             : 
    1599             : 
    1600             : /* {{{ mysqlnd_conn_data::ssl_set */
    1601             : static enum_func_status
    1602           3 : MYSQLND_METHOD(mysqlnd_conn_data, ssl_set)(MYSQLND_CONN_DATA * const conn, const char * key, const char * const cert,
    1603             :                                                                           const char * const ca, const char * const capath, const char * const cipher TSRMLS_DC)
    1604             : {
    1605           3 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, ssl_set);
    1606           3 :         enum_func_status ret = FAIL;
    1607           3 :         MYSQLND_NET * net = conn->net;
    1608           3 :         DBG_ENTER("mysqlnd_conn_data::ssl_set");
    1609             : 
    1610           3 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1611          15 :                 ret = (PASS == net->data->m.set_client_option(net, MYSQLND_OPT_SSL_KEY, key TSRMLS_CC) &&
    1612           3 :                         PASS == net->data->m.set_client_option(net, MYSQLND_OPT_SSL_CERT, cert TSRMLS_CC) &&
    1613           3 :                         PASS == net->data->m.set_client_option(net, MYSQLND_OPT_SSL_CA, ca TSRMLS_CC) &&
    1614           3 :                         PASS == net->data->m.set_client_option(net, MYSQLND_OPT_SSL_CAPATH, capath TSRMLS_CC) &&
    1615           3 :                         PASS == net->data->m.set_client_option(net, MYSQLND_OPT_SSL_CIPHER, cipher TSRMLS_CC)) ? PASS : FAIL;
    1616             : 
    1617           3 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1618             :         }
    1619           3 :         DBG_RETURN(ret);
    1620             : }
    1621             : /* }}} */
    1622             : 
    1623             : 
    1624             : /* {{{ mysqlnd_conn_data::escape_string */
    1625             : static zend_ulong
    1626       12980 : MYSQLND_METHOD(mysqlnd_conn_data, escape_string)(MYSQLND_CONN_DATA * const conn, char * newstr, const char * escapestr, size_t escapestr_len TSRMLS_DC)
    1627             : {
    1628       12980 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, escape_string);
    1629       12980 :         zend_ulong ret = FAIL;
    1630       12980 :         DBG_ENTER("mysqlnd_conn_data::escape_string");
    1631       12980 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    1632             : 
    1633       12980 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1634       12980 :                 DBG_INF_FMT("server_status=%u", conn->upsert_status->server_status);
    1635       12980 :                 if (conn->upsert_status->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES) {
    1636           9 :                         ret = mysqlnd_cset_escape_quotes(conn->charset, newstr, escapestr, escapestr_len TSRMLS_CC);
    1637             :                 } else {
    1638       12971 :                         ret = mysqlnd_cset_escape_slashes(conn->charset, newstr, escapestr, escapestr_len TSRMLS_CC);
    1639             :                 }
    1640       12980 :                 conn->m->local_tx_end(conn, this_func, PASS TSRMLS_CC);
    1641             :         }
    1642       12980 :         DBG_RETURN(ret);
    1643             : }
    1644             : /* }}} */
    1645             : 
    1646             : 
    1647             : /* {{{ mysqlnd_conn_data::dump_debug_info */
    1648             : static enum_func_status
    1649           2 : MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1650             : {
    1651           2 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, server_dump_debug_information);
    1652           2 :         enum_func_status ret = FAIL;
    1653           2 :         DBG_ENTER("mysqlnd_conn_data::dump_debug_info");
    1654           2 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    1655           2 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1656           2 :                 ret = conn->m->simple_command(conn, COM_DEBUG, NULL, 0, PROT_EOF_PACKET, FALSE, TRUE TSRMLS_CC);
    1657             : 
    1658           2 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1659             :         }
    1660             : 
    1661           2 :         DBG_RETURN(ret);
    1662             : }
    1663             : /* }}} */
    1664             : 
    1665             : 
    1666             : /* {{{ mysqlnd_conn_data::select_db */
    1667             : static enum_func_status
    1668         238 : MYSQLND_METHOD(mysqlnd_conn_data, select_db)(MYSQLND_CONN_DATA * const conn, const char * const db, unsigned int db_len TSRMLS_DC)
    1669             : {
    1670         238 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, select_db);
    1671         238 :         enum_func_status ret = FAIL;
    1672             : 
    1673         238 :         DBG_ENTER("mysqlnd_conn_data::select_db");
    1674         238 :         DBG_INF_FMT("conn=%llu db=%s", conn->thread_id, db);
    1675             : 
    1676         238 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1677         238 :                 ret = conn->m->simple_command(conn, COM_INIT_DB, (zend_uchar*) db, db_len, PROT_OK_PACKET, FALSE, TRUE TSRMLS_CC);
    1678             :                 /*
    1679             :                   The server sends 0 but libmysql doesn't read it and has established
    1680             :                   a protocol of giving back -1. Thus we have to follow it :(
    1681             :                 */
    1682         238 :                 SET_ERROR_AFF_ROWS(conn);
    1683         238 :                 if (ret == PASS) {
    1684         228 :                         if (conn->connect_or_select_db) {
    1685         228 :                                 mnd_pefree(conn->connect_or_select_db, conn->persistent);
    1686             :                         }
    1687         228 :                         conn->connect_or_select_db = mnd_pestrndup(db, db_len, conn->persistent);
    1688         228 :                         conn->connect_or_select_db_len = db_len;
    1689         228 :                         if (!conn->connect_or_select_db) {
    1690             :                                 /* OOM */
    1691           0 :                                 SET_OOM_ERROR(*conn->error_info);
    1692           0 :                                 ret = FAIL;
    1693             :                         }
    1694             :                 }
    1695         238 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1696             :         }
    1697         238 :         DBG_RETURN(ret);
    1698             : }
    1699             : /* }}} */
    1700             : 
    1701             : 
    1702             : /* {{{ mysqlnd_conn_data::ping */
    1703             : static enum_func_status
    1704          47 : MYSQLND_METHOD(mysqlnd_conn_data, ping)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1705             : {
    1706          47 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, ping);
    1707          47 :         enum_func_status ret = FAIL;
    1708             : 
    1709          47 :         DBG_ENTER("mysqlnd_conn_data::ping");
    1710          47 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    1711             : 
    1712          47 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1713          47 :                 ret = conn->m->simple_command(conn, COM_PING, NULL, 0, PROT_OK_PACKET, TRUE, TRUE TSRMLS_CC);
    1714             :                 /*
    1715             :                   The server sends 0 but libmysql doesn't read it and has established
    1716             :                   a protocol of giving back -1. Thus we have to follow it :(
    1717             :                 */
    1718          47 :                 SET_ERROR_AFF_ROWS(conn);
    1719             : 
    1720          47 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1721             :         }
    1722          47 :         DBG_INF_FMT("ret=%u", ret);
    1723          47 :         DBG_RETURN(ret);
    1724             : }
    1725             : /* }}} */
    1726             : 
    1727             : 
    1728             : /* {{{ mysqlnd_conn_data::statistic */
    1729             : static enum_func_status
    1730          19 : MYSQLND_METHOD(mysqlnd_conn_data, statistic)(MYSQLND_CONN_DATA * conn, zend_string **message TSRMLS_DC)
    1731             : {
    1732          19 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, get_server_statistics);
    1733          19 :         enum_func_status ret = FAIL;
    1734             :         MYSQLND_PACKET_STATS * stats_header;
    1735             : 
    1736          19 :         DBG_ENTER("mysqlnd_conn_data::statistic");
    1737          19 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    1738             : 
    1739          19 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1740             :                 do {
    1741          19 :                         ret = conn->m->simple_command(conn, COM_STATISTICS, NULL, 0, PROT_LAST, FALSE, TRUE TSRMLS_CC);
    1742          19 :                         if (FAIL == ret) {
    1743           3 :                                 break;
    1744             :                         }
    1745          16 :                         stats_header = conn->protocol->m.get_stats_packet(conn->protocol, FALSE TSRMLS_CC);
    1746          16 :                         if (!stats_header) {
    1747           0 :                                 SET_OOM_ERROR(*conn->error_info);
    1748           0 :                                 break;
    1749             :                         }
    1750             : 
    1751          16 :                         if (PASS == (ret = PACKET_READ(stats_header, conn))) {
    1752             :                                 /* will be freed by Zend, thus don't use the mnd_ allocator */
    1753          32 :                                 *message = zend_string_init(stats_header->message, stats_header->message_len, 0); 
    1754          16 :                                 DBG_INF((*message)->val);
    1755             :                         }
    1756          16 :                         PACKET_FREE(stats_header);
    1757             :                 } while (0);
    1758             : 
    1759          19 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1760             :         }
    1761          19 :         DBG_RETURN(ret);
    1762             : }
    1763             : /* }}} */
    1764             : 
    1765             : 
    1766             : /* {{{ mysqlnd_conn_data::kill */
    1767             : static enum_func_status
    1768          17 : MYSQLND_METHOD(mysqlnd_conn_data, kill)(MYSQLND_CONN_DATA * conn, unsigned int pid TSRMLS_DC)
    1769             : {
    1770          17 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, kill_connection);
    1771          17 :         enum_func_status ret = FAIL;
    1772             :         zend_uchar buff[4];
    1773             : 
    1774          17 :         DBG_ENTER("mysqlnd_conn_data::kill");
    1775          17 :         DBG_INF_FMT("conn=%llu pid=%u", conn->thread_id, pid);
    1776             : 
    1777          17 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1778          17 :                 int4store(buff, pid);
    1779             : 
    1780             :                 /* If we kill ourselves don't expect OK packet, PROT_LAST will skip it */
    1781          17 :                 if (pid != conn->thread_id) {
    1782           3 :                         ret = conn->m->simple_command(conn, COM_PROCESS_KILL, buff, 4, PROT_OK_PACKET, FALSE, TRUE TSRMLS_CC);
    1783             :                         /*
    1784             :                           The server sends 0 but libmysql doesn't read it and has established
    1785             :                           a protocol of giving back -1. Thus we have to follow it :(
    1786             :                         */
    1787           3 :                         SET_ERROR_AFF_ROWS(conn);
    1788          14 :                 } else if (PASS == (ret = conn->m->simple_command(conn, COM_PROCESS_KILL, buff, 4, PROT_LAST, FALSE, TRUE TSRMLS_CC))) {
    1789          11 :                         CONN_SET_STATE(conn, CONN_QUIT_SENT);
    1790          11 :                         conn->m->send_close(conn TSRMLS_CC);
    1791             :                 }
    1792             : 
    1793          17 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1794             :         }
    1795          17 :         DBG_RETURN(ret);
    1796             : }
    1797             : /* }}} */
    1798             : 
    1799             : 
    1800             : /* {{{ mysqlnd_conn_data::set_charset */
    1801             : static enum_func_status
    1802          97 : MYSQLND_METHOD(mysqlnd_conn_data, set_charset)(MYSQLND_CONN_DATA * const conn, const char * const csname TSRMLS_DC)
    1803             : {
    1804          97 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, set_charset);
    1805          97 :         enum_func_status ret = FAIL;
    1806          97 :         const MYSQLND_CHARSET * const charset = mysqlnd_find_charset_name(csname);
    1807             : 
    1808          97 :         DBG_ENTER("mysqlnd_conn_data::set_charset");
    1809          97 :         DBG_INF_FMT("conn=%llu cs=%s", conn->thread_id, csname);
    1810             : 
    1811          97 :         if (!charset) {
    1812           2 :                 SET_CLIENT_ERROR(*conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE,
    1813             :                                                  "Invalid characterset or character set not supported");
    1814           2 :                 DBG_RETURN(ret);
    1815             :         }
    1816             : 
    1817          95 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1818             :                 char * query;
    1819          95 :                 size_t query_len = mnd_sprintf(&query, 0, "SET NAMES %s", csname);
    1820             : 
    1821          95 :                 if (FAIL == (ret = conn->m->query(conn, query, query_len TSRMLS_CC))) {
    1822           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error executing query");
    1823          95 :                 } else if (conn->error_info->error_no) {
    1824           0 :                         ret = FAIL;
    1825             :                 } else {
    1826          95 :                         conn->charset = charset;
    1827             :                 }
    1828          95 :                 mnd_sprintf_free(query);
    1829             : 
    1830          95 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1831             :         }
    1832             : 
    1833          95 :         DBG_INF(ret == PASS? "PASS":"FAIL");
    1834          95 :         DBG_RETURN(ret);
    1835             : }
    1836             : /* }}} */
    1837             : 
    1838             : 
    1839             : /* {{{ mysqlnd_conn_data::refresh */
    1840             : static enum_func_status
    1841           0 : MYSQLND_METHOD(mysqlnd_conn_data, refresh)(MYSQLND_CONN_DATA * const conn, uint8_t options TSRMLS_DC)
    1842             : {
    1843           0 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, refresh_server);
    1844           0 :         enum_func_status ret = FAIL;
    1845             :         zend_uchar bits[1];
    1846           0 :         DBG_ENTER("mysqlnd_conn_data::refresh");
    1847           0 :         DBG_INF_FMT("conn=%llu options=%lu", conn->thread_id, options);
    1848             : 
    1849           0 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1850           0 :                 int1store(bits, options);
    1851             : 
    1852           0 :                 ret = conn->m->simple_command(conn, COM_REFRESH, bits, 1, PROT_OK_PACKET, FALSE, TRUE TSRMLS_CC);
    1853             : 
    1854           0 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1855             :         }
    1856           0 :         DBG_RETURN(ret);
    1857             : }
    1858             : /* }}} */
    1859             : 
    1860             : 
    1861             : /* {{{ mysqlnd_conn_data::shutdown */
    1862             : static enum_func_status
    1863           0 : MYSQLND_METHOD(mysqlnd_conn_data, shutdown)(MYSQLND_CONN_DATA * const conn, uint8_t level TSRMLS_DC)
    1864             : {
    1865           0 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, shutdown_server);
    1866           0 :         enum_func_status ret = FAIL;
    1867             :         zend_uchar bits[1];
    1868           0 :         DBG_ENTER("mysqlnd_conn_data::shutdown");
    1869           0 :         DBG_INF_FMT("conn=%llu level=%lu", conn->thread_id, level);
    1870             : 
    1871           0 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    1872           0 :                 int1store(bits, level);
    1873             : 
    1874           0 :                 ret = conn->m->simple_command(conn, COM_SHUTDOWN, bits, 1, PROT_OK_PACKET, FALSE, TRUE TSRMLS_CC);
    1875             : 
    1876           0 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    1877             :         }
    1878           0 :         DBG_RETURN(ret);        
    1879             : }
    1880             : /* }}} */
    1881             : 
    1882             : 
    1883             : /* {{{ mysqlnd_send_close */
    1884             : static enum_func_status
    1885        3821 : MYSQLND_METHOD(mysqlnd_conn_data, send_close)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1886             : {
    1887        3821 :         enum_func_status ret = PASS;
    1888        3821 :         MYSQLND_NET * net = conn->net;
    1889        3821 :         php_stream * net_stream = net->data->m.get_stream(net TSRMLS_CC);
    1890             :         enum mysqlnd_connection_state state;
    1891             : 
    1892        3821 :         DBG_ENTER("mysqlnd_send_close");
    1893        3821 :         DBG_INF_FMT("conn=%llu net->data->stream->abstract=%p", conn->thread_id, net_stream? net_stream->abstract:NULL);
    1894             : 
    1895        3821 :         if (CONN_GET_STATE(conn) >= CONN_READY) {
    1896        3748 :                 MYSQLND_DEC_CONN_STATISTIC(conn->stats, STAT_OPENED_CONNECTIONS);
    1897        3748 :                 if (conn->persistent) {
    1898        2307 :                         MYSQLND_DEC_CONN_STATISTIC(conn->stats, STAT_OPENED_PERSISTENT_CONNECTIONS);
    1899             :                 }
    1900             :         }
    1901        3821 :         state = CONN_GET_STATE(conn);
    1902        3821 :         DBG_INF_FMT("state=%u", state);
    1903        3821 :         switch (state) {
    1904             :                 case CONN_READY:
    1905        1793 :                         DBG_INF("Connection clean, sending COM_QUIT");
    1906        1793 :                         if (net_stream) {
    1907        1793 :                                 ret = conn->m->simple_command(conn, COM_QUIT, NULL, 0, PROT_LAST, TRUE, TRUE TSRMLS_CC);
    1908        1793 :                                 net->data->m.close_stream(net, conn->stats, conn->error_info TSRMLS_CC);
    1909             :                         }
    1910        1793 :                         CONN_SET_STATE(conn, CONN_QUIT_SENT);
    1911        1793 :                         break;
    1912             :                 case CONN_SENDING_LOAD_DATA:
    1913             :                         /*
    1914             :                           Don't send COM_QUIT if we are in a middle of a LOAD DATA or we
    1915             :                           will crash (assert) a debug server.
    1916             :                         */
    1917             :                 case CONN_NEXT_RESULT_PENDING:
    1918             :                 case CONN_QUERY_SENT:
    1919             :                 case CONN_FETCHING_DATA:
    1920          15 :                         MYSQLND_INC_GLOBAL_STATISTIC(STAT_CLOSE_IN_MIDDLE);
    1921          15 :                         DBG_ERR_FMT("Brutally closing connection [%p][%s]", conn, conn->scheme);
    1922             :                         /*
    1923             :                           Do nothing, the connection will be brutally closed
    1924             :                           and the server will catch it and free close from its side.
    1925             :                         */
    1926             :                         /* Fall-through */
    1927             :                 case CONN_ALLOCED:
    1928             :                         /*
    1929             :                           Allocated but not connected or there was failure when trying
    1930             :                           to connect with pre-allocated connect.
    1931             : 
    1932             :                           Fall-through
    1933             :                         */
    1934          88 :                         CONN_SET_STATE(conn, CONN_QUIT_SENT);
    1935             :                         /* Fall-through */
    1936             :                 case CONN_QUIT_SENT:
    1937             :                         /* The user has killed its own connection */
    1938        2028 :                         net->data->m.close_stream(net, conn->stats, conn->error_info TSRMLS_CC);
    1939             :                         break;
    1940             :         }
    1941             : 
    1942        3821 :         DBG_RETURN(ret);
    1943             : }
    1944             : /* }}} */
    1945             : 
    1946             : 
    1947             : /* {{{ mysqlnd_conn_data::get_reference */
    1948             : static MYSQLND_CONN_DATA *
    1949       11533 : MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1950             : {
    1951       11533 :         DBG_ENTER("mysqlnd_conn_data::get_reference");
    1952       11533 :         ++conn->refcount;
    1953       11533 :         DBG_INF_FMT("conn=%llu new_refcount=%u", conn->thread_id, conn->refcount);
    1954       11533 :         DBG_RETURN(conn);
    1955             : }
    1956             : /* }}} */
    1957             : 
    1958             : 
    1959             : /* {{{ mysqlnd_conn_data::free_reference */
    1960             : static enum_func_status
    1961       11493 : MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1962             : {
    1963       11493 :         enum_func_status ret = PASS;
    1964       11493 :         DBG_ENTER("mysqlnd_conn_data::free_reference");
    1965       11493 :         DBG_INF_FMT("conn=%llu old_refcount=%u", conn->thread_id, conn->refcount);
    1966       11493 :         if (!(--conn->refcount)) {
    1967             :                 /*
    1968             :                   No multithreading issues as we don't share the connection :)
    1969             :                   This will free the object too, of course because references has
    1970             :                   reached zero.
    1971             :                 */
    1972        1899 :                 ret = conn->m->send_close(conn TSRMLS_CC);
    1973        1899 :                 conn->m->dtor(conn TSRMLS_CC);
    1974             :         }
    1975       11493 :         DBG_RETURN(ret);
    1976             : }
    1977             : /* }}} */
    1978             : 
    1979             : 
    1980             : /* {{{ mysqlnd_conn_data::get_state */
    1981             : static enum mysqlnd_connection_state
    1982      104296 : MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_state)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    1983             : {
    1984      104296 :         DBG_ENTER("mysqlnd_conn_data::get_state");
    1985      104296 :         DBG_RETURN(conn->state);
    1986             : }
    1987             : /* }}} */
    1988             : 
    1989             : 
    1990             : /* {{{ mysqlnd_conn_data::set_state */
    1991             : static void
    1992       70125 : MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, set_state)(MYSQLND_CONN_DATA * const conn, enum mysqlnd_connection_state new_state TSRMLS_DC)
    1993             : {
    1994       70125 :         DBG_ENTER("mysqlnd_conn_data::set_state");
    1995       70125 :         DBG_INF_FMT("New state=%u", new_state);
    1996       70125 :         conn->state = new_state;
    1997       70125 :         DBG_VOID_RETURN;
    1998             : }
    1999             : /* }}} */
    2000             : 
    2001             : 
    2002             : /* {{{ mysqlnd_conn_data::field_count */
    2003             : static unsigned int
    2004       20340 : MYSQLND_METHOD(mysqlnd_conn_data, field_count)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2005             : {
    2006       20340 :         return conn->field_count;
    2007             : }
    2008             : /* }}} */
    2009             : 
    2010             : 
    2011             : /* {{{ mysqlnd_conn_data::server_status */
    2012             : static unsigned int
    2013        6595 : MYSQLND_METHOD(mysqlnd_conn_data, server_status)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2014             : {
    2015        6595 :         return conn->upsert_status->server_status;
    2016             : }
    2017             : /* }}} */
    2018             : 
    2019             : 
    2020             : /* {{{ mysqlnd_conn_data::insert_id */
    2021             : static uint64_t
    2022          58 : MYSQLND_METHOD(mysqlnd_conn_data, insert_id)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2023             : {
    2024          58 :         return conn->upsert_status->last_insert_id;
    2025             : }
    2026             : /* }}} */
    2027             : 
    2028             : 
    2029             : /* {{{ mysqlnd_conn_data::affected_rows */
    2030             : static uint64_t
    2031        1989 : MYSQLND_METHOD(mysqlnd_conn_data, affected_rows)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2032             : {
    2033        1989 :         return conn->upsert_status->affected_rows;
    2034             : }
    2035             : /* }}} */
    2036             : 
    2037             : 
    2038             : /* {{{ mysqlnd_conn_data::warning_count */
    2039             : static unsigned int
    2040          20 : MYSQLND_METHOD(mysqlnd_conn_data, warning_count)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2041             : {
    2042          20 :         return conn->upsert_status->warning_count;
    2043             : }
    2044             : /* }}} */
    2045             : 
    2046             : 
    2047             : /* {{{ mysqlnd_conn_data::info */
    2048             : static const char *
    2049          23 : MYSQLND_METHOD(mysqlnd_conn_data, info)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2050             : {
    2051          23 :         return conn->last_message;
    2052             : }
    2053             : /* }}} */
    2054             : 
    2055             : /* {{{ mysqlnd_get_client_info */
    2056        3502 : PHPAPI const char * mysqlnd_get_client_info()
    2057             : {
    2058        3502 :         return MYSQLND_VERSION;
    2059             : }
    2060             : /* }}} */
    2061             : 
    2062             : 
    2063             : /* {{{ mysqlnd_get_client_version */
    2064          18 : PHPAPI unsigned int mysqlnd_get_client_version()
    2065             : {
    2066          18 :         return MYSQLND_VERSION_ID;
    2067             : }
    2068             : /* }}} */
    2069             : 
    2070             : 
    2071             : /* {{{ mysqlnd_conn_data::get_server_info */
    2072             : static const char *
    2073          20 : MYSQLND_METHOD(mysqlnd_conn_data, get_server_info)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2074             : {
    2075          20 :         return conn->server_version;
    2076             : }
    2077             : /* }}} */
    2078             : 
    2079             : 
    2080             : /* {{{ mysqlnd_conn_data::get_host_info */
    2081             : static const char *
    2082           9 : MYSQLND_METHOD(mysqlnd_conn_data, get_host_info)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2083             : {
    2084           9 :         return conn->host_info;
    2085             : }
    2086             : /* }}} */
    2087             : 
    2088             : 
    2089             : /* {{{ mysqlnd_conn_data::get_proto_info */
    2090             : static unsigned int
    2091          12 : MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2092             : {
    2093          12 :         return conn->protocol_version;
    2094             : }
    2095             : /* }}} */
    2096             : 
    2097             : 
    2098             : /* {{{ mysqlnd_conn_data::charset_name */
    2099             : static const char *
    2100          12 : MYSQLND_METHOD(mysqlnd_conn_data, charset_name)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2101             : {
    2102          12 :         return conn->charset->name;
    2103             : }
    2104             : /* }}} */
    2105             : 
    2106             : 
    2107             : /* {{{ mysqlnd_conn_data::thread_id */
    2108             : static uint64_t
    2109         100 : MYSQLND_METHOD(mysqlnd_conn_data, thread_id)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2110             : {
    2111         100 :         return conn->thread_id;
    2112             : }
    2113             : /* }}} */
    2114             : 
    2115             : 
    2116             : /* {{{ mysqlnd_conn_data::get_server_version */
    2117             : static zend_ulong
    2118        1093 : MYSQLND_METHOD(mysqlnd_conn_data, get_server_version)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2119             : {
    2120             :         zend_long major, minor, patch;
    2121             :         char *p;
    2122             : 
    2123        1093 :         if (!(p = conn->server_version)) {
    2124           0 :                 return 0;
    2125             :         }
    2126             : 
    2127        1093 :         major = ZEND_STRTOL(p, &p, 10);
    2128        1093 :         p += 1; /* consume the dot */
    2129        1093 :         minor = ZEND_STRTOL(p, &p, 10);
    2130        1093 :         p += 1; /* consume the dot */
    2131        1093 :         patch = ZEND_STRTOL(p, &p, 10);
    2132             : 
    2133        1093 :         return (zend_ulong)(major * Z_L(10000) + (zend_ulong)(minor * Z_L(100) + patch));
    2134             : }
    2135             : /* }}} */
    2136             : 
    2137             : 
    2138             : /* {{{ mysqlnd_conn_data::more_results */
    2139             : static zend_bool
    2140        3096 : MYSQLND_METHOD(mysqlnd_conn_data, more_results)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2141             : {
    2142        3096 :         DBG_ENTER("mysqlnd_conn_data::more_results");
    2143             :         /* (conn->state == CONN_NEXT_RESULT_PENDING) too */
    2144        3096 :         DBG_RETURN(conn->upsert_status->server_status & SERVER_MORE_RESULTS_EXISTS? TRUE:FALSE);
    2145             : }
    2146             : /* }}} */
    2147             : 
    2148             : 
    2149             : /* {{{ mysqlnd_conn_data::next_result */
    2150             : static enum_func_status
    2151          72 : MYSQLND_METHOD(mysqlnd_conn_data, next_result)(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2152             : {
    2153          72 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, next_result);
    2154          72 :         enum_func_status ret = FAIL;
    2155             : 
    2156          72 :         DBG_ENTER("mysqlnd_conn_data::next_result");
    2157          72 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    2158             : 
    2159          72 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2160             :                 do {
    2161          72 :                         if (CONN_GET_STATE(conn) != CONN_NEXT_RESULT_PENDING) {
    2162          14 :                                 break;
    2163             :                         }
    2164             : 
    2165          58 :                         SET_EMPTY_ERROR(*conn->error_info);
    2166          58 :                         SET_ERROR_AFF_ROWS(conn);
    2167             :                         /*
    2168             :                           We are sure that there is a result set, since conn->state is set accordingly
    2169             :                           in mysqlnd_store_result() or mysqlnd_fetch_row_unbuffered()
    2170             :                         */
    2171          58 :                         if (FAIL == (ret = conn->m->query_read_result_set_header(conn, NULL TSRMLS_CC))) {
    2172             :                                 /*
    2173             :                                   There can be an error in the middle of a multi-statement, which will cancel the multi-statement.
    2174             :                                   So there are no more results and we should just return FALSE, error_no has been set
    2175             :                                 */
    2176           0 :                                 if (!conn->error_info->error_no) {
    2177           0 :                                         DBG_ERR_FMT("Serious error. %s::%u", __FILE__, __LINE__);
    2178           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Serious error. PID=%d", getpid());
    2179           0 :                                         CONN_SET_STATE(conn, CONN_QUIT_SENT);
    2180           0 :                                         conn->m->send_close(conn TSRMLS_CC);
    2181             :                                 } else {
    2182           0 :                                         DBG_INF_FMT("Error from the server : (%u) %s", conn->error_info->error_no, conn->error_info->error);
    2183             :                                 }
    2184           0 :                                 break;
    2185             :                         }
    2186          58 :                         if (conn->last_query_type == QUERY_UPSERT && conn->upsert_status->affected_rows) {
    2187           8 :                                 MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, conn->upsert_status->affected_rows);
    2188             :                         }
    2189             :                 } while (0);
    2190          72 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    2191             :         }
    2192             : 
    2193          72 :         DBG_RETURN(ret);
    2194             : }
    2195             : /* }}} */
    2196             : 
    2197             : 
    2198             : /* {{{ mysqlnd_field_type_name */
    2199           0 : PHPAPI const char *mysqlnd_field_type_name(enum mysqlnd_field_types field_type)
    2200             : {
    2201           0 :         switch(field_type) {
    2202             :                 case FIELD_TYPE_STRING:
    2203             :                 case FIELD_TYPE_VAR_STRING:
    2204           0 :                         return "string";
    2205             :                 case FIELD_TYPE_TINY:
    2206             :                 case FIELD_TYPE_SHORT:
    2207             :                 case FIELD_TYPE_LONG:
    2208             :                 case FIELD_TYPE_LONGLONG:
    2209             :                 case FIELD_TYPE_INT24:
    2210           0 :                         return "int";
    2211             :                 case FIELD_TYPE_FLOAT:
    2212             :                 case FIELD_TYPE_DOUBLE:
    2213             :                 case FIELD_TYPE_DECIMAL:
    2214             :                 case FIELD_TYPE_NEWDECIMAL:
    2215           0 :                         return "real";
    2216             :                 case FIELD_TYPE_TIMESTAMP:
    2217           0 :                         return "timestamp";
    2218             :                 case FIELD_TYPE_YEAR:
    2219           0 :                         return "year";
    2220             :                 case FIELD_TYPE_DATE:
    2221             :                 case FIELD_TYPE_NEWDATE:
    2222           0 :                         return "date";
    2223             :                 case FIELD_TYPE_TIME:
    2224           0 :                         return "time";
    2225             :                 case FIELD_TYPE_SET:
    2226           0 :                         return "set";
    2227             :                 case FIELD_TYPE_ENUM:
    2228           0 :                         return "enum";
    2229             :                 case FIELD_TYPE_GEOMETRY:
    2230           0 :                         return "geometry";
    2231             :                 case FIELD_TYPE_DATETIME:
    2232           0 :                         return "datetime";
    2233             :                 case FIELD_TYPE_TINY_BLOB:
    2234             :                 case FIELD_TYPE_MEDIUM_BLOB:
    2235             :                 case FIELD_TYPE_LONG_BLOB:
    2236             :                 case FIELD_TYPE_BLOB:
    2237           0 :                         return "blob";
    2238             :                 case FIELD_TYPE_NULL:
    2239           0 :                         return "null";
    2240             :                 case FIELD_TYPE_BIT:
    2241           0 :                         return "bit";
    2242             :                 default:
    2243           0 :                         return "unknown";
    2244             :         }
    2245             : }
    2246             : /* }}} */
    2247             : 
    2248             : 
    2249             : /* {{{ mysqlnd_conn_data::change_user */
    2250             : static enum_func_status
    2251          75 : MYSQLND_METHOD(mysqlnd_conn_data, change_user)(MYSQLND_CONN_DATA * const conn,
    2252             :                                                                                   const char * user,
    2253             :                                                                                   const char * passwd,
    2254             :                                                                                   const char * db,
    2255             :                                                                                   zend_bool silent,
    2256             :                                                                                   size_t passwd_len
    2257             :                                                                                   TSRMLS_DC)
    2258             : {
    2259          75 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, change_user);
    2260          75 :         enum_func_status ret = FAIL;
    2261             : 
    2262          75 :         DBG_ENTER("mysqlnd_conn_data::change_user");
    2263          75 :         DBG_INF_FMT("conn=%llu user=%s passwd=%s db=%s silent=%u",
    2264             :                                 conn->thread_id, user?user:"", passwd?"***":"null", db?db:"", (silent == TRUE)?1:0 );
    2265             : 
    2266          75 :         if (PASS != conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2267           0 :                 goto end;
    2268             :         }
    2269             : 
    2270          75 :         SET_EMPTY_ERROR(*conn->error_info);
    2271          75 :         SET_ERROR_AFF_ROWS(conn);
    2272             : 
    2273          75 :         if (!user) {
    2274           0 :                 user = "";
    2275             :         }
    2276          75 :         if (!passwd) {
    2277           0 :                 passwd = "";
    2278             :         }
    2279          75 :         if (!db) {
    2280           3 :                 db = "";
    2281             :         }
    2282             : 
    2283             :         /* XXX: passwords that have \0 inside work during auth, but in this case won't work with change user */
    2284         225 :         ret = mysqlnd_run_authentication(conn, user, passwd, passwd_len, db, strlen(db),
    2285         150 :                                                                         conn->auth_plugin_data, conn->auth_plugin_data_len, conn->options->auth_protocol,
    2286          75 :                                                                         0 /*charset not used*/, conn->options, conn->server_capabilities, silent, TRUE/*is_change*/ TSRMLS_CC);
    2287             : 
    2288             :         /*
    2289             :           Here we should close all statements. Unbuffered queries should not be a
    2290             :           problem as we won't allow sending COM_CHANGE_USER.
    2291             :         */
    2292          75 :         conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);    
    2293             : end:
    2294          75 :         DBG_INF(ret == PASS? "PASS":"FAIL");
    2295          75 :         DBG_RETURN(ret);
    2296             : }
    2297             : /* }}} */
    2298             : 
    2299             : 
    2300             : /* {{{ mysqlnd_conn_data::set_client_option */
    2301             : static enum_func_status
    2302        5470 : MYSQLND_METHOD(mysqlnd_conn_data, set_client_option)(MYSQLND_CONN_DATA * const conn,
    2303             :                                                                                                 enum mysqlnd_option option,
    2304             :                                                                                                 const char * const value
    2305             :                                                                                                 TSRMLS_DC)
    2306             : {
    2307        5470 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, set_client_option);
    2308        5470 :         enum_func_status ret = PASS;
    2309        5470 :         DBG_ENTER("mysqlnd_conn_data::set_client_option");
    2310        5470 :         DBG_INF_FMT("conn=%llu option=%u", conn->thread_id, option);
    2311             : 
    2312        5470 :         if (PASS != conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2313           0 :                 goto end;
    2314             :         }
    2315        5470 :         switch (option) {
    2316             :                 case MYSQL_OPT_COMPRESS:
    2317             : #ifdef WHEN_SUPPORTED_BY_MYSQLI
    2318             :                 case MYSQL_OPT_READ_TIMEOUT:
    2319             :                 case MYSQL_OPT_WRITE_TIMEOUT:
    2320             : #endif
    2321             :                 case MYSQLND_OPT_SSL_KEY:
    2322             :                 case MYSQLND_OPT_SSL_CERT:
    2323             :                 case MYSQLND_OPT_SSL_CA:
    2324             :                 case MYSQLND_OPT_SSL_CAPATH:
    2325             :                 case MYSQLND_OPT_SSL_CIPHER:
    2326             :                 case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
    2327             :                 case MYSQL_OPT_CONNECT_TIMEOUT:
    2328             :                 case MYSQLND_OPT_NET_CMD_BUFFER_SIZE:
    2329             :                 case MYSQLND_OPT_NET_READ_BUFFER_SIZE:
    2330             :                 case MYSQL_SERVER_PUBLIC_KEY:
    2331         240 :                         ret = conn->net->data->m.set_client_option(conn->net, option, value TSRMLS_CC);
    2332         240 :                         break;
    2333             : #ifdef MYSQLND_STRING_TO_INT_CONVERSION
    2334             :                 case MYSQLND_OPT_INT_AND_FLOAT_NATIVE:
    2335          20 :                         conn->options->int_and_float_native = *(unsigned int*) value;
    2336          20 :                         break;
    2337             : #endif
    2338             :                 case MYSQL_OPT_LOCAL_INFILE:
    2339        2748 :                         if (value && (*(unsigned int*) value) ? 1 : 0) {
    2340        1360 :                                 conn->options->flags |= CLIENT_LOCAL_FILES;
    2341             :                         } else {
    2342          28 :                                 conn->options->flags &= ~CLIENT_LOCAL_FILES;
    2343             :                         }
    2344        1388 :                         break;
    2345             :                 case MYSQL_INIT_COMMAND:
    2346             :                 {
    2347             :                         char ** new_init_commands;
    2348             :                         char * new_command;
    2349             :                         /* when num_commands is 0, then realloc will be effectively a malloc call, internally */
    2350             :                         /* Don't assign to conn->options->init_commands because in case of OOM we will lose the pointer and leak */
    2351          13 :                         new_init_commands = mnd_perealloc(conn->options->init_commands, sizeof(char *) * (conn->options->num_commands + 1), conn->persistent);
    2352          13 :                         if (!new_init_commands) {
    2353           0 :                                 goto oom;
    2354             :                         }
    2355          13 :                         conn->options->init_commands = new_init_commands;
    2356          13 :                         new_command = mnd_pestrdup(value, conn->persistent);
    2357          13 :                         if (!new_command) {
    2358           0 :                                 goto oom;
    2359             :                         }
    2360          13 :                         conn->options->init_commands[conn->options->num_commands] = new_command;
    2361          13 :                         ++conn->options->num_commands;
    2362          13 :                         break;
    2363             :                 }
    2364             :                 case MYSQL_READ_DEFAULT_FILE:
    2365             :                 case MYSQL_READ_DEFAULT_GROUP:
    2366             : #ifdef WHEN_SUPPORTED_BY_MYSQLI
    2367             :                 case MYSQL_SET_CLIENT_IP:
    2368             :                 case MYSQL_REPORT_DATA_TRUNCATION:
    2369             : #endif
    2370             :                         /* currently not supported. Todo!! */
    2371           8 :                         break;
    2372             :                 case MYSQL_SET_CHARSET_NAME:
    2373             :                 {
    2374             :                         char * new_charset_name;
    2375          41 :                         if (!mysqlnd_find_charset_name(value)) {
    2376           0 :                                 SET_CLIENT_ERROR(*conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, "Unknown character set");
    2377           0 :                                 ret = FAIL;
    2378           0 :                                 break;
    2379             :                         }
    2380             :                                 
    2381          41 :                         new_charset_name = mnd_pestrdup(value, conn->persistent);
    2382          41 :                         if (!new_charset_name) {
    2383           0 :                                 goto oom;
    2384             :                         }
    2385          41 :                         if (conn->options->charset_name) {
    2386          36 :                                 mnd_pefree(conn->options->charset_name, conn->persistent);
    2387             :                         }
    2388          41 :                         conn->options->charset_name = new_charset_name;
    2389          41 :                         DBG_INF_FMT("charset=%s", conn->options->charset_name);
    2390          41 :                         break;
    2391             :                 }
    2392             :                 case MYSQL_OPT_NAMED_PIPE:
    2393           0 :                         conn->options->protocol = MYSQL_PROTOCOL_PIPE;
    2394           0 :                         break;
    2395             :                 case MYSQL_OPT_PROTOCOL:
    2396           0 :                         if (*(unsigned int*) value < MYSQL_PROTOCOL_LAST) {
    2397           0 :                                 conn->options->protocol = *(unsigned int*) value;
    2398             :                         }
    2399           0 :                         break;
    2400             : #ifdef WHEN_SUPPORTED_BY_MYSQLI
    2401             :                 case MYSQL_SET_CHARSET_DIR:
    2402             :                 case MYSQL_OPT_RECONNECT:
    2403             :                         /* we don't need external character sets, all character sets are
    2404             :                            compiled in. For compatibility we just ignore this setting.
    2405             :                            Same for protocol, we don't support old protocol */
    2406             :                 case MYSQL_OPT_USE_REMOTE_CONNECTION:
    2407             :                 case MYSQL_OPT_USE_EMBEDDED_CONNECTION:
    2408             :                 case MYSQL_OPT_GUESS_CONNECTION:
    2409             :                         /* todo: throw an error, we don't support embedded */
    2410             :                         break;
    2411             : #endif
    2412             :                 case MYSQLND_OPT_MAX_ALLOWED_PACKET:
    2413        1874 :                         if (*(unsigned int*) value > (1<<16)) {
    2414        1874 :                                 conn->options->max_allowed_packet = *(unsigned int*) value;
    2415             :                         }
    2416        1874 :                         break;
    2417             :                 case MYSQLND_OPT_AUTH_PROTOCOL:
    2418             :                 {
    2419        1886 :                         char * new_auth_protocol = value? mnd_pestrdup(value, conn->persistent) : NULL;
    2420        1886 :                         if (value && !new_auth_protocol) {
    2421           0 :                                 goto oom;
    2422             :                         }
    2423        1886 :                         if (conn->options->auth_protocol) {
    2424          61 :                                 mnd_pefree(conn->options->auth_protocol, conn->persistent);
    2425             :                         }
    2426        1886 :                         conn->options->auth_protocol = new_auth_protocol;
    2427        1886 :                         DBG_INF_FMT("auth_protocol=%s", conn->options->auth_protocol);
    2428        1886 :                         break;
    2429             :                 }
    2430             :                 case MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS:
    2431           0 :                         if (value && (*(unsigned int*) value) ? 1 : 0) {
    2432           0 :                                 conn->options->flags |= CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
    2433             :                         } else {
    2434           0 :                                 conn->options->flags &= ~CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
    2435             :                         }
    2436           0 :                         break;
    2437             :                 case MYSQL_OPT_CONNECT_ATTR_RESET:
    2438           0 :                         if (conn->options->connect_attr) {
    2439           0 :                                 DBG_INF_FMT("Before reset %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
    2440           0 :                                 zend_hash_clean(conn->options->connect_attr);
    2441             :                         }
    2442           0 :                         break;
    2443             :                 case MYSQL_OPT_CONNECT_ATTR_DELETE:
    2444           0 :                         if (conn->options->connect_attr && value) {
    2445           0 :                                 DBG_INF_FMT("Before delete %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
    2446           0 :                                 zend_hash_str_del(conn->options->connect_attr, value, strlen(value));
    2447           0 :                                 DBG_INF_FMT("%d left", zend_hash_num_elements(conn->options->connect_attr));
    2448             :                         }
    2449           0 :                         break;
    2450             : #ifdef WHEN_SUPPORTED_BY_MYSQLI
    2451             :                 case MYSQL_SHARED_MEMORY_BASE_NAME:
    2452             :                 case MYSQL_OPT_USE_RESULT:
    2453             :                 case MYSQL_SECURE_AUTH:
    2454             :                         /* not sure, todo ? */
    2455             : #endif
    2456             :                 default:
    2457           0 :                         ret = FAIL;
    2458             :         }
    2459        5470 :         conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);    
    2460        5470 :         DBG_RETURN(ret);
    2461             : oom:
    2462           0 :         SET_OOM_ERROR(*conn->error_info);
    2463           0 :         conn->m->local_tx_end(conn, this_func, FAIL TSRMLS_CC);   
    2464             : end:
    2465           0 :         DBG_RETURN(FAIL);
    2466             : }
    2467             : /* }}} */
    2468             : 
    2469             : 
    2470             : /* {{{ mysqlnd_conn_data::set_client_option_2d */
    2471             : static enum_func_status
    2472        1875 : MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d)(MYSQLND_CONN_DATA * const conn,
    2473             :                                                                                                                 enum mysqlnd_option option,
    2474             :                                                                                                                 const char * const key,
    2475             :                                                                                                                 const char * const value
    2476             :                                                                                                                 TSRMLS_DC)
    2477             : {
    2478        1875 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, set_client_option_2d);
    2479        1875 :         enum_func_status ret = PASS;
    2480        1875 :         DBG_ENTER("mysqlnd_conn_data::set_client_option_2d");
    2481        1875 :         DBG_INF_FMT("conn=%llu option=%u", conn->thread_id, option);
    2482             : 
    2483        1875 :         if (PASS != conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2484           0 :                 goto end;
    2485             :         }
    2486        1875 :         switch (option) {
    2487             :                 case MYSQL_OPT_CONNECT_ATTR_ADD:
    2488        1875 :                         if (!conn->options->connect_attr) {
    2489        1873 :                                 DBG_INF("Initializing connect_attr hash");
    2490        1873 :                                 conn->options->connect_attr = mnd_pemalloc(sizeof(HashTable), conn->persistent);
    2491        1873 :                                 if (!conn->options->connect_attr) {
    2492           0 :                                         goto oom;
    2493             :                                 }
    2494        1873 :                                 zend_hash_init(conn->options->connect_attr, 0, NULL, ZVAL_PTR_DTOR, conn->persistent);
    2495             :                         }
    2496        1875 :                         DBG_INF_FMT("Adding [%s][%s]", key, value);
    2497             :                         {
    2498             :                                 zval attrz;
    2499        3750 :                                 ZVAL_NEW_STR(&attrz, zend_string_init(value, strlen(value), 1));
    2500        1875 :                                 zend_hash_str_update(conn->options->connect_attr, key, strlen(key), &attrz);
    2501             :                         }
    2502        1875 :                         break;
    2503             :                 default:
    2504           0 :                         ret = FAIL;
    2505             :         }
    2506        1875 :         conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);    
    2507        1875 :         DBG_RETURN(ret);
    2508             : oom:
    2509           0 :         SET_OOM_ERROR(*conn->error_info);
    2510           0 :         conn->m->local_tx_end(conn, this_func, FAIL TSRMLS_CC);   
    2511             : end:
    2512           0 :         DBG_RETURN(FAIL);
    2513             : }
    2514             : /* }}} */
    2515             : 
    2516             : 
    2517             : /* {{{ mysqlnd_conn_data::use_result */
    2518             : static MYSQLND_RES *
    2519          77 : MYSQLND_METHOD(mysqlnd_conn_data, use_result)(MYSQLND_CONN_DATA * const conn, const unsigned int flags TSRMLS_DC)
    2520             : {
    2521          77 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, use_result);
    2522          77 :         MYSQLND_RES * result = NULL;
    2523             : 
    2524          77 :         DBG_ENTER("mysqlnd_conn_data::use_result");
    2525          77 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    2526             : 
    2527          77 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2528             :                 do {
    2529          77 :                         if (!conn->current_result) {
    2530          10 :                                 break;
    2531             :                         }
    2532             : 
    2533             :                         /* Nothing to store for UPSERT/LOAD DATA */
    2534          67 :                         if (conn->last_query_type != QUERY_SELECT || CONN_GET_STATE(conn) != CONN_FETCHING_DATA) {
    2535           0 :                                 SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
    2536           0 :                                 DBG_ERR("Command out of sync");
    2537           0 :                                 break;
    2538             :                         }
    2539             : 
    2540          67 :                         MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_UNBUFFERED_SETS);
    2541             : 
    2542          67 :                         conn->current_result->conn = conn->m->get_reference(conn TSRMLS_CC);
    2543          67 :                         result = conn->current_result->m.use_result(conn->current_result, FALSE TSRMLS_CC);
    2544             : 
    2545          67 :                         if (!result) {
    2546           0 :                                 conn->current_result->m.free_result(conn->current_result, TRUE TSRMLS_CC);
    2547             :                         }
    2548          67 :                         conn->current_result = NULL;
    2549             :                 } while (0);
    2550             : 
    2551          77 :                 conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS TSRMLS_CC);      
    2552             :         }
    2553             : 
    2554          77 :         DBG_RETURN(result);
    2555             : }
    2556             : /* }}} */
    2557             : 
    2558             : 
    2559             : /* {{{ mysqlnd_conn_data::store_result */
    2560             : static MYSQLND_RES *
    2561        2877 : MYSQLND_METHOD(mysqlnd_conn_data, store_result)(MYSQLND_CONN_DATA * const conn, const unsigned int flags TSRMLS_DC)
    2562             : {
    2563        2877 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, store_result);
    2564        2877 :         MYSQLND_RES * result = NULL;
    2565             : 
    2566        2877 :         DBG_ENTER("mysqlnd_conn_data::store_result");
    2567        2877 :         DBG_INF_FMT("conn=%llu conn=%p", conn->thread_id, conn);
    2568             : 
    2569        2877 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2570             :                 do {
    2571        2877 :                         unsigned int f = flags;
    2572        2877 :                         if (!conn->current_result) {
    2573         633 :                                 break;
    2574             :                         }
    2575             : 
    2576             :                         /* Nothing to store for UPSERT/LOAD DATA*/
    2577        2244 :                         if (conn->last_query_type != QUERY_SELECT || CONN_GET_STATE(conn) != CONN_FETCHING_DATA) {
    2578           0 :                                 SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
    2579           0 :                                 DBG_ERR("Command out of sync");
    2580           0 :                                 break;
    2581             :                         }
    2582             : 
    2583        2244 :                         MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_BUFFERED_SETS);
    2584             : 
    2585             :                         /* overwrite */
    2586        2244 :                         if ((conn->m->get_client_api_capabilities(conn TSRMLS_CC) & MYSQLND_CLIENT_KNOWS_RSET_COPY_DATA)) {
    2587        1739 :                                 if (MYSQLND_G(fetch_data_copy)) {
    2588           0 :                                         f &= ~MYSQLND_STORE_NO_COPY;
    2589           0 :                                         f |= MYSQLND_STORE_COPY;
    2590             :                                 }
    2591             :                         } else {
    2592             :                                 /* if for some reason PDO borks something */
    2593         505 :                                 if (!(f & (MYSQLND_STORE_NO_COPY | MYSQLND_STORE_COPY))) {
    2594           0 :                                         f |= MYSQLND_STORE_COPY;
    2595             :                                 }
    2596             :                         }
    2597        2244 :                         if (!(f & (MYSQLND_STORE_NO_COPY | MYSQLND_STORE_COPY))) {
    2598           0 :                                 SET_CLIENT_ERROR(*conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Unknown fetch mode");
    2599           0 :                                 DBG_ERR("Unknown fetch mode");
    2600           0 :                                 break;                          
    2601             :                         }
    2602        2244 :                         result = conn->current_result->m.store_result(conn->current_result, conn, f TSRMLS_CC);
    2603        2244 :                         if (!result) {
    2604           0 :                                 conn->current_result->m.free_result(conn->current_result, TRUE TSRMLS_CC);
    2605             :                         }
    2606        2244 :                         conn->current_result = NULL;
    2607             :                 } while (0);
    2608             : 
    2609        2877 :                 conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS TSRMLS_CC);      
    2610             :         }
    2611        2877 :         DBG_RETURN(result);
    2612             : }
    2613             : /* }}} */
    2614             : 
    2615             : 
    2616             : /* {{{ mysqlnd_conn_data::get_connection_stats */
    2617             : static void
    2618           5 : MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats)(const MYSQLND_CONN_DATA * const conn,
    2619             :                                                                                                    zval * return_value TSRMLS_DC ZEND_FILE_LINE_DC)
    2620             : {
    2621           5 :         DBG_ENTER("mysqlnd_conn_data::get_connection_stats");
    2622           5 :         mysqlnd_fill_stats_hash(conn->stats, mysqlnd_stats_values_names, return_value TSRMLS_CC ZEND_FILE_LINE_CC);
    2623           5 :         DBG_VOID_RETURN;
    2624             : }
    2625             : /* }}} */
    2626             : 
    2627             : 
    2628             : /* {{{ mysqlnd_conn_data::set_autocommit */
    2629             : static enum_func_status
    2630          35 : MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit)(MYSQLND_CONN_DATA * conn, unsigned int mode TSRMLS_DC)
    2631             : {
    2632          35 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, set_autocommit);
    2633          35 :         enum_func_status ret = FAIL;
    2634          35 :         DBG_ENTER("mysqlnd_conn_data::set_autocommit");
    2635             : 
    2636          35 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2637          35 :                 ret = conn->m->query(conn, (mode) ? "SET AUTOCOMMIT=1":"SET AUTOCOMMIT=0", sizeof("SET AUTOCOMMIT=1") - 1 TSRMLS_CC);
    2638          35 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);    
    2639             :         }
    2640             : 
    2641          35 :         DBG_RETURN(ret);
    2642             : }
    2643             : /* }}} */
    2644             : 
    2645             : 
    2646             : /* {{{ mysqlnd_conn_data::tx_commit */
    2647             : static enum_func_status
    2648           0 : MYSQLND_METHOD(mysqlnd_conn_data, tx_commit)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
    2649             : {
    2650           0 :         return conn->m->tx_commit_or_rollback(conn, TRUE, TRANS_COR_NO_OPT, NULL TSRMLS_CC);
    2651             : }
    2652             : /* }}} */
    2653             : 
    2654             : 
    2655             : /* {{{ mysqlnd_conn_data::tx_rollback */
    2656             : static enum_func_status
    2657           0 : MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
    2658             : {
    2659           0 :         return conn->m->tx_commit_or_rollback(conn, FALSE, TRANS_COR_NO_OPT, NULL TSRMLS_CC);
    2660             : }
    2661             : /* }}} */
    2662             : 
    2663             : 
    2664             : /* {{{ mysqlnd_tx_cor_options_to_string */
    2665             : static void
    2666          41 : MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string)(const MYSQLND_CONN_DATA * const conn, smart_str * str, const unsigned int mode TSRMLS_DC)
    2667             : {
    2668          42 :         if (mode & TRANS_COR_AND_CHAIN && !(mode & TRANS_COR_AND_NO_CHAIN)) {
    2669           1 :                 if (str->s && str->s->len) {
    2670             :                         smart_str_appendl(str, " ", sizeof(" ") - 1);
    2671             :                 }
    2672             :                 smart_str_appendl(str, "AND CHAIN", sizeof("AND CHAIN") - 1);
    2673          40 :         } else if (mode & TRANS_COR_AND_NO_CHAIN && !(mode & TRANS_COR_AND_CHAIN)) {
    2674           0 :                 if (str->s && str->s->len) {
    2675             :                         smart_str_appendl(str, " ", sizeof(" ") - 1);
    2676             :                 }
    2677             :                 smart_str_appendl(str, "AND NO CHAIN", sizeof("AND NO CHAIN") - 1);
    2678             :         }
    2679             : 
    2680          41 :         if (mode & TRANS_COR_RELEASE && !(mode & TRANS_COR_NO_RELEASE)) {
    2681           0 :                 if (str->s && str->s->len) {
    2682             :                         smart_str_appendl(str, " ", sizeof(" ") - 1);
    2683             :                 }
    2684             :                 smart_str_appendl(str, "RELEASE", sizeof("RELEASE") - 1);
    2685          41 :         } else if (mode & TRANS_COR_NO_RELEASE && !(mode & TRANS_COR_RELEASE)) {
    2686           1 :                 if (str->s && str->s->len) {
    2687             :                         smart_str_appendl(str, " ", sizeof(" ") - 1);
    2688             :                 }
    2689             :                 smart_str_appendl(str, "NO RELEASE", sizeof("NO RELEASE") - 1);
    2690             :         }
    2691             :         smart_str_0(str);
    2692          41 : }
    2693             : /* }}} */
    2694             : 
    2695             : 
    2696             : /* {{{ mysqlnd_escape_string_for_tx_name_in_comment */
    2697             : static char *
    2698          43 : mysqlnd_escape_string_for_tx_name_in_comment(const char * const name TSRMLS_DC)
    2699             : {
    2700          43 :         char * ret = NULL;
    2701          43 :         DBG_ENTER("mysqlnd_escape_string_for_tx_name_in_comment");
    2702          43 :         if (name) {
    2703           6 :                 zend_bool warned = FALSE;
    2704           6 :                 const char * p_orig = name;
    2705             :                 char * p_copy;
    2706           6 :                 p_copy = ret = mnd_emalloc(strlen(name) + 1 + 2 + 2 + 1); /* space, open, close, NullS */
    2707           6 :                 *p_copy++ = ' ';
    2708           6 :                 *p_copy++ = '/';
    2709           6 :                 *p_copy++ = '*';
    2710             :                 while (1) {
    2711          78 :                         register char v = *p_orig;
    2712          78 :                         if (v == 0) {
    2713           6 :                                 break;
    2714             :                         }
    2715         126 :                         if ((v >= '0' && v <= '9') ||
    2716             :                                 (v >= 'a' && v <= 'z') ||
    2717             :                                 (v >= 'A' && v <= 'Z') ||
    2718             :                                 v == '-' ||
    2719             :                                 v == '_' ||
    2720             :                                 v == ' ' ||
    2721             :                                 v == '=')
    2722             :                         {
    2723          54 :                                 *p_copy++ = v;
    2724          18 :                         } else if (warned == FALSE) {
    2725           3 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Transaction name truncated. Must be only [0-9A-Za-z\\-_=]+");
    2726           3 :                                 warned = TRUE;
    2727             :                         }
    2728          72 :                         ++p_orig;
    2729          72 :                 }
    2730           6 :                 *p_copy++ = '*';
    2731           6 :                 *p_copy++ = '/';
    2732           6 :                 *p_copy++ = 0;
    2733             :         }
    2734          43 :         DBG_RETURN(ret);
    2735             : }
    2736             : /* }}} */
    2737             : 
    2738             : 
    2739             : /* {{{ mysqlnd_conn_data::tx_commit_ex */
    2740             : static enum_func_status
    2741          41 : MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback)(MYSQLND_CONN_DATA * conn, const zend_bool commit, const unsigned int flags, const char * const name TSRMLS_DC)
    2742             : {
    2743          41 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, tx_commit_or_rollback);
    2744          41 :         enum_func_status ret = FAIL;
    2745          41 :         DBG_ENTER("mysqlnd_conn_data::tx_commit_or_rollback");
    2746             : 
    2747          41 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2748             :                 do {
    2749          41 :                         smart_str tmp_str = {0, 0};
    2750          41 :                         conn->m->tx_cor_options_to_string(conn, &tmp_str, flags TSRMLS_CC);
    2751             :                         smart_str_0(&tmp_str);
    2752             : 
    2753             : 
    2754             :                         {
    2755             :                                 char * query;
    2756             :                                 size_t query_len;
    2757          41 :                                 char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name TSRMLS_CC);
    2758             :                                 
    2759          41 :                                 query_len = mnd_sprintf(&query, 0, (commit? "COMMIT%s %s":"ROLLBACK%s %s"),
    2760             :                                                                                 name_esc? name_esc:"", tmp_str.s? tmp_str.s->val:"");
    2761             :                                 smart_str_free(&tmp_str);
    2762          41 :                                 if (name_esc) {
    2763           5 :                                         mnd_efree(name_esc);
    2764           5 :                                         name_esc = NULL;
    2765             :                                 }
    2766          41 :                                 if (!query) {
    2767           0 :                                         SET_OOM_ERROR(*conn->error_info);
    2768           0 :                                         break;
    2769             :                                 }
    2770             : 
    2771          41 :                                 ret = conn->m->query(conn, query, query_len TSRMLS_CC);
    2772          41 :                                 mnd_sprintf_free(query);
    2773             :                         }
    2774             :                 } while (0);
    2775          41 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);    
    2776             :         }
    2777             : 
    2778          41 :         DBG_RETURN(ret);
    2779             : }
    2780             : /* }}} */
    2781             : 
    2782             : 
    2783             : /* {{{ mysqlnd_conn_data::tx_begin */
    2784             : static enum_func_status
    2785           2 : MYSQLND_METHOD(mysqlnd_conn_data, tx_begin)(MYSQLND_CONN_DATA * conn, const unsigned int mode, const char * const name TSRMLS_DC)
    2786             : {
    2787           2 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, tx_begin);
    2788           2 :         enum_func_status ret = FAIL;
    2789           2 :         DBG_ENTER("mysqlnd_conn_data::tx_begin");
    2790             : 
    2791           2 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2792             :                 do {
    2793           2 :                         smart_str tmp_str = {0, 0};
    2794           2 :                         if (mode & TRANS_START_WITH_CONSISTENT_SNAPSHOT) {
    2795           1 :                                 if (tmp_str.s) {
    2796             :                                         smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
    2797             :                                 }
    2798             :                                 smart_str_appendl(&tmp_str, "WITH CONSISTENT SNAPSHOT", sizeof("WITH CONSISTENT SNAPSHOT") - 1);
    2799             :                         }
    2800           2 :                         if (mode & (TRANS_START_READ_WRITE | TRANS_START_READ_ONLY)) {
    2801           0 :                                 zend_ulong server_version = conn->m->get_server_version(conn TSRMLS_CC);
    2802           0 :                                 if (server_version < 50605L) {
    2803           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "This server version doesn't support 'READ WRITE' and 'READ ONLY'. Minimum 5.6.5 is required");
    2804             :                                         smart_str_free(&tmp_str);
    2805           0 :                                         break;
    2806           0 :                                 } else if (mode & TRANS_START_READ_WRITE) {
    2807           0 :                                         if (tmp_str.s && tmp_str.s->len) {
    2808             :                                                 smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
    2809             :                                         }
    2810             :                                         smart_str_appendl(&tmp_str, "READ WRITE", sizeof("READ WRITE") - 1);
    2811           0 :                                 } else if (mode & TRANS_START_READ_ONLY) {
    2812           0 :                                         if (tmp_str.s && tmp_str.s->len) {
    2813             :                                                 smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
    2814             :                                         }
    2815             :                                         smart_str_appendl(&tmp_str, "READ ONLY", sizeof("READ ONLY") - 1);
    2816             :                                 }
    2817             :                         }
    2818             :                         smart_str_0(&tmp_str);
    2819             : 
    2820             :                         {
    2821           2 :                                 char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name TSRMLS_CC);
    2822             :                                 char * query;
    2823           2 :                                 unsigned int query_len = mnd_sprintf(&query, 0, "START TRANSACTION%s %s", name_esc? name_esc:"", tmp_str.s? tmp_str.s->val:"");
    2824             :                                 smart_str_free(&tmp_str);
    2825           2 :                                 if (name_esc) {
    2826           1 :                                         mnd_efree(name_esc);
    2827           1 :                                         name_esc = NULL;
    2828             :                                 }
    2829           2 :                                 if (!query) {
    2830           0 :                                         SET_OOM_ERROR(*conn->error_info);
    2831           0 :                                         break;
    2832             :                                 }
    2833           2 :                                 ret = conn->m->query(conn, query, query_len TSRMLS_CC);
    2834           2 :                                 mnd_sprintf_free(query);
    2835             :                         }
    2836             :                 } while (0);
    2837           2 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);    
    2838             :         }
    2839             : 
    2840           2 :         DBG_RETURN(ret);
    2841             : }
    2842             : /* }}} */
    2843             : 
    2844             : 
    2845             : /* {{{ mysqlnd_conn_data::tx_savepoint */
    2846             : static enum_func_status
    2847           4 : MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint)(MYSQLND_CONN_DATA * conn, const char * const name TSRMLS_DC)
    2848             : {
    2849           4 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, tx_savepoint);
    2850           4 :         enum_func_status ret = FAIL;
    2851           4 :         DBG_ENTER("mysqlnd_conn_data::tx_savepoint");
    2852             : 
    2853           4 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2854             :                 do {
    2855             :                         char * query;
    2856             :                         unsigned int query_len;
    2857           4 :                         if (!name) {
    2858           0 :                                 SET_CLIENT_ERROR(*conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
    2859           0 :                                 break;
    2860             :                         }
    2861           4 :                         query_len = mnd_sprintf(&query, 0, "SAVEPOINT `%s`", name);
    2862           4 :                         if (!query) {
    2863           0 :                                 SET_OOM_ERROR(*conn->error_info);
    2864           0 :                                 break;
    2865             :                         }
    2866           4 :                         ret = conn->m->query(conn, query, query_len TSRMLS_CC);
    2867           4 :                         mnd_sprintf_free(query);
    2868             :                 } while (0);
    2869           4 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);    
    2870             :         }
    2871             : 
    2872           4 :         DBG_RETURN(ret);
    2873             : }
    2874             : /* }}} */
    2875             : 
    2876             : 
    2877             : /* {{{ mysqlnd_conn_data::tx_savepoint_release */
    2878             : static enum_func_status
    2879           0 : MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release)(MYSQLND_CONN_DATA * conn, const char * const name TSRMLS_DC)
    2880             : {
    2881           0 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_data_methods, tx_savepoint_release);
    2882           0 :         enum_func_status ret = FAIL;
    2883           0 :         DBG_ENTER("mysqlnd_conn_data::tx_savepoint_release");
    2884             : 
    2885           0 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    2886             :                 do {
    2887             :                         char * query;
    2888             :                         unsigned int query_len;
    2889           0 :                         if (!name) {
    2890           0 :                                 SET_CLIENT_ERROR(*conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
    2891           0 :                                 break;
    2892             :                         }
    2893           0 :                         query_len = mnd_sprintf(&query, 0, "RELEASE SAVEPOINT `%s`", name);
    2894           0 :                         if (!query) {
    2895           0 :                                 SET_OOM_ERROR(*conn->error_info);
    2896           0 :                                 break;
    2897             :                         }
    2898           0 :                         ret = conn->m->query(conn, query, query_len TSRMLS_CC);
    2899           0 :                         mnd_sprintf_free(query);
    2900             :                 } while (0);
    2901           0 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);    
    2902             :         }
    2903             : 
    2904           0 :         DBG_RETURN(ret);
    2905             : }
    2906             : /* }}} */
    2907             : 
    2908             : 
    2909             : /* {{{ mysqlnd_conn_data::negotiate_client_api_capabilities */
    2910             : static unsigned int
    2911        1941 : MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities)(MYSQLND_CONN_DATA * const conn, const unsigned int flags TSRMLS_DC)
    2912             : {
    2913        1941 :         unsigned int ret = 0;
    2914        1941 :         DBG_ENTER("mysqlnd_conn_data::negotiate_client_api_capabilities");
    2915        1941 :         if (conn) {
    2916        1941 :                 ret = conn->client_api_capabilities;
    2917        1941 :                 conn->client_api_capabilities = flags;
    2918             :         }
    2919             : 
    2920        1941 :         DBG_RETURN(ret);
    2921             : }
    2922             : /* }}} */
    2923             : 
    2924             : 
    2925             : /* {{{ mysqlnd_conn_data::get_client_api_capabilities */
    2926             : static unsigned int
    2927        2244 : MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities)(const MYSQLND_CONN_DATA * const conn TSRMLS_DC)
    2928             : {
    2929        2244 :         DBG_ENTER("mysqlnd_conn_data::get_client_api_capabilities");
    2930        2244 :         DBG_RETURN(conn? conn->client_api_capabilities : 0);
    2931             : }
    2932             : /* }}} */
    2933             : 
    2934             : 
    2935             : /* {{{ mysqlnd_conn_data::local_tx_start */
    2936             : static enum_func_status
    2937      100012 : MYSQLND_METHOD(mysqlnd_conn_data, local_tx_start)(MYSQLND_CONN_DATA * conn, size_t this_func TSRMLS_DC)
    2938             : {
    2939      100012 :         enum_func_status ret = PASS;
    2940      100012 :         DBG_ENTER("mysqlnd_conn_data::local_tx_start");
    2941      100012 :         DBG_RETURN(ret);
    2942             : }
    2943             : /* }}} */
    2944             : 
    2945             : 
    2946             : /* {{{ mysqlnd_conn_data::local_tx_end */
    2947             : static enum_func_status
    2948      100012 : MYSQLND_METHOD(mysqlnd_conn_data, local_tx_end)(MYSQLND_CONN_DATA * conn, size_t this_func, enum_func_status status TSRMLS_DC)
    2949             : {
    2950      100012 :         DBG_ENTER("mysqlnd_conn_data::local_tx_end");
    2951      100012 :         DBG_RETURN(status);
    2952             : }
    2953             : /* }}} */
    2954             : 
    2955             : 
    2956             : /* {{{ mysqlnd_conn_data::init */
    2957             : static enum_func_status
    2958        1941 : MYSQLND_METHOD(mysqlnd_conn_data, init)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
    2959             : {
    2960        1941 :         DBG_ENTER("mysqlnd_conn_data::init");
    2961        1941 :         mysqlnd_stats_init(&conn->stats, STAT_LAST);
    2962        1941 :         SET_ERROR_AFF_ROWS(conn);
    2963             : 
    2964        1941 :         conn->net = mysqlnd_net_init(conn->persistent, conn->stats, conn->error_info TSRMLS_CC);
    2965        1941 :         conn->protocol = mysqlnd_protocol_init(conn->persistent TSRMLS_CC);
    2966             : 
    2967        1941 :         DBG_RETURN(conn->stats && conn->net && conn->protocol? PASS:FAIL);
    2968             : }
    2969             : /* }}} */
    2970             : 
    2971             : 
    2972             : MYSQLND_STMT * _mysqlnd_stmt_init(MYSQLND_CONN_DATA * const conn TSRMLS_DC);
    2973             : 
    2974             : 
    2975             : MYSQLND_CLASS_METHODS_START(mysqlnd_conn_data)
    2976             :         MYSQLND_METHOD(mysqlnd_conn_data, init),
    2977             :         MYSQLND_METHOD(mysqlnd_conn_data, connect),
    2978             : 
    2979             :         MYSQLND_METHOD(mysqlnd_conn_data, escape_string),
    2980             :         MYSQLND_METHOD(mysqlnd_conn_data, set_charset),
    2981             :         MYSQLND_METHOD(mysqlnd_conn_data, query),
    2982             :         MYSQLND_METHOD(mysqlnd_conn_data, send_query),
    2983             :         MYSQLND_METHOD(mysqlnd_conn_data, reap_query),
    2984             :         MYSQLND_METHOD(mysqlnd_conn_data, use_result),
    2985             :         MYSQLND_METHOD(mysqlnd_conn_data, store_result),
    2986             :         MYSQLND_METHOD(mysqlnd_conn_data, next_result),
    2987             :         MYSQLND_METHOD(mysqlnd_conn_data, more_results),
    2988             : 
    2989             :         _mysqlnd_stmt_init,
    2990             : 
    2991             :         MYSQLND_METHOD(mysqlnd_conn_data, shutdown),
    2992             :         MYSQLND_METHOD(mysqlnd_conn_data, refresh),
    2993             : 
    2994             :         MYSQLND_METHOD(mysqlnd_conn_data, ping),
    2995             :         MYSQLND_METHOD(mysqlnd_conn_data, kill),
    2996             :         MYSQLND_METHOD(mysqlnd_conn_data, select_db),
    2997             :         MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info),
    2998             :         MYSQLND_METHOD(mysqlnd_conn_data, change_user),
    2999             : 
    3000             :         MYSQLND_METHOD(mysqlnd_conn_data, errno),
    3001             :         MYSQLND_METHOD(mysqlnd_conn_data, error),
    3002             :         MYSQLND_METHOD(mysqlnd_conn_data, sqlstate),
    3003             :         MYSQLND_METHOD(mysqlnd_conn_data, thread_id),
    3004             : 
    3005             :         MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats),
    3006             : 
    3007             :         MYSQLND_METHOD(mysqlnd_conn_data, get_server_version),
    3008             :         MYSQLND_METHOD(mysqlnd_conn_data, get_server_info),
    3009             :         MYSQLND_METHOD(mysqlnd_conn_data, statistic),
    3010             :         MYSQLND_METHOD(mysqlnd_conn_data, get_host_info),
    3011             :         MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info),
    3012             :         MYSQLND_METHOD(mysqlnd_conn_data, info),
    3013             :         MYSQLND_METHOD(mysqlnd_conn_data, charset_name),
    3014             :         MYSQLND_METHOD(mysqlnd_conn_data, list_fields),
    3015             :         MYSQLND_METHOD(mysqlnd_conn_data, list_method),
    3016             : 
    3017             :         MYSQLND_METHOD(mysqlnd_conn_data, insert_id),
    3018             :         MYSQLND_METHOD(mysqlnd_conn_data, affected_rows),
    3019             :         MYSQLND_METHOD(mysqlnd_conn_data, warning_count),
    3020             :         MYSQLND_METHOD(mysqlnd_conn_data, field_count),
    3021             : 
    3022             :         MYSQLND_METHOD(mysqlnd_conn_data, server_status),
    3023             : 
    3024             :         MYSQLND_METHOD(mysqlnd_conn_data, set_server_option),
    3025             :         MYSQLND_METHOD(mysqlnd_conn_data, set_client_option),
    3026             :         MYSQLND_METHOD(mysqlnd_conn_data, free_contents),
    3027             :         MYSQLND_METHOD(mysqlnd_conn_data, free_options),
    3028             : 
    3029             :         MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor),
    3030             : 
    3031             :         mysqlnd_query_read_result_set_header,
    3032             : 
    3033             :         MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference),
    3034             :         MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference),
    3035             :         MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_state),
    3036             :         MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, set_state),
    3037             : 
    3038             :         MYSQLND_METHOD(mysqlnd_conn_data, simple_command),
    3039             :         MYSQLND_METHOD(mysqlnd_conn_data, simple_command_handle_response),
    3040             :         MYSQLND_METHOD(mysqlnd_conn_data, restart_psession),
    3041             :         MYSQLND_METHOD(mysqlnd_conn_data, end_psession),
    3042             :         MYSQLND_METHOD(mysqlnd_conn_data, send_close),
    3043             : 
    3044             :         MYSQLND_METHOD(mysqlnd_conn_data, ssl_set),
    3045             :         mysqlnd_result_init,
    3046             :         MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit),
    3047             :         MYSQLND_METHOD(mysqlnd_conn_data, tx_commit),
    3048             :         MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback),
    3049             :         MYSQLND_METHOD(mysqlnd_conn_data, tx_begin),
    3050             :         MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback),
    3051             :         MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string),
    3052             :         MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint),
    3053             :         MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release),
    3054             : 
    3055             :         MYSQLND_METHOD(mysqlnd_conn_data, local_tx_start),
    3056             :         MYSQLND_METHOD(mysqlnd_conn_data, local_tx_end),
    3057             :         MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands),
    3058             :         MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags),
    3059             :         MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake),
    3060             :         MYSQLND_METHOD(mysqlnd_conn_data, simple_command_send_request),
    3061             :         MYSQLND_METHOD(mysqlnd_conn_data, fetch_auth_plugin_by_name),
    3062             : 
    3063             :         MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d),
    3064             : 
    3065             :         MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities),
    3066             :         MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities)
    3067             : MYSQLND_CLASS_METHODS_END;
    3068             : 
    3069             : 
    3070             : /* {{{ mysqlnd_conn::get_reference */
    3071             : static MYSQLND *
    3072           0 : MYSQLND_METHOD(mysqlnd_conn, clone_object)(MYSQLND * const conn TSRMLS_DC)
    3073             : {
    3074             :         MYSQLND * ret;
    3075           0 :         DBG_ENTER("mysqlnd_conn::get_reference");
    3076           0 :         ret = MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory).clone_connection_object(conn TSRMLS_CC);
    3077           0 :         DBG_RETURN(ret);
    3078             : }
    3079             : /* }}} */
    3080             : 
    3081             : 
    3082             : /* {{{ mysqlnd_conn_data::dtor */
    3083             : static void
    3084        1899 : MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor)(MYSQLND * conn TSRMLS_DC)
    3085             : {
    3086        1899 :         DBG_ENTER("mysqlnd_conn::dtor");
    3087        1899 :         DBG_INF_FMT("conn=%llu", conn->data->thread_id);
    3088             : 
    3089        1899 :         conn->data->m->free_reference(conn->data TSRMLS_CC);
    3090             : 
    3091        1899 :         mnd_pefree(conn, conn->persistent);
    3092             : 
    3093        1899 :         DBG_VOID_RETURN;
    3094             : }
    3095             : /* }}} */
    3096             : 
    3097             : 
    3098             : /* {{{ mysqlnd_conn_data::close */
    3099             : static enum_func_status
    3100        1899 : MYSQLND_METHOD(mysqlnd_conn, close)(MYSQLND * conn_handle, enum_connection_close_type close_type TSRMLS_DC)
    3101             : {
    3102        1899 :         size_t this_func = STRUCT_OFFSET(struct st_mysqlnd_conn_methods, close);
    3103        1899 :         MYSQLND_CONN_DATA * conn = conn_handle->data;
    3104        1899 :         enum_func_status ret = FAIL;
    3105             : 
    3106        1899 :         DBG_ENTER("mysqlnd_conn::close");
    3107        1899 :         DBG_INF_FMT("conn=%llu", conn->thread_id);
    3108             : 
    3109        1899 :         if (PASS == conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
    3110        1899 :                 if (CONN_GET_STATE(conn) >= CONN_READY) {
    3111             :                         static enum_mysqlnd_collected_stats close_type_to_stat_map[MYSQLND_CLOSE_LAST] = {
    3112             :                                 STAT_CLOSE_EXPLICIT,
    3113             :                                 STAT_CLOSE_IMPLICIT,
    3114             :                                 STAT_CLOSE_DISCONNECT
    3115             :                         };
    3116        1826 :                         MYSQLND_INC_CONN_STATISTIC(conn->stats, close_type_to_stat_map[close_type]);
    3117             :                 }
    3118             : 
    3119             :                 /*
    3120             :                   Close now, free_reference will try,
    3121             :                   if we are last, but that's not a problem.
    3122             :                 */
    3123        1899 :                 ret = conn->m->send_close(conn TSRMLS_CC);
    3124             : 
    3125             :                 /* do it after free_reference/dtor and we might crash */
    3126        1899 :                 conn->m->local_tx_end(conn, this_func, ret TSRMLS_CC);
    3127             : 
    3128        1899 :                 conn_handle->m->dtor(conn_handle TSRMLS_CC);
    3129             :         }
    3130        1899 :         DBG_RETURN(ret);
    3131             : }
    3132             : /* }}} */
    3133             : 
    3134             : 
    3135             : MYSQLND_CLASS_METHODS_START(mysqlnd_conn)
    3136             :         MYSQLND_METHOD(mysqlnd_conn, connect),
    3137             :         MYSQLND_METHOD(mysqlnd_conn, clone_object),
    3138             :         MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor),
    3139             :         MYSQLND_METHOD(mysqlnd_conn, close)
    3140             : MYSQLND_CLASS_METHODS_END;
    3141             : 
    3142             : 
    3143             : /* {{{ _mysqlnd_init */
    3144             : PHPAPI MYSQLND *
    3145        1941 : _mysqlnd_init(unsigned int flags, zend_bool persistent TSRMLS_DC)
    3146             : {
    3147             :         MYSQLND * ret;
    3148        1941 :         DBG_ENTER("mysqlnd_init");
    3149        1941 :         ret = MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory).get_connection(persistent TSRMLS_CC);
    3150        1941 :         if (ret && ret->data) {
    3151        1941 :                 ret->data->m->negotiate_client_api_capabilities(ret->data, flags TSRMLS_CC);
    3152             :         }
    3153        1941 :         DBG_RETURN(ret);
    3154             : }
    3155             : /* }}} */
    3156             : 
    3157             : /*
    3158             :  * Local variables:
    3159             :  * tab-width: 4
    3160             :  * c-basic-offset: 4
    3161             :  * End:
    3162             :  * vim600: noet sw=4 ts=4 fdm=marker
    3163             :  * vim<600: noet sw=4 ts=4
    3164             :  */

Generated by: LCOV version 1.10

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

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