xref: /PHP-7.4/ext/pdo/pdo_dbh.c (revision 68dcaa29)
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: Wez Furlong <wez@php.net>                                    |
16   |         Marcus Boerger <helly@php.net>                               |
17   |         Sterling Hughes <sterling@php.net>                           |
18   +----------------------------------------------------------------------+
19 */
20 
21 /* The PDO Database Handle Class */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "php.h"
28 #include "php_ini.h"
29 #include "ext/standard/info.h"
30 #include "php_pdo.h"
31 #include "php_pdo_driver.h"
32 #include "php_pdo_int.h"
33 #include "zend_exceptions.h"
34 #include "zend_object_handlers.h"
35 #include "zend_hash.h"
36 #include "zend_interfaces.h"
37 
38 static int pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value);
39 
pdo_throw_exception(unsigned int driver_errcode,char * driver_errmsg,pdo_error_type * pdo_error)40 void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_error_type *pdo_error)
41 {
42 		zval error_info,pdo_exception;
43 		char *pdo_exception_message;
44 
45 		object_init_ex(&pdo_exception, php_pdo_get_exception());
46 		array_init(&error_info);
47 
48 		add_next_index_string(&error_info, *pdo_error);
49 		add_next_index_long(&error_info, driver_errcode);
50 		add_next_index_string(&error_info, driver_errmsg);
51 
52 		spprintf(&pdo_exception_message, 0,"SQLSTATE[%s] [%d] %s",*pdo_error, driver_errcode, driver_errmsg);
53 		zend_update_property(php_pdo_get_exception(), &pdo_exception, "errorInfo", sizeof("errorInfo")-1, &error_info);
54 		zend_update_property_long(php_pdo_get_exception(), &pdo_exception, "code", sizeof("code")-1, driver_errcode);
55 		zend_update_property_string(
56 			php_pdo_get_exception(),
57 			&pdo_exception,
58 			"message",
59 			sizeof("message")-1,
60 			pdo_exception_message
61 		);
62 		efree(pdo_exception_message);
63 		zval_ptr_dtor(&error_info);
64 		zend_throw_exception_object(&pdo_exception);
65 }
66 
pdo_raise_impl_error(pdo_dbh_t * dbh,pdo_stmt_t * stmt,const char * sqlstate,const char * supp)67 void pdo_raise_impl_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *sqlstate, const char *supp) /* {{{ */
68 {
69 	pdo_error_type *pdo_err = &dbh->error_code;
70 	char *message = NULL;
71 	const char *msg;
72 
73 	if (dbh && dbh->error_mode == PDO_ERRMODE_SILENT) {
74 #if 0
75 		/* BUG: if user is running in silent mode and hits an error at the driver level
76 		 * when they use the PDO methods to call up the error information, they may
77 		 * get bogus information */
78 		return;
79 #endif
80 	}
81 
82 	if (stmt) {
83 		pdo_err = &stmt->error_code;
84 	}
85 
86 	strncpy(*pdo_err, sqlstate, 6);
87 
88 	/* hash sqlstate to error messages */
89 	msg = pdo_sqlstate_state_to_description(*pdo_err);
90 	if (!msg) {
91 		msg = "<<Unknown error>>";
92 	}
93 
94 	if (supp) {
95 		spprintf(&message, 0, "SQLSTATE[%s]: %s: %s", *pdo_err, msg, supp);
96 	} else {
97 		spprintf(&message, 0, "SQLSTATE[%s]: %s", *pdo_err, msg);
98 	}
99 
100 	if (dbh && dbh->error_mode != PDO_ERRMODE_EXCEPTION) {
101 		php_error_docref(NULL, E_WARNING, "%s", message);
102 	} else {
103 		zval ex, info;
104 		zend_class_entry *def_ex = php_pdo_get_exception_base(1), *pdo_ex = php_pdo_get_exception();
105 
106 		object_init_ex(&ex, pdo_ex);
107 
108 		zend_update_property_string(def_ex, &ex, "message", sizeof("message")-1, message);
109 		zend_update_property_string(def_ex, &ex, "code", sizeof("code")-1, *pdo_err);
110 
111 		array_init(&info);
112 
113 		add_next_index_string(&info, *pdo_err);
114 		add_next_index_long(&info, 0);
115 		zend_update_property(pdo_ex, &ex, "errorInfo", sizeof("errorInfo")-1, &info);
116 		zval_ptr_dtor(&info);
117 
118 		zend_throw_exception_object(&ex);
119 	}
120 
121 	if (message) {
122 		efree(message);
123 	}
124 }
125 /* }}} */
126 
pdo_handle_error(pdo_dbh_t * dbh,pdo_stmt_t * stmt)127 PDO_API void pdo_handle_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt) /* {{{ */
128 {
129 	pdo_error_type *pdo_err = &dbh->error_code;
130 	const char *msg = "<<Unknown>>";
131 	char *supp = NULL;
132 	zend_long native_code = 0;
133 	zend_string *message = NULL;
134 	zval info;
135 
136 	if (dbh == NULL || dbh->error_mode == PDO_ERRMODE_SILENT) {
137 		return;
138 	}
139 
140 	if (stmt) {
141 		pdo_err = &stmt->error_code;
142 	}
143 
144 	/* hash sqlstate to error messages */
145 	msg = pdo_sqlstate_state_to_description(*pdo_err);
146 	if (!msg) {
147 		msg = "<<Unknown error>>";
148 	}
149 
150 	ZVAL_UNDEF(&info);
151 	if (dbh->methods->fetch_err) {
152 		array_init(&info);
153 
154 		add_next_index_string(&info, *pdo_err);
155 
156 		if (dbh->methods->fetch_err(dbh, stmt, &info)) {
157 			zval *item;
158 
159 			if ((item = zend_hash_index_find(Z_ARRVAL(info), 1)) != NULL) {
160 				native_code = Z_LVAL_P(item);
161 			}
162 
163 			if ((item = zend_hash_index_find(Z_ARRVAL(info), 2)) != NULL) {
164 				supp = estrndup(Z_STRVAL_P(item), Z_STRLEN_P(item));
165 			}
166 		}
167 	}
168 
169 	if (supp) {
170 		message = strpprintf(0, "SQLSTATE[%s]: %s: " ZEND_LONG_FMT " %s", *pdo_err, msg, native_code, supp);
171 	} else {
172 		message = strpprintf(0, "SQLSTATE[%s]: %s", *pdo_err, msg);
173 	}
174 
175 	if (dbh->error_mode == PDO_ERRMODE_WARNING) {
176 		php_error_docref(NULL, E_WARNING, "%s", ZSTR_VAL(message));
177 	} else if (EG(exception) == NULL) {
178 		zval ex;
179 		zend_class_entry *def_ex = php_pdo_get_exception_base(1), *pdo_ex = php_pdo_get_exception();
180 
181 		object_init_ex(&ex, pdo_ex);
182 
183 		zend_update_property_str(def_ex, &ex, "message", sizeof("message") - 1, message);
184 		zend_update_property_string(def_ex, &ex, "code", sizeof("code") - 1, *pdo_err);
185 
186 		if (!Z_ISUNDEF(info)) {
187 			zend_update_property(pdo_ex, &ex, "errorInfo", sizeof("errorInfo") - 1, &info);
188 		}
189 
190 		zend_throw_exception_object(&ex);
191 	}
192 
193 	if (!Z_ISUNDEF(info)) {
194 		zval_ptr_dtor(&info);
195 	}
196 
197 	if (message) {
198 		zend_string_release_ex(message, 0);
199 	}
200 
201 	if (supp) {
202 		efree(supp);
203 	}
204 }
205 /* }}} */
206 
dsn_from_uri(char * uri,char * buf,size_t buflen)207 static char *dsn_from_uri(char *uri, char *buf, size_t buflen) /* {{{ */
208 {
209 	php_stream *stream;
210 	char *dsn = NULL;
211 
212 	stream = php_stream_open_wrapper(uri, "rb", REPORT_ERRORS, NULL);
213 	if (stream) {
214 		dsn = php_stream_get_line(stream, buf, buflen, NULL);
215 		php_stream_close(stream);
216 	}
217 	return dsn;
218 }
219 /* }}} */
220 
221 /* {{{ proto PDO::__construct(string dsn[, string username[, string passwd [, array options]]])
222    */
PHP_METHOD(PDO,dbh_constructor)223 static PHP_METHOD(PDO, dbh_constructor)
224 {
225 	zval *object = ZEND_THIS;
226 	pdo_dbh_t *dbh = NULL;
227 	zend_bool is_persistent = 0;
228 	char *data_source;
229 	size_t data_source_len;
230 	char *colon;
231 	char *username=NULL, *password=NULL;
232 	size_t usernamelen, passwordlen;
233 	pdo_driver_t *driver = NULL;
234 	zval *options = NULL;
235 	char alt_dsn[512];
236 	int call_factory = 1;
237 	zend_error_handling zeh;
238 
239 	ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 1, 4)
240 		Z_PARAM_STRING(data_source, data_source_len)
241 		Z_PARAM_OPTIONAL
242 		Z_PARAM_STRING_EX(username, usernamelen, 1, 0)
243 		Z_PARAM_STRING_EX(password, passwordlen, 1, 0)
244 		Z_PARAM_ARRAY_EX(options, 1, 0)
245 	ZEND_PARSE_PARAMETERS_END();
246 
247 	/* parse the data source name */
248 	colon = strchr(data_source, ':');
249 
250 	if (!colon) {
251 		/* let's see if this string has a matching dsn in the php.ini */
252 		char *ini_dsn = NULL;
253 
254 		snprintf(alt_dsn, sizeof(alt_dsn), "pdo.dsn.%s", data_source);
255 		if (FAILURE == cfg_get_string(alt_dsn, &ini_dsn)) {
256 			zend_throw_exception_ex(php_pdo_get_exception(), 0, "invalid data source name");
257 			return;
258 		}
259 
260 		data_source = ini_dsn;
261 		colon = strchr(data_source, ':');
262 
263 		if (!colon) {
264 			zend_throw_exception_ex(php_pdo_get_exception(), 0, "invalid data source name (via INI: %s)", alt_dsn);
265 			return;
266 		}
267 	}
268 
269 	if (!strncmp(data_source, "uri:", sizeof("uri:")-1)) {
270 		/* the specified URI holds connection details */
271 		data_source = dsn_from_uri(data_source + sizeof("uri:")-1, alt_dsn, sizeof(alt_dsn));
272 		if (!data_source) {
273 			zend_throw_exception_ex(php_pdo_get_exception(), 0, "invalid data source URI");
274 			return;
275 		}
276 		colon = strchr(data_source, ':');
277 		if (!colon) {
278 			zend_throw_exception_ex(php_pdo_get_exception(), 0, "invalid data source name (via URI)");
279 			return;
280 		}
281 	}
282 
283 	driver = pdo_find_driver(data_source, colon - data_source);
284 
285 	if (!driver) {
286 		/* NB: don't want to include the data_source in the error message as
287 		 * it might contain a password */
288 		zend_throw_exception_ex(php_pdo_get_exception(), 0, "could not find driver");
289 		return;
290 	}
291 
292 	dbh = Z_PDO_DBH_P(object);
293 
294 	/* is this supposed to be a persistent connection ? */
295 	if (options) {
296 		int plen = 0;
297 		char *hashkey = NULL;
298 		zend_resource *le;
299 		pdo_dbh_t *pdbh = NULL;
300 		zval *v;
301 
302 		if ((v = zend_hash_index_find_deref(Z_ARRVAL_P(options), PDO_ATTR_PERSISTENT)) != NULL) {
303 			if (Z_TYPE_P(v) == IS_STRING &&
304 				!is_numeric_string(Z_STRVAL_P(v), Z_STRLEN_P(v), NULL, NULL, 0) && Z_STRLEN_P(v) > 0) {
305 				/* user specified key */
306 				plen = spprintf(&hashkey, 0, "PDO:DBH:DSN=%s:%s:%s:%s", data_source,
307 						username ? username : "",
308 						password ? password : "",
309 						Z_STRVAL_P(v));
310 				is_persistent = 1;
311 			} else {
312 				is_persistent = zval_get_long(v) ? 1 : 0;
313 				plen = spprintf(&hashkey, 0, "PDO:DBH:DSN=%s:%s:%s", data_source,
314 						username ? username : "",
315 						password ? password : "");
316 			}
317 		}
318 
319 		if (is_persistent) {
320 			/* let's see if we have one cached.... */
321 			if ((le = zend_hash_str_find_ptr(&EG(persistent_list), hashkey, plen)) != NULL) {
322 				if (le->type == php_pdo_list_entry()) {
323 					pdbh = (pdo_dbh_t*)le->ptr;
324 
325 					/* is the connection still alive ? */
326 					if (pdbh->methods->check_liveness && FAILURE == (pdbh->methods->check_liveness)(pdbh)) {
327 						/* nope... need to kill it */
328 						pdbh->refcount--;
329 						zend_list_close(le);
330 						pdbh = NULL;
331 					}
332 				}
333 			}
334 
335 			if (pdbh) {
336 				call_factory = 0;
337 			} else {
338 				/* need a brand new pdbh */
339 				pdbh = pecalloc(1, sizeof(*pdbh), 1);
340 
341 				pdbh->refcount = 1;
342 				pdbh->is_persistent = 1;
343 				pdbh->persistent_id = pemalloc(plen + 1, 1);
344 				memcpy((char *)pdbh->persistent_id, hashkey, plen+1);
345 				pdbh->persistent_id_len = plen;
346 				pdbh->def_stmt_ce = dbh->def_stmt_ce;
347 			}
348 		}
349 
350 		if (pdbh) {
351 			efree(dbh);
352 			/* switch over to the persistent one */
353 			Z_PDO_OBJECT_P(object)->inner = pdbh;
354 			pdbh->refcount++;
355 			dbh = pdbh;
356 		}
357 
358 		if (hashkey) {
359 			efree(hashkey);
360 		}
361 	}
362 
363 	if (call_factory) {
364 		dbh->data_source_len = strlen(colon + 1);
365 		dbh->data_source = (const char*)pestrdup(colon + 1, is_persistent);
366 		dbh->username = username ? pestrdup(username, is_persistent) : NULL;
367 		dbh->password = password ? pestrdup(password, is_persistent) : NULL;
368 		dbh->default_fetch_type = PDO_FETCH_BOTH;
369 	}
370 
371 	dbh->auto_commit = pdo_attr_lval(options, PDO_ATTR_AUTOCOMMIT, 1);
372 
373 	if (!dbh->data_source || (username && !dbh->username) || (password && !dbh->password)) {
374 		php_error_docref(NULL, E_ERROR, "out of memory");
375 	}
376 
377 	zend_replace_error_handling(EH_THROW, pdo_exception_ce, &zeh);
378 
379 	if (!call_factory) {
380 		/* we got a persistent guy from our cache */
381 		goto options;
382 	}
383 
384 	if (driver->db_handle_factory(dbh, options)) {
385 		/* all set */
386 
387 		if (is_persistent) {
388 			/* register in the persistent list etc. */
389 			/* we should also need to replace the object store entry,
390 			   since it was created with emalloc */
391 			if ((zend_register_persistent_resource(
392 						(char*)dbh->persistent_id, dbh->persistent_id_len, dbh, php_pdo_list_entry())) == NULL) {
393 				php_error_docref(NULL, E_ERROR, "Failed to register persistent entry");
394 			}
395 		}
396 
397 		dbh->driver = driver;
398 options:
399 		if (options) {
400 			zval *attr_value;
401 			zend_ulong long_key;
402 			zend_string *str_key = NULL;
403 
404 			ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(options), long_key, str_key, attr_value) {
405 				if (str_key) {
406 					continue;
407 				}
408 				ZVAL_DEREF(attr_value);
409 				pdo_dbh_attribute_set(dbh, long_key, attr_value);
410 			} ZEND_HASH_FOREACH_END();
411 		}
412 
413 		zend_restore_error_handling(&zeh);
414 		return;
415 	}
416 
417 	/* the connection failed; things will tidy up in free_storage */
418 	/* XXX raise exception */
419 	zend_restore_error_handling(&zeh);
420 	if (!EG(exception)) {
421 		zend_throw_exception(pdo_exception_ce, "Constructor failed", 0);
422 	}
423 }
424 /* }}} */
425 
pdo_stmt_instantiate(pdo_dbh_t * dbh,zval * object,zend_class_entry * dbstmt_ce,zval * ctor_args)426 static zval *pdo_stmt_instantiate(pdo_dbh_t *dbh, zval *object, zend_class_entry *dbstmt_ce, zval *ctor_args) /* {{{ */
427 {
428 	if (!Z_ISUNDEF_P(ctor_args)) {
429 		if (Z_TYPE_P(ctor_args) != IS_ARRAY) {
430 			pdo_raise_impl_error(dbh, NULL, "HY000", "constructor arguments must be passed as an array");
431 			return NULL;
432 		}
433 		if (!dbstmt_ce->constructor) {
434 			pdo_raise_impl_error(dbh, NULL, "HY000", "user-supplied statement does not accept constructor arguments");
435 			return NULL;
436 		}
437 	}
438 
439 	if (UNEXPECTED(object_init_ex(object, dbstmt_ce) != SUCCESS)) {
440 		return NULL;
441 	}
442 
443 	return object;
444 } /* }}} */
445 
pdo_stmt_construct(zend_execute_data * execute_data,pdo_stmt_t * stmt,zval * object,zend_class_entry * dbstmt_ce,zval * ctor_args)446 static void pdo_stmt_construct(zend_execute_data *execute_data, pdo_stmt_t *stmt, zval *object, zend_class_entry *dbstmt_ce, zval *ctor_args) /* {{{ */
447 {
448 	zval query_string;
449 	zval z_key;
450 
451 	ZVAL_STRINGL(&query_string, stmt->query_string, stmt->query_stringlen);
452 	ZVAL_STRINGL(&z_key, "queryString", sizeof("queryString") - 1);
453 	zend_std_write_property(object, &z_key, &query_string, NULL);
454 	zval_ptr_dtor(&query_string);
455 	zval_ptr_dtor(&z_key);
456 
457 	if (dbstmt_ce->constructor) {
458 		zend_fcall_info fci;
459 		zend_fcall_info_cache fcc;
460 		zval retval;
461 
462 		fci.size = sizeof(zend_fcall_info);
463 		ZVAL_UNDEF(&fci.function_name);
464 		fci.object = Z_OBJ_P(object);
465 		fci.retval = &retval;
466 		fci.param_count = 0;
467 		fci.params = NULL;
468 		fci.no_separation = 1;
469 
470 		zend_fcall_info_args(&fci, ctor_args);
471 
472 		fcc.function_handler = dbstmt_ce->constructor;
473 		fcc.called_scope = Z_OBJCE_P(object);
474 		fcc.object = Z_OBJ_P(object);
475 
476 		if (zend_call_function(&fci, &fcc) != FAILURE) {
477 			zval_ptr_dtor(&retval);
478 		}
479 
480 		zend_fcall_info_args_clear(&fci, 1);
481 	}
482 }
483 /* }}} */
484 
485 /* {{{ proto object PDO::prepare(string statement [, array options])
486    Prepares a statement for execution and returns a statement object */
PHP_METHOD(PDO,prepare)487 static PHP_METHOD(PDO, prepare)
488 {
489 	pdo_stmt_t *stmt;
490 	char *statement;
491 	size_t statement_len;
492 	zval *options = NULL, *opt, *item, ctor_args;
493 	zend_class_entry *dbstmt_ce, *pce;
494 	pdo_dbh_object_t *dbh_obj = Z_PDO_OBJECT_P(ZEND_THIS);
495 	pdo_dbh_t *dbh = dbh_obj->inner;
496 
497 	ZEND_PARSE_PARAMETERS_START(1, 2)
498 		Z_PARAM_STRING(statement, statement_len)
499 		Z_PARAM_OPTIONAL
500 		Z_PARAM_ARRAY(options)
501 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
502 
503 	PDO_DBH_CLEAR_ERR();
504 	PDO_CONSTRUCT_CHECK;
505 
506 	if (ZEND_NUM_ARGS() > 1 && (opt = zend_hash_index_find(Z_ARRVAL_P(options), PDO_ATTR_STATEMENT_CLASS)) != NULL) {
507 		if (Z_TYPE_P(opt) != IS_ARRAY || (item = zend_hash_index_find(Z_ARRVAL_P(opt), 0)) == NULL
508 			|| Z_TYPE_P(item) != IS_STRING
509 			|| (pce = zend_lookup_class(Z_STR_P(item))) == NULL
510 		) {
511 			pdo_raise_impl_error(dbh, NULL, "HY000",
512 				"PDO::ATTR_STATEMENT_CLASS requires format array(classname, array(ctor_args)); "
513 				"the classname must be a string specifying an existing class"
514 				);
515 			PDO_HANDLE_DBH_ERR();
516 			RETURN_FALSE;
517 		}
518 		dbstmt_ce = pce;
519 		if (!instanceof_function(dbstmt_ce, pdo_dbstmt_ce)) {
520 			pdo_raise_impl_error(dbh, NULL, "HY000",
521 				"user-supplied statement class must be derived from PDOStatement");
522 			PDO_HANDLE_DBH_ERR();
523 			RETURN_FALSE;
524 		}
525 		if (dbstmt_ce->constructor && !(dbstmt_ce->constructor->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED))) {
526 			pdo_raise_impl_error(dbh, NULL, "HY000",
527 				"user-supplied statement class cannot have a public constructor");
528 			PDO_HANDLE_DBH_ERR();
529 			RETURN_FALSE;
530 		}
531 		if ((item = zend_hash_index_find(Z_ARRVAL_P(opt), 1)) != NULL) {
532 			if (Z_TYPE_P(item) != IS_ARRAY) {
533 				pdo_raise_impl_error(dbh, NULL, "HY000",
534 					"PDO::ATTR_STATEMENT_CLASS requires format array(classname, ctor_args); "
535 					"ctor_args must be an array"
536 				);
537 				PDO_HANDLE_DBH_ERR();
538 				RETURN_FALSE;
539 			}
540 			ZVAL_COPY_VALUE(&ctor_args, item);
541 		} else {
542 			ZVAL_UNDEF(&ctor_args);
543 		}
544 	} else {
545 		dbstmt_ce = dbh->def_stmt_ce;
546 		ZVAL_COPY_VALUE(&ctor_args, &dbh->def_stmt_ctor_args);
547 	}
548 
549 	if (!pdo_stmt_instantiate(dbh, return_value, dbstmt_ce, &ctor_args)) {
550 		if (EXPECTED(!EG(exception))) {
551 			pdo_raise_impl_error(dbh, NULL, "HY000",
552 				"failed to instantiate user-supplied statement class"
553 				);
554 		}
555 		PDO_HANDLE_DBH_ERR();
556 		RETURN_FALSE;
557 	}
558 	stmt = Z_PDO_STMT_P(return_value);
559 
560 	/* unconditionally keep this for later reference */
561 	stmt->query_string = estrndup(statement, statement_len);
562 	stmt->query_stringlen = statement_len;
563 	stmt->default_fetch_type = dbh->default_fetch_type;
564 	stmt->dbh = dbh;
565 	/* give it a reference to me */
566 	ZVAL_OBJ(&stmt->database_object_handle, &dbh_obj->std);
567 	Z_ADDREF(stmt->database_object_handle);
568 	/* we haven't created a lazy object yet */
569 	ZVAL_UNDEF(&stmt->lazy_object_ref);
570 
571 	if (dbh->methods->preparer(dbh, statement, statement_len, stmt, options)) {
572 		pdo_stmt_construct(execute_data, stmt, return_value, dbstmt_ce, &ctor_args);
573 		return;
574 	}
575 
576 	PDO_HANDLE_DBH_ERR();
577 
578 	/* kill the object handle for the stmt here */
579 	zval_ptr_dtor(return_value);
580 
581 	RETURN_FALSE;
582 }
583 /* }}} */
584 
585 /* {{{ proto bool PDO::beginTransaction()
586    Initiates a transaction */
PHP_METHOD(PDO,beginTransaction)587 static PHP_METHOD(PDO, beginTransaction)
588 {
589 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
590 
591 	if (zend_parse_parameters_none() == FAILURE) {
592 		return;
593 	}
594 	PDO_CONSTRUCT_CHECK;
595 
596 	if (dbh->in_txn) {
597 		zend_throw_exception_ex(php_pdo_get_exception(), 0, "There is already an active transaction");
598 		RETURN_FALSE;
599 	}
600 
601 	if (!dbh->methods->begin) {
602 		/* TODO: this should be an exception; see the auto-commit mode
603 		 * comments below */
604 		zend_throw_exception_ex(php_pdo_get_exception(), 0, "This driver doesn't support transactions");
605 		RETURN_FALSE;
606 	}
607 
608 	if (dbh->methods->begin(dbh)) {
609 		dbh->in_txn = 1;
610 		RETURN_TRUE;
611 	}
612 
613 	PDO_HANDLE_DBH_ERR();
614 	RETURN_FALSE;
615 }
616 /* }}} */
617 
618 /* {{{ proto bool PDO::commit()
619    Commit a transaction */
PHP_METHOD(PDO,commit)620 static PHP_METHOD(PDO, commit)
621 {
622 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
623 
624 	if (zend_parse_parameters_none() == FAILURE) {
625 		return;
626 	}
627 	PDO_CONSTRUCT_CHECK;
628 
629 	if (!dbh->in_txn) {
630 		zend_throw_exception_ex(php_pdo_get_exception(), 0, "There is no active transaction");
631 		RETURN_FALSE;
632 	}
633 
634 	if (dbh->methods->commit(dbh)) {
635 		dbh->in_txn = 0;
636 		RETURN_TRUE;
637 	}
638 
639 	PDO_HANDLE_DBH_ERR();
640 	RETURN_FALSE;
641 }
642 /* }}} */
643 
644 /* {{{ proto bool PDO::rollBack()
645    roll back a transaction */
PHP_METHOD(PDO,rollBack)646 static PHP_METHOD(PDO, rollBack)
647 {
648 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
649 
650 	if (zend_parse_parameters_none() == FAILURE) {
651 		return;
652 	}
653 	PDO_CONSTRUCT_CHECK;
654 
655 	if (!dbh->in_txn) {
656 		zend_throw_exception_ex(php_pdo_get_exception(), 0, "There is no active transaction");
657 		RETURN_FALSE;
658 	}
659 
660 	if (dbh->methods->rollback(dbh)) {
661 		dbh->in_txn = 0;
662 		RETURN_TRUE;
663 	}
664 
665 	PDO_HANDLE_DBH_ERR();
666 	RETURN_FALSE;
667 }
668 /* }}} */
669 
670 /* {{{ proto bool PDO::inTransaction()
671    determine if inside a transaction */
PHP_METHOD(PDO,inTransaction)672 static PHP_METHOD(PDO, inTransaction)
673 {
674 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
675 
676 	if (zend_parse_parameters_none() == FAILURE) {
677 		return;
678 	}
679 	PDO_CONSTRUCT_CHECK;
680 
681 	if (!dbh->methods->in_transaction) {
682 		RETURN_BOOL(dbh->in_txn);
683 	}
684 
685 	RETURN_BOOL(dbh->methods->in_transaction(dbh));
686 }
687 /* }}} */
688 
pdo_dbh_attribute_set(pdo_dbh_t * dbh,zend_long attr,zval * value)689 static int pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value) /* {{{ */
690 {
691 	zend_long lval;
692 
693 #define PDO_LONG_PARAM_CHECK \
694 	if (Z_TYPE_P(value) != IS_LONG && Z_TYPE_P(value) != IS_STRING && Z_TYPE_P(value) != IS_FALSE && Z_TYPE_P(value) != IS_TRUE) { \
695 		pdo_raise_impl_error(dbh, NULL, "HY000", "attribute value must be an integer"); \
696 		PDO_HANDLE_DBH_ERR(); \
697 		return FAILURE; \
698 	} \
699 
700 	switch (attr) {
701 		case PDO_ATTR_ERRMODE:
702 			PDO_LONG_PARAM_CHECK;
703 			lval = zval_get_long(value);
704 			switch (lval) {
705 				case PDO_ERRMODE_SILENT:
706 				case PDO_ERRMODE_WARNING:
707 				case PDO_ERRMODE_EXCEPTION:
708 					dbh->error_mode = lval;
709 					return SUCCESS;
710 				default:
711 					pdo_raise_impl_error(dbh, NULL, "HY000", "invalid error mode");
712 					PDO_HANDLE_DBH_ERR();
713 					return FAILURE;
714 			}
715 			return FAILURE;
716 
717 		case PDO_ATTR_CASE:
718 			PDO_LONG_PARAM_CHECK;
719 			lval = zval_get_long(value);
720 			switch (lval) {
721 				case PDO_CASE_NATURAL:
722 				case PDO_CASE_UPPER:
723 				case PDO_CASE_LOWER:
724 					dbh->desired_case = lval;
725 					return SUCCESS;
726 				default:
727 					pdo_raise_impl_error(dbh, NULL, "HY000", "invalid case folding mode");
728 					PDO_HANDLE_DBH_ERR();
729 					return FAILURE;
730 			}
731 			return FAILURE;
732 
733 		case PDO_ATTR_ORACLE_NULLS:
734 			PDO_LONG_PARAM_CHECK;
735 			dbh->oracle_nulls = zval_get_long(value);
736 			return SUCCESS;
737 
738 		case PDO_ATTR_DEFAULT_FETCH_MODE:
739 			if (Z_TYPE_P(value) == IS_ARRAY) {
740 				zval *tmp;
741 				if ((tmp = zend_hash_index_find(Z_ARRVAL_P(value), 0)) != NULL && Z_TYPE_P(tmp) == IS_LONG) {
742 					if (Z_LVAL_P(tmp) == PDO_FETCH_INTO || Z_LVAL_P(tmp) == PDO_FETCH_CLASS) {
743 						pdo_raise_impl_error(dbh, NULL, "HY000", "FETCH_INTO and FETCH_CLASS are not yet supported as default fetch modes");
744 						return FAILURE;
745 					}
746 				}
747 			} else {
748 				PDO_LONG_PARAM_CHECK;
749 			}
750 			lval = zval_get_long(value);
751 			if (lval == PDO_FETCH_USE_DEFAULT) {
752 				pdo_raise_impl_error(dbh, NULL, "HY000", "invalid fetch mode type");
753 				return FAILURE;
754 			}
755 			dbh->default_fetch_type = lval;
756 			return SUCCESS;
757 
758 		case PDO_ATTR_STRINGIFY_FETCHES:
759 			PDO_LONG_PARAM_CHECK;
760 			dbh->stringify = zval_get_long(value) ? 1 : 0;
761 			return SUCCESS;
762 
763 		case PDO_ATTR_STATEMENT_CLASS: {
764 			/* array(string classname, array(mixed ctor_args)) */
765 			zend_class_entry *pce;
766 			zval *item;
767 
768 			if (dbh->is_persistent) {
769 				pdo_raise_impl_error(dbh, NULL, "HY000",
770 					"PDO::ATTR_STATEMENT_CLASS cannot be used with persistent PDO instances"
771 					);
772 				PDO_HANDLE_DBH_ERR();
773 				return FAILURE;
774 			}
775 			if (Z_TYPE_P(value) != IS_ARRAY
776 				|| (item = zend_hash_index_find(Z_ARRVAL_P(value), 0)) == NULL
777 				|| Z_TYPE_P(item) != IS_STRING
778 				|| (pce = zend_lookup_class(Z_STR_P(item))) == NULL
779 			) {
780 				pdo_raise_impl_error(dbh, NULL, "HY000",
781 					"PDO::ATTR_STATEMENT_CLASS requires format array(classname, array(ctor_args)); "
782 					"the classname must be a string specifying an existing class"
783 					);
784 				PDO_HANDLE_DBH_ERR();
785 				return FAILURE;
786 			}
787 			if (!instanceof_function(pce, pdo_dbstmt_ce)) {
788 				pdo_raise_impl_error(dbh, NULL, "HY000",
789 					"user-supplied statement class must be derived from PDOStatement");
790 				PDO_HANDLE_DBH_ERR();
791 				return FAILURE;
792 			}
793 			if (pce->constructor && !(pce->constructor->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED))) {
794 				pdo_raise_impl_error(dbh, NULL, "HY000",
795 					"user-supplied statement class cannot have a public constructor");
796 				PDO_HANDLE_DBH_ERR();
797 				return FAILURE;
798 			}
799 			dbh->def_stmt_ce = pce;
800 			if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) {
801 				zval_ptr_dtor(&dbh->def_stmt_ctor_args);
802 				ZVAL_UNDEF(&dbh->def_stmt_ctor_args);
803 			}
804 			if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 1)) != NULL) {
805 				if (Z_TYPE_P(item) != IS_ARRAY) {
806 					pdo_raise_impl_error(dbh, NULL, "HY000",
807 						"PDO::ATTR_STATEMENT_CLASS requires format array(classname, array(ctor_args)); "
808 						"ctor_args must be an array"
809 					);
810 					PDO_HANDLE_DBH_ERR();
811 					return FAILURE;
812 				}
813 				ZVAL_COPY(&dbh->def_stmt_ctor_args, item);
814 			}
815 			return SUCCESS;
816 		}
817 
818 		default:
819 			;
820 	}
821 
822 	if (!dbh->methods->set_attribute) {
823 		goto fail;
824 	}
825 
826 	PDO_DBH_CLEAR_ERR();
827 	if (dbh->methods->set_attribute(dbh, attr, value)) {
828 		return SUCCESS;
829 	}
830 
831 fail:
832 	if (!dbh->methods->set_attribute) {
833 		pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support setting attributes");
834 	} else {
835 		PDO_HANDLE_DBH_ERR();
836 	}
837 	return FAILURE;
838 }
839 /* }}} */
840 
841 /* {{{ proto bool PDO::setAttribute(int attribute, mixed value)
842    Set an attribute */
PHP_METHOD(PDO,setAttribute)843 static PHP_METHOD(PDO, setAttribute)
844 {
845 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
846 	zend_long attr;
847 	zval *value;
848 
849 	ZEND_PARSE_PARAMETERS_START(2, 2)
850 		Z_PARAM_LONG(attr)
851 		Z_PARAM_ZVAL(value)
852 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
853 
854 	PDO_DBH_CLEAR_ERR();
855 	PDO_CONSTRUCT_CHECK;
856 
857 	if (pdo_dbh_attribute_set(dbh, attr, value) != FAILURE) {
858  		RETURN_TRUE;
859  	}
860  	RETURN_FALSE;
861 }
862 /* }}} */
863 
864 /* {{{ proto mixed PDO::getAttribute(int attribute)
865    Get an attribute */
PHP_METHOD(PDO,getAttribute)866 static PHP_METHOD(PDO, getAttribute)
867 {
868 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
869 	zend_long attr;
870 
871 	ZEND_PARSE_PARAMETERS_START(1, 1)
872 		Z_PARAM_LONG(attr)
873 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
874 
875 	PDO_DBH_CLEAR_ERR();
876 	PDO_CONSTRUCT_CHECK;
877 
878 	/* handle generic PDO-level attributes */
879 	switch (attr) {
880 		case PDO_ATTR_PERSISTENT:
881 			RETURN_BOOL(dbh->is_persistent);
882 
883 		case PDO_ATTR_CASE:
884 			RETURN_LONG(dbh->desired_case);
885 
886 		case PDO_ATTR_ORACLE_NULLS:
887 			RETURN_LONG(dbh->oracle_nulls);
888 
889 		case PDO_ATTR_ERRMODE:
890 			RETURN_LONG(dbh->error_mode);
891 
892 		case PDO_ATTR_DRIVER_NAME:
893 			RETURN_STRINGL((char*)dbh->driver->driver_name, dbh->driver->driver_name_len);
894 
895 		case PDO_ATTR_STATEMENT_CLASS:
896 			array_init(return_value);
897 			add_next_index_str(return_value, zend_string_copy(dbh->def_stmt_ce->name));
898 			if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) {
899 				Z_TRY_ADDREF(dbh->def_stmt_ctor_args);
900 				add_next_index_zval(return_value, &dbh->def_stmt_ctor_args);
901 			}
902 			return;
903 		case PDO_ATTR_DEFAULT_FETCH_MODE:
904 			RETURN_LONG(dbh->default_fetch_type);
905 		default:
906 			break;
907 	}
908 
909 	if (!dbh->methods->get_attribute) {
910 		pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support getting attributes");
911 		RETURN_FALSE;
912 	}
913 
914 	switch (dbh->methods->get_attribute(dbh, attr, return_value)) {
915 		case -1:
916 			PDO_HANDLE_DBH_ERR();
917 			RETURN_FALSE;
918 
919 		case 0:
920 			pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support that attribute");
921 			RETURN_FALSE;
922 
923 		default:
924 			return;
925 	}
926 }
927 /* }}} */
928 
929 /* {{{ proto int PDO::exec(string query)
930    Execute a query that does not return a row set, returning the number of affected rows */
PHP_METHOD(PDO,exec)931 static PHP_METHOD(PDO, exec)
932 {
933 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
934 	char *statement;
935 	size_t statement_len;
936 	zend_long ret;
937 
938 	ZEND_PARSE_PARAMETERS_START(1, 1)
939 		Z_PARAM_STRING(statement, statement_len)
940 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
941 
942 	if (!statement_len) {
943 		pdo_raise_impl_error(dbh, NULL, "HY000",  "trying to execute an empty query");
944 		RETURN_FALSE;
945 	}
946 	PDO_DBH_CLEAR_ERR();
947 	PDO_CONSTRUCT_CHECK;
948 	ret = dbh->methods->doer(dbh, statement, statement_len);
949 	if(ret == -1) {
950 		PDO_HANDLE_DBH_ERR();
951 		RETURN_FALSE;
952 	} else {
953 		RETURN_LONG(ret);
954 	}
955 }
956 /* }}} */
957 
958 /* {{{ proto string PDO::lastInsertId([string seqname])
959    Returns the id of the last row that we affected on this connection.  Some databases require a sequence or table name to be passed in.  Not always meaningful. */
PHP_METHOD(PDO,lastInsertId)960 static PHP_METHOD(PDO, lastInsertId)
961 {
962 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
963 	char *name = NULL;
964 	size_t namelen;
965 
966 	ZEND_PARSE_PARAMETERS_START(0, 1)
967 		Z_PARAM_OPTIONAL
968 		Z_PARAM_STRING_EX(name, namelen, 1, 0)
969 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
970 
971 	PDO_DBH_CLEAR_ERR();
972 	PDO_CONSTRUCT_CHECK;
973 	if (!dbh->methods->last_id) {
974 		pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support lastInsertId()");
975 		RETURN_FALSE;
976 	} else {
977 		size_t id_len;
978 		char *id;
979 		id = dbh->methods->last_id(dbh, name, &id_len);
980 		if (!id) {
981 			PDO_HANDLE_DBH_ERR();
982 			RETURN_FALSE;
983 		} else {
984 			//??? use zend_string ?
985 			RETVAL_STRINGL(id, id_len);
986 			efree(id);
987 		}
988 	}
989 }
990 /* }}} */
991 
992 /* {{{ proto string PDO::errorCode()
993    Fetch the error code associated with the last operation on the database handle */
PHP_METHOD(PDO,errorCode)994 static PHP_METHOD(PDO, errorCode)
995 {
996 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
997 
998 	if (zend_parse_parameters_none() == FAILURE) {
999 		return;
1000 	}
1001 	PDO_CONSTRUCT_CHECK;
1002 
1003 	if (dbh->query_stmt) {
1004 		RETURN_STRING(dbh->query_stmt->error_code);
1005 	}
1006 
1007 	if (dbh->error_code[0] == '\0') {
1008 		RETURN_NULL();
1009 	}
1010 
1011 	/**
1012 	 * Making sure that we fallback to the default implementation
1013 	 * if the dbh->error_code is not null.
1014 	 */
1015 	RETURN_STRING(dbh->error_code);
1016 }
1017 /* }}} */
1018 
1019 /* {{{ proto int PDO::errorInfo()
1020    Fetch extended error information associated with the last operation on the database handle */
PHP_METHOD(PDO,errorInfo)1021 static PHP_METHOD(PDO, errorInfo)
1022 {
1023 	int error_count;
1024 	int error_count_diff 	 = 0;
1025 	int error_expected_count = 3;
1026 
1027 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
1028 
1029 	if (zend_parse_parameters_none() == FAILURE) {
1030 		return;
1031 	}
1032 
1033 	PDO_CONSTRUCT_CHECK;
1034 
1035 	array_init(return_value);
1036 
1037 	if (dbh->query_stmt) {
1038 		add_next_index_string(return_value, dbh->query_stmt->error_code);
1039 		if(!strncmp(dbh->query_stmt->error_code, PDO_ERR_NONE, sizeof(PDO_ERR_NONE))) goto fill_array;
1040 	} else {
1041 		add_next_index_string(return_value, dbh->error_code);
1042 		if(!strncmp(dbh->error_code, PDO_ERR_NONE, sizeof(PDO_ERR_NONE))) goto fill_array;
1043 	}
1044 
1045 	if (dbh->methods->fetch_err) {
1046 		dbh->methods->fetch_err(dbh, dbh->query_stmt, return_value);
1047 	}
1048 
1049 fill_array:
1050 	/**
1051 	 * In order to be consistent, we have to make sure we add the good amount
1052 	 * of nulls depending on the current number of elements. We make a simple
1053 	 * difference and add the needed elements
1054 	 */
1055 	error_count = zend_hash_num_elements(Z_ARRVAL_P(return_value));
1056 
1057 	if (error_expected_count > error_count) {
1058 		int current_index;
1059 
1060 		error_count_diff = error_expected_count - error_count;
1061 		for (current_index = 0; current_index < error_count_diff; current_index++) {
1062 			add_next_index_null(return_value);
1063 		}
1064 	}
1065 }
1066 /* }}} */
1067 
1068 /* {{{ proto object PDO::query(string sql [, PDOStatement::setFetchMode() args])
1069    Prepare and execute $sql; returns the statement object for iteration */
PHP_METHOD(PDO,query)1070 static PHP_METHOD(PDO, query)
1071 {
1072 	pdo_stmt_t *stmt;
1073 	char *statement;
1074 	size_t statement_len;
1075 	pdo_dbh_object_t *dbh_obj = Z_PDO_OBJECT_P(ZEND_THIS);
1076 	pdo_dbh_t *dbh = dbh_obj->inner;
1077 
1078 	/* Return a meaningful error when no parameters were passed */
1079 	if (!ZEND_NUM_ARGS()) {
1080 		zend_parse_parameters(0, "z|z", NULL, NULL);
1081 		RETURN_FALSE;
1082 	}
1083 
1084 	if (FAILURE == zend_parse_parameters(1, "s", &statement,
1085 			&statement_len)) {
1086 		RETURN_FALSE;
1087 	}
1088 
1089 	PDO_DBH_CLEAR_ERR();
1090 	PDO_CONSTRUCT_CHECK;
1091 
1092 	if (!pdo_stmt_instantiate(dbh, return_value, dbh->def_stmt_ce, &dbh->def_stmt_ctor_args)) {
1093 		if (EXPECTED(!EG(exception))) {
1094 			pdo_raise_impl_error(dbh, NULL, "HY000", "failed to instantiate user supplied statement class");
1095 		}
1096 		return;
1097 	}
1098 	stmt = Z_PDO_STMT_P(return_value);
1099 
1100 	/* unconditionally keep this for later reference */
1101 	stmt->query_string = estrndup(statement, statement_len);
1102 	stmt->query_stringlen = statement_len;
1103 
1104 	stmt->default_fetch_type = dbh->default_fetch_type;
1105 	stmt->active_query_string = stmt->query_string;
1106 	stmt->active_query_stringlen = statement_len;
1107 	stmt->dbh = dbh;
1108 	/* give it a reference to me */
1109 	ZVAL_OBJ(&stmt->database_object_handle, &dbh_obj->std);
1110 	Z_ADDREF(stmt->database_object_handle);
1111 	/* we haven't created a lazy object yet */
1112 	ZVAL_UNDEF(&stmt->lazy_object_ref);
1113 
1114 	if (dbh->methods->preparer(dbh, statement, statement_len, stmt, NULL)) {
1115 		PDO_STMT_CLEAR_ERR();
1116 		if (ZEND_NUM_ARGS() == 1 || SUCCESS == pdo_stmt_setup_fetch_mode(INTERNAL_FUNCTION_PARAM_PASSTHRU, stmt, 1)) {
1117 
1118 			/* now execute the statement */
1119 			PDO_STMT_CLEAR_ERR();
1120 			if (stmt->methods->executer(stmt)) {
1121 				int ret = 1;
1122 				if (!stmt->executed) {
1123 					if (stmt->dbh->alloc_own_columns) {
1124 						ret = pdo_stmt_describe_columns(stmt);
1125 					}
1126 					stmt->executed = 1;
1127 				}
1128 				if (ret) {
1129 					pdo_stmt_construct(execute_data, stmt, return_value, dbh->def_stmt_ce, &dbh->def_stmt_ctor_args);
1130 					return;
1131 				}
1132 			}
1133 		}
1134 		/* something broke */
1135 		dbh->query_stmt = stmt;
1136 		ZVAL_OBJ(&dbh->query_stmt_zval, Z_OBJ_P(return_value));
1137 		Z_DELREF(stmt->database_object_handle);
1138 		ZVAL_UNDEF(&stmt->database_object_handle);
1139 		PDO_HANDLE_STMT_ERR();
1140 	} else {
1141 		PDO_HANDLE_DBH_ERR();
1142 		zval_ptr_dtor(return_value);
1143 	}
1144 
1145 	RETURN_FALSE;
1146 }
1147 /* }}} */
1148 
1149 /* {{{ proto string PDO::quote(string string [, int paramtype])
1150    quotes string for use in a query.  The optional paramtype acts as a hint for drivers that have alternate quoting styles.  The default value is PDO_PARAM_STR */
PHP_METHOD(PDO,quote)1151 static PHP_METHOD(PDO, quote)
1152 {
1153 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
1154 	char *str;
1155 	size_t str_len;
1156 	zend_long paramtype = PDO_PARAM_STR;
1157 	char *qstr;
1158 	size_t qlen;
1159 
1160 	ZEND_PARSE_PARAMETERS_START(1, 2)
1161 		Z_PARAM_STRING(str, str_len)
1162 		Z_PARAM_OPTIONAL
1163 		Z_PARAM_LONG(paramtype)
1164 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1165 
1166 	PDO_DBH_CLEAR_ERR();
1167 	PDO_CONSTRUCT_CHECK;
1168 	if (!dbh->methods->quoter) {
1169 		pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support quoting");
1170 		RETURN_FALSE;
1171 	}
1172 
1173 	if (dbh->methods->quoter(dbh, str, str_len, &qstr, &qlen, paramtype)) {
1174 		RETVAL_STRINGL(qstr, qlen);
1175 		efree(qstr);
1176 		return;
1177 	}
1178 	PDO_HANDLE_DBH_ERR();
1179 	RETURN_FALSE;
1180 }
1181 /* }}} */
1182 
1183 /* {{{ proto array PDO::getAvailableDrivers()
1184    Return array of available PDO drivers */
PHP_METHOD(PDO,getAvailableDrivers)1185 static PHP_METHOD(PDO, getAvailableDrivers)
1186 {
1187 	pdo_driver_t *pdriver;
1188 
1189 	if (zend_parse_parameters_none() == FAILURE) {
1190 		return;
1191 	}
1192 
1193 	array_init(return_value);
1194 
1195 	ZEND_HASH_FOREACH_PTR(&pdo_driver_hash, pdriver) {
1196 		add_next_index_stringl(return_value, (char*)pdriver->driver_name, pdriver->driver_name_len);
1197 	} ZEND_HASH_FOREACH_END();
1198 }
1199 /* }}} */
1200 
1201 /* {{{ arginfo */
1202 ZEND_BEGIN_ARG_INFO_EX(arginfo_pdo___construct, 0, 0, 1)
1203 	ZEND_ARG_INFO(0, dsn)
1204 	ZEND_ARG_INFO(0, username)
1205 	ZEND_ARG_INFO(0, passwd)
1206 	ZEND_ARG_INFO(0, options) /* array */
1207 ZEND_END_ARG_INFO()
1208 
1209 ZEND_BEGIN_ARG_INFO_EX(arginfo_pdo_prepare, 0, 0, 1)
1210 	ZEND_ARG_INFO(0, statement)
1211 	ZEND_ARG_INFO(0, options) /* array */
1212 ZEND_END_ARG_INFO()
1213 
1214 ZEND_BEGIN_ARG_INFO(arginfo_pdo_setattribute, 0)
1215 	ZEND_ARG_INFO(0, attribute)
1216 	ZEND_ARG_INFO(0, value)
1217 ZEND_END_ARG_INFO()
1218 
1219 ZEND_BEGIN_ARG_INFO(arginfo_pdo_getattribute, 0)
1220 	ZEND_ARG_INFO(0, attribute)
1221 ZEND_END_ARG_INFO()
1222 
1223 ZEND_BEGIN_ARG_INFO(arginfo_pdo_exec, 0)
1224 	ZEND_ARG_INFO(0, query)
1225 ZEND_END_ARG_INFO()
1226 
1227 ZEND_BEGIN_ARG_INFO_EX(arginfo_pdo_lastinsertid, 0, 0, 0)
1228 	ZEND_ARG_INFO(0, seqname)
1229 ZEND_END_ARG_INFO()
1230 
1231 ZEND_BEGIN_ARG_INFO_EX(arginfo_pdo_quote, 0, 0, 1)
1232 	ZEND_ARG_INFO(0, string)
1233 	ZEND_ARG_INFO(0, paramtype)
1234 ZEND_END_ARG_INFO()
1235 
1236 ZEND_BEGIN_ARG_INFO(arginfo_pdo__void, 0)
1237 ZEND_END_ARG_INFO()
1238 /* }}} */
1239 
1240 const zend_function_entry pdo_dbh_functions[] = /* {{{ */ {
1241 	ZEND_MALIAS(PDO, __construct, dbh_constructor,	arginfo_pdo___construct,	ZEND_ACC_PUBLIC)
1242 	PHP_ME(PDO, prepare, 				arginfo_pdo_prepare,		ZEND_ACC_PUBLIC)
1243 	PHP_ME(PDO, beginTransaction,       arginfo_pdo__void,         ZEND_ACC_PUBLIC)
1244 	PHP_ME(PDO, commit,                 arginfo_pdo__void,         ZEND_ACC_PUBLIC)
1245 	PHP_ME(PDO, rollBack,               arginfo_pdo__void,         ZEND_ACC_PUBLIC)
1246 	PHP_ME(PDO, inTransaction,          arginfo_pdo__void,         ZEND_ACC_PUBLIC)
1247 	PHP_ME(PDO, setAttribute,	arginfo_pdo_setattribute,	ZEND_ACC_PUBLIC)
1248 	PHP_ME(PDO, exec,			arginfo_pdo_exec,		ZEND_ACC_PUBLIC)
1249 	PHP_ME(PDO, query,			NULL,					ZEND_ACC_PUBLIC)
1250 	PHP_ME(PDO, lastInsertId,	arginfo_pdo_lastinsertid,	ZEND_ACC_PUBLIC)
1251 	PHP_ME(PDO, errorCode,              arginfo_pdo__void,         ZEND_ACC_PUBLIC)
1252 	PHP_ME(PDO, errorInfo,              arginfo_pdo__void,         ZEND_ACC_PUBLIC)
1253 	PHP_ME(PDO, getAttribute,	arginfo_pdo_getattribute,	ZEND_ACC_PUBLIC)
1254 	PHP_ME(PDO, quote,			arginfo_pdo_quote,		ZEND_ACC_PUBLIC)
1255 	PHP_ME(PDO, getAvailableDrivers,    arginfo_pdo__void,         ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1256 	PHP_FE_END
1257 };
1258 /* }}} */
1259 
cls_method_dtor(zval * el)1260 static void cls_method_dtor(zval *el) /* {{{ */ {
1261 	zend_function *func = (zend_function*)Z_PTR_P(el);
1262 	if (func->common.function_name) {
1263 		zend_string_release_ex(func->common.function_name, 0);
1264 	}
1265 	efree(func);
1266 }
1267 /* }}} */
1268 
cls_method_pdtor(zval * el)1269 static void cls_method_pdtor(zval *el) /* {{{ */ {
1270 	zend_function *func = (zend_function*)Z_PTR_P(el);
1271 	if (func->common.function_name) {
1272 		zend_string_release_ex(func->common.function_name, 1);
1273 	}
1274 	pefree(func, 1);
1275 }
1276 /* }}} */
1277 
1278 /* {{{ overloaded object handlers for PDO class */
pdo_hash_methods(pdo_dbh_object_t * dbh_obj,int kind)1279 int pdo_hash_methods(pdo_dbh_object_t *dbh_obj, int kind)
1280 {
1281 	const zend_function_entry *funcs;
1282 	zend_internal_function func;
1283 	size_t namelen;
1284 	char *lc_name;
1285 	pdo_dbh_t *dbh = dbh_obj->inner;
1286 
1287 	if (!dbh || !dbh->methods || !dbh->methods->get_driver_methods) {
1288 		return 0;
1289 	}
1290 	funcs =	dbh->methods->get_driver_methods(dbh, kind);
1291 	if (!funcs) {
1292 		return 0;
1293 	}
1294 
1295 	dbh->cls_methods[kind] = pemalloc(sizeof(HashTable), dbh->is_persistent);
1296 	zend_hash_init_ex(dbh->cls_methods[kind], 8, NULL,
1297 			dbh->is_persistent? cls_method_pdtor : cls_method_dtor, dbh->is_persistent, 0);
1298 
1299 	memset(&func, 0, sizeof(func));
1300 
1301 	while (funcs->fname) {
1302 		func.type = ZEND_INTERNAL_FUNCTION;
1303 		func.handler = funcs->handler;
1304 		func.function_name = zend_string_init(funcs->fname, strlen(funcs->fname), dbh->is_persistent);
1305 		func.scope = dbh_obj->std.ce;
1306 		func.prototype = NULL;
1307 		if (funcs->flags) {
1308 			func.fn_flags = funcs->flags | ZEND_ACC_NEVER_CACHE;
1309 		} else {
1310 			func.fn_flags = ZEND_ACC_PUBLIC | ZEND_ACC_NEVER_CACHE;
1311 		}
1312 		if (funcs->arg_info) {
1313 			zend_internal_function_info *info = (zend_internal_function_info*)funcs->arg_info;
1314 
1315 			func.arg_info = (zend_internal_arg_info*)funcs->arg_info + 1;
1316 			func.num_args = funcs->num_args;
1317 			if (info->required_num_args == (uint32_t)-1) {
1318 				func.required_num_args = funcs->num_args;
1319 			} else {
1320 				func.required_num_args = info->required_num_args;
1321 			}
1322 			if (info->return_reference) {
1323 				func.fn_flags |= ZEND_ACC_RETURN_REFERENCE;
1324 			}
1325 			if (funcs->arg_info[funcs->num_args].is_variadic) {
1326 				func.fn_flags |= ZEND_ACC_VARIADIC;
1327 				/* Don't count the variadic argument */
1328 				func.num_args--;
1329 			}
1330 		} else {
1331 			func.arg_info = NULL;
1332 			func.num_args = 0;
1333 			func.required_num_args = 0;
1334 		}
1335 		zend_set_function_arg_flags((zend_function*)&func);
1336 		namelen = strlen(funcs->fname);
1337 		lc_name = emalloc(namelen+1);
1338 		zend_str_tolower_copy(lc_name, funcs->fname, namelen);
1339 		zend_hash_str_add_mem(dbh->cls_methods[kind], lc_name, namelen, &func, sizeof(func));
1340 		efree(lc_name);
1341 		funcs++;
1342 	}
1343 
1344 	return 1;
1345 }
1346 
dbh_method_get(zend_object ** object,zend_string * method_name,const zval * key)1347 static zend_function *dbh_method_get(zend_object **object, zend_string *method_name, const zval *key)
1348 {
1349 	zend_function *fbc = NULL;
1350 	pdo_dbh_object_t *dbh_obj = php_pdo_dbh_fetch_object(*object);
1351 	zend_string *lc_method_name;
1352 
1353 	if ((fbc = zend_std_get_method(object, method_name, key)) == NULL) {
1354 		/* not a pre-defined method, nor a user-defined method; check
1355 		 * the driver specific methods */
1356 		if (!dbh_obj->inner->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH]) {
1357 			if (!pdo_hash_methods(dbh_obj,
1358 				PDO_DBH_DRIVER_METHOD_KIND_DBH)
1359 				|| !dbh_obj->inner->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH]) {
1360 				goto out;
1361 			}
1362 		}
1363 
1364 		lc_method_name = zend_string_tolower(method_name);
1365 		fbc = zend_hash_find_ptr(dbh_obj->inner->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH], lc_method_name);
1366 		zend_string_release_ex(lc_method_name, 0);
1367 	}
1368 
1369 out:
1370 	return fbc;
1371 }
1372 
dbh_compare(zval * object1,zval * object2)1373 static int dbh_compare(zval *object1, zval *object2)
1374 {
1375 	return -1;
1376 }
1377 
dbh_get_gc(zval * object,zval ** gc_data,int * gc_count)1378 static HashTable *dbh_get_gc(zval *object, zval **gc_data, int *gc_count)
1379 {
1380 	pdo_dbh_t *dbh = Z_PDO_DBH_P(object);
1381 	*gc_data = &dbh->def_stmt_ctor_args;
1382 	*gc_count = 1;
1383 	return zend_std_get_properties(object);
1384 }
1385 
1386 static zend_object_handlers pdo_dbh_object_handlers;
1387 static void pdo_dbh_free_storage(zend_object *std);
1388 
pdo_dbh_init(void)1389 void pdo_dbh_init(void)
1390 {
1391 	zend_class_entry ce;
1392 
1393 	INIT_CLASS_ENTRY(ce, "PDO", pdo_dbh_functions);
1394 	pdo_dbh_ce = zend_register_internal_class(&ce);
1395 	pdo_dbh_ce->create_object = pdo_dbh_new;
1396 	pdo_dbh_ce->serialize = zend_class_serialize_deny;
1397 	pdo_dbh_ce->unserialize = zend_class_unserialize_deny;
1398 
1399 	memcpy(&pdo_dbh_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
1400 	pdo_dbh_object_handlers.offset = XtOffsetOf(pdo_dbh_object_t, std);
1401 	pdo_dbh_object_handlers.dtor_obj = zend_objects_destroy_object;
1402 	pdo_dbh_object_handlers.free_obj = pdo_dbh_free_storage;
1403 	pdo_dbh_object_handlers.clone_obj = NULL;
1404 	pdo_dbh_object_handlers.get_method = dbh_method_get;
1405 	pdo_dbh_object_handlers.compare_objects = dbh_compare;
1406 	pdo_dbh_object_handlers.get_gc = dbh_get_gc;
1407 
1408 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_BOOL", (zend_long)PDO_PARAM_BOOL);
1409 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_NULL", (zend_long)PDO_PARAM_NULL);
1410 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_INT",  (zend_long)PDO_PARAM_INT);
1411 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_STR",  (zend_long)PDO_PARAM_STR);
1412 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_LOB",  (zend_long)PDO_PARAM_LOB);
1413 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_STMT", (zend_long)PDO_PARAM_STMT);
1414 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_INPUT_OUTPUT", (zend_long)PDO_PARAM_INPUT_OUTPUT);
1415 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_STR_NATL", (zend_long)PDO_PARAM_STR_NATL);
1416 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_STR_CHAR", (zend_long)PDO_PARAM_STR_CHAR);
1417 
1418 
1419 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_ALLOC",		(zend_long)PDO_PARAM_EVT_ALLOC);
1420 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_FREE",			(zend_long)PDO_PARAM_EVT_FREE);
1421 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_EXEC_PRE",		(zend_long)PDO_PARAM_EVT_EXEC_PRE);
1422 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_EXEC_POST",	(zend_long)PDO_PARAM_EVT_EXEC_POST);
1423 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_FETCH_PRE",	(zend_long)PDO_PARAM_EVT_FETCH_PRE);
1424 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_FETCH_POST",	(zend_long)PDO_PARAM_EVT_FETCH_POST);
1425 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_NORMALIZE",	(zend_long)PDO_PARAM_EVT_NORMALIZE);
1426 
1427 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_LAZY", (zend_long)PDO_FETCH_LAZY);
1428 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_ASSOC", (zend_long)PDO_FETCH_ASSOC);
1429 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_NUM",  (zend_long)PDO_FETCH_NUM);
1430 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_BOTH", (zend_long)PDO_FETCH_BOTH);
1431 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_OBJ",  (zend_long)PDO_FETCH_OBJ);
1432 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_BOUND", (zend_long)PDO_FETCH_BOUND);
1433 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_COLUMN", (zend_long)PDO_FETCH_COLUMN);
1434 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_CLASS", (zend_long)PDO_FETCH_CLASS);
1435 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_INTO", (zend_long)PDO_FETCH_INTO);
1436 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_FUNC", (zend_long)PDO_FETCH_FUNC);
1437 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_GROUP", (zend_long)PDO_FETCH_GROUP);
1438 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_UNIQUE", (zend_long)PDO_FETCH_UNIQUE);
1439 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_KEY_PAIR", (zend_long)PDO_FETCH_KEY_PAIR);
1440 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_CLASSTYPE", (zend_long)PDO_FETCH_CLASSTYPE);
1441 
1442 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_SERIALIZE",(zend_long)PDO_FETCH_SERIALIZE);
1443 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_PROPS_LATE", (zend_long)PDO_FETCH_PROPS_LATE);
1444 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_NAMED", (zend_long)PDO_FETCH_NAMED);
1445 
1446 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_AUTOCOMMIT",	(zend_long)PDO_ATTR_AUTOCOMMIT);
1447 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_PREFETCH",		(zend_long)PDO_ATTR_PREFETCH);
1448 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_TIMEOUT", 		(zend_long)PDO_ATTR_TIMEOUT);
1449 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_ERRMODE", 		(zend_long)PDO_ATTR_ERRMODE);
1450 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_SERVER_VERSION",	(zend_long)PDO_ATTR_SERVER_VERSION);
1451 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_CLIENT_VERSION", 	(zend_long)PDO_ATTR_CLIENT_VERSION);
1452 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_SERVER_INFO",		(zend_long)PDO_ATTR_SERVER_INFO);
1453 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_CONNECTION_STATUS", 	(zend_long)PDO_ATTR_CONNECTION_STATUS);
1454 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_CASE",		 	(zend_long)PDO_ATTR_CASE);
1455 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_CURSOR_NAME", 	(zend_long)PDO_ATTR_CURSOR_NAME);
1456 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_CURSOR",	 	(zend_long)PDO_ATTR_CURSOR);
1457 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_ORACLE_NULLS",	(zend_long)PDO_ATTR_ORACLE_NULLS);
1458 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_PERSISTENT",	(zend_long)PDO_ATTR_PERSISTENT);
1459 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_STATEMENT_CLASS",		(zend_long)PDO_ATTR_STATEMENT_CLASS);
1460 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_FETCH_TABLE_NAMES",		(zend_long)PDO_ATTR_FETCH_TABLE_NAMES);
1461 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_FETCH_CATALOG_NAMES",		(zend_long)PDO_ATTR_FETCH_CATALOG_NAMES);
1462 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_DRIVER_NAME",		(zend_long)PDO_ATTR_DRIVER_NAME);
1463 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_STRINGIFY_FETCHES", (zend_long)PDO_ATTR_STRINGIFY_FETCHES);
1464 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_MAX_COLUMN_LEN", (zend_long)PDO_ATTR_MAX_COLUMN_LEN);
1465 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_EMULATE_PREPARES", (zend_long)PDO_ATTR_EMULATE_PREPARES);
1466 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_DEFAULT_FETCH_MODE", (zend_long)PDO_ATTR_DEFAULT_FETCH_MODE);
1467 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_DEFAULT_STR_PARAM", (zend_long)PDO_ATTR_DEFAULT_STR_PARAM);
1468 
1469 	REGISTER_PDO_CLASS_CONST_LONG("ERRMODE_SILENT",	(zend_long)PDO_ERRMODE_SILENT);
1470 	REGISTER_PDO_CLASS_CONST_LONG("ERRMODE_WARNING",	(zend_long)PDO_ERRMODE_WARNING);
1471 	REGISTER_PDO_CLASS_CONST_LONG("ERRMODE_EXCEPTION",	(zend_long)PDO_ERRMODE_EXCEPTION);
1472 
1473 	REGISTER_PDO_CLASS_CONST_LONG("CASE_NATURAL",	(zend_long)PDO_CASE_NATURAL);
1474 	REGISTER_PDO_CLASS_CONST_LONG("CASE_LOWER",	(zend_long)PDO_CASE_LOWER);
1475 	REGISTER_PDO_CLASS_CONST_LONG("CASE_UPPER",	(zend_long)PDO_CASE_UPPER);
1476 
1477 	REGISTER_PDO_CLASS_CONST_LONG("NULL_NATURAL",	(zend_long)PDO_NULL_NATURAL);
1478 	REGISTER_PDO_CLASS_CONST_LONG("NULL_EMPTY_STRING",	(zend_long)PDO_NULL_EMPTY_STRING);
1479 	REGISTER_PDO_CLASS_CONST_LONG("NULL_TO_STRING",	(zend_long)PDO_NULL_TO_STRING);
1480 
1481 	REGISTER_PDO_CLASS_CONST_STRING("ERR_NONE",	PDO_ERR_NONE);
1482 
1483 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_NEXT", (zend_long)PDO_FETCH_ORI_NEXT);
1484 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_PRIOR", (zend_long)PDO_FETCH_ORI_PRIOR);
1485 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_FIRST", (zend_long)PDO_FETCH_ORI_FIRST);
1486 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_LAST", (zend_long)PDO_FETCH_ORI_LAST);
1487 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_ABS", (zend_long)PDO_FETCH_ORI_ABS);
1488 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_REL", (zend_long)PDO_FETCH_ORI_REL);
1489 
1490 	REGISTER_PDO_CLASS_CONST_LONG("CURSOR_FWDONLY", (zend_long)PDO_CURSOR_FWDONLY);
1491 	REGISTER_PDO_CLASS_CONST_LONG("CURSOR_SCROLL", (zend_long)PDO_CURSOR_SCROLL);
1492 }
1493 
dbh_free(pdo_dbh_t * dbh,zend_bool free_persistent)1494 static void dbh_free(pdo_dbh_t *dbh, zend_bool free_persistent)
1495 {
1496 	int i;
1497 
1498 	if (dbh->query_stmt) {
1499 		zval_ptr_dtor(&dbh->query_stmt_zval);
1500 		dbh->query_stmt = NULL;
1501 	}
1502 
1503 	if (dbh->is_persistent) {
1504 #if ZEND_DEBUG
1505 		ZEND_ASSERT(!free_persistent || (dbh->refcount == 1));
1506 #endif
1507 		if (!free_persistent && (--dbh->refcount)) {
1508 			return;
1509 		}
1510 	}
1511 
1512 	if (dbh->methods) {
1513 		dbh->methods->closer(dbh);
1514 	}
1515 
1516 	if (dbh->data_source) {
1517 		pefree((char *)dbh->data_source, dbh->is_persistent);
1518 	}
1519 	if (dbh->username) {
1520 		pefree(dbh->username, dbh->is_persistent);
1521 	}
1522 	if (dbh->password) {
1523 		pefree(dbh->password, dbh->is_persistent);
1524 	}
1525 
1526 	if (dbh->persistent_id) {
1527 		pefree((char *)dbh->persistent_id, dbh->is_persistent);
1528 	}
1529 
1530 	if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) {
1531 		zval_ptr_dtor(&dbh->def_stmt_ctor_args);
1532 	}
1533 
1534 	for (i = 0; i < PDO_DBH_DRIVER_METHOD_KIND__MAX; i++) {
1535 		if (dbh->cls_methods[i]) {
1536 			zend_hash_destroy(dbh->cls_methods[i]);
1537 			pefree(dbh->cls_methods[i], dbh->is_persistent);
1538 		}
1539 	}
1540 
1541 	pefree(dbh, dbh->is_persistent);
1542 }
1543 
pdo_dbh_free_storage(zend_object * std)1544 static void pdo_dbh_free_storage(zend_object *std)
1545 {
1546 	pdo_dbh_t *dbh = php_pdo_dbh_fetch_inner(std);
1547 	if (dbh->in_txn && dbh->methods && dbh->methods->rollback) {
1548 		dbh->methods->rollback(dbh);
1549 		dbh->in_txn = 0;
1550 	}
1551 
1552 	if (dbh->is_persistent && dbh->methods && dbh->methods->persistent_shutdown) {
1553 		dbh->methods->persistent_shutdown(dbh);
1554 	}
1555 	zend_object_std_dtor(std);
1556 	dbh_free(dbh, 0);
1557 }
1558 
pdo_dbh_new(zend_class_entry * ce)1559 zend_object *pdo_dbh_new(zend_class_entry *ce)
1560 {
1561 	pdo_dbh_object_t *dbh;
1562 
1563 	dbh = zend_object_alloc(sizeof(pdo_dbh_object_t), ce);
1564 	zend_object_std_init(&dbh->std, ce);
1565 	object_properties_init(&dbh->std, ce);
1566 	rebuild_object_properties(&dbh->std);
1567 	dbh->inner = ecalloc(1, sizeof(pdo_dbh_t));
1568 	dbh->inner->def_stmt_ce = pdo_dbstmt_ce;
1569 
1570 	dbh->std.handlers = &pdo_dbh_object_handlers;
1571 
1572 	return &dbh->std;
1573 }
1574 
1575 /* }}} */
1576 
ZEND_RSRC_DTOR_FUNC(php_pdo_pdbh_dtor)1577 ZEND_RSRC_DTOR_FUNC(php_pdo_pdbh_dtor) /* {{{ */
1578 {
1579 	if (res->ptr) {
1580 		pdo_dbh_t *dbh = (pdo_dbh_t*)res->ptr;
1581 		dbh_free(dbh, 1);
1582 		res->ptr = NULL;
1583 	}
1584 }
1585 /* }}} */
1586