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