xref: /php-src/ext/pdo_mysql/mysql_driver.c (revision 11accb5c)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | 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 "ext/pdo/php_pdo.h"
27 #include "ext/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 void 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_VOID_RETURN;
144 }
145 /* }}} */
146 
147 /* {{{ mysql_handle_closer */
mysql_handle_closer(pdo_dbh_t * dbh)148 static void 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 			H->server = NULL;
158 		}
159 		if (H->einfo.errmsg) {
160 			pefree(H->einfo.errmsg, dbh->is_persistent);
161 			H->einfo.errmsg = NULL;
162 		}
163 		pefree(H, dbh->is_persistent);
164 		dbh->driver_data = NULL;
165 	}
166 }
167 /* }}} */
168 
169 /* {{{ mysql_handle_preparer */
mysql_handle_preparer(pdo_dbh_t * dbh,zend_string * sql,pdo_stmt_t * stmt,zval * driver_options)170 static bool mysql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
171 {
172 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
173 	pdo_mysql_stmt *S = ecalloc(1, sizeof(pdo_mysql_stmt));
174 	zend_string *nsql = NULL;
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) ZSTR_LEN(sql), ZSTR_VAL(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, sql, &nsql);
196 
197 	if (ret == 1) {
198 		/* query was rewritten */
199 		sql = nsql;
200 	} else if (ret == -1) {
201 		/* failed to parse */
202 		strcpy(dbh->error_code, stmt->error_code);
203 		PDO_DBG_RETURN(false);
204 	}
205 
206 	if (!(S->stmt = mysql_stmt_init(H->server))) {
207 		pdo_mysql_error(dbh);
208 		if (nsql) {
209 			zend_string_release(nsql);
210 		}
211 		PDO_DBG_RETURN(false);
212 	}
213 
214 	if (mysql_stmt_prepare(S->stmt, ZSTR_VAL(sql), ZSTR_LEN(sql))) {
215 		if (nsql) {
216 			zend_string_release(nsql);
217 		}
218 		/* TODO: might need to pull statement specific info here? */
219 		/* if the query isn't supported by the protocol, fallback to emulation */
220 		if (mysql_errno(H->server) == 1295) {
221 			mysql_stmt_close(S->stmt);
222 			S->stmt = NULL;
223 			goto fallback;
224 		}
225 		pdo_mysql_error(dbh);
226 		PDO_DBG_RETURN(false);
227 	}
228 	if (nsql) {
229 		zend_string_release(nsql);
230 	}
231 
232 	S->num_params = mysql_stmt_param_count(S->stmt);
233 
234 	if (S->num_params) {
235 #ifdef PDO_USE_MYSQLND
236 		S->params = NULL;
237 #else
238 		S->params = ecalloc(S->num_params, sizeof(MYSQL_BIND));
239 		S->in_null = ecalloc(S->num_params, sizeof(my_bool));
240 		S->in_length = ecalloc(S->num_params, sizeof(zend_ulong));
241 #endif
242 	}
243 	dbh->alloc_own_columns = 1;
244 
245 	S->max_length = pdo_attr_lval(driver_options, PDO_ATTR_MAX_COLUMN_LEN, 0);
246 
247 	PDO_DBG_RETURN(true);
248 
249 fallback:
250 end:
251 	stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
252 
253 	PDO_DBG_RETURN(true);
254 }
255 /* }}} */
256 
257 /* {{{ mysql_handle_doer */
mysql_handle_doer(pdo_dbh_t * dbh,const zend_string * sql)258 static zend_long mysql_handle_doer(pdo_dbh_t *dbh, const zend_string *sql)
259 {
260 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
261 	PDO_DBG_ENTER("mysql_handle_doer");
262 	PDO_DBG_INF_FMT("dbh=%p", dbh);
263 	PDO_DBG_INF_FMT("sql=%.*s", (int)ZSTR_LEN(sql), ZSTR_VAL(sql));
264 
265 	if (mysql_real_query(H->server, ZSTR_VAL(sql), ZSTR_LEN(sql))) {
266 		pdo_mysql_error(dbh);
267 		PDO_DBG_RETURN(-1);
268 	} else {
269 		my_ulonglong c = mysql_affected_rows(H->server);
270 		if (c == (my_ulonglong) -1) {
271 			pdo_mysql_error(dbh);
272 			PDO_DBG_RETURN(H->einfo.errcode ? -1 : 0);
273 		} else {
274 
275 			/* MULTI_QUERY support - eat up all unfetched result sets */
276 			MYSQL_RES* result;
277 			while (mysql_more_results(H->server)) {
278 				if (mysql_next_result(H->server)) {
279 					pdo_mysql_error(dbh);
280 					PDO_DBG_RETURN(-1);
281 				}
282 				result = mysql_store_result(H->server);
283 				if (result) {
284 					mysql_free_result(result);
285 				}
286 			}
287 			PDO_DBG_RETURN((int)c);
288 		}
289 	}
290 }
291 /* }}} */
292 
293 /* {{{ pdo_mysql_last_insert_id */
pdo_mysql_last_insert_id(pdo_dbh_t * dbh,const zend_string * name)294 static zend_string *pdo_mysql_last_insert_id(pdo_dbh_t *dbh, const zend_string *name)
295 {
296 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
297 	PDO_DBG_ENTER("pdo_mysql_last_insert_id");
298 	PDO_DBG_RETURN(zend_u64_to_str(mysql_insert_id(H->server)));
299 }
300 /* }}} */
301 
302 #if defined(PDO_USE_MYSQLND) || MYSQL_VERSION_ID < 50707 || defined(MARIADB_BASE_VERSION)
303 # define mysql_real_escape_string_quote(mysql, to, from, length, quote) \
304 	mysql_real_escape_string(mysql, to, from, length)
305 #endif
306 
307 /* {{{ mysql_handle_quoter */
mysql_handle_quoter(pdo_dbh_t * dbh,const zend_string * unquoted,enum pdo_param_type paramtype)308 static zend_string* mysql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype )
309 {
310 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
311 	bool use_national_character_set = 0;
312 	size_t quotedlen;
313 
314 	if (H->assume_national_character_set_strings) {
315 		use_national_character_set = 1;
316 	}
317 	if ((paramtype & PDO_PARAM_STR_NATL) == PDO_PARAM_STR_NATL) {
318 		use_national_character_set = 1;
319 	}
320 	if ((paramtype & PDO_PARAM_STR_CHAR) == PDO_PARAM_STR_CHAR) {
321 		use_national_character_set = 0;
322 	}
323 
324 	PDO_DBG_ENTER("mysql_handle_quoter");
325 	PDO_DBG_INF_FMT("dbh=%p", dbh);
326 	PDO_DBG_INF_FMT("unquoted=%.*s", (int)ZSTR_LEN(unquoted), ZSTR_VAL(unquoted));
327 
328 	zend_string *quoted_str = zend_string_safe_alloc(2, ZSTR_LEN(unquoted), 3 + (use_national_character_set ? 1 : 0), false);
329 	char *quoted = ZSTR_VAL(quoted_str);
330 
331 	if (use_national_character_set) {
332 		quotedlen = mysql_real_escape_string_quote(H->server, quoted + 2, ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), '\'');
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, ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), '\'');
339 		quoted[0] = '\'';
340 	}
341 
342 	quoted[++quotedlen] = '\'';
343 	quoted[++quotedlen] = '\0';
344 	PDO_DBG_INF_FMT("quoted=%.*s", (int)quotedlen, quoted);
345 
346 	quoted_str = zend_string_truncate(quoted_str, quotedlen, false);
347 
348 	PDO_DBG_RETURN(quoted_str);
349 }
350 /* }}} */
351 
352 /* {{{ mysql_handle_begin */
mysql_handle_begin(pdo_dbh_t * dbh)353 static bool mysql_handle_begin(pdo_dbh_t *dbh)
354 {
355 	zend_long return_value;
356 	zend_string *command;
357 
358 	PDO_DBG_ENTER("mysql_handle_begin");
359 	PDO_DBG_INF_FMT("dbh=%p", dbh);
360 
361 	command = ZSTR_INIT_LITERAL("START TRANSACTION", 0);
362 	return_value = mysql_handle_doer(dbh, command);
363 	zend_string_release_ex(command, 0);
364 	PDO_DBG_RETURN(0 <= return_value);
365 }
366 /* }}} */
367 
368 /* {{{ mysql_handle_commit */
mysql_handle_commit(pdo_dbh_t * dbh)369 static bool mysql_handle_commit(pdo_dbh_t *dbh)
370 {
371 	PDO_DBG_ENTER("mysql_handle_commit");
372 	PDO_DBG_INF_FMT("dbh=%p", dbh);
373 	if (mysql_commit(((pdo_mysql_db_handle *)dbh->driver_data)->server)) {
374 		pdo_mysql_error(dbh);
375 		PDO_DBG_RETURN(false);
376 	}
377 	PDO_DBG_RETURN(true);
378 }
379 /* }}} */
380 
381 /* {{{ mysql_handle_rollback */
mysql_handle_rollback(pdo_dbh_t * dbh)382 static bool mysql_handle_rollback(pdo_dbh_t *dbh)
383 {
384 	PDO_DBG_ENTER("mysql_handle_rollback");
385 	PDO_DBG_INF_FMT("dbh=%p", dbh);
386 	if (mysql_rollback(((pdo_mysql_db_handle *)dbh->driver_data)->server)) {
387 		pdo_mysql_error(dbh);
388 		PDO_DBG_RETURN(false);
389 	}
390 	PDO_DBG_RETURN(true);
391 }
392 /* }}} */
393 
394 /* {{{ mysql_handle_autocommit */
mysql_handle_autocommit(pdo_dbh_t * dbh)395 static inline int mysql_handle_autocommit(pdo_dbh_t *dbh)
396 {
397 	PDO_DBG_ENTER("mysql_handle_autocommit");
398 	PDO_DBG_INF_FMT("dbh=%p", dbh);
399 	PDO_DBG_INF_FMT("dbh->autocommit=%d", dbh->auto_commit);
400 	if (mysql_autocommit(((pdo_mysql_db_handle *)dbh->driver_data)->server, dbh->auto_commit)) {
401 		pdo_mysql_error(dbh);
402 		PDO_DBG_RETURN(0);
403 	}
404 	PDO_DBG_RETURN(1);
405 }
406 /* }}} */
407 
408 /* {{{ pdo_mysql_set_attribute */
pdo_mysql_set_attribute(pdo_dbh_t * dbh,zend_long attr,zval * val)409 static bool pdo_mysql_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)
410 {
411 	zend_long lval;
412 	bool bval;
413 	PDO_DBG_ENTER("pdo_mysql_set_attribute");
414 	PDO_DBG_INF_FMT("dbh=%p", dbh);
415 	PDO_DBG_INF_FMT("attr=" ZEND_LONG_FMT, attr);
416 
417 	switch (attr) {
418 		case PDO_ATTR_AUTOCOMMIT:
419 			if (!pdo_get_bool_param(&bval, val)) {
420 				PDO_DBG_RETURN(false);
421 			}
422 			/* ignore if the new value equals the old one */
423 			if (dbh->auto_commit ^ bval) {
424 				dbh->auto_commit = bval;
425 				if (!mysql_handle_autocommit(dbh)) {
426 					PDO_DBG_RETURN(false);
427 				}
428 			}
429 			PDO_DBG_RETURN(true);
430 
431 		case PDO_ATTR_DEFAULT_STR_PARAM:
432 			if (!pdo_get_long_param(&lval, val)) {
433 				PDO_DBG_RETURN(false);
434 			}
435 			((pdo_mysql_db_handle *)dbh->driver_data)->assume_national_character_set_strings = lval == PDO_PARAM_STR_NATL;
436 			PDO_DBG_RETURN(true);
437 
438 		case PDO_MYSQL_ATTR_USE_BUFFERED_QUERY:
439 			if (!pdo_get_bool_param(&bval, val)) {
440 				PDO_DBG_RETURN(false);
441 			}
442 			/* ignore if the new value equals the old one */
443 			((pdo_mysql_db_handle *)dbh->driver_data)->buffered = bval;
444 			PDO_DBG_RETURN(true);
445 
446 		case PDO_MYSQL_ATTR_DIRECT_QUERY:
447 		case PDO_ATTR_EMULATE_PREPARES:
448 			if (!pdo_get_bool_param(&bval, val)) {
449 				PDO_DBG_RETURN(false);
450 			}
451 			/* ignore if the new value equals the old one */
452 			((pdo_mysql_db_handle *)dbh->driver_data)->emulate_prepare = bval;
453 			PDO_DBG_RETURN(true);
454 
455 		case PDO_ATTR_FETCH_TABLE_NAMES:
456 			if (!pdo_get_bool_param(&bval, val)) {
457 				PDO_DBG_RETURN(false);
458 			}
459 			((pdo_mysql_db_handle *)dbh->driver_data)->fetch_table_names = bval;
460 			PDO_DBG_RETURN(true);
461 
462 #ifdef PDO_USE_MYSQLND
463 		case PDO_ATTR_STRINGIFY_FETCHES:
464 			if (!pdo_get_bool_param(&bval, val)) {
465 				PDO_DBG_RETURN(false);
466 			}
467 			unsigned int int_and_float_native = !bval;
468 			pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
469 			if (mysql_options(H->server, MYSQLND_OPT_INT_AND_FLOAT_NATIVE, (const char *) &int_and_float_native)) {
470 				pdo_mysql_error(dbh);
471 				PDO_DBG_RETURN(false);
472 			}
473 			PDO_DBG_RETURN(true);
474 #else
475 		case PDO_MYSQL_ATTR_MAX_BUFFER_SIZE:
476 			if (!pdo_get_long_param(&lval, val)) {
477 				PDO_DBG_RETURN(false);
478 			}
479 			if (lval < 0) {
480 				/* TODO: Johannes, can we throw a warning here? */
481 				((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size = 1024*1024;
482 				PDO_DBG_INF_FMT("Adjusting invalid buffer size to =%l", ((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size);
483 			} else {
484 				((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size = lval;
485 			}
486 			PDO_DBG_RETURN(true);
487 			break;
488 #endif
489 
490 		default:
491 			PDO_DBG_RETURN(false);
492 	}
493 }
494 /* }}} */
495 
496 /* {{{ pdo_mysql_get_attribute */
pdo_mysql_get_attribute(pdo_dbh_t * dbh,zend_long attr,zval * return_value)497 static int pdo_mysql_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
498 {
499 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
500 
501 	PDO_DBG_ENTER("pdo_mysql_get_attribute");
502 	PDO_DBG_INF_FMT("dbh=%p", dbh);
503 	PDO_DBG_INF_FMT("attr=" ZEND_LONG_FMT, attr);
504 	switch (attr) {
505 		case PDO_ATTR_CLIENT_VERSION:
506 			ZVAL_STRING(return_value, (char *)mysql_get_client_info());
507 			break;
508 
509 		case PDO_ATTR_SERVER_VERSION:
510 			ZVAL_STRING(return_value, (char *)mysql_get_server_info(H->server));
511 			break;
512 
513 		case PDO_ATTR_CONNECTION_STATUS:
514 			ZVAL_STRING(return_value, (char *)mysql_get_host_info(H->server));
515 			break;
516 		case PDO_ATTR_SERVER_INFO: {
517 #ifdef PDO_USE_MYSQLND
518 			zend_string *tmp;
519 
520 			if (mysqlnd_stat(H->server, &tmp) == PASS) {
521 				ZVAL_STR(return_value, tmp);
522 #else
523 			char *tmp;
524 			if ((tmp = (char *)mysql_stat(H->server))) {
525 				ZVAL_STRING(return_value, tmp);
526 #endif
527 			} else {
528 				pdo_mysql_error(dbh);
529 				PDO_DBG_RETURN(-1);
530 			}
531 		}
532 			break;
533 
534 		case PDO_ATTR_AUTOCOMMIT:
535 			ZVAL_BOOL(return_value, dbh->auto_commit);
536 			break;
537 
538 		case PDO_ATTR_DEFAULT_STR_PARAM:
539 			ZVAL_LONG(return_value, H->assume_national_character_set_strings ? PDO_PARAM_STR_NATL : PDO_PARAM_STR_CHAR);
540 			break;
541 
542 		case PDO_MYSQL_ATTR_USE_BUFFERED_QUERY:
543 			ZVAL_BOOL(return_value, H->buffered);
544 			break;
545 
546 		case PDO_ATTR_EMULATE_PREPARES:
547 		case PDO_MYSQL_ATTR_DIRECT_QUERY:
548 			ZVAL_BOOL(return_value, H->emulate_prepare);
549 			break;
550 
551 #ifndef PDO_USE_MYSQLND
552 		case PDO_MYSQL_ATTR_MAX_BUFFER_SIZE:
553 			ZVAL_LONG(return_value, H->max_buffer_size);
554 			break;
555 #endif
556 
557 		case PDO_MYSQL_ATTR_LOCAL_INFILE:
558 			ZVAL_BOOL(return_value, H->local_infile);
559 			break;
560 
561 #if (MYSQL_VERSION_ID >= 80021 && !defined(MARIADB_BASE_VERSION)) || defined(PDO_USE_MYSQLND)
562 		case PDO_MYSQL_ATTR_LOCAL_INFILE_DIRECTORY:
563 		{
564 			const char* local_infile_directory = NULL;
565 #ifdef PDO_USE_MYSQLND
566 			local_infile_directory = H->server->data->options->local_infile_directory;
567 #else
568 			mysql_get_option(H->server, MYSQL_OPT_LOAD_DATA_LOCAL_DIR, &local_infile_directory);
569 #endif
570 			if (local_infile_directory) {
571 				ZVAL_STRING(return_value, local_infile_directory);
572 			} else {
573 				ZVAL_NULL(return_value);
574 			}
575 			break;
576 		}
577 #endif
578 
579 		case PDO_ATTR_FETCH_TABLE_NAMES:
580 			ZVAL_BOOL(return_value, H->fetch_table_names);
581 			break;
582 
583 		default:
584 			PDO_DBG_RETURN(0);
585 	}
586 
587 	PDO_DBG_RETURN(1);
588 }
589 /* }}} */
590 
591 /* {{{ pdo_mysql_check_liveness */
592 static zend_result pdo_mysql_check_liveness(pdo_dbh_t *dbh)
593 {
594 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
595 
596 	PDO_DBG_ENTER("pdo_mysql_check_liveness");
597 	PDO_DBG_INF_FMT("dbh=%p", dbh);
598 
599 	if (mysql_ping(H->server)) {
600 		PDO_DBG_RETURN(FAILURE);
601 	}
602 	PDO_DBG_RETURN(SUCCESS);
603 }
604 /* }}} */
605 
606 /* {{{ pdo_mysql_request_shutdown */
607 static void pdo_mysql_request_shutdown(pdo_dbh_t *dbh)
608 {
609 	PDO_DBG_ENTER("pdo_mysql_request_shutdown");
610 	PDO_DBG_INF_FMT("dbh=%p", dbh);
611 
612 #ifdef PDO_USE_MYSQLND
613 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
614 	if (H->server) {
615 		mysqlnd_end_psession(H->server);
616 	}
617 #endif
618 }
619 /* }}} */
620 
621 #ifdef PDO_USE_MYSQLND
622 # define pdo_mysql_get_server_status(m) mysqlnd_get_server_status(m)
623 #else
624 # define pdo_mysql_get_server_status(m) (m)->server_status
625 #endif
626 
627 /* {{{ pdo_mysql_in_transaction */
628 static bool pdo_mysql_in_transaction(pdo_dbh_t *dbh)
629 {
630 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
631 	PDO_DBG_ENTER("pdo_mysql_in_transaction");
632 	PDO_DBG_RETURN((pdo_mysql_get_server_status(H->server) & SERVER_STATUS_IN_TRANS) != 0);
633 }
634 /* }}} */
635 
636 /* {{{ mysql_methods */
637 static const struct pdo_dbh_methods mysql_methods = {
638 	mysql_handle_closer,
639 	mysql_handle_preparer,
640 	mysql_handle_doer,
641 	mysql_handle_quoter,
642 	mysql_handle_begin,
643 	mysql_handle_commit,
644 	mysql_handle_rollback,
645 	pdo_mysql_set_attribute,
646 	pdo_mysql_last_insert_id,
647 	pdo_mysql_fetch_error_func,
648 	pdo_mysql_get_attribute,
649 	pdo_mysql_check_liveness,
650 	NULL,
651 	pdo_mysql_request_shutdown,
652 	pdo_mysql_in_transaction,
653 	NULL, /* get_gc */
654     pdo_mysql_scanner
655 };
656 /* }}} */
657 
658 #ifdef PHP_WIN32
659 # define PDO_DEFAULT_MYSQL_UNIX_ADDR	NULL
660 #else
661 # define PDO_DEFAULT_MYSQL_UNIX_ADDR	PDO_MYSQL_G(default_socket)
662 #endif
663 
664 /* {{{ pdo_mysql_handle_factory */
665 static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
666 {
667 	pdo_mysql_db_handle *H;
668 	size_t i;
669 	int ret = 0;
670 	char *host = NULL, *unix_socket = NULL;
671 	unsigned int port = 3306;
672 	char *dbname;
673 	struct pdo_data_src_parser vars[] = {
674 		{ "charset",  NULL,	0 },
675 		{ "dbname",   "",	0 },
676 		{ "host",     "localhost",	0 },
677 		{ "port",     "3306",	0 },
678 		{ "unix_socket",  PDO_DEFAULT_MYSQL_UNIX_ADDR,	0 },
679 		{ "user",     NULL,	0 },
680 		{ "password", NULL,	0 },
681 	};
682 	int connect_opts = 0
683 #ifdef CLIENT_MULTI_RESULTS
684 		|CLIENT_MULTI_RESULTS
685 #endif
686 		;
687 #ifdef PDO_USE_MYSQLND
688 	size_t dbname_len = 0;
689 	size_t password_len = 0;
690 #endif
691 
692 #ifdef CLIENT_MULTI_STATEMENTS
693 	if (!driver_options) {
694 		connect_opts |= CLIENT_MULTI_STATEMENTS;
695 	} else if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MULTI_STATEMENTS, 1)) {
696 		connect_opts |= CLIENT_MULTI_STATEMENTS;
697 	}
698 #endif
699 
700 	PDO_DBG_ENTER("pdo_mysql_handle_factory");
701 	PDO_DBG_INF_FMT("dbh=%p", dbh);
702 #ifdef CLIENT_MULTI_RESULTS
703 	PDO_DBG_INF("multi results");
704 #endif
705 
706 	php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 7);
707 
708 	H = pecalloc(1, sizeof(pdo_mysql_db_handle), dbh->is_persistent);
709 
710 	H->einfo.errcode = 0;
711 	H->einfo.errmsg = NULL;
712 
713 	/* allocate an environment */
714 
715 	/* handle for the server */
716 	if (!(H->server = pdo_mysql_init(dbh->is_persistent))) {
717 		pdo_mysql_error(dbh);
718 		goto cleanup;
719 	}
720 #ifdef PDO_USE_MYSQLND
721 	if (dbh->is_persistent) {
722 		mysqlnd_restart_psession(H->server);
723 	}
724 #endif
725 
726 	dbh->driver_data = H;
727 
728 	dbh->skip_param_evt =
729 		1 << PDO_PARAM_EVT_FREE |
730 		1 << PDO_PARAM_EVT_EXEC_POST |
731 		1 << PDO_PARAM_EVT_FETCH_PRE |
732 		1 << PDO_PARAM_EVT_FETCH_POST |
733 		1 << PDO_PARAM_EVT_NORMALIZE;
734 
735 #ifndef PDO_USE_MYSQLND
736 	H->max_buffer_size = 1024*1024;
737 #endif
738 
739 	H->assume_national_character_set_strings = 0;
740 	H->buffered = H->emulate_prepare = 1;
741 
742 	/* handle MySQL options */
743 	if (driver_options) {
744 		zend_long connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
745 		zend_string *init_cmd = NULL;
746 #ifndef PDO_USE_MYSQLND
747 		zend_string *default_file = NULL, *default_group = NULL;
748 #endif
749 		zend_long compress = 0;
750 		zend_string *ssl_key = NULL, *ssl_cert = NULL, *ssl_ca = NULL, *ssl_capath = NULL, *ssl_cipher = NULL;
751 		H->buffered = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_USE_BUFFERED_QUERY, 1);
752 
753 		H->emulate_prepare = pdo_attr_lval(driver_options,
754 			PDO_MYSQL_ATTR_DIRECT_QUERY, H->emulate_prepare);
755 		H->emulate_prepare = pdo_attr_lval(driver_options,
756 			PDO_ATTR_EMULATE_PREPARES, H->emulate_prepare);
757 
758 		H->assume_national_character_set_strings = pdo_attr_lval(driver_options,
759 			PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL;
760 
761 #ifndef PDO_USE_MYSQLND
762 		H->max_buffer_size = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MAX_BUFFER_SIZE, H->max_buffer_size);
763 #endif
764 
765 		if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_FOUND_ROWS, 0)) {
766 			connect_opts |= CLIENT_FOUND_ROWS;
767 		}
768 
769 		if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_IGNORE_SPACE, 0)) {
770 			connect_opts |= CLIENT_IGNORE_SPACE;
771 		}
772 
773 		if (mysql_options(H->server, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout)) {
774 			pdo_mysql_error(dbh);
775 			goto cleanup;
776 		}
777 
778 		if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_LOCAL_INFILE, 0)) {
779 			H->local_infile = 1;
780 #ifndef PDO_USE_MYSQLND
781 			if (PG(open_basedir) && PG(open_basedir)[0] != '\0') {
782 				H->local_infile = 0;
783 			}
784 #endif
785 		}
786 
787 #if (MYSQL_VERSION_ID >= 80021 && !defined(MARIADB_BASE_VERSION)) || defined(PDO_USE_MYSQLND)
788 		zend_string *local_infile_directory = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_LOCAL_INFILE_DIRECTORY, NULL);
789 		if (local_infile_directory && !php_check_open_basedir(ZSTR_VAL(local_infile_directory))) {
790 			if (mysql_options(H->server, MYSQL_OPT_LOAD_DATA_LOCAL_DIR, (const char *)ZSTR_VAL(local_infile_directory))) {
791 				zend_string_release(local_infile_directory);
792 				pdo_mysql_error(dbh);
793 				goto cleanup;
794 			}
795 			zend_string_release(local_infile_directory);
796 		}
797 #endif
798 #ifdef MYSQL_OPT_RECONNECT
799 		/* since 5.0.3, the default for this option is 0 if not specified.
800 		 * we want the old behaviour
801 		 * mysqlnd doesn't support reconnect, thus we don't have "|| defined(PDO_USE_MYSQLND)"
802 		*/
803 		{
804 			zend_long reconnect = 1;
805 			mysql_options(H->server, MYSQL_OPT_RECONNECT, (const char*)&reconnect);
806 		}
807 #endif
808 		init_cmd = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_INIT_COMMAND, NULL);
809 		if (init_cmd) {
810 			if (mysql_options(H->server, MYSQL_INIT_COMMAND, (const char *)ZSTR_VAL(init_cmd))) {
811 				zend_string_release_ex(init_cmd, 0);
812 				pdo_mysql_error(dbh);
813 				goto cleanup;
814 			}
815 			zend_string_release_ex(init_cmd, 0);
816 		}
817 #ifndef PDO_USE_MYSQLND
818 		default_file = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_READ_DEFAULT_FILE, NULL);
819 		if (default_file) {
820 			if (mysql_options(H->server, MYSQL_READ_DEFAULT_FILE, (const char *)ZSTR_VAL(default_file))) {
821 				zend_string_release_ex(default_file, 0);
822 				pdo_mysql_error(dbh);
823 				goto cleanup;
824 			}
825 			zend_string_release_ex(default_file, 0);
826 		}
827 
828 		default_group = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_READ_DEFAULT_GROUP, NULL);
829 		if (default_group) {
830 			if (mysql_options(H->server, MYSQL_READ_DEFAULT_GROUP, (const char *)ZSTR_VAL(default_group))) {
831 				zend_string_release_ex(default_group, 0);
832 				pdo_mysql_error(dbh);
833 				goto cleanup;
834 			}
835 			zend_string_release_ex(default_group, 0);
836 		}
837 #endif
838 		compress = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_COMPRESS, 0);
839 		if (compress) {
840 			if (mysql_options(H->server, MYSQL_OPT_COMPRESS, 0)) {
841 				pdo_mysql_error(dbh);
842 				goto cleanup;
843 			}
844 		}
845 
846 		ssl_key = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_KEY, NULL);
847 		ssl_cert = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CERT, NULL);
848 		ssl_ca = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CA, NULL);
849 		ssl_capath = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CAPATH, NULL);
850 		ssl_cipher = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CIPHER, NULL);
851 
852 		if (ssl_key || ssl_cert || ssl_ca || ssl_capath || ssl_cipher) {
853 			mysql_ssl_set(H->server,
854 					ssl_key? ZSTR_VAL(ssl_key) : NULL,
855 					ssl_cert? ZSTR_VAL(ssl_cert) : NULL,
856 					ssl_ca? ZSTR_VAL(ssl_ca) : NULL,
857 					ssl_capath? ZSTR_VAL(ssl_capath) : NULL,
858 					ssl_cipher? ZSTR_VAL(ssl_cipher) : NULL);
859 			if (ssl_key) {
860 				zend_string_release_ex(ssl_key, 0);
861 			}
862 			if (ssl_cert) {
863 				zend_string_release_ex(ssl_cert, 0);
864 			}
865 			if (ssl_ca) {
866 				zend_string_release_ex(ssl_ca, 0);
867 			}
868 			if (ssl_capath) {
869 				zend_string_release_ex(ssl_capath, 0);
870 			}
871 			if (ssl_cipher) {
872 				zend_string_release_ex(ssl_cipher, 0);
873 			}
874 		}
875 
876 #if MYSQL_VERSION_ID > 50605 || defined(PDO_USE_MYSQLND)
877 		{
878 			zend_string *public_key = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SERVER_PUBLIC_KEY, NULL);
879 			if (public_key) {
880 				if (mysql_options(H->server, MYSQL_SERVER_PUBLIC_KEY, ZSTR_VAL(public_key))) {
881 					pdo_mysql_error(dbh);
882 					zend_string_release_ex(public_key, 0);
883 					goto cleanup;
884 				}
885 				zend_string_release_ex(public_key, 0);
886 			}
887 		}
888 #endif
889 
890 #ifdef PDO_USE_MYSQLND
891 		{
892 			zend_long ssl_verify_cert = pdo_attr_lval(driver_options,
893 					PDO_MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, -1);
894 			if (ssl_verify_cert != -1) {
895 				connect_opts |= ssl_verify_cert ?
896 					CLIENT_SSL_VERIFY_SERVER_CERT:
897 					CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
898 			}
899 		}
900 #endif
901 	}
902 
903 	/* Always explicitly set the LOCAL_INFILE option. */
904 	unsigned int local_infile = H->local_infile;
905 	if (mysql_options(H->server, MYSQL_OPT_LOCAL_INFILE, (const char *)&local_infile)) {
906 		pdo_mysql_error(dbh);
907 		goto cleanup;
908 	}
909 
910 #ifdef PDO_USE_MYSQLND
911 	unsigned int int_and_float_native = !pdo_attr_lval(driver_options, PDO_ATTR_STRINGIFY_FETCHES, dbh->stringify);
912 	if (mysql_options(H->server, MYSQLND_OPT_INT_AND_FLOAT_NATIVE, (const char *) &int_and_float_native)) {
913 		pdo_mysql_error(dbh);
914 		goto cleanup;
915 	}
916 #endif
917 
918 	if (vars[0].optval && mysql_options(H->server, MYSQL_SET_CHARSET_NAME, vars[0].optval)) {
919 		pdo_mysql_error(dbh);
920 		goto cleanup;
921 	}
922 
923 	dbname = vars[1].optval;
924 	host = vars[2].optval;
925 	if(vars[3].optval) {
926 		port = atoi(vars[3].optval);
927 	}
928 
929 #ifdef PHP_WIN32
930 	if (vars[2].optval && !strcmp(".", vars[2].optval)) {
931 #else
932 	if (vars[2].optval && !strcmp("localhost", vars[2].optval)) {
933 #endif
934 		unix_socket = vars[4].optval;
935 	}
936 
937 	if (!dbh->username && vars[5].optval) {
938 		dbh->username = pestrdup(vars[5].optval, dbh->is_persistent);
939 	}
940 
941 	if (!dbh->password && vars[6].optval) {
942 		dbh->password = pestrdup(vars[6].optval, dbh->is_persistent);
943 	}
944 
945 	/* TODO: - Check zval cache + ZTS */
946 #ifdef PDO_USE_MYSQLND
947 	if (dbname) {
948 		dbname_len = strlen(dbname);
949 	}
950 
951 	if (dbh->password) {
952 		password_len = strlen(dbh->password);
953 	}
954 
955 	if (mysqlnd_connect(H->server, host, dbh->username, dbh->password, password_len, dbname, dbname_len,
956 						port, unix_socket, connect_opts, MYSQLND_CLIENT_NO_FLAG) == NULL) {
957 #else
958 	if (mysql_real_connect(H->server, host, dbh->username, dbh->password, dbname, port, unix_socket, connect_opts) == NULL) {
959 #endif
960 		pdo_mysql_error(dbh);
961 		goto cleanup;
962 	}
963 
964 	if (!dbh->auto_commit) {
965 		mysql_handle_autocommit(dbh);
966 	}
967 
968 	H->attached = 1;
969 
970 	dbh->alloc_own_columns = 1;
971 	dbh->max_escaped_char_length = 2;
972 	dbh->methods = &mysql_methods;
973 
974 	ret = 1;
975 
976 cleanup:
977 	for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
978 		if (vars[i].freeme) {
979 			efree(vars[i].optval);
980 		}
981 	}
982 
983 	dbh->methods = &mysql_methods;
984 
985 	PDO_DBG_RETURN(ret);
986 }
987 /* }}} */
988 
989 const pdo_driver_t pdo_mysql_driver = {
990 	PDO_DRIVER_HEADER(mysql),
991 	pdo_mysql_handle_factory
992 };
993