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