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

Generated by: LCOV version 1.10

Generated at Sun, 01 Mar 2015 23:22:28 +0000 (11 hours ago)

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