xref: /PHP-7.4/ext/pdo_mysql/mysql_driver.c (revision 6afbb741)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) The PHP Group                                          |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: George Schlossnagle <george@omniti.com>                      |
16   |         Wez Furlong <wez@php.net>                                    |
17   |         Johannes Schlueter <johannes@mysql.com>                      |
18   +----------------------------------------------------------------------+
19 */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "php.h"
26 #include "php_ini.h"
27 #include "ext/standard/info.h"
28 #include "pdo/php_pdo.h"
29 #include "pdo/php_pdo_driver.h"
30 #include "php_pdo_mysql.h"
31 #include "php_pdo_mysql_int.h"
32 #ifndef PDO_USE_MYSQLND
33 #include <mysqld_error.h>
34 #endif
35 #include "zend_exceptions.h"
36 
37 #if defined(PDO_USE_MYSQLND)
38 #	define pdo_mysql_init(persistent) mysqlnd_init(MYSQLND_CLIENT_NO_FLAG, persistent)
39 #else
40 #	define pdo_mysql_init(persistent) mysql_init(NULL)
41 #endif
42 
43 /* {{{ _pdo_mysql_error */
_pdo_mysql_error(pdo_dbh_t * dbh,pdo_stmt_t * stmt,const char * file,int line)44 int _pdo_mysql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line)
45 {
46 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
47 	pdo_error_type *pdo_err;
48 	pdo_mysql_error_info *einfo;
49 	pdo_mysql_stmt *S = NULL;
50 
51 	PDO_DBG_ENTER("_pdo_mysql_error");
52 	PDO_DBG_INF_FMT("file=%s line=%d", file, line);
53 	if (stmt) {
54 		S = (pdo_mysql_stmt*)stmt->driver_data;
55 		pdo_err = &stmt->error_code;
56 		einfo   = &S->einfo;
57 	} else {
58 		pdo_err = &dbh->error_code;
59 		einfo   = &H->einfo;
60 	}
61 
62 	if (S && S->stmt) {
63 		einfo->errcode = mysql_stmt_errno(S->stmt);
64 	} else {
65 		einfo->errcode = mysql_errno(H->server);
66 	}
67 
68 	einfo->file = file;
69 	einfo->line = line;
70 
71 	if (einfo->errmsg) {
72 		pefree(einfo->errmsg, dbh->is_persistent);
73 		einfo->errmsg = NULL;
74 	}
75 
76 	if (einfo->errcode) {
77 		if (einfo->errcode == 2014) {
78 			einfo->errmsg = pestrdup(
79 				"Cannot execute queries while other unbuffered queries are active.  "
80 				"Consider using PDOStatement::fetchAll().  Alternatively, if your code "
81 				"is only ever going to run against mysql, you may enable query "
82 				"buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.",
83 				dbh->is_persistent);
84 		} else if (einfo->errcode == 2057) {
85 			einfo->errmsg = pestrdup(
86 				"A stored procedure returning result sets of different size was called. "
87 				"This is not supported by libmysql",
88 				dbh->is_persistent);
89 
90 		} else {
91 			if (S && S->stmt) {
92 				einfo->errmsg = pestrdup(mysql_stmt_error(S->stmt), dbh->is_persistent);
93 			} else {
94 				einfo->errmsg = pestrdup(mysql_error(H->server), dbh->is_persistent);
95 			}
96 		}
97 	} else { /* no error */
98 		strcpy(*pdo_err, PDO_ERR_NONE);
99 		PDO_DBG_RETURN(0);
100 	}
101 
102 	if (S && S->stmt) {
103 		strcpy(*pdo_err, mysql_stmt_sqlstate(S->stmt));
104 	} else {
105 		strcpy(*pdo_err, mysql_sqlstate(H->server));
106 	}
107 
108 	if (!dbh->methods) {
109 		PDO_DBG_INF("Throwing exception");
110 		pdo_throw_exception(einfo->errcode, einfo->errmsg, pdo_err);
111 	}
112 
113 	PDO_DBG_RETURN(einfo->errcode);
114 }
115 /* }}} */
116 
117 /* {{{ pdo_mysql_fetch_error_func */
pdo_mysql_fetch_error_func(pdo_dbh_t * dbh,pdo_stmt_t * stmt,zval * info)118 static int pdo_mysql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info)
119 {
120 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
121 	pdo_mysql_error_info *einfo = &H->einfo;
122 
123 	PDO_DBG_ENTER("pdo_mysql_fetch_error_func");
124 	PDO_DBG_INF_FMT("dbh=%p stmt=%p", dbh, stmt);
125 	if (stmt) {
126 		pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
127 		einfo = &S->einfo;
128 	} else {
129 		einfo = &H->einfo;
130 	}
131 
132 	if (einfo->errcode) {
133 		add_next_index_long(info, einfo->errcode);
134 		add_next_index_string(info, einfo->errmsg);
135 	}
136 
137 	PDO_DBG_RETURN(1);
138 }
139 /* }}} */
140 
141 /* {{{ mysql_handle_closer */
mysql_handle_closer(pdo_dbh_t * dbh)142 static int mysql_handle_closer(pdo_dbh_t *dbh)
143 {
144 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
145 
146 	PDO_DBG_ENTER("mysql_handle_closer");
147 	PDO_DBG_INF_FMT("dbh=%p", dbh);
148 	if (H) {
149 		if (H->server) {
150 			mysql_close(H->server);
151 		}
152 		if (H->einfo.errmsg) {
153 			pefree(H->einfo.errmsg, dbh->is_persistent);
154 		}
155 		pefree(H, dbh->is_persistent);
156 		dbh->driver_data = NULL;
157 	}
158 	PDO_DBG_RETURN(0);
159 }
160 /* }}} */
161 
162 /* {{{ mysql_handle_preparer */
mysql_handle_preparer(pdo_dbh_t * dbh,const char * sql,size_t sql_len,pdo_stmt_t * stmt,zval * driver_options)163 static int mysql_handle_preparer(pdo_dbh_t *dbh, const char *sql, size_t sql_len, pdo_stmt_t *stmt, zval *driver_options)
164 {
165 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
166 	pdo_mysql_stmt *S = ecalloc(1, sizeof(pdo_mysql_stmt));
167 	char *nsql = NULL;
168 	size_t nsql_len = 0;
169 	int ret;
170 	int server_version;
171 
172 	PDO_DBG_ENTER("mysql_handle_preparer");
173 	PDO_DBG_INF_FMT("dbh=%p", dbh);
174 	PDO_DBG_INF_FMT("sql=%.*s", (int)sql_len, sql);
175 
176 	S->H = H;
177 	stmt->driver_data = S;
178 	stmt->methods = &mysql_stmt_methods;
179 
180 	if (H->emulate_prepare) {
181 		goto end;
182 	}
183 
184 	server_version = mysql_get_server_version(H->server);
185 	if (server_version < 40100) {
186 		goto fallback;
187 	}
188 	stmt->supports_placeholders = PDO_PLACEHOLDER_POSITIONAL;
189 	ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len);
190 
191 	if (ret == 1) {
192 		/* query was rewritten */
193 		sql = nsql;
194 		sql_len = nsql_len;
195 	} else if (ret == -1) {
196 		/* failed to parse */
197 		strcpy(dbh->error_code, stmt->error_code);
198 		PDO_DBG_RETURN(0);
199 	}
200 
201 	if (!(S->stmt = mysql_stmt_init(H->server))) {
202 		pdo_mysql_error(dbh);
203 		if (nsql) {
204 			efree(nsql);
205 		}
206 		PDO_DBG_RETURN(0);
207 	}
208 
209 	if (mysql_stmt_prepare(S->stmt, sql, sql_len)) {
210 		if (nsql) {
211 			efree(nsql);
212 		}
213 		/* TODO: might need to pull statement specific info here? */
214 		/* if the query isn't supported by the protocol, fallback to emulation */
215 		if (mysql_errno(H->server) == 1295) {
216 			mysql_stmt_close(S->stmt);
217 			S->stmt = NULL;
218 			goto fallback;
219 		}
220 		pdo_mysql_error(dbh);
221 		PDO_DBG_RETURN(0);
222 	}
223 	if (nsql) {
224 		efree(nsql);
225 	}
226 
227 	S->num_params = mysql_stmt_param_count(S->stmt);
228 
229 	if (S->num_params) {
230 		S->params_given = 0;
231 #if defined(PDO_USE_MYSQLND)
232 		S->params = NULL;
233 #else
234 		S->params = ecalloc(S->num_params, sizeof(MYSQL_BIND));
235 		S->in_null = ecalloc(S->num_params, sizeof(my_bool));
236 		S->in_length = ecalloc(S->num_params, sizeof(zend_ulong));
237 #endif
238 	}
239 	dbh->alloc_own_columns = 1;
240 
241 	S->max_length = pdo_attr_lval(driver_options, PDO_ATTR_MAX_COLUMN_LEN, 0);
242 
243 	PDO_DBG_RETURN(1);
244 
245 fallback:
246 end:
247 	stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
248 
249 	PDO_DBG_RETURN(1);
250 }
251 /* }}} */
252 
253 /* {{{ mysql_handle_doer */
mysql_handle_doer(pdo_dbh_t * dbh,const char * sql,size_t sql_len)254 static zend_long mysql_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
255 {
256 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
257 	PDO_DBG_ENTER("mysql_handle_doer");
258 	PDO_DBG_INF_FMT("dbh=%p", dbh);
259 	PDO_DBG_INF_FMT("sql=%.*s", (int)sql_len, sql);
260 
261 	if (mysql_real_query(H->server, sql, sql_len)) {
262 		pdo_mysql_error(dbh);
263 		PDO_DBG_RETURN(-1);
264 	} else {
265 		my_ulonglong c = mysql_affected_rows(H->server);
266 		if (c == (my_ulonglong) -1) {
267 			pdo_mysql_error(dbh);
268 			PDO_DBG_RETURN(H->einfo.errcode ? -1 : 0);
269 		} else {
270 
271 			/* MULTI_QUERY support - eat up all unfetched result sets */
272 			MYSQL_RES* result;
273 			while (mysql_more_results(H->server)) {
274 				if (mysql_next_result(H->server)) {
275 					pdo_mysql_error(dbh);
276 					PDO_DBG_RETURN(-1);
277 				}
278 				result = mysql_store_result(H->server);
279 				if (result) {
280 					mysql_free_result(result);
281 				}
282 			}
283 			PDO_DBG_RETURN((int)c);
284 		}
285 	}
286 }
287 /* }}} */
288 
289 /* {{{ pdo_mysql_last_insert_id */
pdo_mysql_last_insert_id(pdo_dbh_t * dbh,const char * name,size_t * len)290 static char *pdo_mysql_last_insert_id(pdo_dbh_t *dbh, const char *name, size_t *len)
291 {
292 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
293 	char *id = php_pdo_int64_to_str(mysql_insert_id(H->server));
294 	PDO_DBG_ENTER("pdo_mysql_last_insert_id");
295 	*len = strlen(id);
296 	PDO_DBG_RETURN(id);
297 }
298 /* }}} */
299 
300 #if defined(PDO_USE_MYSQLND) || MYSQL_VERSION_ID < 50707 || defined(MARIADB_BASE_VERSION)
301 # define mysql_real_escape_string_quote(mysql, to, from, length, quote) \
302 	mysql_real_escape_string(mysql, to, from, length)
303 #endif
304 
305 /* {{{ mysql_handle_quoter */
mysql_handle_quoter(pdo_dbh_t * dbh,const char * unquoted,size_t unquotedlen,char ** quoted,size_t * quotedlen,enum pdo_param_type paramtype)306 static int mysql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype )
307 {
308 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
309 	zend_bool use_national_character_set = 0;
310 
311 	if (H->assume_national_character_set_strings) {
312 		use_national_character_set = 1;
313 	}
314 	if ((paramtype & PDO_PARAM_STR_NATL) == PDO_PARAM_STR_NATL) {
315 		use_national_character_set = 1;
316 	}
317 	if ((paramtype & PDO_PARAM_STR_CHAR) == PDO_PARAM_STR_CHAR) {
318 		use_national_character_set = 0;
319 	}
320 
321 	PDO_DBG_ENTER("mysql_handle_quoter");
322 	PDO_DBG_INF_FMT("dbh=%p", dbh);
323 	PDO_DBG_INF_FMT("unquoted=%.*s", (int)unquotedlen, unquoted);
324 	*quoted = safe_emalloc(2, unquotedlen, 3 + (use_national_character_set ? 1 : 0));
325 
326 	if (use_national_character_set) {
327 		*quotedlen = mysql_real_escape_string_quote(H->server, *quoted + 2, unquoted, unquotedlen, '\'');
328 		(*quoted)[0] = 'N';
329 		(*quoted)[1] = '\'';
330 
331 		++*quotedlen; /* N prefix */
332 	} else {
333 		*quotedlen = mysql_real_escape_string_quote(H->server, *quoted + 1, unquoted, unquotedlen, '\'');
334 		(*quoted)[0] = '\'';
335 	}
336 
337 	(*quoted)[++*quotedlen] = '\'';
338 	(*quoted)[++*quotedlen] = '\0';
339 	PDO_DBG_INF_FMT("quoted=%.*s", (int)*quotedlen, *quoted);
340 	PDO_DBG_RETURN(1);
341 }
342 /* }}} */
343 
344 /* {{{ mysql_handle_begin */
mysql_handle_begin(pdo_dbh_t * dbh)345 static int mysql_handle_begin(pdo_dbh_t *dbh)
346 {
347 	PDO_DBG_ENTER("mysql_handle_quoter");
348 	PDO_DBG_INF_FMT("dbh=%p", dbh);
349 	PDO_DBG_RETURN(0 <= mysql_handle_doer(dbh, ZEND_STRL("START TRANSACTION")));
350 }
351 /* }}} */
352 
353 /* {{{ mysql_handle_commit */
mysql_handle_commit(pdo_dbh_t * dbh)354 static int mysql_handle_commit(pdo_dbh_t *dbh)
355 {
356 	PDO_DBG_ENTER("mysql_handle_commit");
357 	PDO_DBG_INF_FMT("dbh=%p", dbh);
358 	if (mysql_commit(((pdo_mysql_db_handle *)dbh->driver_data)->server)) {
359 		pdo_mysql_error(dbh);
360 		PDO_DBG_RETURN(0);
361 	}
362 	PDO_DBG_RETURN(1);
363 }
364 /* }}} */
365 
366 /* {{{ mysql_handle_rollback */
mysql_handle_rollback(pdo_dbh_t * dbh)367 static int mysql_handle_rollback(pdo_dbh_t *dbh)
368 {
369 	PDO_DBG_ENTER("mysql_handle_rollback");
370 	PDO_DBG_INF_FMT("dbh=%p", dbh);
371 	if (mysql_rollback(((pdo_mysql_db_handle *)dbh->driver_data)->server)) {
372 		pdo_mysql_error(dbh);
373 		PDO_DBG_RETURN(0);
374 	}
375 	PDO_DBG_RETURN(1);
376 }
377 /* }}} */
378 
379 /* {{{ mysql_handle_autocommit */
mysql_handle_autocommit(pdo_dbh_t * dbh)380 static inline int mysql_handle_autocommit(pdo_dbh_t *dbh)
381 {
382 	PDO_DBG_ENTER("mysql_handle_autocommit");
383 	PDO_DBG_INF_FMT("dbh=%p", dbh);
384 	PDO_DBG_INF_FMT("dbh->autocommit=%d", dbh->auto_commit);
385 	if (mysql_autocommit(((pdo_mysql_db_handle *)dbh->driver_data)->server, dbh->auto_commit)) {
386 		pdo_mysql_error(dbh);
387 		PDO_DBG_RETURN(0);
388 	}
389 	PDO_DBG_RETURN(1);
390 }
391 /* }}} */
392 
393 /* {{{ pdo_mysql_set_attribute */
pdo_mysql_set_attribute(pdo_dbh_t * dbh,zend_long attr,zval * val)394 static int pdo_mysql_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)
395 {
396 	zend_long lval = zval_get_long(val);
397 	zend_bool bval = lval ? 1 : 0;
398 	PDO_DBG_ENTER("pdo_mysql_set_attribute");
399 	PDO_DBG_INF_FMT("dbh=%p", dbh);
400 	PDO_DBG_INF_FMT("attr=%l", attr);
401 	switch (attr) {
402 		case PDO_ATTR_AUTOCOMMIT:
403 			/* ignore if the new value equals the old one */
404 			if (dbh->auto_commit ^ bval) {
405 				dbh->auto_commit = bval;
406 				if (!mysql_handle_autocommit(dbh)) {
407 					PDO_DBG_RETURN(0);
408 				}
409 			}
410 			PDO_DBG_RETURN(1);
411 
412 		case PDO_ATTR_DEFAULT_STR_PARAM:
413 			((pdo_mysql_db_handle *)dbh->driver_data)->assume_national_character_set_strings = lval == PDO_PARAM_STR_NATL;
414 			PDO_DBG_RETURN(1);
415 
416 		case PDO_MYSQL_ATTR_USE_BUFFERED_QUERY:
417 			/* ignore if the new value equals the old one */
418 			((pdo_mysql_db_handle *)dbh->driver_data)->buffered = bval;
419 			PDO_DBG_RETURN(1);
420 
421 		case PDO_MYSQL_ATTR_DIRECT_QUERY:
422 		case PDO_ATTR_EMULATE_PREPARES:
423 			/* ignore if the new value equals the old one */
424 			((pdo_mysql_db_handle *)dbh->driver_data)->emulate_prepare = bval;
425 			PDO_DBG_RETURN(1);
426 
427 		case PDO_ATTR_FETCH_TABLE_NAMES:
428 			((pdo_mysql_db_handle *)dbh->driver_data)->fetch_table_names = bval;
429 			PDO_DBG_RETURN(1);
430 
431 #ifndef PDO_USE_MYSQLND
432 		case PDO_MYSQL_ATTR_MAX_BUFFER_SIZE:
433 			if (lval < 0) {
434 				/* TODO: Johannes, can we throw a warning here? */
435  				((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size = 1024*1024;
436 				PDO_DBG_INF_FMT("Adjusting invalid buffer size to =%l", ((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size);
437 			} else {
438 				((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size = lval;
439 			}
440 			PDO_DBG_RETURN(1);
441 			break;
442 #endif
443 
444 		default:
445 			PDO_DBG_RETURN(0);
446 	}
447 }
448 /* }}} */
449 
450 /* {{{ pdo_mysql_get_attribute */
pdo_mysql_get_attribute(pdo_dbh_t * dbh,zend_long attr,zval * return_value)451 static int pdo_mysql_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
452 {
453 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
454 
455 	PDO_DBG_ENTER("pdo_mysql_get_attribute");
456 	PDO_DBG_INF_FMT("dbh=%p", dbh);
457 	PDO_DBG_INF_FMT("attr=%l", attr);
458 	switch (attr) {
459 		case PDO_ATTR_CLIENT_VERSION:
460 			ZVAL_STRING(return_value, (char *)mysql_get_client_info());
461 			break;
462 
463 		case PDO_ATTR_SERVER_VERSION:
464 			ZVAL_STRING(return_value, (char *)mysql_get_server_info(H->server));
465 			break;
466 
467 		case PDO_ATTR_CONNECTION_STATUS:
468 			ZVAL_STRING(return_value, (char *)mysql_get_host_info(H->server));
469 			break;
470 		case PDO_ATTR_SERVER_INFO: {
471 #if defined(PDO_USE_MYSQLND)
472 			zend_string *tmp;
473 
474 			if (mysqlnd_stat(H->server, &tmp) == PASS) {
475 				ZVAL_STR(return_value, tmp);
476 #else
477 			char *tmp;
478 			if ((tmp = (char *)mysql_stat(H->server))) {
479 				ZVAL_STRING(return_value, tmp);
480 #endif
481 			} else {
482 				pdo_mysql_error(dbh);
483 				PDO_DBG_RETURN(-1);
484 			}
485 		}
486 			break;
487 
488 		case PDO_ATTR_AUTOCOMMIT:
489 			ZVAL_LONG(return_value, dbh->auto_commit);
490 			break;
491 
492 		case PDO_ATTR_DEFAULT_STR_PARAM:
493 			ZVAL_LONG(return_value, H->assume_national_character_set_strings ? PDO_PARAM_STR_NATL : PDO_PARAM_STR_CHAR);
494 			break;
495 
496 		case PDO_MYSQL_ATTR_USE_BUFFERED_QUERY:
497 			ZVAL_LONG(return_value, H->buffered);
498 			break;
499 
500 		case PDO_ATTR_EMULATE_PREPARES:
501 		case PDO_MYSQL_ATTR_DIRECT_QUERY:
502 			ZVAL_LONG(return_value, H->emulate_prepare);
503 			break;
504 
505 #ifndef PDO_USE_MYSQLND
506 		case PDO_MYSQL_ATTR_MAX_BUFFER_SIZE:
507 			ZVAL_LONG(return_value, H->max_buffer_size);
508 			break;
509 #else
510 		case PDO_MYSQL_ATTR_LOCAL_INFILE:
511 			ZVAL_BOOL(
512 				return_value,
513 				(H->server->data->options->flags & CLIENT_LOCAL_FILES) == CLIENT_LOCAL_FILES);
514 			break;
515 #endif
516 
517 		default:
518 			PDO_DBG_RETURN(0);
519 	}
520 
521 	PDO_DBG_RETURN(1);
522 }
523 /* }}} */
524 
525 /* {{{ pdo_mysql_check_liveness */
526 static int pdo_mysql_check_liveness(pdo_dbh_t *dbh)
527 {
528 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
529 
530 	PDO_DBG_ENTER("pdo_mysql_check_liveness");
531 	PDO_DBG_INF_FMT("dbh=%p", dbh);
532 
533 	if (mysql_ping(H->server)) {
534 		PDO_DBG_RETURN(FAILURE);
535 	}
536 	PDO_DBG_RETURN(SUCCESS);
537 }
538 /* }}} */
539 
540 /* {{{ pdo_mysql_request_shutdown */
541 static void pdo_mysql_request_shutdown(pdo_dbh_t *dbh)
542 {
543 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
544 
545 	PDO_DBG_ENTER("pdo_mysql_request_shutdown");
546 	PDO_DBG_INF_FMT("dbh=%p", dbh);
547 #ifdef PDO_USE_MYSQLND
548 	if (H->server) {
549 		mysqlnd_end_psession(H->server);
550 	}
551 #endif
552 }
553 /* }}} */
554 
555 /* {{{ mysql_methods */
556 static const struct pdo_dbh_methods mysql_methods = {
557 	mysql_handle_closer,
558 	mysql_handle_preparer,
559 	mysql_handle_doer,
560 	mysql_handle_quoter,
561 	mysql_handle_begin,
562 	mysql_handle_commit,
563 	mysql_handle_rollback,
564 	pdo_mysql_set_attribute,
565 	pdo_mysql_last_insert_id,
566 	pdo_mysql_fetch_error_func,
567 	pdo_mysql_get_attribute,
568 	pdo_mysql_check_liveness,
569 	NULL,
570 	pdo_mysql_request_shutdown,
571 	NULL
572 };
573 /* }}} */
574 
575 #ifdef PHP_WIN32
576 # define PDO_DEFAULT_MYSQL_UNIX_ADDR	NULL
577 #else
578 # define PDO_DEFAULT_MYSQL_UNIX_ADDR	PDO_MYSQL_G(default_socket)
579 #endif
580 
581 /* {{{ pdo_mysql_handle_factory */
582 static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
583 {
584 	pdo_mysql_db_handle *H;
585 	size_t i;
586 	int ret = 0;
587 	char *host = NULL, *unix_socket = NULL;
588 	unsigned int port = 3306;
589 	char *dbname;
590 	struct pdo_data_src_parser vars[] = {
591 		{ "charset",  NULL,	0 },
592 		{ "dbname",   "",	0 },
593 		{ "host",     "localhost",	0 },
594 		{ "port",     "3306",	0 },
595 		{ "unix_socket",  PDO_DEFAULT_MYSQL_UNIX_ADDR,	0 },
596 		{ "user",     NULL,	0 },
597 		{ "password", NULL,	0 },
598 	};
599 	int connect_opts = 0
600 #ifdef CLIENT_MULTI_RESULTS
601 		|CLIENT_MULTI_RESULTS
602 #endif
603 		;
604 #if defined(PDO_USE_MYSQLND)
605 	size_t dbname_len = 0;
606 	size_t password_len = 0;
607 #endif
608 
609 #ifdef CLIENT_MULTI_STATEMENTS
610 	if (!driver_options) {
611 		connect_opts |= CLIENT_MULTI_STATEMENTS;
612 	} else if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MULTI_STATEMENTS, 1)) {
613 		connect_opts |= CLIENT_MULTI_STATEMENTS;
614 	}
615 #endif
616 
617 	PDO_DBG_ENTER("pdo_mysql_handle_factory");
618 	PDO_DBG_INF_FMT("dbh=%p", dbh);
619 #ifdef CLIENT_MULTI_RESULTS
620 	PDO_DBG_INF("multi results");
621 #endif
622 
623 	php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 7);
624 
625 	H = pecalloc(1, sizeof(pdo_mysql_db_handle), dbh->is_persistent);
626 
627 	H->einfo.errcode = 0;
628 	H->einfo.errmsg = NULL;
629 
630 	/* allocate an environment */
631 
632 	/* handle for the server */
633 	if (!(H->server = pdo_mysql_init(dbh->is_persistent))) {
634 		pdo_mysql_error(dbh);
635 		goto cleanup;
636 	}
637 #if defined(PDO_USE_MYSQLND)
638 	if (dbh->is_persistent) {
639 		mysqlnd_restart_psession(H->server);
640 	}
641 #endif
642 
643 	dbh->driver_data = H;
644 
645 	dbh->skip_param_evt =
646 		1 << PDO_PARAM_EVT_FREE |
647 		1 << PDO_PARAM_EVT_EXEC_POST |
648 		1 << PDO_PARAM_EVT_FETCH_PRE |
649 		1 << PDO_PARAM_EVT_FETCH_POST |
650 		1 << PDO_PARAM_EVT_NORMALIZE;
651 
652 #ifndef PDO_USE_MYSQLND
653 	H->max_buffer_size = 1024*1024;
654 #endif
655 
656 	H->assume_national_character_set_strings = 0;
657 	H->buffered = H->emulate_prepare = 1;
658 
659 	/* handle MySQL options */
660 	if (driver_options) {
661 		zend_long connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
662 		unsigned int local_infile = (unsigned int) pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_LOCAL_INFILE, 0);
663 		zend_string *init_cmd = NULL;
664 #ifndef PDO_USE_MYSQLND
665 		zend_string *default_file = NULL, *default_group = NULL;
666 #endif
667 		zend_long compress = 0;
668 		zend_string *ssl_key = NULL, *ssl_cert = NULL, *ssl_ca = NULL, *ssl_capath = NULL, *ssl_cipher = NULL;
669 		H->buffered = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_USE_BUFFERED_QUERY, 1);
670 
671 		H->emulate_prepare = pdo_attr_lval(driver_options,
672 			PDO_MYSQL_ATTR_DIRECT_QUERY, H->emulate_prepare);
673 		H->emulate_prepare = pdo_attr_lval(driver_options,
674 			PDO_ATTR_EMULATE_PREPARES, H->emulate_prepare);
675 
676 		H->assume_national_character_set_strings = pdo_attr_lval(driver_options,
677 			PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL;
678 
679 #ifndef PDO_USE_MYSQLND
680 		H->max_buffer_size = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MAX_BUFFER_SIZE, H->max_buffer_size);
681 #endif
682 
683 		if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_FOUND_ROWS, 0)) {
684 			connect_opts |= CLIENT_FOUND_ROWS;
685 		}
686 
687 		if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_IGNORE_SPACE, 0)) {
688 			connect_opts |= CLIENT_IGNORE_SPACE;
689 		}
690 
691 		if (mysql_options(H->server, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout)) {
692 			pdo_mysql_error(dbh);
693 			goto cleanup;
694 		}
695 
696 #ifndef PDO_USE_MYSQLND
697 		if (PG(open_basedir) && PG(open_basedir)[0] != '\0') {
698 			local_infile = 0;
699 		}
700 #endif
701 #if defined(MYSQL_OPT_LOCAL_INFILE) || defined(PDO_USE_MYSQLND)
702 		if (mysql_options(H->server, MYSQL_OPT_LOCAL_INFILE, (const char *)&local_infile)) {
703 			pdo_mysql_error(dbh);
704 			goto cleanup;
705 		}
706 #endif
707 #ifdef MYSQL_OPT_RECONNECT
708 		/* since 5.0.3, the default for this option is 0 if not specified.
709 		 * we want the old behaviour
710 		 * mysqlnd doesn't support reconnect, thus we don't have "|| defined(PDO_USE_MYSQLND)"
711 		*/
712 		{
713 			zend_long reconnect = 1;
714 			mysql_options(H->server, MYSQL_OPT_RECONNECT, (const char*)&reconnect);
715 		}
716 #endif
717 		init_cmd = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_INIT_COMMAND, NULL);
718 		if (init_cmd) {
719 			if (mysql_options(H->server, MYSQL_INIT_COMMAND, (const char *)ZSTR_VAL(init_cmd))) {
720 				zend_string_release_ex(init_cmd, 0);
721 				pdo_mysql_error(dbh);
722 				goto cleanup;
723 			}
724 			zend_string_release_ex(init_cmd, 0);
725 		}
726 #ifndef PDO_USE_MYSQLND
727 		default_file = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_READ_DEFAULT_FILE, NULL);
728 		if (default_file) {
729 			if (mysql_options(H->server, MYSQL_READ_DEFAULT_FILE, (const char *)ZSTR_VAL(default_file))) {
730 				zend_string_release_ex(default_file, 0);
731 				pdo_mysql_error(dbh);
732 				goto cleanup;
733 			}
734 			zend_string_release_ex(default_file, 0);
735 		}
736 
737 		default_group = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_READ_DEFAULT_GROUP, NULL);
738 		if (default_group) {
739 			if (mysql_options(H->server, MYSQL_READ_DEFAULT_GROUP, (const char *)ZSTR_VAL(default_group))) {
740 				zend_string_release_ex(default_group, 0);
741 				pdo_mysql_error(dbh);
742 				goto cleanup;
743 			}
744 			zend_string_release_ex(default_group, 0);
745 		}
746 #endif
747 		compress = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_COMPRESS, 0);
748 		if (compress) {
749 			if (mysql_options(H->server, MYSQL_OPT_COMPRESS, 0)) {
750 				pdo_mysql_error(dbh);
751 				goto cleanup;
752 			}
753 		}
754 
755 		ssl_key = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_KEY, NULL);
756 		ssl_cert = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CERT, NULL);
757 		ssl_ca = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CA, NULL);
758 		ssl_capath = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CAPATH, NULL);
759 		ssl_cipher = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CIPHER, NULL);
760 
761 		if (ssl_key || ssl_cert || ssl_ca || ssl_capath || ssl_cipher) {
762 			mysql_ssl_set(H->server,
763 					ssl_key? ZSTR_VAL(ssl_key) : NULL,
764 					ssl_cert? ZSTR_VAL(ssl_cert) : NULL,
765 					ssl_ca? ZSTR_VAL(ssl_ca) : NULL,
766 					ssl_capath? ZSTR_VAL(ssl_capath) : NULL,
767 					ssl_cipher? ZSTR_VAL(ssl_cipher) : NULL);
768 			if (ssl_key) {
769 				zend_string_release_ex(ssl_key, 0);
770 			}
771 			if (ssl_cert) {
772 				zend_string_release_ex(ssl_cert, 0);
773 			}
774 			if (ssl_ca) {
775 				zend_string_release_ex(ssl_ca, 0);
776 			}
777 			if (ssl_capath) {
778 				zend_string_release_ex(ssl_capath, 0);
779 			}
780 			if (ssl_cipher) {
781 				zend_string_release_ex(ssl_cipher, 0);
782 			}
783 		}
784 
785 #if MYSQL_VERSION_ID > 50605 || defined(PDO_USE_MYSQLND)
786 		{
787 			zend_string *public_key = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SERVER_PUBLIC_KEY, NULL);
788 			if (public_key) {
789 				if (mysql_options(H->server, MYSQL_SERVER_PUBLIC_KEY, ZSTR_VAL(public_key))) {
790 					pdo_mysql_error(dbh);
791 					zend_string_release_ex(public_key, 0);
792 					goto cleanup;
793 				}
794 				zend_string_release_ex(public_key, 0);
795 			}
796 		}
797 #endif
798 
799 #ifdef PDO_USE_MYSQLND
800 		{
801 			zend_long ssl_verify_cert = pdo_attr_lval(driver_options,
802 					PDO_MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, -1);
803 			if (ssl_verify_cert != -1) {
804 				connect_opts |= ssl_verify_cert ?
805 					CLIENT_SSL_VERIFY_SERVER_CERT:
806 					CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
807 			}
808 		}
809 #endif
810 	} else {
811 #if defined(MYSQL_OPT_LOCAL_INFILE) || defined(PDO_USE_MYSQLND)
812 		// in case there are no driver options disable 'local infile' explicitly
813 		unsigned int local_infile = 0;
814 		if (mysql_options(H->server, MYSQL_OPT_LOCAL_INFILE, (const char *)&local_infile)) {
815 			pdo_mysql_error(dbh);
816 			goto cleanup;
817 		}
818 #endif
819 	}
820 
821 #ifdef PDO_MYSQL_HAS_CHARSET
822 	if (vars[0].optval && mysql_options(H->server, MYSQL_SET_CHARSET_NAME, vars[0].optval)) {
823 		pdo_mysql_error(dbh);
824 		goto cleanup;
825 	}
826 #endif
827 
828 	dbname = vars[1].optval;
829 	host = vars[2].optval;
830 	if(vars[3].optval) {
831 		port = atoi(vars[3].optval);
832 	}
833 
834 #ifdef PHP_WIN32
835 	if (vars[2].optval && !strcmp(".", vars[2].optval)) {
836 #else
837 	if (vars[2].optval && !strcmp("localhost", vars[2].optval)) {
838 #endif
839 		unix_socket = vars[4].optval;
840 	}
841 
842 	if (!dbh->username && vars[5].optval) {
843 		dbh->username = pestrdup(vars[5].optval, dbh->is_persistent);
844 	}
845 
846 	if (!dbh->password && vars[6].optval) {
847 		dbh->password = pestrdup(vars[6].optval, dbh->is_persistent);
848 	}
849 
850 	/* TODO: - Check zval cache + ZTS */
851 #ifdef PDO_USE_MYSQLND
852 	if (dbname) {
853 		dbname_len = strlen(dbname);
854 	}
855 
856 	if (dbh->password) {
857 		password_len = strlen(dbh->password);
858 	}
859 
860 	if (mysqlnd_connect(H->server, host, dbh->username, dbh->password, password_len, dbname, dbname_len,
861 						port, unix_socket, connect_opts, MYSQLND_CLIENT_NO_FLAG) == NULL) {
862 #else
863 	if (mysql_real_connect(H->server, host, dbh->username, dbh->password, dbname, port, unix_socket, connect_opts) == NULL) {
864 #endif
865 		pdo_mysql_error(dbh);
866 		goto cleanup;
867 	}
868 
869 	if (!dbh->auto_commit) {
870 		mysql_handle_autocommit(dbh);
871 	}
872 
873 	H->attached = 1;
874 
875 	dbh->alloc_own_columns = 1;
876 	dbh->max_escaped_char_length = 2;
877 	dbh->methods = &mysql_methods;
878 
879 	ret = 1;
880 
881 cleanup:
882 	for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
883 		if (vars[i].freeme) {
884 			efree(vars[i].optval);
885 		}
886 	}
887 
888 	dbh->methods = &mysql_methods;
889 
890 	PDO_DBG_RETURN(ret);
891 }
892 /* }}} */
893 
894 const pdo_driver_t pdo_mysql_driver = {
895 	PDO_DRIVER_HEADER(mysql),
896 	pdo_mysql_handle_factory
897 };
898