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