xref: /PHP-7.4/ext/mysqlnd/mysqlnd_connection.c (revision d26069a2)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 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@php.net>                             |
16   |          Ulf Wendel <uw@php.net>                                     |
17   +----------------------------------------------------------------------+
18 */
19 
20 #include "php.h"
21 #include "mysqlnd.h"
22 #include "mysqlnd_connection.h"
23 #include "mysqlnd_vio.h"
24 #include "mysqlnd_protocol_frame_codec.h"
25 #include "mysqlnd_auth.h"
26 #include "mysqlnd_wireprotocol.h"
27 #include "mysqlnd_priv.h"
28 #include "mysqlnd_result.h"
29 #include "mysqlnd_statistics.h"
30 #include "mysqlnd_charset.h"
31 #include "mysqlnd_debug.h"
32 #include "mysqlnd_ext_plugin.h"
33 #include "zend_smart_str.h"
34 
35 
36 extern MYSQLND_CHARSET *mysqlnd_charsets;
37 
38 PHPAPI const char * const mysqlnd_server_gone = "MySQL server has gone away";
39 PHPAPI const char * const mysqlnd_out_of_sync = "Commands out of sync; you can't run this command now";
40 PHPAPI const char * const mysqlnd_out_of_memory = "Out of memory";
41 
42 PHPAPI MYSQLND_STATS * mysqlnd_global_stats = NULL;
43 
44 
45 /* {{{ mysqlnd_upsert_status::reset */
46 void
MYSQLND_METHOD(mysqlnd_upsert_status,reset)47 MYSQLND_METHOD(mysqlnd_upsert_status, reset)(MYSQLND_UPSERT_STATUS * const upsert_status)
48 {
49 	upsert_status->warning_count = 0;
50 	upsert_status->server_status = 0;
51 	upsert_status->affected_rows = 0;
52 	upsert_status->last_insert_id = 0;
53 }
54 /* }}} */
55 
56 
57 /* {{{ mysqlnd_upsert_status::set_affected_rows_to_error */
58 void
MYSQLND_METHOD(mysqlnd_upsert_status,set_affected_rows_to_error)59 MYSQLND_METHOD(mysqlnd_upsert_status, set_affected_rows_to_error)(MYSQLND_UPSERT_STATUS * upsert_status)
60 {
61 	upsert_status->affected_rows = (uint64_t) ~0;
62 }
63 /* }}} */
64 
65 
66 MYSQLND_CLASS_METHODS_START(mysqlnd_upsert_status)
67 	MYSQLND_METHOD(mysqlnd_upsert_status, reset),
68 	MYSQLND_METHOD(mysqlnd_upsert_status, set_affected_rows_to_error),
69 MYSQLND_CLASS_METHODS_END;
70 
71 
72 /* {{{ mysqlnd_upsert_status_init */
73 void
mysqlnd_upsert_status_init(MYSQLND_UPSERT_STATUS * const upsert_status)74 mysqlnd_upsert_status_init(MYSQLND_UPSERT_STATUS * const upsert_status)
75 {
76 	upsert_status->m = &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_upsert_status);
77 	upsert_status->m->reset(upsert_status);
78 }
79 /* }}} */
80 
81 
82 /* {{{ mysqlnd_error_list_pdtor */
83 static void
mysqlnd_error_list_pdtor(void * pDest)84 mysqlnd_error_list_pdtor(void * pDest)
85 {
86 	MYSQLND_ERROR_LIST_ELEMENT * element = (MYSQLND_ERROR_LIST_ELEMENT *) pDest;
87 
88 	DBG_ENTER("mysqlnd_error_list_pdtor");
89 	if (element->error) {
90 		mnd_pefree(element->error, TRUE);
91 	}
92 	DBG_VOID_RETURN;
93 }
94 /* }}} */
95 
96 
97 /* {{{ mysqlnd_error_info::reset */
98 static void
MYSQLND_METHOD(mysqlnd_error_info,reset)99 MYSQLND_METHOD(mysqlnd_error_info, reset)(MYSQLND_ERROR_INFO * const info)
100 {
101 	DBG_ENTER("mysqlnd_error_info::reset");
102 
103 	info->error_no = 0;
104 	info->error[0] = '\0';
105 	memset(&info->sqlstate, 0, sizeof(info->sqlstate));
106 	zend_llist_clean(&info->error_list);
107 
108 	DBG_VOID_RETURN;
109 }
110 /* }}} */
111 
112 
113 /* {{{ mysqlnd_error_info::set_client_error */
114 static void
MYSQLND_METHOD(mysqlnd_error_info,set_client_error)115 MYSQLND_METHOD(mysqlnd_error_info, set_client_error)(MYSQLND_ERROR_INFO * const info,
116 													 const unsigned int err_no,
117 													 const char * const sqlstate,
118 													 const char * const error)
119 {
120 	DBG_ENTER("mysqlnd_error_info::set_client_error");
121 	if (err_no) {
122 		MYSQLND_ERROR_LIST_ELEMENT error_for_the_list = {0};
123 
124 		info->error_no = err_no;
125 		strlcpy(info->sqlstate, sqlstate, sizeof(info->sqlstate));
126 		strlcpy(info->error, error, sizeof(info->error));
127 
128 		error_for_the_list.error_no = err_no;
129 		strlcpy(error_for_the_list.sqlstate, sqlstate, sizeof(error_for_the_list.sqlstate));
130 		error_for_the_list.error = mnd_pestrdup(error, TRUE);
131 		if (error_for_the_list.error) {
132 			DBG_INF_FMT("adding error [%s] to the list", error_for_the_list.error);
133 			zend_llist_add_element(&info->error_list, &error_for_the_list);
134 		}
135 	} else {
136 		info->m->reset(info);
137 	}
138 	DBG_VOID_RETURN;
139 }
140 /* }}} */
141 
142 
143 MYSQLND_CLASS_METHODS_START(mysqlnd_error_info)
144 	MYSQLND_METHOD(mysqlnd_error_info, reset),
145 	MYSQLND_METHOD(mysqlnd_error_info, set_client_error),
146 MYSQLND_CLASS_METHODS_END;
147 
148 
149 
150 /* {{{ mysqlnd_error_info_init */
151 PHPAPI enum_func_status
mysqlnd_error_info_init(MYSQLND_ERROR_INFO * const info,const zend_bool persistent)152 mysqlnd_error_info_init(MYSQLND_ERROR_INFO * const info, const zend_bool persistent)
153 {
154 	DBG_ENTER("mysqlnd_error_info_init");
155 	info->m = mysqlnd_error_info_get_methods();
156 	info->m->reset(info);
157 
158 	zend_llist_init(&info->error_list, sizeof(MYSQLND_ERROR_LIST_ELEMENT), (llist_dtor_func_t) mysqlnd_error_list_pdtor, persistent);
159 	info->persistent = persistent;
160 	DBG_RETURN(PASS);
161 }
162 /* }}} */
163 
164 
165 /* {{{ mysqlnd_error_info_free_contents */
166 PHPAPI void
mysqlnd_error_info_free_contents(MYSQLND_ERROR_INFO * const info)167 mysqlnd_error_info_free_contents(MYSQLND_ERROR_INFO * const info)
168 {
169 	DBG_ENTER("mysqlnd_error_info_free_contents");
170 	info->m->reset(info);
171 	DBG_VOID_RETURN;
172 }
173 /* }}} */
174 
175 
176 
177 
178 /* {{{ mysqlnd_connection_state::get */
179 static enum mysqlnd_connection_state
MYSQLND_METHOD(mysqlnd_connection_state,get)180 MYSQLND_METHOD(mysqlnd_connection_state, get)(const struct st_mysqlnd_connection_state * const state_struct)
181 {
182 	DBG_ENTER("mysqlnd_connection_state::get");
183 	DBG_INF_FMT("State=%u", state_struct->state);
184 	DBG_RETURN(state_struct->state);
185 }
186 /* }}} */
187 
188 
189 /* {{{ mysqlnd_connection_state::set */
190 static void
MYSQLND_METHOD(mysqlnd_connection_state,set)191 MYSQLND_METHOD(mysqlnd_connection_state, set)(struct st_mysqlnd_connection_state * const state_struct, const enum mysqlnd_connection_state state)
192 {
193 	DBG_ENTER("mysqlnd_connection_state::set");
194 	DBG_INF_FMT("New state=%u", state);
195 	state_struct->state = state;
196 	DBG_VOID_RETURN;
197 }
198 /* }}} */
199 
200 
201 MYSQLND_CLASS_METHODS_START(mysqlnd_connection_state)
202 	MYSQLND_METHOD(mysqlnd_connection_state, get),
203 	MYSQLND_METHOD(mysqlnd_connection_state, set),
204 MYSQLND_CLASS_METHODS_END;
205 
206 
207 /* {{{ mysqlnd_connection_state_init */
208 PHPAPI void
mysqlnd_connection_state_init(struct st_mysqlnd_connection_state * const state)209 mysqlnd_connection_state_init(struct st_mysqlnd_connection_state * const state)
210 {
211 	DBG_ENTER("mysqlnd_connection_state_init");
212 	state->m = &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_connection_state);
213 	state->state = CONN_ALLOCED;
214 	DBG_VOID_RETURN;
215 }
216 /* }}} */
217 
218 
219 
220 /* {{{ mysqlnd_conn_data::free_options */
221 static void
MYSQLND_METHOD(mysqlnd_conn_data,free_options)222 MYSQLND_METHOD(mysqlnd_conn_data, free_options)(MYSQLND_CONN_DATA * conn)
223 {
224 	zend_bool pers = conn->persistent;
225 
226 	if (conn->options->charset_name) {
227 		mnd_pefree(conn->options->charset_name, pers);
228 		conn->options->charset_name = NULL;
229 	}
230 	if (conn->options->auth_protocol) {
231 		mnd_pefree(conn->options->auth_protocol, pers);
232 		conn->options->auth_protocol = NULL;
233 	}
234 	if (conn->options->num_commands) {
235 		unsigned int i;
236 		for (i = 0; i < conn->options->num_commands; i++) {
237 			/* allocated with pestrdup */
238 			mnd_pefree(conn->options->init_commands[i], pers);
239 		}
240 		mnd_pefree(conn->options->init_commands, pers);
241 		conn->options->init_commands = NULL;
242 	}
243 	if (conn->options->cfg_file) {
244 		mnd_pefree(conn->options->cfg_file, pers);
245 		conn->options->cfg_file = NULL;
246 	}
247 	if (conn->options->cfg_section) {
248 		mnd_pefree(conn->options->cfg_section, pers);
249 		conn->options->cfg_section = NULL;
250 	}
251 	if (conn->options->connect_attr) {
252 		zend_hash_destroy(conn->options->connect_attr);
253 		mnd_pefree(conn->options->connect_attr, pers);
254 		conn->options->connect_attr = NULL;
255 	}
256 }
257 /* }}} */
258 
259 
260 /* {{{ mysqlnd_conn_data::free_contents */
261 static void
MYSQLND_METHOD(mysqlnd_conn_data,free_contents)262 MYSQLND_METHOD(mysqlnd_conn_data, free_contents)(MYSQLND_CONN_DATA * conn)
263 {
264 	zend_bool pers = conn->persistent;
265 
266 	DBG_ENTER("mysqlnd_conn_data::free_contents");
267 
268 	if (conn->current_result) {
269 		conn->current_result->m.free_result(conn->current_result, TRUE);
270 		conn->current_result = NULL;
271 	}
272 
273 	if (conn->protocol_frame_codec) {
274 		conn->protocol_frame_codec->data->m.free_contents(conn->protocol_frame_codec);
275 	}
276 
277 	if (conn->vio) {
278 		conn->vio->data->m.free_contents(conn->vio);
279 	}
280 
281 	DBG_INF("Freeing memory of members");
282 
283 	if (conn->hostname.s) {
284 		mnd_pefree(conn->hostname.s, pers);
285 		conn->hostname.s = NULL;
286 	}
287 	if (conn->username.s) {
288 		mnd_pefree(conn->username.s, pers);
289 		conn->username.s = NULL;
290 	}
291 	if (conn->password.s) {
292 		mnd_pefree(conn->password.s, pers);
293 		conn->password.s = NULL;
294 	}
295 	if (conn->connect_or_select_db.s) {
296 		mnd_pefree(conn->connect_or_select_db.s, pers);
297 		conn->connect_or_select_db.s = NULL;
298 	}
299 	if (conn->unix_socket.s) {
300 		mnd_pefree(conn->unix_socket.s, pers);
301 		conn->unix_socket.s = NULL;
302 	}
303 	DBG_INF_FMT("scheme=%s", conn->scheme.s);
304 	if (conn->scheme.s) {
305 		mnd_pefree(conn->scheme.s, pers);
306 		conn->scheme.s = NULL;
307 	}
308 	if (conn->server_version) {
309 		mnd_pefree(conn->server_version, pers);
310 		conn->server_version = NULL;
311 	}
312 	if (conn->host_info) {
313 		mnd_pefree(conn->host_info, pers);
314 		conn->host_info = NULL;
315 	}
316 	if (conn->authentication_plugin_data.s) {
317 		mnd_pefree(conn->authentication_plugin_data.s, pers);
318 		conn->authentication_plugin_data.s = NULL;
319 	}
320 	if (conn->last_message.s) {
321 		mnd_efree(conn->last_message.s);
322 		conn->last_message.s = NULL;
323 	}
324 
325 	conn->charset = NULL;
326 	conn->greet_charset = NULL;
327 
328 	DBG_VOID_RETURN;
329 }
330 /* }}} */
331 
332 
333 /* {{{ mysqlnd_conn_data::dtor */
334 static void
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data,dtor)335 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor)(MYSQLND_CONN_DATA * conn)
336 {
337 	DBG_ENTER("mysqlnd_conn_data::dtor");
338 	DBG_INF_FMT("conn=%llu", conn->thread_id);
339 
340 	conn->m->free_contents(conn);
341 	conn->m->free_options(conn);
342 
343 	if (conn->error_info) {
344 		mysqlnd_error_info_free_contents(conn->error_info);
345 		conn->error_info = NULL;
346 	}
347 
348 	if (conn->protocol_frame_codec) {
349 		mysqlnd_pfc_free(conn->protocol_frame_codec, conn->stats, conn->error_info);
350 		conn->protocol_frame_codec = NULL;
351 	}
352 
353 	if (conn->vio) {
354 		mysqlnd_vio_free(conn->vio, conn->stats, conn->error_info);
355 		conn->vio = NULL;
356 	}
357 
358 	if (conn->payload_decoder_factory) {
359 		mysqlnd_protocol_payload_decoder_factory_free(conn->payload_decoder_factory);
360 		conn->payload_decoder_factory = NULL;
361 	}
362 
363 	if (conn->stats) {
364 		mysqlnd_stats_end(conn->stats, conn->persistent);
365 	}
366 
367 	mnd_pefree(conn, conn->persistent);
368 
369 	DBG_VOID_RETURN;
370 }
371 /* }}} */
372 
373 
374 /* {{{ mysqlnd_conn_data::set_server_option */
375 static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data,set_server_option)376 MYSQLND_METHOD(mysqlnd_conn_data, set_server_option)(MYSQLND_CONN_DATA * const conn, enum_mysqlnd_server_option option)
377 {
378 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), set_server_option);
379 	enum_func_status ret = FAIL;
380 	DBG_ENTER("mysqlnd_conn_data::set_server_option");
381 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
382 		ret = conn->command->set_option(conn, option);
383 		conn->m->local_tx_end(conn, this_func, ret);
384 	}
385 	DBG_RETURN(ret);
386 }
387 /* }}} */
388 
389 
390 /* {{{ mysqlnd_conn_data::restart_psession */
391 static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data,restart_psession)392 MYSQLND_METHOD(mysqlnd_conn_data, restart_psession)(MYSQLND_CONN_DATA * conn)
393 {
394 	DBG_ENTER("mysqlnd_conn_data::restart_psession");
395 	MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_REUSED);
396 	conn->current_result = NULL;
397 	conn->last_message.s = NULL;
398 	DBG_RETURN(PASS);
399 }
400 /* }}} */
401 
402 
403 /* {{{ mysqlnd_conn_data::end_psession */
404 static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data,end_psession)405 MYSQLND_METHOD(mysqlnd_conn_data, end_psession)(MYSQLND_CONN_DATA * conn)
406 {
407 	DBG_ENTER("mysqlnd_conn_data::end_psession");
408 	/* Free here what should not be seen by the next script */
409 	if (conn->current_result) {
410 		conn->current_result->m.free_result(conn->current_result, TRUE);
411 		conn->current_result = NULL;
412 	}
413 	if (conn->last_message.s) {
414 		mnd_efree(conn->last_message.s);
415 		conn->last_message.s = NULL;
416 	}
417 	conn->error_info = &conn->error_info_impl;
418 	DBG_RETURN(PASS);
419 }
420 /* }}} */
421 
422 
423 /* {{{ mysqlnd_conn_data::fetch_auth_plugin_by_name */
424 static struct st_mysqlnd_authentication_plugin *
MYSQLND_METHOD(mysqlnd_conn_data,fetch_auth_plugin_by_name)425 MYSQLND_METHOD(mysqlnd_conn_data, fetch_auth_plugin_by_name)(const char * const requested_protocol)
426 {
427 	struct st_mysqlnd_authentication_plugin * auth_plugin;
428 	char * plugin_name = NULL;
429 	DBG_ENTER("mysqlnd_conn_data::fetch_auth_plugin_by_name");
430 
431 	mnd_sprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);
432 	DBG_INF_FMT("looking for %s auth plugin", plugin_name);
433 	auth_plugin = mysqlnd_plugin_find(plugin_name);
434 	mnd_sprintf_free(plugin_name);
435 
436 	DBG_RETURN(auth_plugin);
437 }
438 /* }}} */
439 
440 
441 /* {{{ mysqlnd_conn_data::execute_init_commands */
442 static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data,execute_init_commands)443 MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands)(MYSQLND_CONN_DATA * conn)
444 {
445 	enum_func_status ret = PASS;
446 
447 	DBG_ENTER("mysqlnd_conn_data::execute_init_commands");
448 	if (conn->options->init_commands) {
449 		unsigned int current_command = 0;
450 		for (; current_command < conn->options->num_commands; ++current_command) {
451 			const char * const command = conn->options->init_commands[current_command];
452 			if (command) {
453 				MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_EXECUTED_COUNT);
454 				if (PASS != conn->m->query(conn, command, strlen(command))) {
455 					MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_FAILED_COUNT);
456 					ret = FAIL;
457 					break;
458 				}
459 				do {
460 					if (conn->last_query_type == QUERY_SELECT) {
461 						MYSQLND_RES * result = conn->m->use_result(conn, 0);
462 						if (result) {
463 							result->m.free_result(result, TRUE);
464 						}
465 					}
466 				} while (conn->m->next_result(conn) != FAIL);
467 			}
468 		}
469 	}
470 	DBG_RETURN(ret);
471 }
472 /* }}} */
473 
474 
475 /* {{{ mysqlnd_conn_data::get_updated_connect_flags */
476 static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data,get_updated_connect_flags)477 MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags)(MYSQLND_CONN_DATA * conn, unsigned int mysql_flags)
478 {
479 #ifdef MYSQLND_COMPRESSION_ENABLED
480 	MYSQLND_PFC * pfc = conn->protocol_frame_codec;
481 #endif
482 	MYSQLND_VIO * vio = conn->vio;
483 
484 	DBG_ENTER("mysqlnd_conn_data::get_updated_connect_flags");
485 	/* allow CLIENT_LOCAL_FILES capability, although extensions basing on mysqlnd
486 		shouldn't allow 'load data local infile' by default due to security issues */
487 	mysql_flags |= MYSQLND_CAPABILITIES;
488 
489 	mysql_flags |= conn->options->flags; /* use the flags from set_client_option() */
490 
491 #ifndef MYSQLND_COMPRESSION_ENABLED
492 	if (mysql_flags & CLIENT_COMPRESS) {
493 		mysql_flags &= ~CLIENT_COMPRESS;
494 	}
495 #else
496 	if (pfc && pfc->data->flags & MYSQLND_PROTOCOL_FLAG_USE_COMPRESSION) {
497 		mysql_flags |= CLIENT_COMPRESS;
498 	}
499 #endif
500 #ifndef MYSQLND_SSL_SUPPORTED
501 	if (mysql_flags & CLIENT_SSL) {
502 		mysql_flags &= ~CLIENT_SSL;
503 	}
504 #else
505 	if (vio && (vio->data->options.ssl_key ||
506 				vio->data->options.ssl_cert ||
507 				vio->data->options.ssl_ca ||
508 				vio->data->options.ssl_capath ||
509 				vio->data->options.ssl_cipher))
510 	{
511 		mysql_flags |= CLIENT_SSL;
512 	}
513 #endif
514 
515     if (conn->options->connect_attr && zend_hash_num_elements(conn->options->connect_attr)) {
516 		mysql_flags |= CLIENT_CONNECT_ATTRS;
517 	}
518 
519 	DBG_RETURN(mysql_flags);
520 }
521 /* }}} */
522 
523 
524 /* {{{ mysqlnd_conn_data::connect_handshake */
525 static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data,connect_handshake)526 MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake)(MYSQLND_CONN_DATA * conn,
527 						const MYSQLND_CSTRING * const scheme,
528 						const MYSQLND_CSTRING * const username,
529 						const MYSQLND_CSTRING * const password,
530 						const MYSQLND_CSTRING * const database,
531 						const unsigned int mysql_flags)
532 {
533 	enum_func_status ret = FAIL;
534 	DBG_ENTER("mysqlnd_conn_data::connect_handshake");
535 
536 	if (PASS == conn->vio->data->m.connect(conn->vio, *scheme, conn->persistent, conn->stats, conn->error_info) &&
537 		PASS == conn->protocol_frame_codec->data->m.reset(conn->protocol_frame_codec, conn->stats, conn->error_info))
538 	{
539 		size_t client_flags = mysql_flags;
540 
541 		ret = conn->command->handshake(conn, *username, *password, *database, client_flags);
542 	}
543 	DBG_RETURN(ret);
544 }
545 /* }}} */
546 
547 /* {{{ mysqlnd_conn_data::get_scheme */
548 static MYSQLND_STRING
MYSQLND_METHOD(mysqlnd_conn_data,get_scheme)549 MYSQLND_METHOD(mysqlnd_conn_data, get_scheme)(MYSQLND_CONN_DATA * conn, MYSQLND_CSTRING hostname, MYSQLND_CSTRING *socket_or_pipe, unsigned int port, zend_bool * unix_socket, zend_bool * named_pipe)
550 {
551 	MYSQLND_STRING transport;
552 	DBG_ENTER("mysqlnd_conn_data::get_scheme");
553 #ifndef PHP_WIN32
554 	if (hostname.l == sizeof("localhost") - 1 && !strncasecmp(hostname.s, "localhost", hostname.l)) {
555 		DBG_INF_FMT("socket=%s", socket_or_pipe->s? socket_or_pipe->s:"n/a");
556 		if (!socket_or_pipe->s) {
557 			socket_or_pipe->s = "/tmp/mysql.sock";
558 			socket_or_pipe->l = strlen(socket_or_pipe->s);
559 		}
560 		transport.l = mnd_sprintf(&transport.s, 0, "unix://%s", socket_or_pipe->s);
561 		*unix_socket = TRUE;
562 #else
563 	if (hostname.l == sizeof(".") - 1 && hostname.s[0] == '.') {
564 		/* named pipe in socket */
565 		if (!socket_or_pipe->s) {
566 			socket_or_pipe->s = "\\\\.\\pipe\\MySQL";
567 			socket_or_pipe->l = strlen(socket_or_pipe->s);
568 		}
569 		transport.l = mnd_sprintf(&transport.s, 0, "pipe://%s", socket_or_pipe->s);
570 		*named_pipe = TRUE;
571 #endif
572 	} else {
573 		if (!port) {
574 			port = 3306;
575 		}
576 		transport.l = mnd_sprintf(&transport.s, 0, "tcp://%s:%u", hostname.s, port);
577 	}
578 	DBG_INF_FMT("transport=%s", transport.s? transport.s:"OOM");
579 	DBG_RETURN(transport);
580 }
581 /* }}} */
582 
583 
584 /* {{{ mysqlnd_conn_data::connect */
585 static enum_func_status
586 MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
587 						MYSQLND_CSTRING hostname,
588 						MYSQLND_CSTRING username,
589 						MYSQLND_CSTRING password,
590 						MYSQLND_CSTRING database,
591 						unsigned int port,
592 						MYSQLND_CSTRING socket_or_pipe,
593 						unsigned int mysql_flags
594 					)
595 {
596 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), connect);
597 	zend_bool unix_socket = FALSE;
598 	zend_bool named_pipe = FALSE;
599 	zend_bool reconnect = FALSE;
600 	zend_bool saved_compression = FALSE;
601 	zend_bool local_tx_started = FALSE;
602 	MYSQLND_PFC * pfc = conn->protocol_frame_codec;
603 	MYSQLND_STRING transport = { NULL, 0 };
604 
605 	DBG_ENTER("mysqlnd_conn_data::connect");
606 	DBG_INF_FMT("conn=%p", conn);
607 
608 	if (PASS != conn->m->local_tx_start(conn, this_func)) {
609 		goto err;
610 	}
611 	local_tx_started = TRUE;
612 
613 	SET_EMPTY_ERROR(conn->error_info);
614 	UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
615 
616 	DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u persistent=%u state=%u",
617 				hostname.s?hostname.s:"", username.s?username.s:"", database.s?database.s:"", port, mysql_flags,
618 				conn? conn->persistent:0, conn? (int)GET_CONNECTION_STATE(&conn->state):-1);
619 
620 	if (GET_CONNECTION_STATE(&conn->state) > CONN_ALLOCED) {
621 		DBG_INF("Connecting on a connected handle.");
622 
623 		if (GET_CONNECTION_STATE(&conn->state) < CONN_QUIT_SENT) {
624 			MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CLOSE_IMPLICIT);
625 			reconnect = TRUE;
626 			conn->m->send_close(conn);
627 		}
628 
629 		conn->m->free_contents(conn);
630 		/* Now reconnect using the same handle */
631 		if (pfc->data->compressed) {
632 			/*
633 			  we need to save the state. As we will re-connect, pfc->compressed should be off, or
634 			  we will look for a compression header as part of the greet message, but there will
635 			  be none.
636 			*/
637 			saved_compression = TRUE;
638 			pfc->data->compressed = FALSE;
639 		}
640 		if (pfc->data->ssl) {
641 			pfc->data->ssl = FALSE;
642 		}
643 	} else {
644 		unsigned int max_allowed_size = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
645 		conn->m->set_client_option(conn, MYSQLND_OPT_MAX_ALLOWED_PACKET, (char *)&max_allowed_size);
646 	}
647 
648 	if (!hostname.s || !hostname.s[0]) {
649 		hostname.s = "localhost";
650 		hostname.l = strlen(hostname.s);
651 	}
652 	if (!username.s) {
653 		DBG_INF_FMT("no user given, using empty string");
654 		username.s = "";
655 		username.l = 0;
656 	}
657 	if (!password.s) {
658 		DBG_INF_FMT("no password given, using empty string");
659 		password.s = "";
660 		password.l = 0;
661 	}
662 	if (!database.s || !database.s[0]) {
663 		DBG_INF_FMT("no db given, using empty string");
664 		database.s = "";
665 		database.l = 0;
666 	} else {
667 		mysql_flags |= CLIENT_CONNECT_WITH_DB;
668 	}
669 
670 	transport = conn->m->get_scheme(conn, hostname, &socket_or_pipe, port, &unix_socket, &named_pipe);
671 
672 	mysql_flags = conn->m->get_updated_connect_flags(conn, mysql_flags);
673 
674 	{
675 		const MYSQLND_CSTRING scheme = { transport.s, transport.l };
676 		if (FAIL == conn->m->connect_handshake(conn, &scheme, &username, &password, &database, mysql_flags)) {
677 			goto err;
678 		}
679 	}
680 
681 	{
682 		SET_CONNECTION_STATE(&conn->state, CONN_READY);
683 
684 		if (saved_compression) {
685 			pfc->data->compressed = TRUE;
686 		}
687 		/*
688 		  If a connect on a existing handle is performed and mysql_flags is
689 		  passed which doesn't CLIENT_COMPRESS, then we need to overwrite the value
690 		  which we set based on saved_compression.
691 		*/
692 		pfc->data->compressed = mysql_flags & CLIENT_COMPRESS? TRUE:FALSE;
693 
694 
695 		conn->scheme.s = mnd_pestrndup(transport.s, transport.l, conn->persistent);
696 		conn->scheme.l = transport.l;
697 		if (transport.s) {
698 			mnd_sprintf_free(transport.s);
699 			transport.s = NULL;
700 		}
701 
702 		if (!conn->scheme.s) {
703 			goto err; /* OOM */
704 		}
705 
706 		conn->username.l		= username.l;
707 		conn->username.s		= mnd_pestrndup(username.s, conn->username.l, conn->persistent);
708 		conn->password.l		= password.l;
709 		conn->password.s		= mnd_pestrndup(password.s, conn->password.l, conn->persistent);
710 		conn->port				= port;
711 		conn->connect_or_select_db.l = database.l;
712 		conn->connect_or_select_db.s = mnd_pestrndup(database.s, conn->connect_or_select_db.l, conn->persistent);
713 
714 		if (!conn->username.s || !conn->password.s|| !conn->connect_or_select_db.s) {
715 			SET_OOM_ERROR(conn->error_info);
716 			goto err; /* OOM */
717 		}
718 
719 		if (!unix_socket && !named_pipe) {
720 			conn->hostname.s = mnd_pestrndup(hostname.s, hostname.l, conn->persistent);
721 			if (!conn->hostname.s) {
722 				SET_OOM_ERROR(conn->error_info);
723 				goto err; /* OOM */
724 			}
725 			conn->hostname.l = hostname.l;
726 			{
727 				char *p;
728 				mnd_sprintf(&p, 0, "%s via TCP/IP", conn->hostname.s);
729 				if (!p) {
730 					SET_OOM_ERROR(conn->error_info);
731 					goto err; /* OOM */
732 				}
733 				conn->host_info = mnd_pestrdup(p, conn->persistent);
734 				mnd_sprintf_free(p);
735 				if (!conn->host_info) {
736 					SET_OOM_ERROR(conn->error_info);
737 					goto err; /* OOM */
738 				}
739 			}
740 		} else {
741 			conn->unix_socket.s = mnd_pestrdup(socket_or_pipe.s, conn->persistent);
742 			if (unix_socket) {
743 				conn->host_info = mnd_pestrdup("Localhost via UNIX socket", conn->persistent);
744 			} else if (named_pipe) {
745 				char *p;
746 				mnd_sprintf(&p, 0, "%s via named pipe", conn->unix_socket.s);
747 				if (!p) {
748 					SET_OOM_ERROR(conn->error_info);
749 					goto err; /* OOM */
750 				}
751 				conn->host_info =  mnd_pestrdup(p, conn->persistent);
752 				mnd_sprintf_free(p);
753 				if (!conn->host_info) {
754 					SET_OOM_ERROR(conn->error_info);
755 					goto err; /* OOM */
756 				}
757 			} else {
758 				php_error_docref(NULL, E_WARNING, "Impossible. Should be either socket or a pipe. Report a bug!");
759 			}
760 			if (!conn->unix_socket.s || !conn->host_info) {
761 				SET_OOM_ERROR(conn->error_info);
762 				goto err; /* OOM */
763 			}
764 			conn->unix_socket.l = strlen(conn->unix_socket.s);
765 		}
766 
767 		SET_EMPTY_ERROR(conn->error_info);
768 
769 		mysqlnd_local_infile_default(conn);
770 
771 		if (FAIL == conn->m->execute_init_commands(conn)) {
772 			goto err;
773 		}
774 
775 		MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_CONNECT_SUCCESS, 1, STAT_OPENED_CONNECTIONS, 1);
776 		if (reconnect) {
777 			MYSQLND_INC_GLOBAL_STATISTIC(STAT_RECONNECT);
778 		}
779 		if (conn->persistent) {
780 			MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_PCONNECT_SUCCESS, 1, STAT_OPENED_PERSISTENT_CONNECTIONS, 1);
781 		}
782 
783 		DBG_INF_FMT("connection_id=%llu", conn->thread_id);
784 
785 		conn->m->local_tx_end(conn, this_func, PASS);
786 		DBG_RETURN(PASS);
787 	}
788 err:
789 	if (transport.s) {
790 		mnd_sprintf_free(transport.s);
791 		transport.s = NULL;
792 	}
793 
794 	DBG_ERR_FMT("[%u] %.128s (trying to connect via %s)", conn->error_info->error_no, conn->error_info->error, conn->scheme.s);
795 	if (!conn->error_info->error_no) {
796 		SET_CLIENT_ERROR(conn->error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, conn->error_info->error);
797 		php_error_docref(NULL, E_WARNING, "[%u] %.128s (trying to connect via %s)", conn->error_info->error_no, conn->error_info->error, conn->scheme.s);
798 	}
799 
800 	conn->m->free_contents(conn);
801 	MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_FAILURE);
802 	if (TRUE == local_tx_started) {
803 		conn->m->local_tx_end(conn, this_func, FAIL);
804 	}
805 
806 	DBG_RETURN(FAIL);
807 }
808 /* }}} */
809 
810 
811 /* {{{ mysqlnd_conn::connect */
812 static enum_func_status
813 MYSQLND_METHOD(mysqlnd_conn, connect)(MYSQLND * conn_handle,
814 						const MYSQLND_CSTRING hostname,
815 						const MYSQLND_CSTRING username,
816 						const MYSQLND_CSTRING password,
817 						const MYSQLND_CSTRING database,
818 						unsigned int port,
819 						const MYSQLND_CSTRING socket_or_pipe,
820 						unsigned int mysql_flags)
821 {
822 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), connect);
823 	enum_func_status ret = FAIL;
824 	MYSQLND_CONN_DATA * conn = conn_handle->data;
825 
826 	DBG_ENTER("mysqlnd_conn::connect");
827 
828 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
829 		mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_client_name", "mysqlnd");
830         if (hostname.l > 0) {
831             mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_server_host", hostname.s);
832         }
833 		ret = conn->m->connect(conn, hostname, username, password, database, port, socket_or_pipe, mysql_flags);
834 
835 		conn->m->local_tx_end(conn, this_func, FAIL);
836 	}
837 	DBG_RETURN(ret);
838 }
839 /* }}} */
840 
841 
842 /* {{{ mysqlnd_conn_data::query */
843 /*
844   If conn->error_info->error_no is not zero, then we had an error.
845   Still the result from the query is PASS
846 */
847 static enum_func_status
848 MYSQLND_METHOD(mysqlnd_conn_data, query)(MYSQLND_CONN_DATA * conn, const char * const query, const size_t query_len)
849 {
850 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), query);
851 	enum_func_status ret = FAIL;
852 	DBG_ENTER("mysqlnd_conn_data::query");
853 	DBG_INF_FMT("conn=%p conn=%llu query=%s", conn, conn->thread_id, query);
854 
855 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
856 		if (PASS == conn->m->send_query(conn, query, query_len, MYSQLND_SEND_QUERY_IMPLICIT, NULL, NULL) &&
857 			PASS == conn->m->reap_query(conn, MYSQLND_REAP_RESULT_IMPLICIT))
858 		{
859 			ret = PASS;
860 			if (conn->last_query_type == QUERY_UPSERT && UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status)) {
861 				MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status));
862 			}
863 		}
864 		conn->m->local_tx_end(conn, this_func, ret);
865 	}
866 	DBG_RETURN(ret);
867 }
868 /* }}} */
869 
870 
871 /* {{{ mysqlnd_conn_data::send_query */
872 static enum_func_status
873 MYSQLND_METHOD(mysqlnd_conn_data, send_query)(MYSQLND_CONN_DATA * conn, const char * const query, const size_t query_len,
874 											  enum_mysqlnd_send_query_type type, zval *read_cb, zval *err_cb)
875 {
876 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), send_query);
877 	enum_func_status ret = FAIL;
878 	DBG_ENTER("mysqlnd_conn_data::send_query");
879 	DBG_INF_FMT("conn=%llu query=%s", conn->thread_id, query);
880 	DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
881 
882 	if (type == MYSQLND_SEND_QUERY_IMPLICIT || PASS == conn->m->local_tx_start(conn, this_func))
883 	{
884 		const MYSQLND_CSTRING query_string = {query, query_len};
885 
886 		ret = conn->command->query(conn, query_string);
887 
888 		if (type == MYSQLND_SEND_QUERY_EXPLICIT) {
889 			conn->m->local_tx_end(conn, this_func, ret);
890 		}
891 	}
892 	DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
893 	DBG_RETURN(ret);
894 }
895 /* }}} */
896 
897 
898 /* {{{ mysqlnd_conn_data::reap_query */
899 static enum_func_status
900 MYSQLND_METHOD(mysqlnd_conn_data, reap_query)(MYSQLND_CONN_DATA * conn, enum_mysqlnd_reap_result_type type)
901 {
902 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), reap_query);
903 	enum_func_status ret = FAIL;
904 	DBG_ENTER("mysqlnd_conn_data::reap_query");
905 	DBG_INF_FMT("conn=%llu", conn->thread_id);
906 
907 	DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
908 	if (type == MYSQLND_REAP_RESULT_IMPLICIT || PASS == conn->m->local_tx_start(conn, this_func))
909 	{
910 		ret = conn->command->reap_result(conn);
911 
912 		if (type == MYSQLND_REAP_RESULT_EXPLICIT) {
913 			conn->m->local_tx_end(conn, this_func, ret);
914 		}
915 	}
916 	DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
917 	DBG_RETURN(ret);
918 }
919 /* }}} */
920 
921 
922 /* {{{ mysqlnd_conn_data::list_method */
923 MYSQLND_RES *
924 MYSQLND_METHOD(mysqlnd_conn_data, list_method)(MYSQLND_CONN_DATA * conn, const char * const query, const char * const achtung_wild, const char * const par1)
925 {
926 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), list_method);
927 	char * show_query = NULL;
928 	size_t show_query_len;
929 	MYSQLND_RES * result = NULL;
930 
931 	DBG_ENTER("mysqlnd_conn_data::list_method");
932 	DBG_INF_FMT("conn=%llu query=%s wild=%u", conn->thread_id, query, achtung_wild);
933 
934 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
935 		if (par1) {
936 			if (achtung_wild) {
937 				show_query_len = mnd_sprintf(&show_query, 0, query, par1, achtung_wild);
938 			} else {
939 				show_query_len = mnd_sprintf(&show_query, 0, query, par1);
940 			}
941 		} else {
942 			if (achtung_wild) {
943 				show_query_len = mnd_sprintf(&show_query, 0, query, achtung_wild);
944 			} else {
945 				show_query_len = strlen(show_query = (char *)query);
946 			}
947 		}
948 
949 		if (PASS == conn->m->query(conn, show_query, show_query_len)) {
950 			result = conn->m->store_result(conn, MYSQLND_STORE_NO_COPY);
951 		}
952 		if (show_query != query) {
953 			mnd_sprintf_free(show_query);
954 		}
955 		conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS);
956 	}
957 	DBG_RETURN(result);
958 }
959 /* }}} */
960 
961 
962 /* {{{ mysqlnd_conn_data::err_no */
963 static unsigned int
964 MYSQLND_METHOD(mysqlnd_conn_data, err_no)(const MYSQLND_CONN_DATA * const conn)
965 {
966 	return conn->error_info->error_no;
967 }
968 /* }}} */
969 
970 
971 /* {{{ mysqlnd_conn_data::error */
972 static const char *
973 MYSQLND_METHOD(mysqlnd_conn_data, error)(const MYSQLND_CONN_DATA * const conn)
974 {
975 	return conn->error_info->error;
976 }
977 /* }}} */
978 
979 
980 /* {{{ mysqlnd_conn_data::sqlstate */
981 static const char *
982 MYSQLND_METHOD(mysqlnd_conn_data, sqlstate)(const MYSQLND_CONN_DATA * const conn)
983 {
984 	return conn->error_info->sqlstate[0] ? conn->error_info->sqlstate:MYSQLND_SQLSTATE_NULL;
985 }
986 /* }}} */
987 
988 
989 /* {{{ mysqlnd_old_escape_string */
990 PHPAPI  zend_ulong
991 mysqlnd_old_escape_string(char * newstr, const char * escapestr, size_t escapestr_len)
992 {
993 	DBG_ENTER("mysqlnd_old_escape_string");
994 	DBG_RETURN(mysqlnd_cset_escape_slashes(mysqlnd_find_charset_name("latin1"), newstr, escapestr, escapestr_len));
995 }
996 /* }}} */
997 
998 
999 /* {{{ mysqlnd_conn_data::ssl_set */
1000 static enum_func_status
1001 MYSQLND_METHOD(mysqlnd_conn_data, ssl_set)(MYSQLND_CONN_DATA * const conn, const char * key, const char * const cert,
1002 									  const char * const ca, const char * const capath, const char * const cipher)
1003 {
1004 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), ssl_set);
1005 	enum_func_status ret = FAIL;
1006 	MYSQLND_VIO * vio = conn->vio;
1007 	DBG_ENTER("mysqlnd_conn_data::ssl_set");
1008 
1009 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
1010 		ret = (PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_KEY, key) &&
1011 			PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CERT, cert) &&
1012 			PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CA, ca) &&
1013 			PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CAPATH, capath) &&
1014 			PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CIPHER, cipher)) ? PASS : FAIL;
1015 
1016 		conn->m->local_tx_end(conn, this_func, ret);
1017 	}
1018 	DBG_RETURN(ret);
1019 }
1020 /* }}} */
1021 
1022 
1023 /* {{{ mysqlnd_conn_data::escape_string */
1024 static zend_ulong
1025 MYSQLND_METHOD(mysqlnd_conn_data, escape_string)(MYSQLND_CONN_DATA * const conn, char * newstr, const char * escapestr, size_t escapestr_len)
1026 {
1027 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), escape_string);
1028 	zend_ulong ret = FAIL;
1029 	DBG_ENTER("mysqlnd_conn_data::escape_string");
1030 	DBG_INF_FMT("conn=%llu", conn->thread_id);
1031 
1032 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
1033 		DBG_INF_FMT("server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
1034 		if (UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status) & SERVER_STATUS_NO_BACKSLASH_ESCAPES) {
1035 			ret = mysqlnd_cset_escape_quotes(conn->charset, newstr, escapestr, escapestr_len);
1036 		} else {
1037 			ret = mysqlnd_cset_escape_slashes(conn->charset, newstr, escapestr, escapestr_len);
1038 		}
1039 		conn->m->local_tx_end(conn, this_func, PASS);
1040 	}
1041 	DBG_RETURN(ret);
1042 }
1043 /* }}} */
1044 
1045 
1046 /* {{{ mysqlnd_conn_data::dump_debug_info */
1047 static enum_func_status
1048 MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info)(MYSQLND_CONN_DATA * const conn)
1049 {
1050 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), server_dump_debug_information);
1051 	enum_func_status ret = FAIL;
1052 	DBG_ENTER("mysqlnd_conn_data::dump_debug_info");
1053 	DBG_INF_FMT("conn=%llu", conn->thread_id);
1054 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
1055 		ret = conn->command->debug(conn);
1056 		conn->m->local_tx_end(conn, this_func, ret);
1057 	}
1058 
1059 	DBG_RETURN(ret);
1060 }
1061 /* }}} */
1062 
1063 
1064 /* {{{ mysqlnd_conn_data::select_db */
1065 static enum_func_status
1066 MYSQLND_METHOD(mysqlnd_conn_data, select_db)(MYSQLND_CONN_DATA * const conn, const char * const db, const size_t db_len)
1067 {
1068 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), select_db);
1069 	enum_func_status ret = FAIL;
1070 
1071 	DBG_ENTER("mysqlnd_conn_data::select_db");
1072 	DBG_INF_FMT("conn=%llu db=%s", conn->thread_id, db);
1073 
1074 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
1075 		const MYSQLND_CSTRING database = {db, db_len};
1076 
1077 		ret = conn->command->init_db(conn, database);
1078 		conn->m->local_tx_end(conn, this_func, ret);
1079 	}
1080 	DBG_RETURN(ret);
1081 }
1082 /* }}} */
1083 
1084 
1085 /* {{{ mysqlnd_conn_data::ping */
1086 static enum_func_status
1087 MYSQLND_METHOD(mysqlnd_conn_data, ping)(MYSQLND_CONN_DATA * const conn)
1088 {
1089 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), ping);
1090 	enum_func_status ret = FAIL;
1091 
1092 	DBG_ENTER("mysqlnd_conn_data::ping");
1093 	DBG_INF_FMT("conn=%llu", conn->thread_id);
1094 
1095 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
1096 		ret = conn->command->ping(conn);
1097 		conn->m->local_tx_end(conn, this_func, ret);
1098 	}
1099 	DBG_INF_FMT("ret=%u", ret);
1100 	DBG_RETURN(ret);
1101 }
1102 /* }}} */
1103 
1104 
1105 /* {{{ mysqlnd_conn_data::statistic */
1106 static enum_func_status
1107 MYSQLND_METHOD(mysqlnd_conn_data, statistic)(MYSQLND_CONN_DATA * conn, zend_string **message)
1108 {
1109 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), get_server_statistics);
1110 	enum_func_status ret = FAIL;
1111 
1112 	DBG_ENTER("mysqlnd_conn_data::statistic");
1113 	DBG_INF_FMT("conn=%llu", conn->thread_id);
1114 
1115 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
1116 		ret = conn->command->statistics(conn, message);
1117 		conn->m->local_tx_end(conn, this_func, ret);
1118 	}
1119 	DBG_RETURN(ret);
1120 }
1121 /* }}} */
1122 
1123 
1124 /* {{{ mysqlnd_conn_data::kill */
1125 static enum_func_status
1126 MYSQLND_METHOD(mysqlnd_conn_data, kill)(MYSQLND_CONN_DATA * conn, unsigned int pid)
1127 {
1128 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), kill_connection);
1129 	enum_func_status ret = FAIL;
1130 
1131 	DBG_ENTER("mysqlnd_conn_data::kill");
1132 	DBG_INF_FMT("conn=%llu pid=%u", conn->thread_id, pid);
1133 
1134 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
1135 		const unsigned int process_id = pid;
1136 		/* 'unsigned char' is promoted to 'int' when passed through '...' */
1137 		const unsigned int read_response = (pid != conn->thread_id);
1138 
1139 		ret = conn->command->process_kill(conn, process_id, read_response);
1140 		conn->m->local_tx_end(conn, this_func, ret);
1141 	}
1142 	DBG_RETURN(ret);
1143 }
1144 /* }}} */
1145 
1146 
1147 /* {{{ mysqlnd_conn_data::set_charset */
1148 static enum_func_status
1149 MYSQLND_METHOD(mysqlnd_conn_data, set_charset)(MYSQLND_CONN_DATA * const conn, const char * const csname)
1150 {
1151 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), set_charset);
1152 	enum_func_status ret = FAIL;
1153 	const MYSQLND_CHARSET * const charset = mysqlnd_find_charset_name(csname);
1154 
1155 	DBG_ENTER("mysqlnd_conn_data::set_charset");
1156 	DBG_INF_FMT("conn=%llu cs=%s", conn->thread_id, csname);
1157 
1158 	if (!charset) {
1159 		SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE,
1160 						 "Invalid characterset or character set not supported");
1161 		DBG_RETURN(ret);
1162 	}
1163 
1164 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
1165 		char * query;
1166 		size_t query_len = mnd_sprintf(&query, 0, "SET NAMES %s", csname);
1167 
1168 		if (FAIL == (ret = conn->m->query(conn, query, query_len))) {
1169 			php_error_docref(NULL, E_WARNING, "Error executing query");
1170 		} else if (conn->error_info->error_no) {
1171 			ret = FAIL;
1172 		} else {
1173 			conn->charset = charset;
1174 		}
1175 		mnd_sprintf_free(query);
1176 
1177 		conn->m->local_tx_end(conn, this_func, ret);
1178 	}
1179 
1180 	DBG_INF(ret == PASS? "PASS":"FAIL");
1181 	DBG_RETURN(ret);
1182 }
1183 /* }}} */
1184 
1185 
1186 /* {{{ mysqlnd_conn_data::refresh */
1187 static enum_func_status
1188 MYSQLND_METHOD(mysqlnd_conn_data, refresh)(MYSQLND_CONN_DATA * const conn, uint8_t options)
1189 {
1190 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), refresh_server);
1191 	enum_func_status ret = FAIL;
1192 	DBG_ENTER("mysqlnd_conn_data::refresh");
1193 	DBG_INF_FMT("conn=%llu options=%lu", conn->thread_id, options);
1194 
1195 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
1196 		ret = conn->command->refresh(conn, options);
1197 		conn->m->local_tx_end(conn, this_func, ret);
1198 	}
1199 	DBG_RETURN(ret);
1200 }
1201 /* }}} */
1202 
1203 
1204 /* {{{ mysqlnd_conn_data::shutdown */
1205 static enum_func_status
1206 MYSQLND_METHOD(mysqlnd_conn_data, shutdown)(MYSQLND_CONN_DATA * const conn, uint8_t level)
1207 {
1208 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), shutdown_server);
1209 	enum_func_status ret = FAIL;
1210 	DBG_ENTER("mysqlnd_conn_data::shutdown");
1211 	DBG_INF_FMT("conn=%llu level=%lu", conn->thread_id, level);
1212 
1213 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
1214 		ret = conn->command->shutdown(conn, level);
1215 		conn->m->local_tx_end(conn, this_func, ret);
1216 	}
1217 	DBG_RETURN(ret);
1218 }
1219 /* }}} */
1220 
1221 
1222 /* {{{ mysqlnd_send_close */
1223 static enum_func_status
1224 MYSQLND_METHOD(mysqlnd_conn_data, send_close)(MYSQLND_CONN_DATA * const conn)
1225 {
1226 	enum_func_status ret = PASS;
1227 	MYSQLND_VIO * vio = conn->vio;
1228 	php_stream * net_stream = vio->data->m.get_stream(vio);
1229 	enum mysqlnd_connection_state state = GET_CONNECTION_STATE(&conn->state);
1230 
1231 	DBG_ENTER("mysqlnd_send_close");
1232 	DBG_INF_FMT("conn=%llu vio->data->stream->abstract=%p", conn->thread_id, net_stream? net_stream->abstract:NULL);
1233 	DBG_INF_FMT("state=%u", state);
1234 
1235 	if (state >= CONN_READY) {
1236 		MYSQLND_DEC_GLOBAL_STATISTIC(STAT_OPENED_CONNECTIONS);
1237 		if (conn->persistent) {
1238 			MYSQLND_DEC_GLOBAL_STATISTIC(STAT_OPENED_PERSISTENT_CONNECTIONS);
1239 		}
1240 	}
1241 	switch (state) {
1242 		case CONN_READY:
1243 			DBG_INF("Connection clean, sending COM_QUIT");
1244 			if (net_stream) {
1245 				ret = conn->command->quit(conn);
1246 				vio->data->m.close_stream(vio, conn->stats, conn->error_info);
1247 			}
1248 			SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
1249 			break;
1250 		case CONN_SENDING_LOAD_DATA:
1251 			/*
1252 			  Don't send COM_QUIT if we are in a middle of a LOAD DATA or we
1253 			  will crash (assert) a debug server.
1254 			*/
1255 		case CONN_NEXT_RESULT_PENDING:
1256 		case CONN_QUERY_SENT:
1257 		case CONN_FETCHING_DATA:
1258 			MYSQLND_INC_GLOBAL_STATISTIC(STAT_CLOSE_IN_MIDDLE);
1259 			DBG_ERR_FMT("Brutally closing connection [%p][%s]", conn, conn->scheme.s);
1260 			/*
1261 			  Do nothing, the connection will be brutally closed
1262 			  and the server will catch it and free close from its side.
1263 			*/
1264 			/* Fall-through */
1265 		case CONN_ALLOCED:
1266 			/*
1267 			  Allocated but not connected or there was failure when trying
1268 			  to connect with pre-allocated connect.
1269 
1270 			  Fall-through
1271 			*/
1272 			SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
1273 			/* Fall-through */
1274 		case CONN_QUIT_SENT:
1275 			/* The user has killed its own connection */
1276 			vio->data->m.close_stream(vio, conn->stats, conn->error_info);
1277 			break;
1278 	}
1279 
1280 	DBG_RETURN(ret);
1281 }
1282 /* }}} */
1283 
1284 
1285 /* {{{ mysqlnd_conn_data::get_reference */
1286 static MYSQLND_CONN_DATA *
1287 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference)(MYSQLND_CONN_DATA * const conn)
1288 {
1289 	DBG_ENTER("mysqlnd_conn_data::get_reference");
1290 	++conn->refcount;
1291 	DBG_INF_FMT("conn=%llu new_refcount=%u", conn->thread_id, conn->refcount);
1292 	DBG_RETURN(conn);
1293 }
1294 /* }}} */
1295 
1296 
1297 /* {{{ mysqlnd_conn_data::free_reference */
1298 static enum_func_status
1299 MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference)(MYSQLND_CONN_DATA * const conn)
1300 {
1301 	enum_func_status ret = PASS;
1302 	DBG_ENTER("mysqlnd_conn_data::free_reference");
1303 	DBG_INF_FMT("conn=%llu old_refcount=%u", conn->thread_id, conn->refcount);
1304 	if (!(--conn->refcount)) {
1305 		/*
1306 		  No multithreading issues as we don't share the connection :)
1307 		  This will free the object too, of course because references has
1308 		  reached zero.
1309 		*/
1310 		ret = conn->m->send_close(conn);
1311 		conn->m->dtor(conn);
1312 	}
1313 	DBG_RETURN(ret);
1314 }
1315 /* }}} */
1316 
1317 
1318 /* {{{ mysqlnd_conn_data::field_count */
1319 static unsigned int
1320 MYSQLND_METHOD(mysqlnd_conn_data, field_count)(const MYSQLND_CONN_DATA * const conn)
1321 {
1322 	return conn->field_count;
1323 }
1324 /* }}} */
1325 
1326 
1327 /* {{{ mysqlnd_conn_data::server_status */
1328 static unsigned int
1329 MYSQLND_METHOD(mysqlnd_conn_data, server_status)(const MYSQLND_CONN_DATA * const conn)
1330 {
1331 	return UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status);
1332 }
1333 /* }}} */
1334 
1335 
1336 /* {{{ mysqlnd_conn_data::insert_id */
1337 static uint64_t
1338 MYSQLND_METHOD(mysqlnd_conn_data, insert_id)(const MYSQLND_CONN_DATA * const conn)
1339 {
1340 	return UPSERT_STATUS_GET_LAST_INSERT_ID(conn->upsert_status);
1341 }
1342 /* }}} */
1343 
1344 
1345 /* {{{ mysqlnd_conn_data::affected_rows */
1346 static uint64_t
1347 MYSQLND_METHOD(mysqlnd_conn_data, affected_rows)(const MYSQLND_CONN_DATA * const conn)
1348 {
1349 	return UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status);
1350 }
1351 /* }}} */
1352 
1353 
1354 /* {{{ mysqlnd_conn_data::warning_count */
1355 static unsigned int
1356 MYSQLND_METHOD(mysqlnd_conn_data, warning_count)(const MYSQLND_CONN_DATA * const conn)
1357 {
1358 	return UPSERT_STATUS_GET_WARNINGS(conn->upsert_status);
1359 }
1360 /* }}} */
1361 
1362 
1363 /* {{{ mysqlnd_conn_data::info */
1364 static const char *
1365 MYSQLND_METHOD(mysqlnd_conn_data, info)(const MYSQLND_CONN_DATA * const conn)
1366 {
1367 	return conn->last_message.s;
1368 }
1369 /* }}} */
1370 
1371 
1372 /* {{{ mysqlnd_get_client_info */
1373 PHPAPI const char * mysqlnd_get_client_info()
1374 {
1375 	return PHP_MYSQLND_VERSION;
1376 }
1377 /* }}} */
1378 
1379 
1380 /* {{{ mysqlnd_get_client_version */
1381 PHPAPI unsigned long mysqlnd_get_client_version()
1382 {
1383 	return MYSQLND_VERSION_ID;
1384 }
1385 /* }}} */
1386 
1387 
1388 /* {{{ mysqlnd_conn_data::get_server_info */
1389 static const char *
1390 MYSQLND_METHOD(mysqlnd_conn_data, get_server_info)(const MYSQLND_CONN_DATA * const conn)
1391 {
1392 	return conn->server_version;
1393 }
1394 /* }}} */
1395 
1396 
1397 /* {{{ mysqlnd_conn_data::get_host_info */
1398 static const char *
1399 MYSQLND_METHOD(mysqlnd_conn_data, get_host_info)(const MYSQLND_CONN_DATA * const conn)
1400 {
1401 	return conn->host_info;
1402 }
1403 /* }}} */
1404 
1405 
1406 /* {{{ mysqlnd_conn_data::get_proto_info */
1407 static unsigned int
1408 MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info)(const MYSQLND_CONN_DATA * const conn)
1409 {
1410 	return conn->protocol_version;
1411 }
1412 /* }}} */
1413 
1414 
1415 /* {{{ mysqlnd_conn_data::charset_name */
1416 static const char *
1417 MYSQLND_METHOD(mysqlnd_conn_data, charset_name)(const MYSQLND_CONN_DATA * const conn)
1418 {
1419 	return conn->charset->name;
1420 }
1421 /* }}} */
1422 
1423 
1424 /* {{{ mysqlnd_conn_data::thread_id */
1425 static uint64_t
1426 MYSQLND_METHOD(mysqlnd_conn_data, thread_id)(const MYSQLND_CONN_DATA * const conn)
1427 {
1428 	return conn->thread_id;
1429 }
1430 /* }}} */
1431 
1432 
1433 /* {{{ mysqlnd_conn_data::get_server_version */
1434 static zend_ulong
1435 MYSQLND_METHOD(mysqlnd_conn_data, get_server_version)(const MYSQLND_CONN_DATA * const conn)
1436 {
1437 	zend_long major, minor, patch;
1438 	char *p;
1439 
1440 	if (!(p = conn->server_version)) {
1441 		return 0;
1442 	}
1443 
1444 #define MARIA_DB_VERSION_HACK_PREFIX "5.5.5-"
1445 
1446 	if (conn->server_capabilities & CLIENT_PLUGIN_AUTH
1447 		&& !strncmp(p, MARIA_DB_VERSION_HACK_PREFIX, sizeof(MARIA_DB_VERSION_HACK_PREFIX)-1))
1448 	{
1449 		p += sizeof(MARIA_DB_VERSION_HACK_PREFIX)-1;
1450 	}
1451 
1452 	major = ZEND_STRTOL(p, &p, 10);
1453 	p += 1; /* consume the dot */
1454 	minor = ZEND_STRTOL(p, &p, 10);
1455 	p += 1; /* consume the dot */
1456 	patch = ZEND_STRTOL(p, &p, 10);
1457 
1458 	return (zend_ulong)(major * Z_L(10000) + (zend_ulong)(minor * Z_L(100) + patch));
1459 }
1460 /* }}} */
1461 
1462 
1463 /* {{{ mysqlnd_conn_data::more_results */
1464 static zend_bool
1465 MYSQLND_METHOD(mysqlnd_conn_data, more_results)(const MYSQLND_CONN_DATA * const conn)
1466 {
1467 	DBG_ENTER("mysqlnd_conn_data::more_results");
1468 	/* (conn->state == CONN_NEXT_RESULT_PENDING) too */
1469 	DBG_RETURN(UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status) & SERVER_MORE_RESULTS_EXISTS? TRUE:FALSE);
1470 }
1471 /* }}} */
1472 
1473 
1474 /* {{{ mysqlnd_conn_data::next_result */
1475 static enum_func_status
1476 MYSQLND_METHOD(mysqlnd_conn_data, next_result)(MYSQLND_CONN_DATA * const conn)
1477 {
1478 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), next_result);
1479 	enum_func_status ret = FAIL;
1480 
1481 	DBG_ENTER("mysqlnd_conn_data::next_result");
1482 	DBG_INF_FMT("conn=%llu", conn->thread_id);
1483 
1484 	SET_EMPTY_ERROR(conn->error_info);
1485 
1486 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
1487 		do {
1488 			if (GET_CONNECTION_STATE(&conn->state) != CONN_NEXT_RESULT_PENDING) {
1489 				break;
1490 			}
1491 
1492 			UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
1493 			/*
1494 			  We are sure that there is a result set, since conn->state is set accordingly
1495 			  in mysqlnd_store_result() or mysqlnd_fetch_row_unbuffered()
1496 			*/
1497 			if (FAIL == (ret = conn->m->query_read_result_set_header(conn, NULL))) {
1498 				/*
1499 				  There can be an error in the middle of a multi-statement, which will cancel the multi-statement.
1500 				  So there are no more results and we should just return FALSE, error_no has been set
1501 				*/
1502 				if (!conn->error_info->error_no) {
1503 					DBG_ERR_FMT("Serious error. %s::%u", __FILE__, __LINE__);
1504 					php_error_docref(NULL, E_WARNING, "Serious error. PID=%d", getpid());
1505 					SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
1506 					conn->m->send_close(conn);
1507 				} else {
1508 					DBG_INF_FMT("Error from the server : (%u) %s", conn->error_info->error_no, conn->error_info->error);
1509 				}
1510 				break;
1511 			}
1512 			if (conn->last_query_type == QUERY_UPSERT && UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status)) {
1513 				MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status));
1514 			}
1515 		} while (0);
1516 		conn->m->local_tx_end(conn, this_func, ret);
1517 	}
1518 
1519 	DBG_RETURN(ret);
1520 }
1521 /* }}} */
1522 
1523 
1524 /* {{{ mysqlnd_field_type_name */
1525 PHPAPI const char * mysqlnd_field_type_name(const enum mysqlnd_field_types field_type)
1526 {
1527 	switch(field_type) {
1528 		case FIELD_TYPE_JSON:
1529 			return "json";
1530 		case FIELD_TYPE_STRING:
1531 		case FIELD_TYPE_VAR_STRING:
1532 			return "string";
1533 		case FIELD_TYPE_TINY:
1534 		case FIELD_TYPE_SHORT:
1535 		case FIELD_TYPE_LONG:
1536 		case FIELD_TYPE_LONGLONG:
1537 		case FIELD_TYPE_INT24:
1538 			return "int";
1539 		case FIELD_TYPE_FLOAT:
1540 		case FIELD_TYPE_DOUBLE:
1541 		case FIELD_TYPE_DECIMAL:
1542 		case FIELD_TYPE_NEWDECIMAL:
1543 			return "real";
1544 		case FIELD_TYPE_TIMESTAMP:
1545 			return "timestamp";
1546 		case FIELD_TYPE_YEAR:
1547 			return "year";
1548 		case FIELD_TYPE_DATE:
1549 		case FIELD_TYPE_NEWDATE:
1550 			return "date";
1551 		case FIELD_TYPE_TIME:
1552 			return "time";
1553 		case FIELD_TYPE_SET:
1554 			return "set";
1555 		case FIELD_TYPE_ENUM:
1556 			return "enum";
1557 		case FIELD_TYPE_GEOMETRY:
1558 			return "geometry";
1559 		case FIELD_TYPE_DATETIME:
1560 			return "datetime";
1561 		case FIELD_TYPE_TINY_BLOB:
1562 		case FIELD_TYPE_MEDIUM_BLOB:
1563 		case FIELD_TYPE_LONG_BLOB:
1564 		case FIELD_TYPE_BLOB:
1565 			return "blob";
1566 		case FIELD_TYPE_NULL:
1567 			return "null";
1568 		case FIELD_TYPE_BIT:
1569 			return "bit";
1570 		default:
1571 			return "unknown";
1572 	}
1573 }
1574 /* }}} */
1575 
1576 
1577 /* {{{ mysqlnd_conn_data::change_user */
1578 static enum_func_status
1579 MYSQLND_METHOD(mysqlnd_conn_data, change_user)(MYSQLND_CONN_DATA * const conn,
1580 											   const char * user,
1581 											   const char * passwd,
1582 											   const char * db,
1583 											   zend_bool silent,
1584 											   size_t passwd_len
1585 			)
1586 {
1587 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), change_user);
1588 	enum_func_status ret = FAIL;
1589 
1590 	DBG_ENTER("mysqlnd_conn_data::change_user");
1591 	DBG_INF_FMT("conn=%llu user=%s passwd=%s db=%s silent=%u",
1592 				conn->thread_id, user?user:"", passwd?"***":"null", db?db:"", silent == TRUE);
1593 
1594 	if (PASS != conn->m->local_tx_start(conn, this_func)) {
1595 		goto end;
1596 	}
1597 
1598 	SET_EMPTY_ERROR(conn->error_info);
1599 	UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
1600 
1601 	if (!user) {
1602 		user = "";
1603 	}
1604 	if (!passwd) {
1605 		passwd = "";
1606 		passwd_len = 0;
1607 	}
1608 	if (!db) {
1609 		db = "";
1610 	}
1611 
1612 	/* XXX: passwords that have \0 inside work during auth, but in this case won't work with change user */
1613 	ret = mysqlnd_run_authentication(conn, user, passwd, passwd_len, db, strlen(db),
1614 									 conn->authentication_plugin_data, conn->options->auth_protocol,
1615 									0 /*charset not used*/, conn->options, conn->server_capabilities, silent, TRUE/*is_change*/);
1616 
1617 	/*
1618 	  Here we should close all statements. Unbuffered queries should not be a
1619 	  problem as we won't allow sending COM_CHANGE_USER.
1620 	*/
1621 	conn->m->local_tx_end(conn, this_func, ret);
1622 end:
1623 	DBG_INF(ret == PASS? "PASS":"FAIL");
1624 	DBG_RETURN(ret);
1625 }
1626 /* }}} */
1627 
1628 
1629 /* {{{ mysqlnd_conn_data::set_client_option */
1630 static enum_func_status
1631 MYSQLND_METHOD(mysqlnd_conn_data, set_client_option)(MYSQLND_CONN_DATA * const conn,
1632 												enum_mysqlnd_client_option option,
1633 												const char * const value
1634 												)
1635 {
1636 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), set_client_option);
1637 	enum_func_status ret = PASS;
1638 	DBG_ENTER("mysqlnd_conn_data::set_client_option");
1639 	DBG_INF_FMT("conn=%llu option=%u", conn->thread_id, option);
1640 
1641 	if (PASS != conn->m->local_tx_start(conn, this_func)) {
1642 		goto end;
1643 	}
1644 	switch (option) {
1645 		case MYSQL_OPT_READ_TIMEOUT:
1646 		case MYSQL_OPT_WRITE_TIMEOUT:
1647 		case MYSQLND_OPT_SSL_KEY:
1648 		case MYSQLND_OPT_SSL_CERT:
1649 		case MYSQLND_OPT_SSL_CA:
1650 		case MYSQLND_OPT_SSL_CAPATH:
1651 		case MYSQLND_OPT_SSL_CIPHER:
1652 		case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
1653 		case MYSQL_OPT_CONNECT_TIMEOUT:
1654 		case MYSQLND_OPT_NET_READ_BUFFER_SIZE:
1655 			ret = conn->vio->data->m.set_client_option(conn->vio, option, value);
1656 			break;
1657 		case MYSQLND_OPT_NET_CMD_BUFFER_SIZE:
1658 		case MYSQL_OPT_COMPRESS:
1659 		case MYSQL_SERVER_PUBLIC_KEY:
1660 			ret = conn->protocol_frame_codec->data->m.set_client_option(conn->protocol_frame_codec, option, value);
1661 			break;
1662 #ifdef MYSQLND_STRING_TO_INT_CONVERSION
1663 		case MYSQLND_OPT_INT_AND_FLOAT_NATIVE:
1664 			conn->options->int_and_float_native = *(unsigned int*) value;
1665 			break;
1666 #endif
1667 		case MYSQL_OPT_LOCAL_INFILE:
1668 			if (value && (*(unsigned int*) value) ? 1 : 0) {
1669 				conn->options->flags |= CLIENT_LOCAL_FILES;
1670 			} else {
1671 				conn->options->flags &= ~CLIENT_LOCAL_FILES;
1672 			}
1673 			break;
1674 		case MYSQL_INIT_COMMAND:
1675 		{
1676 			char ** new_init_commands;
1677 			char * new_command;
1678 			/* when num_commands is 0, then realloc will be effectively a malloc call, internally */
1679 			/* Don't assign to conn->options->init_commands because in case of OOM we will lose the pointer and leak */
1680 			new_init_commands = mnd_perealloc(conn->options->init_commands, sizeof(char *) * (conn->options->num_commands + 1), conn->persistent);
1681 			if (!new_init_commands) {
1682 				goto oom;
1683 			}
1684 			conn->options->init_commands = new_init_commands;
1685 			new_command = mnd_pestrdup(value, conn->persistent);
1686 			if (!new_command) {
1687 				goto oom;
1688 			}
1689 			conn->options->init_commands[conn->options->num_commands] = new_command;
1690 			++conn->options->num_commands;
1691 			break;
1692 		}
1693 		case MYSQL_READ_DEFAULT_FILE:
1694 		case MYSQL_READ_DEFAULT_GROUP:
1695 #ifdef WHEN_SUPPORTED_BY_MYSQLI
1696 		case MYSQL_SET_CLIENT_IP:
1697 		case MYSQL_REPORT_DATA_TRUNCATION:
1698 #endif
1699 			/* currently not supported. Todo!! */
1700 			break;
1701 		case MYSQL_SET_CHARSET_NAME:
1702 		{
1703 			char * new_charset_name;
1704 			if (!mysqlnd_find_charset_name(value)) {
1705 				SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, "Unknown character set");
1706 				ret = FAIL;
1707 				break;
1708 			}
1709 
1710 			new_charset_name = mnd_pestrdup(value, conn->persistent);
1711 			if (!new_charset_name) {
1712 				goto oom;
1713 			}
1714 			if (conn->options->charset_name) {
1715 				mnd_pefree(conn->options->charset_name, conn->persistent);
1716 			}
1717 			conn->options->charset_name = new_charset_name;
1718 			DBG_INF_FMT("charset=%s", conn->options->charset_name);
1719 			break;
1720 		}
1721 		case MYSQL_OPT_NAMED_PIPE:
1722 			conn->options->protocol = MYSQL_PROTOCOL_PIPE;
1723 			break;
1724 		case MYSQL_OPT_PROTOCOL:
1725 			if (*(unsigned int*) value < MYSQL_PROTOCOL_LAST) {
1726 				conn->options->protocol = *(unsigned int*) value;
1727 			}
1728 			break;
1729 #ifdef WHEN_SUPPORTED_BY_MYSQLI
1730 		case MYSQL_SET_CHARSET_DIR:
1731 		case MYSQL_OPT_RECONNECT:
1732 			/* we don't need external character sets, all character sets are
1733 			   compiled in. For compatibility we just ignore this setting.
1734 			   Same for protocol, we don't support old protocol */
1735 		case MYSQL_OPT_USE_REMOTE_CONNECTION:
1736 		case MYSQL_OPT_USE_EMBEDDED_CONNECTION:
1737 		case MYSQL_OPT_GUESS_CONNECTION:
1738 			/* todo: throw an error, we don't support embedded */
1739 			break;
1740 #endif
1741 		case MYSQLND_OPT_MAX_ALLOWED_PACKET:
1742 			if (*(unsigned int*) value > (1<<16)) {
1743 				conn->options->max_allowed_packet = *(unsigned int*) value;
1744 			}
1745 			break;
1746 		case MYSQLND_OPT_AUTH_PROTOCOL:
1747 		{
1748 			char * new_auth_protocol = value? mnd_pestrdup(value, conn->persistent) : NULL;
1749 			if (value && !new_auth_protocol) {
1750 				goto oom;
1751 			}
1752 			if (conn->options->auth_protocol) {
1753 				mnd_pefree(conn->options->auth_protocol, conn->persistent);
1754 			}
1755 			conn->options->auth_protocol = new_auth_protocol;
1756 			DBG_INF_FMT("auth_protocol=%s", conn->options->auth_protocol);
1757 			break;
1758 		}
1759 		case MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS:
1760 			if (value && (*(unsigned int*) value) ? 1 : 0) {
1761 				conn->options->flags |= CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
1762 			} else {
1763 				conn->options->flags &= ~CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
1764 			}
1765 			break;
1766 		case MYSQL_OPT_CONNECT_ATTR_RESET:
1767 			if (conn->options->connect_attr) {
1768 				DBG_INF_FMT("Before reset %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
1769 				zend_hash_clean(conn->options->connect_attr);
1770 			}
1771 			break;
1772 		case MYSQL_OPT_CONNECT_ATTR_DELETE:
1773 			if (conn->options->connect_attr && value) {
1774 				DBG_INF_FMT("Before delete %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
1775 				zend_hash_str_del(conn->options->connect_attr, value, strlen(value));
1776 				DBG_INF_FMT("%d left", zend_hash_num_elements(conn->options->connect_attr));
1777 			}
1778 			break;
1779 #ifdef WHEN_SUPPORTED_BY_MYSQLI
1780 		case MYSQL_SHARED_MEMORY_BASE_NAME:
1781 		case MYSQL_OPT_USE_RESULT:
1782 		case MYSQL_SECURE_AUTH:
1783 			/* not sure, todo ? */
1784 #endif
1785 		default:
1786 			ret = FAIL;
1787 	}
1788 	conn->m->local_tx_end(conn, this_func, ret);
1789 	DBG_RETURN(ret);
1790 oom:
1791 	SET_OOM_ERROR(conn->error_info);
1792 	conn->m->local_tx_end(conn, this_func, FAIL);
1793 end:
1794 	DBG_RETURN(FAIL);
1795 }
1796 /* }}} */
1797 
1798 
1799 /* {{{ mysqlnd_conn_data::set_client_option_2d */
1800 static enum_func_status
1801 MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d)(MYSQLND_CONN_DATA * const conn,
1802 														const enum_mysqlnd_client_option option,
1803 														const char * const key,
1804 														const char * const value
1805 														)
1806 {
1807 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), set_client_option_2d);
1808 	enum_func_status ret = PASS;
1809 	DBG_ENTER("mysqlnd_conn_data::set_client_option_2d");
1810 	DBG_INF_FMT("conn=%llu option=%u", conn->thread_id, option);
1811 
1812 	if (PASS != conn->m->local_tx_start(conn, this_func)) {
1813 		goto end;
1814 	}
1815 	switch (option) {
1816 		case MYSQL_OPT_CONNECT_ATTR_ADD:
1817 			if (!conn->options->connect_attr) {
1818 				DBG_INF("Initializing connect_attr hash");
1819 				conn->options->connect_attr = mnd_pemalloc(sizeof(HashTable), conn->persistent);
1820 				if (!conn->options->connect_attr) {
1821 					goto oom;
1822 				}
1823 				zend_hash_init(conn->options->connect_attr, 0, NULL, conn->persistent ? ZVAL_INTERNAL_PTR_DTOR : ZVAL_PTR_DTOR, conn->persistent);
1824 			}
1825 			DBG_INF_FMT("Adding [%s][%s]", key, value);
1826 			{
1827 				zval attrz;
1828 				zend_string *str;
1829 
1830 				if (conn->persistent) {
1831 					str = zend_string_init(key, strlen(key), 1);
1832 					GC_MAKE_PERSISTENT_LOCAL(str);
1833 					ZVAL_NEW_STR(&attrz, zend_string_init(value, strlen(value), 1));
1834 					GC_MAKE_PERSISTENT_LOCAL(Z_COUNTED(attrz));
1835 				} else {
1836 					str = zend_string_init(key, strlen(key), 0);
1837 					ZVAL_NEW_STR(&attrz, zend_string_init(value, strlen(value), 0));
1838 				}
1839 				zend_hash_update(conn->options->connect_attr, str, &attrz);
1840 				zend_string_release_ex(str, 1);
1841 			}
1842 			break;
1843 		default:
1844 			ret = FAIL;
1845 	}
1846 	conn->m->local_tx_end(conn, this_func, ret);
1847 	DBG_RETURN(ret);
1848 oom:
1849 	SET_OOM_ERROR(conn->error_info);
1850 	conn->m->local_tx_end(conn, this_func, FAIL);
1851 end:
1852 	DBG_RETURN(FAIL);
1853 }
1854 /* }}} */
1855 
1856 
1857 /* {{{ mysqlnd_conn_data::use_result */
1858 static MYSQLND_RES *
1859 MYSQLND_METHOD(mysqlnd_conn_data, use_result)(MYSQLND_CONN_DATA * const conn, const unsigned int flags)
1860 {
1861 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), use_result);
1862 	MYSQLND_RES * result = NULL;
1863 
1864 	DBG_ENTER("mysqlnd_conn_data::use_result");
1865 	DBG_INF_FMT("conn=%llu", conn->thread_id);
1866 
1867 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
1868 		do {
1869 			if (!conn->current_result) {
1870 				break;
1871 			}
1872 
1873 			/* Nothing to store for UPSERT/LOAD DATA */
1874 			if (conn->last_query_type != QUERY_SELECT || GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) {
1875 				SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
1876 				DBG_ERR("Command out of sync");
1877 				break;
1878 			}
1879 
1880 			MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_UNBUFFERED_SETS);
1881 
1882 			conn->current_result->conn = conn->m->get_reference(conn);
1883 			result = conn->current_result->m.use_result(conn->current_result, FALSE);
1884 
1885 			if (!result) {
1886 				conn->current_result->m.free_result(conn->current_result, TRUE);
1887 			}
1888 			conn->current_result = NULL;
1889 		} while (0);
1890 
1891 		conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS);
1892 	}
1893 
1894 	DBG_RETURN(result);
1895 }
1896 /* }}} */
1897 
1898 
1899 /* {{{ mysqlnd_conn_data::store_result */
1900 static MYSQLND_RES *
1901 MYSQLND_METHOD(mysqlnd_conn_data, store_result)(MYSQLND_CONN_DATA * const conn, const unsigned int flags)
1902 {
1903 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), store_result);
1904 	MYSQLND_RES * result = NULL;
1905 
1906 	DBG_ENTER("mysqlnd_conn_data::store_result");
1907 	DBG_INF_FMT("conn=%llu conn=%p", conn->thread_id, conn);
1908 
1909 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
1910 		do {
1911 			unsigned int f = flags;
1912 			if (!conn->current_result) {
1913 				break;
1914 			}
1915 
1916 			/* Nothing to store for UPSERT/LOAD DATA*/
1917 			if (conn->last_query_type != QUERY_SELECT || GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) {
1918 				SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
1919 				DBG_ERR("Command out of sync");
1920 				break;
1921 			}
1922 
1923 			MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_BUFFERED_SETS);
1924 
1925 			/* overwrite */
1926 			if ((conn->m->get_client_api_capabilities(conn) & MYSQLND_CLIENT_KNOWS_RSET_COPY_DATA)) {
1927 				if (MYSQLND_G(fetch_data_copy)) {
1928 					f &= ~MYSQLND_STORE_NO_COPY;
1929 					f |= MYSQLND_STORE_COPY;
1930 				}
1931 			} else {
1932 				/* if for some reason PDO borks something */
1933 				if (!(f & (MYSQLND_STORE_NO_COPY | MYSQLND_STORE_COPY))) {
1934 					f |= MYSQLND_STORE_COPY;
1935 				}
1936 			}
1937 			if (!(f & (MYSQLND_STORE_NO_COPY | MYSQLND_STORE_COPY))) {
1938 				SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Unknown fetch mode");
1939 				DBG_ERR("Unknown fetch mode");
1940 				break;
1941 			}
1942 			result = conn->current_result->m.store_result(conn->current_result, conn, f);
1943 			if (!result) {
1944 				conn->current_result->m.free_result(conn->current_result, TRUE);
1945 			}
1946 			conn->current_result = NULL;
1947 		} while (0);
1948 
1949 		conn->m->local_tx_end(conn, this_func, result == NULL? FAIL:PASS);
1950 	}
1951 	DBG_RETURN(result);
1952 }
1953 /* }}} */
1954 
1955 
1956 /* {{{ mysqlnd_conn_data::get_connection_stats */
1957 static void
1958 MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats)(const MYSQLND_CONN_DATA * const conn,
1959 														zval * return_value ZEND_FILE_LINE_DC)
1960 {
1961 	DBG_ENTER("mysqlnd_conn_data::get_connection_stats");
1962 	mysqlnd_fill_stats_hash(conn->stats, mysqlnd_stats_values_names, return_value ZEND_FILE_LINE_CC);
1963 	DBG_VOID_RETURN;
1964 }
1965 /* }}} */
1966 
1967 
1968 /* {{{ mysqlnd_conn_data::set_autocommit */
1969 static enum_func_status
1970 MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit)(MYSQLND_CONN_DATA * conn, unsigned int mode)
1971 {
1972 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), set_autocommit);
1973 	enum_func_status ret = FAIL;
1974 	DBG_ENTER("mysqlnd_conn_data::set_autocommit");
1975 
1976 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
1977 		ret = conn->m->query(conn, (mode) ? "SET AUTOCOMMIT=1":"SET AUTOCOMMIT=0", sizeof("SET AUTOCOMMIT=1") - 1);
1978 		conn->m->local_tx_end(conn, this_func, ret);
1979 	}
1980 
1981 	DBG_RETURN(ret);
1982 }
1983 /* }}} */
1984 
1985 
1986 /* {{{ mysqlnd_conn_data::tx_commit */
1987 static enum_func_status
1988 MYSQLND_METHOD(mysqlnd_conn_data, tx_commit)(MYSQLND_CONN_DATA * conn)
1989 {
1990 	return conn->m->tx_commit_or_rollback(conn, TRUE, TRANS_COR_NO_OPT, NULL);
1991 }
1992 /* }}} */
1993 
1994 
1995 /* {{{ mysqlnd_conn_data::tx_rollback */
1996 static enum_func_status
1997 MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback)(MYSQLND_CONN_DATA * conn)
1998 {
1999 	return conn->m->tx_commit_or_rollback(conn, FALSE, TRANS_COR_NO_OPT, NULL);
2000 }
2001 /* }}} */
2002 
2003 
2004 /* {{{ mysqlnd_tx_cor_options_to_string */
2005 static void
2006 MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string)(const MYSQLND_CONN_DATA * const conn, smart_str * str, const unsigned int mode)
2007 {
2008 	if (mode & TRANS_COR_AND_CHAIN && !(mode & TRANS_COR_AND_NO_CHAIN)) {
2009 		if (str->s && ZSTR_LEN(str->s)) {
2010 			smart_str_appendl(str, " ", sizeof(" ") - 1);
2011 		}
2012 		smart_str_appendl(str, "AND CHAIN", sizeof("AND CHAIN") - 1);
2013 	} else if (mode & TRANS_COR_AND_NO_CHAIN && !(mode & TRANS_COR_AND_CHAIN)) {
2014 		if (str->s && ZSTR_LEN(str->s)) {
2015 			smart_str_appendl(str, " ", sizeof(" ") - 1);
2016 		}
2017 		smart_str_appendl(str, "AND NO CHAIN", sizeof("AND NO CHAIN") - 1);
2018 	}
2019 
2020 	if (mode & TRANS_COR_RELEASE && !(mode & TRANS_COR_NO_RELEASE)) {
2021 		if (str->s && ZSTR_LEN(str->s)) {
2022 			smart_str_appendl(str, " ", sizeof(" ") - 1);
2023 		}
2024 		smart_str_appendl(str, "RELEASE", sizeof("RELEASE") - 1);
2025 	} else if (mode & TRANS_COR_NO_RELEASE && !(mode & TRANS_COR_RELEASE)) {
2026 		if (str->s && ZSTR_LEN(str->s)) {
2027 			smart_str_appendl(str, " ", sizeof(" ") - 1);
2028 		}
2029 		smart_str_appendl(str, "NO RELEASE", sizeof("NO RELEASE") - 1);
2030 	}
2031 	smart_str_0(str);
2032 }
2033 /* }}} */
2034 
2035 
2036 /* {{{ mysqlnd_escape_string_for_tx_name_in_comment */
2037 static char *
2038 mysqlnd_escape_string_for_tx_name_in_comment(const char * const name)
2039 {
2040 	char * ret = NULL;
2041 	DBG_ENTER("mysqlnd_escape_string_for_tx_name_in_comment");
2042 	if (name) {
2043 		zend_bool warned = FALSE;
2044 		const char * p_orig = name;
2045 		char * p_copy;
2046 		p_copy = ret = mnd_emalloc(strlen(name) + 1 + 2 + 2 + 1); /* space, open, close, NullS */
2047 		*p_copy++ = ' ';
2048 		*p_copy++ = '/';
2049 		*p_copy++ = '*';
2050 		while (1) {
2051 			register char v = *p_orig;
2052 			if (v == 0) {
2053 				break;
2054 			}
2055 			if ((v >= '0' && v <= '9') ||
2056 				(v >= 'a' && v <= 'z') ||
2057 				(v >= 'A' && v <= 'Z') ||
2058 				v == '-' ||
2059 				v == '_' ||
2060 				v == ' ' ||
2061 				v == '=')
2062 			{
2063 				*p_copy++ = v;
2064 			} else if (warned == FALSE) {
2065 				php_error_docref(NULL, E_WARNING, "Transaction name truncated. Must be only [0-9A-Za-z\\-_=]+");
2066 				warned = TRUE;
2067 			}
2068 			++p_orig;
2069 		}
2070 		*p_copy++ = '*';
2071 		*p_copy++ = '/';
2072 		*p_copy++ = 0;
2073 	}
2074 	DBG_RETURN(ret);
2075 }
2076 /* }}} */
2077 
2078 
2079 /* {{{ mysqlnd_conn_data::tx_commit_ex */
2080 static enum_func_status
2081 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)
2082 {
2083 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), tx_commit_or_rollback);
2084 	enum_func_status ret = FAIL;
2085 	DBG_ENTER("mysqlnd_conn_data::tx_commit_or_rollback");
2086 
2087 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
2088 		do {
2089 			smart_str tmp_str = {0, 0};
2090 			conn->m->tx_cor_options_to_string(conn, &tmp_str, flags);
2091 			smart_str_0(&tmp_str);
2092 
2093 
2094 			{
2095 				char * query;
2096 				size_t query_len;
2097 				char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name);
2098 
2099 				query_len = mnd_sprintf(&query, 0, (commit? "COMMIT%s %s":"ROLLBACK%s %s"),
2100 										name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
2101 				smart_str_free(&tmp_str);
2102 				if (name_esc) {
2103 					mnd_efree(name_esc);
2104 					name_esc = NULL;
2105 				}
2106 				if (!query) {
2107 					SET_OOM_ERROR(conn->error_info);
2108 					break;
2109 				}
2110 
2111 				ret = conn->m->query(conn, query, query_len);
2112 				mnd_sprintf_free(query);
2113 			}
2114 		} while (0);
2115 		conn->m->local_tx_end(conn, this_func, ret);
2116 	}
2117 
2118 	DBG_RETURN(ret);
2119 }
2120 /* }}} */
2121 
2122 
2123 /* {{{ mysqlnd_conn_data::tx_begin */
2124 static enum_func_status
2125 MYSQLND_METHOD(mysqlnd_conn_data, tx_begin)(MYSQLND_CONN_DATA * conn, const unsigned int mode, const char * const name)
2126 {
2127 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), tx_begin);
2128 	enum_func_status ret = FAIL;
2129 	DBG_ENTER("mysqlnd_conn_data::tx_begin");
2130 
2131 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
2132 		do {
2133 			smart_str tmp_str = {0, 0};
2134 			if (mode & TRANS_START_WITH_CONSISTENT_SNAPSHOT) {
2135 				if (tmp_str.s) {
2136 					smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
2137 				}
2138 				smart_str_appendl(&tmp_str, "WITH CONSISTENT SNAPSHOT", sizeof("WITH CONSISTENT SNAPSHOT") - 1);
2139 			}
2140 			if (mode & TRANS_START_READ_WRITE) {
2141 				if (tmp_str.s && ZSTR_LEN(tmp_str.s)) {
2142 					smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
2143 				}
2144 				smart_str_appendl(&tmp_str, "READ WRITE", sizeof("READ WRITE") - 1);
2145 			} else if (mode & TRANS_START_READ_ONLY) {
2146 				if (tmp_str.s && ZSTR_LEN(tmp_str.s)) {
2147 					smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
2148 				}
2149 				smart_str_appendl(&tmp_str, "READ ONLY", sizeof("READ ONLY") - 1);
2150 			}
2151 			smart_str_0(&tmp_str);
2152 
2153 			{
2154 				char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name);
2155 				char * query;
2156 				unsigned int query_len = mnd_sprintf(&query, 0, "START TRANSACTION%s %s", name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
2157 				smart_str_free(&tmp_str);
2158 				if (name_esc) {
2159 					mnd_efree(name_esc);
2160 					name_esc = NULL;
2161 				}
2162 				if (!query) {
2163 					SET_OOM_ERROR(conn->error_info);
2164 					break;
2165 				}
2166 				ret = conn->m->query(conn, query, query_len);
2167 				mnd_sprintf_free(query);
2168 				if (ret && mode & (TRANS_START_READ_WRITE | TRANS_START_READ_ONLY) &&
2169 					mysqlnd_stmt_errno(conn) == 1064) {
2170 					php_error_docref(NULL, E_WARNING, "This server version doesn't support 'READ WRITE' and 'READ ONLY'. Minimum 5.6.5 is required");
2171 					break;
2172 				}
2173 			}
2174 		} while (0);
2175 		conn->m->local_tx_end(conn, this_func, ret);
2176 	}
2177 
2178 	DBG_RETURN(ret);
2179 }
2180 /* }}} */
2181 
2182 
2183 /* {{{ mysqlnd_conn_data::tx_savepoint */
2184 static enum_func_status
2185 MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint)(MYSQLND_CONN_DATA * conn, const char * const name)
2186 {
2187 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), tx_savepoint);
2188 	enum_func_status ret = FAIL;
2189 	DBG_ENTER("mysqlnd_conn_data::tx_savepoint");
2190 
2191 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
2192 		do {
2193 			char * query;
2194 			unsigned int query_len;
2195 			if (!name) {
2196 				SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
2197 				break;
2198 			}
2199 			query_len = mnd_sprintf(&query, 0, "SAVEPOINT `%s`", name);
2200 			if (!query) {
2201 				SET_OOM_ERROR(conn->error_info);
2202 				break;
2203 			}
2204 			ret = conn->m->query(conn, query, query_len);
2205 			mnd_sprintf_free(query);
2206 		} while (0);
2207 		conn->m->local_tx_end(conn, this_func, ret);
2208 	}
2209 
2210 	DBG_RETURN(ret);
2211 }
2212 /* }}} */
2213 
2214 
2215 /* {{{ mysqlnd_conn_data::tx_savepoint_release */
2216 static enum_func_status
2217 MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release)(MYSQLND_CONN_DATA * conn, const char * const name)
2218 {
2219 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn_data), tx_savepoint_release);
2220 	enum_func_status ret = FAIL;
2221 	DBG_ENTER("mysqlnd_conn_data::tx_savepoint_release");
2222 
2223 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
2224 		do {
2225 			char * query;
2226 			unsigned int query_len;
2227 			if (!name) {
2228 				SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
2229 				break;
2230 			}
2231 			query_len = mnd_sprintf(&query, 0, "RELEASE SAVEPOINT `%s`", name);
2232 			if (!query) {
2233 				SET_OOM_ERROR(conn->error_info);
2234 				break;
2235 			}
2236 			ret = conn->m->query(conn, query, query_len);
2237 			mnd_sprintf_free(query);
2238 		} while (0);
2239 		conn->m->local_tx_end(conn, this_func, ret);
2240 	}
2241 
2242 	DBG_RETURN(ret);
2243 }
2244 /* }}} */
2245 
2246 
2247 /* {{{ mysqlnd_conn_data::negotiate_client_api_capabilities */
2248 static size_t
2249 MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities)(MYSQLND_CONN_DATA * const conn, const size_t flags)
2250 {
2251 	unsigned int ret = 0;
2252 	DBG_ENTER("mysqlnd_conn_data::negotiate_client_api_capabilities");
2253 	if (conn) {
2254 		ret = conn->client_api_capabilities;
2255 		conn->client_api_capabilities = flags;
2256 	}
2257 
2258 	DBG_RETURN(ret);
2259 }
2260 /* }}} */
2261 
2262 
2263 /* {{{ mysqlnd_conn_data::get_client_api_capabilities */
2264 static size_t
2265 MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities)(const MYSQLND_CONN_DATA * const conn)
2266 {
2267 	DBG_ENTER("mysqlnd_conn_data::get_client_api_capabilities");
2268 	DBG_RETURN(conn? conn->client_api_capabilities : 0);
2269 }
2270 /* }}} */
2271 
2272 
2273 /* {{{ mysqlnd_conn_data::local_tx_start */
2274 static enum_func_status
2275 MYSQLND_METHOD(mysqlnd_conn_data, local_tx_start)(MYSQLND_CONN_DATA * conn, const size_t this_func)
2276 {
2277 	DBG_ENTER("mysqlnd_conn_data::local_tx_start");
2278 	DBG_RETURN(PASS);
2279 }
2280 /* }}} */
2281 
2282 
2283 /* {{{ mysqlnd_conn_data::local_tx_end */
2284 static enum_func_status
2285 MYSQLND_METHOD(mysqlnd_conn_data, local_tx_end)(MYSQLND_CONN_DATA * conn, const size_t this_func, const enum_func_status status)
2286 {
2287 	DBG_ENTER("mysqlnd_conn_data::local_tx_end");
2288 	DBG_RETURN(status);
2289 }
2290 /* }}} */
2291 
2292 
2293 /* {{{ _mysqlnd_stmt_init */
2294 MYSQLND_STMT *
2295 MYSQLND_METHOD(mysqlnd_conn_data, stmt_init)(MYSQLND_CONN_DATA * const conn)
2296 {
2297 	MYSQLND_STMT * ret;
2298 	DBG_ENTER("mysqlnd_conn_data::stmt_init");
2299 	ret = conn->object_factory.get_prepared_statement(conn);
2300 	DBG_RETURN(ret);
2301 }
2302 /* }}} */
2303 
2304 
2305 MYSQLND_CLASS_METHODS_START(mysqlnd_conn_data)
2306 	MYSQLND_METHOD(mysqlnd_conn_data, connect),
2307 
2308 	MYSQLND_METHOD(mysqlnd_conn_data, escape_string),
2309 	MYSQLND_METHOD(mysqlnd_conn_data, set_charset),
2310 	MYSQLND_METHOD(mysqlnd_conn_data, query),
2311 	MYSQLND_METHOD(mysqlnd_conn_data, send_query),
2312 	MYSQLND_METHOD(mysqlnd_conn_data, reap_query),
2313 	MYSQLND_METHOD(mysqlnd_conn_data, use_result),
2314 	MYSQLND_METHOD(mysqlnd_conn_data, store_result),
2315 	MYSQLND_METHOD(mysqlnd_conn_data, next_result),
2316 	MYSQLND_METHOD(mysqlnd_conn_data, more_results),
2317 
2318 	MYSQLND_METHOD(mysqlnd_conn_data, stmt_init),
2319 
2320 	MYSQLND_METHOD(mysqlnd_conn_data, shutdown),
2321 	MYSQLND_METHOD(mysqlnd_conn_data, refresh),
2322 
2323 	MYSQLND_METHOD(mysqlnd_conn_data, ping),
2324 	MYSQLND_METHOD(mysqlnd_conn_data, kill),
2325 	MYSQLND_METHOD(mysqlnd_conn_data, select_db),
2326 	MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info),
2327 	MYSQLND_METHOD(mysqlnd_conn_data, change_user),
2328 
2329 	MYSQLND_METHOD(mysqlnd_conn_data, err_no),
2330 	MYSQLND_METHOD(mysqlnd_conn_data, error),
2331 	MYSQLND_METHOD(mysqlnd_conn_data, sqlstate),
2332 	MYSQLND_METHOD(mysqlnd_conn_data, thread_id),
2333 
2334 	MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats),
2335 
2336 	MYSQLND_METHOD(mysqlnd_conn_data, get_server_version),
2337 	MYSQLND_METHOD(mysqlnd_conn_data, get_server_info),
2338 	MYSQLND_METHOD(mysqlnd_conn_data, statistic),
2339 	MYSQLND_METHOD(mysqlnd_conn_data, get_host_info),
2340 	MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info),
2341 	MYSQLND_METHOD(mysqlnd_conn_data, info),
2342 	MYSQLND_METHOD(mysqlnd_conn_data, charset_name),
2343 	MYSQLND_METHOD(mysqlnd_conn_data, list_method),
2344 
2345 	MYSQLND_METHOD(mysqlnd_conn_data, insert_id),
2346 	MYSQLND_METHOD(mysqlnd_conn_data, affected_rows),
2347 	MYSQLND_METHOD(mysqlnd_conn_data, warning_count),
2348 	MYSQLND_METHOD(mysqlnd_conn_data, field_count),
2349 
2350 	MYSQLND_METHOD(mysqlnd_conn_data, server_status),
2351 
2352 	MYSQLND_METHOD(mysqlnd_conn_data, set_server_option),
2353 	MYSQLND_METHOD(mysqlnd_conn_data, set_client_option),
2354 	MYSQLND_METHOD(mysqlnd_conn_data, free_contents),
2355 	MYSQLND_METHOD(mysqlnd_conn_data, free_options),
2356 
2357 	MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor),
2358 
2359 	mysqlnd_query_read_result_set_header,
2360 
2361 	MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference),
2362 	MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference),
2363 
2364 	MYSQLND_METHOD(mysqlnd_conn_data, restart_psession),
2365 	MYSQLND_METHOD(mysqlnd_conn_data, end_psession),
2366 	MYSQLND_METHOD(mysqlnd_conn_data, send_close),
2367 
2368 	MYSQLND_METHOD(mysqlnd_conn_data, ssl_set),
2369 	mysqlnd_result_init,
2370 	MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit),
2371 	MYSQLND_METHOD(mysqlnd_conn_data, tx_commit),
2372 	MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback),
2373 	MYSQLND_METHOD(mysqlnd_conn_data, tx_begin),
2374 	MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback),
2375 	MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string),
2376 	MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint),
2377 	MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release),
2378 
2379 	MYSQLND_METHOD(mysqlnd_conn_data, local_tx_start),
2380 	MYSQLND_METHOD(mysqlnd_conn_data, local_tx_end),
2381 	MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands),
2382 	MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags),
2383 	MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake),
2384 	MYSQLND_METHOD(mysqlnd_conn_data, fetch_auth_plugin_by_name),
2385 
2386 	MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d),
2387 
2388 	MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities),
2389 	MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities),
2390 
2391 	MYSQLND_METHOD(mysqlnd_conn_data, get_scheme)
2392 MYSQLND_CLASS_METHODS_END;
2393 
2394 
2395 /* {{{ mysqlnd_conn::get_reference */
2396 static MYSQLND *
2397 MYSQLND_METHOD(mysqlnd_conn, clone_object)(MYSQLND * const conn)
2398 {
2399 	MYSQLND * ret;
2400 	DBG_ENTER("mysqlnd_conn::get_reference");
2401 	ret = conn->data->object_factory.clone_connection_object(conn);
2402 	DBG_RETURN(ret);
2403 }
2404 /* }}} */
2405 
2406 
2407 /* {{{ mysqlnd_conn_data::dtor */
2408 static void
2409 MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor)(MYSQLND * conn)
2410 {
2411 	DBG_ENTER("mysqlnd_conn::dtor");
2412 	DBG_INF_FMT("conn=%llu", conn->data->thread_id);
2413 
2414 	conn->data->m->free_reference(conn->data);
2415 
2416 	mnd_pefree(conn, conn->persistent);
2417 
2418 	DBG_VOID_RETURN;
2419 }
2420 /* }}} */
2421 
2422 
2423 /* {{{ mysqlnd_conn_data::close */
2424 static enum_func_status
2425 MYSQLND_METHOD(mysqlnd_conn, close)(MYSQLND * conn_handle, const enum_connection_close_type close_type)
2426 {
2427 	const size_t this_func = STRUCT_OFFSET(MYSQLND_CLASS_METHODS_TYPE(mysqlnd_conn), close);
2428 	MYSQLND_CONN_DATA * conn = conn_handle->data;
2429 	enum_func_status ret = FAIL;
2430 
2431 	DBG_ENTER("mysqlnd_conn::close");
2432 	DBG_INF_FMT("conn=%llu", conn->thread_id);
2433 
2434 	if (PASS == conn->m->local_tx_start(conn, this_func)) {
2435 		if (GET_CONNECTION_STATE(&conn->state) >= CONN_READY) {
2436 			static enum_mysqlnd_collected_stats close_type_to_stat_map[MYSQLND_CLOSE_LAST] = {
2437 				STAT_CLOSE_EXPLICIT,
2438 				STAT_CLOSE_IMPLICIT,
2439 				STAT_CLOSE_DISCONNECT
2440 			};
2441 			MYSQLND_INC_CONN_STATISTIC(conn->stats, close_type_to_stat_map[close_type]);
2442 		}
2443 
2444 		/*
2445 		  Close now, free_reference will try,
2446 		  if we are last, but that's not a problem.
2447 		*/
2448 		ret = conn->m->send_close(conn);
2449 
2450 		/* If we do it after free_reference/dtor then we might crash */
2451 		conn->m->local_tx_end(conn, this_func, ret);
2452 
2453 		conn_handle->m->dtor(conn_handle);
2454 	}
2455 	DBG_RETURN(ret);
2456 }
2457 /* }}} */
2458 
2459 
2460 MYSQLND_CLASS_METHODS_START(mysqlnd_conn)
2461 	MYSQLND_METHOD(mysqlnd_conn, connect),
2462 	MYSQLND_METHOD(mysqlnd_conn, clone_object),
2463 	MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor),
2464 	MYSQLND_METHOD(mysqlnd_conn, close)
2465 MYSQLND_CLASS_METHODS_END;
2466 
2467 
2468 #include "php_network.h"
2469 
2470 /* {{{ mysqlnd_stream_array_to_fd_set */
2471 MYSQLND **
2472 mysqlnd_stream_array_check_for_readiness(MYSQLND ** conn_array)
2473 {
2474 	unsigned int cnt = 0;
2475 	MYSQLND **p = conn_array, **p_p;
2476 	MYSQLND **ret = NULL;
2477 
2478 	while (*p) {
2479 		const enum mysqlnd_connection_state conn_state = GET_CONNECTION_STATE(&((*p)->data->state));
2480 		if (conn_state <= CONN_READY || conn_state == CONN_QUIT_SENT) {
2481 			cnt++;
2482 		}
2483 		p++;
2484 	}
2485 	if (cnt) {
2486 		MYSQLND **ret_p = ret = ecalloc(cnt + 1, sizeof(MYSQLND *));
2487 		p_p = p = conn_array;
2488 		while (*p) {
2489 			const enum mysqlnd_connection_state conn_state = GET_CONNECTION_STATE(&((*p)->data->state));
2490 			if (conn_state <= CONN_READY || conn_state == CONN_QUIT_SENT) {
2491 				*ret_p = *p;
2492 				*p = NULL;
2493 				ret_p++;
2494 			} else {
2495 				*p_p = *p;
2496 				p_p++;
2497 			}
2498 			p++;
2499 		}
2500 		*ret_p = NULL;
2501 	}
2502 	return ret;
2503 }
2504 /* }}} */
2505 
2506 
2507 /* {{{ mysqlnd_stream_array_to_fd_set */
2508 static unsigned int
2509 mysqlnd_stream_array_to_fd_set(MYSQLND ** conn_array, fd_set * fds, php_socket_t * max_fd)
2510 {
2511 	php_socket_t this_fd;
2512 	php_stream *stream = NULL;
2513 	unsigned int cnt = 0;
2514 	MYSQLND **p = conn_array;
2515 	DBG_ENTER("mysqlnd_stream_array_to_fd_set");
2516 
2517 	while (*p) {
2518 		/* get the fd.
2519 		 * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
2520 		 * when casting.  It is only used here so that the buffered data warning
2521 		 * is not displayed.
2522 		 * */
2523 		stream = (*p)->data->vio->data->m.get_stream((*p)->data->vio);
2524 		DBG_INF_FMT("conn=%llu stream=%p", (*p)->data->thread_id, stream);
2525 		if (stream != NULL &&
2526 			SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) &&
2527 			ZEND_VALID_SOCKET(this_fd))
2528 		{
2529 
2530 			PHP_SAFE_FD_SET(this_fd, fds);
2531 
2532 			if (this_fd > *max_fd) {
2533 				*max_fd = this_fd;
2534 			}
2535 			++cnt;
2536 		}
2537 		++p;
2538 	}
2539 	DBG_RETURN(cnt ? 1 : 0);
2540 }
2541 /* }}} */
2542 
2543 
2544 /* {{{ mysqlnd_stream_array_from_fd_set */
2545 static unsigned int
2546 mysqlnd_stream_array_from_fd_set(MYSQLND ** conn_array, fd_set * fds)
2547 {
2548 	php_socket_t this_fd;
2549 	php_stream *stream = NULL;
2550 	unsigned int ret = 0;
2551 	zend_bool disproportion = FALSE;
2552 	MYSQLND **fwd = conn_array, **bckwd = conn_array;
2553 	DBG_ENTER("mysqlnd_stream_array_from_fd_set");
2554 
2555 	while (*fwd) {
2556 		stream = (*fwd)->data->vio->data->m.get_stream((*fwd)->data->vio);
2557 		DBG_INF_FMT("conn=%llu stream=%p", (*fwd)->data->thread_id, stream);
2558 		if (stream != NULL && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL,
2559 										(void*)&this_fd, 1) && ZEND_VALID_SOCKET(this_fd)) {
2560 			if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
2561 				if (disproportion) {
2562 					*bckwd = *fwd;
2563 				}
2564 				++bckwd;
2565 				++fwd;
2566 				++ret;
2567 				continue;
2568 			}
2569 		}
2570 		disproportion = TRUE;
2571 		++fwd;
2572 	}
2573 	*bckwd = NULL;/* NULL-terminate the list */
2574 
2575 	DBG_RETURN(ret);
2576 }
2577 /* }}} */
2578 
2579 
2580 #ifndef PHP_WIN32
2581 #define php_select(m, r, w, e, t)	select(m, r, w, e, t)
2582 #else
2583 #include "win32/select.h"
2584 #endif
2585 
2586 
2587 /* {{{ mysqlnd_poll */
2588 PHPAPI enum_func_status
2589 mysqlnd_poll(MYSQLND **r_array, MYSQLND **e_array, MYSQLND ***dont_poll, long sec, long usec, int * desc_num)
2590 {
2591 	struct timeval	tv;
2592 	struct timeval *tv_p = NULL;
2593 	fd_set			rfds, wfds, efds;
2594 	php_socket_t	max_fd = 0;
2595 	int				retval, sets = 0;
2596 	int				set_count, max_set_count = 0;
2597 
2598 	DBG_ENTER("_mysqlnd_poll");
2599 	if (sec < 0 || usec < 0) {
2600 		php_error_docref(NULL, E_WARNING, "Negative values passed for sec and/or usec");
2601 		DBG_RETURN(FAIL);
2602 	}
2603 
2604 	FD_ZERO(&rfds);
2605 	FD_ZERO(&wfds);
2606 	FD_ZERO(&efds);
2607 
2608 	if (r_array != NULL) {
2609 		*dont_poll = mysqlnd_stream_array_check_for_readiness(r_array);
2610 		set_count = mysqlnd_stream_array_to_fd_set(r_array, &rfds, &max_fd);
2611 		if (set_count > max_set_count) {
2612 			max_set_count = set_count;
2613 		}
2614 		sets += set_count;
2615 	}
2616 
2617 	if (e_array != NULL) {
2618 		set_count = mysqlnd_stream_array_to_fd_set(e_array, &efds, &max_fd);
2619 		if (set_count > max_set_count) {
2620 			max_set_count = set_count;
2621 		}
2622 		sets += set_count;
2623 	}
2624 
2625 	if (!sets) {
2626 		php_error_docref(NULL, E_WARNING, *dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
2627 		DBG_ERR_FMT(*dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
2628 		DBG_RETURN(FAIL);
2629 	}
2630 
2631 	PHP_SAFE_MAX_FD(max_fd, max_set_count);
2632 
2633 	/* Solaris + BSD do not like microsecond values which are >= 1 sec */
2634 	if (usec > 999999) {
2635 		tv.tv_sec = sec + (usec / 1000000);
2636 		tv.tv_usec = usec % 1000000;
2637 	} else {
2638 		tv.tv_sec = sec;
2639 		tv.tv_usec = usec;
2640 	}
2641 
2642 	tv_p = &tv;
2643 
2644 	retval = php_select(max_fd + 1, &rfds, &wfds, &efds, tv_p);
2645 
2646 	if (retval == -1) {
2647 		php_error_docref(NULL, E_WARNING, "unable to select [%d]: %s (max_fd=%d)",
2648 						errno, strerror(errno), max_fd);
2649 		DBG_RETURN(FAIL);
2650 	}
2651 
2652 	if (r_array != NULL) {
2653 		mysqlnd_stream_array_from_fd_set(r_array, &rfds);
2654 	}
2655 	if (e_array != NULL) {
2656 		mysqlnd_stream_array_from_fd_set(e_array, &efds);
2657 	}
2658 
2659 	*desc_num = retval;
2660 	DBG_RETURN(PASS);
2661 }
2662 /* }}} */
2663 
2664 
2665 /* {{{ mysqlnd_connect */
2666 PHPAPI MYSQLND * mysqlnd_connection_connect(MYSQLND * conn_handle,
2667 											const char * const host,
2668 											const char * const user,
2669 											const char * const passwd, unsigned int passwd_len,
2670 											const char * const db, unsigned int db_len,
2671 											unsigned int port,
2672 											const char * const sock_or_pipe,
2673 											unsigned int mysql_flags,
2674 											unsigned int client_api_flags
2675 						)
2676 {
2677 	enum_func_status ret = FAIL;
2678 	zend_bool self_alloced = FALSE;
2679 	MYSQLND_CSTRING hostname = { host, host? strlen(host) : 0 };
2680 	MYSQLND_CSTRING username = { user, user? strlen(user) : 0 };
2681 	MYSQLND_CSTRING password = { passwd, passwd_len };
2682 	MYSQLND_CSTRING database = { db, db_len };
2683 	MYSQLND_CSTRING socket_or_pipe = { sock_or_pipe, sock_or_pipe? strlen(sock_or_pipe) : 0 };
2684 
2685 	DBG_ENTER("mysqlnd_connect");
2686 	DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u", host? host:"", user? user:"", db? db:"", port, mysql_flags);
2687 
2688 	if (!conn_handle) {
2689 		self_alloced = TRUE;
2690 		if (!(conn_handle = mysqlnd_connection_init(client_api_flags, FALSE, NULL))) {
2691 			/* OOM */
2692 			DBG_RETURN(NULL);
2693 		}
2694 	}
2695 
2696 	ret = conn_handle->m->connect(conn_handle, hostname, username, password, database, port, socket_or_pipe, mysql_flags);
2697 
2698 	if (ret == FAIL) {
2699 		if (self_alloced) {
2700 			/*
2701 			  We have alloced, thus there are no references to this
2702 			  object - we are free to kill it!
2703 			*/
2704 			conn_handle->m->dtor(conn_handle);
2705 		}
2706 		DBG_RETURN(NULL);
2707 	}
2708 	DBG_RETURN(conn_handle);
2709 }
2710 /* }}} */
2711 
2712 
2713 /* {{{ mysqlnd_connection_init */
2714 PHPAPI MYSQLND *
2715 mysqlnd_connection_init(const size_t client_flags, const zend_bool persistent, MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *object_factory)
2716 {
2717 	MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *factory = object_factory? object_factory : &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory);
2718 	MYSQLND * ret;
2719 	DBG_ENTER("mysqlnd_connection_init");
2720 	ret = factory->get_connection(factory, persistent);
2721 	if (ret && ret->data) {
2722 		ret->data->m->negotiate_client_api_capabilities(ret->data, client_flags);
2723 	}
2724 	DBG_RETURN(ret);
2725 }
2726 /* }}} */
2727