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: 0 1406 0.0 %
Date: 2014-04-16 Functions: 0 90 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:47:51 +0000 (6 days ago)

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