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