2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2013 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 +----------------------------------------------------------------------+
23 /* The PDO Database Handle Class */
31 #include "ext/standard/info.h"
33 #include "php_pdo_driver.h"
34 #include "php_pdo_int.h"
35 #include "zend_exceptions.h"
36 #include "zend_object_handlers.h"
37 #include "zend_hash.h"
39 static int pdo_dbh_attribute_set(pdo_dbh_t
*dbh
, long attr
, zval
*value TSRMLS_DC
);
41 void pdo_raise_impl_error(pdo_dbh_t
*dbh
, pdo_stmt_t
*stmt
, const char *sqlstate
, const char *supp TSRMLS_DC
) /* {{{ */
43 pdo_error_type
*pdo_err
= &dbh
->error_code
;
47 if (dbh
&& dbh
->error_mode
== PDO_ERRMODE_SILENT
) {
49 /* BUG: if user is running in silent mode and hits an error at the driver level
50 * when they use the PDO methods to call up the error information, they may
51 * get bogus information */
57 pdo_err
= &stmt
->error_code
;
60 strncpy(*pdo_err
, sqlstate
, 6);
62 /* hash sqlstate to error messages */
63 msg
= pdo_sqlstate_state_to_description(*pdo_err
);
65 msg
= "<<Unknown error>>";
69 spprintf(&message
, 0, "SQLSTATE[%s]: %s: %s", *pdo_err
, msg
, supp
);
71 spprintf(&message
, 0, "SQLSTATE[%s]: %s", *pdo_err
, msg
);
74 if (dbh
&& dbh
->error_mode
!= PDO_ERRMODE_EXCEPTION
) {
75 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "%s", message
);
78 zend_class_entry
*def_ex
= php_pdo_get_exception_base(1 TSRMLS_CC
), *pdo_ex
= php_pdo_get_exception();
81 object_init_ex(ex
, pdo_ex
);
83 zend_update_property_string(def_ex
, ex
, "message", sizeof("message")-1, message TSRMLS_CC
);
84 zend_update_property_string(def_ex
, ex
, "code", sizeof("code")-1, *pdo_err TSRMLS_CC
);
89 add_next_index_string(info
, *pdo_err
, 1);
90 add_next_index_long(info
, 0);
92 zend_update_property(pdo_ex
, ex
, "errorInfo", sizeof("errorInfo")-1, info TSRMLS_CC
);
95 zend_throw_exception_object(ex TSRMLS_CC
);
104 void pdo_handle_error(pdo_dbh_t
*dbh
, pdo_stmt_t
*stmt TSRMLS_DC
) /* {{{ */
106 pdo_error_type
*pdo_err
= &dbh
->error_code
;
107 const char *msg
= "<<Unknown>>";
109 long native_code
= 0;
110 char *message
= NULL
;
113 if (dbh
== NULL
|| dbh
->error_mode
== PDO_ERRMODE_SILENT
) {
118 pdo_err
= &stmt
->error_code
;
121 /* hash sqlstate to error messages */
122 msg
= pdo_sqlstate_state_to_description(*pdo_err
);
124 msg
= "<<Unknown error>>";
127 if (dbh
->methods
->fetch_err
) {
131 add_next_index_string(info
, *pdo_err
, 1);
133 if (dbh
->methods
->fetch_err(dbh
, stmt
, info TSRMLS_CC
)) {
136 if (SUCCESS
== zend_hash_index_find(Z_ARRVAL_P(info
), 1, (void**)&item
)) {
137 native_code
= Z_LVAL_PP(item
);
140 if (SUCCESS
== zend_hash_index_find(Z_ARRVAL_P(info
), 2, (void**)&item
)) {
141 supp
= estrndup(Z_STRVAL_PP(item
), Z_STRLEN_PP(item
));
147 spprintf(&message
, 0, "SQLSTATE[%s]: %s: %ld %s", *pdo_err
, msg
, native_code
, supp
);
149 spprintf(&message
, 0, "SQLSTATE[%s]: %s", *pdo_err
, msg
);
152 if (dbh
->error_mode
== PDO_ERRMODE_WARNING
) {
153 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "%s", message
);
154 } else if (EG(exception
) == NULL
) {
156 zend_class_entry
*def_ex
= php_pdo_get_exception_base(1 TSRMLS_CC
), *pdo_ex
= php_pdo_get_exception();
159 object_init_ex(ex
, pdo_ex
);
161 zend_update_property_string(def_ex
, ex
, "message", sizeof("message")-1, message TSRMLS_CC
);
162 zend_update_property_string(def_ex
, ex
, "code", sizeof("code")-1, *pdo_err TSRMLS_CC
);
165 zend_update_property(pdo_ex
, ex
, "errorInfo", sizeof("errorInfo")-1, info TSRMLS_CC
);
168 zend_throw_exception_object(ex TSRMLS_CC
);
172 zval_ptr_dtor(&info
);
185 static char *dsn_from_uri(char *uri
, char *buf
, size_t buflen TSRMLS_DC
) /* {{{ */
190 stream
= php_stream_open_wrapper(uri
, "rb", REPORT_ERRORS
, NULL
);
192 dsn
= php_stream_get_line(stream
, buf
, buflen
, NULL
);
193 php_stream_close(stream
);
199 /* {{{ proto void PDO::__construct(string dsn, string username, string passwd [, array options])
201 static PHP_METHOD(PDO
, dbh_constructor
)
203 zval
*object
= getThis();
204 pdo_dbh_t
*dbh
= NULL
;
205 zend_bool is_persistent
= FALSE
;
209 char *username
=NULL
, *password
=NULL
;
210 int usernamelen
, passwordlen
;
211 pdo_driver_t
*driver
= NULL
;
212 zval
*options
= NULL
;
214 int call_factory
= 1;
216 if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|s!s!a!", &data_source
, &data_source_len
,
217 &username
, &usernamelen
, &password
, &passwordlen
, &options
)) {
222 /* parse the data source name */
223 colon
= strchr(data_source
, ':');
226 /* let's see if this string has a matching dsn in the php.ini */
227 char *ini_dsn
= NULL
;
229 snprintf(alt_dsn
, sizeof(alt_dsn
), "pdo.dsn.%s", data_source
);
230 if (FAILURE
== cfg_get_string(alt_dsn
, &ini_dsn
)) {
231 zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC
, "invalid data source name");
237 data_source
= ini_dsn
;
238 colon
= strchr(data_source
, ':');
241 zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC
, "invalid data source name (via INI: %s)", alt_dsn
);
247 if (!strncmp(data_source
, "uri:", sizeof("uri:")-1)) {
248 /* the specified URI holds connection details */
249 data_source
= dsn_from_uri(data_source
+ sizeof("uri:")-1, alt_dsn
, sizeof(alt_dsn
) TSRMLS_CC
);
251 zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC
, "invalid data source URI");
255 colon
= strchr(data_source
, ':');
257 zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC
, "invalid data source name (via URI)");
263 driver
= pdo_find_driver(data_source
, colon
- data_source
);
266 /* NB: don't want to include the data_source in the error message as
267 * it might contain a password */
268 zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC
, "could not find driver");
273 dbh
= (pdo_dbh_t
*) zend_object_store_get_object(object TSRMLS_CC
);
275 /* is this supposed to be a persistent connection ? */
279 char *hashkey
= NULL
;
280 zend_rsrc_list_entry
*le
;
281 pdo_dbh_t
*pdbh
= NULL
;
283 if (SUCCESS
== zend_hash_index_find(Z_ARRVAL_P(options
), PDO_ATTR_PERSISTENT
, (void**)&v
)) {
284 if (Z_TYPE_PP(v
) == IS_STRING
&& !is_numeric_string(Z_STRVAL_PP(v
), Z_STRLEN_PP(v
), NULL
, NULL
, 0) && Z_STRLEN_PP(v
) > 0) {
285 /* user specified key */
286 plen
= spprintf(&hashkey
, 0, "PDO:DBH:DSN=%s:%s:%s:%s", data_source
,
287 username
? username
: "",
288 password
? password
: "",
292 convert_to_long_ex(v
);
293 is_persistent
= Z_LVAL_PP(v
) ? 1 : 0;
294 plen
= spprintf(&hashkey
, 0, "PDO:DBH:DSN=%s:%s:%s", data_source
,
295 username
? username
: "",
296 password
? password
: "");
301 /* let's see if we have one cached.... */
302 if (SUCCESS
== zend_hash_find(&EG(persistent_list
), hashkey
, plen
+1, (void*)&le
)) {
303 if (Z_TYPE_P(le
) == php_pdo_list_entry()) {
304 pdbh
= (pdo_dbh_t
*)le
->ptr
;
306 /* is the connection still alive ? */
307 if (pdbh
->methods
->check_liveness
&& FAILURE
== (pdbh
->methods
->check_liveness
)(pdbh TSRMLS_CC
)) {
308 /* nope... need to kill it */
317 /* need a brand new pdbh */
318 pdbh
= pecalloc(1, sizeof(*pdbh
), 1);
321 php_error_docref(NULL TSRMLS_CC
, E_ERROR
, "out of memory while allocating PDO handle");
325 pdbh
->is_persistent
= 1;
326 if (!(pdbh
->persistent_id
= pemalloc(plen
+ 1, 1))) {
327 php_error_docref(NULL TSRMLS_CC
, E_ERROR
, "out of memory while allocating PDO handle");
329 memcpy((char *)pdbh
->persistent_id
, hashkey
, plen
+1);
330 pdbh
->persistent_id_len
= plen
+1;
332 pdbh
->std
.properties
= NULL
;
337 /* let's copy the emalloc bits over from the other handle */
338 if (pdbh
->std
.properties
) {
339 zend_hash_destroy(dbh
->std
.properties
);
340 efree(dbh
->std
.properties
);
342 pdbh
->std
.ce
= dbh
->std
.ce
;
343 pdbh
->def_stmt_ce
= dbh
->def_stmt_ce
;
344 pdbh
->def_stmt_ctor_args
= dbh
->def_stmt_ctor_args
;
345 pdbh
->std
.properties
= dbh
->std
.properties
;
346 pdbh
->std
.properties_table
= dbh
->std
.properties_table
;
348 /* kill the non-persistent thingamy */
350 /* switch over to the persistent one */
352 zend_object_store_set_object(object
, dbh TSRMLS_CC
);
362 dbh
->data_source_len
= strlen(colon
+ 1);
363 dbh
->data_source
= (const char*)pestrdup(colon
+ 1, is_persistent
);
364 dbh
->username
= username
? pestrdup(username
, is_persistent
) : NULL
;
365 dbh
->password
= password
? pestrdup(password
, is_persistent
) : NULL
;
366 dbh
->default_fetch_type
= PDO_FETCH_BOTH
;
369 dbh
->auto_commit
= pdo_attr_lval(options
, PDO_ATTR_AUTOCOMMIT
, 1 TSRMLS_CC
);
371 if (!dbh
->data_source
|| (username
&& !dbh
->username
) || (password
&& !dbh
->password
)) {
372 php_error_docref(NULL TSRMLS_CC
, E_ERROR
, "out of memory");
376 /* we got a persistent guy from our cache */
380 if (driver
->db_handle_factory(dbh
, options TSRMLS_CC
)) {
384 zend_rsrc_list_entry le
;
386 /* register in the persistent list etc. */
387 /* we should also need to replace the object store entry,
388 since it was created with emalloc */
390 le
.type
= php_pdo_list_entry();
393 if (FAILURE
== zend_hash_update(&EG(persistent_list
),
394 (char*)dbh
->persistent_id
, dbh
->persistent_id_len
, (void*)&le
,
396 php_error_docref(NULL TSRMLS_CC
, E_ERROR
, "Failed to register persistent entry");
400 dbh
->driver
= driver
;
407 zend_hash_internal_pointer_reset(Z_ARRVAL_P(options
));
408 while (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(options
), (void**)&attr_value
)
409 && HASH_KEY_IS_LONG
== zend_hash_get_current_key(Z_ARRVAL_P(options
), &str_key
, &long_key
, 0)) {
411 pdo_dbh_attribute_set(dbh
, long_key
, *attr_value TSRMLS_CC
);
412 zend_hash_move_forward(Z_ARRVAL_P(options
));
419 /* the connection failed; things will tidy up in free_storage */
420 /* XXX raise exception */
425 static zval
*pdo_stmt_instantiate(pdo_dbh_t
*dbh
, zval
*object
, zend_class_entry
*dbstmt_ce
, zval
*ctor_args TSRMLS_DC
) /* {{{ */
428 if (Z_TYPE_P(ctor_args
) != IS_ARRAY
) {
429 pdo_raise_impl_error(dbh
, NULL
, "HY000", "constructor arguments must be passed as an array" TSRMLS_CC
);
432 if (!dbstmt_ce
->constructor
) {
433 pdo_raise_impl_error(dbh
, NULL
, "HY000", "user-supplied statement does not accept constructor arguments" TSRMLS_CC
);
438 Z_TYPE_P(object
) = IS_OBJECT
;
439 object_init_ex(object
, dbstmt_ce
);
440 Z_SET_REFCOUNT_P(object
, 1);
441 Z_SET_ISREF_P(object
);
446 static void pdo_stmt_construct(pdo_stmt_t
*stmt
, zval
*object
, zend_class_entry
*dbstmt_ce
, zval
*ctor_args TSRMLS_DC
) /* {{{ */
451 MAKE_STD_ZVAL(query_string
);
452 ZVAL_STRINGL(query_string
, stmt
->query_string
, stmt
->query_stringlen
, 1);
453 ZVAL_STRINGL(&z_key
, "queryString", sizeof("queryString")-1, 0);
454 std_object_handlers
.write_property(object
, &z_key
, query_string
, 0 TSRMLS_CC
);
455 zval_ptr_dtor(&query_string
);
457 if (dbstmt_ce
->constructor
) {
459 zend_fcall_info_cache fcc
;
462 fci
.size
= sizeof(zend_fcall_info
);
463 fci
.function_table
= &dbstmt_ce
->function_table
;
464 fci
.function_name
= NULL
;
465 fci
.object_ptr
= object
;
466 fci
.symbol_table
= NULL
;
467 fci
.retval_ptr_ptr
= &retval
;
469 HashTable
*ht
= Z_ARRVAL_P(ctor_args
);
473 fci
.params
= safe_emalloc(sizeof(zval
*), ht
->nNumOfElements
, 0);
476 fci
.params
[fci
.param_count
++] = (zval
**)p
->pData
;
483 fci
.no_separation
= 1;
486 fcc
.function_handler
= dbstmt_ce
->constructor
;
487 fcc
.calling_scope
= EG(scope
);
488 fcc
.called_scope
= Z_OBJCE_P(object
);
489 fcc
.object_ptr
= object
;
491 if (zend_call_function(&fci
, &fcc TSRMLS_CC
) == FAILURE
) {
494 object
= NULL
; /* marks failure */
496 zval_ptr_dtor(&retval
);
506 /* {{{ proto object PDO::prepare(string statment [, array options])
507 Prepares a statement for execution and returns a statement object */
508 static PHP_METHOD(PDO
, prepare
)
510 pdo_dbh_t
*dbh
= zend_object_store_get_object(getThis() TSRMLS_CC
);
514 zval
*options
= NULL
, **opt
, **item
, *ctor_args
;
515 zend_class_entry
*dbstmt_ce
, **pce
;
517 if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a", &statement
,
518 &statement_len
, &options
)) {
525 if (ZEND_NUM_ARGS() > 1 && SUCCESS
== zend_hash_index_find(Z_ARRVAL_P(options
), PDO_ATTR_STATEMENT_CLASS
, (void**)&opt
)) {
526 if (Z_TYPE_PP(opt
) != IS_ARRAY
|| zend_hash_index_find(Z_ARRVAL_PP(opt
), 0, (void**)&item
) == FAILURE
527 || Z_TYPE_PP(item
) != IS_STRING
528 || zend_lookup_class(Z_STRVAL_PP(item
), Z_STRLEN_PP(item
), &pce TSRMLS_CC
) == FAILURE
530 pdo_raise_impl_error(dbh
, NULL
, "HY000",
531 "PDO::ATTR_STATEMENT_CLASS requires format array(classname, array(ctor_args)); "
532 "the classname must be a string specifying an existing class"
534 PDO_HANDLE_DBH_ERR();
538 if (!instanceof_function(dbstmt_ce
, pdo_dbstmt_ce TSRMLS_CC
)) {
539 pdo_raise_impl_error(dbh
, NULL
, "HY000",
540 "user-supplied statement class must be derived from PDOStatement" TSRMLS_CC
);
541 PDO_HANDLE_DBH_ERR();
544 if (dbstmt_ce
->constructor
&& !(dbstmt_ce
->constructor
->common
.fn_flags
& (ZEND_ACC_PRIVATE
|ZEND_ACC_PROTECTED
))) {
545 pdo_raise_impl_error(dbh
, NULL
, "HY000",
546 "user-supplied statement class cannot have a public constructor" TSRMLS_CC
);
547 PDO_HANDLE_DBH_ERR();
550 if (zend_hash_index_find(Z_ARRVAL_PP(opt
), 1, (void**)&item
) == SUCCESS
) {
551 if (Z_TYPE_PP(item
) != IS_ARRAY
) {
552 pdo_raise_impl_error(dbh
, NULL
, "HY000",
553 "PDO::ATTR_STATEMENT_CLASS requires format array(classname, ctor_args); "
554 "ctor_args must be an array"
556 PDO_HANDLE_DBH_ERR();
564 dbstmt_ce
= dbh
->def_stmt_ce
;
565 ctor_args
= dbh
->def_stmt_ctor_args
;
568 if (!pdo_stmt_instantiate(dbh
, return_value
, dbstmt_ce
, ctor_args TSRMLS_CC
)) {
569 pdo_raise_impl_error(dbh
, NULL
, "HY000",
570 "failed to instantiate user-supplied statement class"
572 PDO_HANDLE_DBH_ERR();
575 stmt
= (pdo_stmt_t
*)zend_object_store_get_object(return_value TSRMLS_CC
);
577 /* unconditionally keep this for later reference */
578 stmt
->query_string
= estrndup(statement
, statement_len
);
579 stmt
->query_stringlen
= statement_len
;
580 stmt
->default_fetch_type
= dbh
->default_fetch_type
;
582 /* give it a reference to me */
583 zend_objects_store_add_ref(getThis() TSRMLS_CC
);
584 php_pdo_dbh_addref(dbh TSRMLS_CC
);
585 stmt
->database_object_handle
= *getThis();
586 /* we haven't created a lazy object yet */
587 ZVAL_NULL(&stmt
->lazy_object_ref
);
589 if (dbh
->methods
->preparer(dbh
, statement
, statement_len
, stmt
, options TSRMLS_CC
)) {
590 pdo_stmt_construct(stmt
, return_value
, dbstmt_ce
, ctor_args TSRMLS_CC
);
594 PDO_HANDLE_DBH_ERR();
596 /* kill the object handle for the stmt here */
597 zval_dtor(return_value
);
603 /* {{{ proto bool PDO::beginTransaction()
604 Initiates a transaction */
605 static PHP_METHOD(PDO
, beginTransaction
)
607 pdo_dbh_t
*dbh
= zend_object_store_get_object(getThis() TSRMLS_CC
);
609 if (zend_parse_parameters_none() == FAILURE
) {
615 zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC
, "There is already an active transaction");
619 if (!dbh
->methods
->begin
) {
620 /* TODO: this should be an exception; see the auto-commit mode
622 zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC
, "This driver doesn't support transactions");
626 if (dbh
->methods
->begin(dbh TSRMLS_CC
)) {
631 PDO_HANDLE_DBH_ERR();
636 /* {{{ proto bool PDO::commit()
637 Commit a transaction */
638 static PHP_METHOD(PDO
, commit
)
640 pdo_dbh_t
*dbh
= zend_object_store_get_object(getThis() TSRMLS_CC
);
642 if (zend_parse_parameters_none() == FAILURE
) {
648 zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC
, "There is no active transaction");
652 if (dbh
->methods
->commit(dbh TSRMLS_CC
)) {
657 PDO_HANDLE_DBH_ERR();
662 /* {{{ proto bool PDO::rollBack()
663 roll back a transaction */
664 static PHP_METHOD(PDO
, rollBack
)
666 pdo_dbh_t
*dbh
= zend_object_store_get_object(getThis() TSRMLS_CC
);
668 if (zend_parse_parameters_none() == FAILURE
) {
674 zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC
, "There is no active transaction");
678 if (dbh
->methods
->rollback(dbh TSRMLS_CC
)) {
683 PDO_HANDLE_DBH_ERR();
688 /* {{{ proto bool PDO::inTransaction()
689 determine if inside a transaction */
690 static PHP_METHOD(PDO
, inTransaction
)
692 pdo_dbh_t
*dbh
= zend_object_store_get_object(getThis() TSRMLS_CC
);
694 if (zend_parse_parameters_none() == FAILURE
) {
699 if (!dbh
->methods
->in_transaction
) {
700 RETURN_BOOL(dbh
->in_txn
);
703 RETURN_BOOL(dbh
->methods
->in_transaction(dbh TSRMLS_CC
));
707 static int pdo_dbh_attribute_set(pdo_dbh_t
*dbh
, long attr
, zval
*value TSRMLS_DC
) /* {{{ */
710 #define PDO_LONG_PARAM_CHECK \
711 if (Z_TYPE_P(value) != IS_LONG && Z_TYPE_P(value) != IS_STRING && Z_TYPE_P(value) != IS_BOOL) { \
712 pdo_raise_impl_error(dbh, NULL, "HY000", "attribute value must be an integer" TSRMLS_CC); \
713 PDO_HANDLE_DBH_ERR(); \
718 case PDO_ATTR_ERRMODE
:
719 PDO_LONG_PARAM_CHECK
;
720 convert_to_long(value
);
721 switch (Z_LVAL_P(value
)) {
722 case PDO_ERRMODE_SILENT
:
723 case PDO_ERRMODE_WARNING
:
724 case PDO_ERRMODE_EXCEPTION
:
725 dbh
->error_mode
= Z_LVAL_P(value
);
728 pdo_raise_impl_error(dbh
, NULL
, "HY000", "invalid error mode" TSRMLS_CC
);
729 PDO_HANDLE_DBH_ERR();
735 PDO_LONG_PARAM_CHECK
;
736 convert_to_long(value
);
737 switch (Z_LVAL_P(value
)) {
738 case PDO_CASE_NATURAL
:
741 dbh
->desired_case
= Z_LVAL_P(value
);
744 pdo_raise_impl_error(dbh
, NULL
, "HY000", "invalid case folding mode" TSRMLS_CC
);
745 PDO_HANDLE_DBH_ERR();
750 case PDO_ATTR_ORACLE_NULLS
:
751 PDO_LONG_PARAM_CHECK
;
752 convert_to_long(value
);
753 dbh
->oracle_nulls
= Z_LVAL_P(value
);
756 case PDO_ATTR_DEFAULT_FETCH_MODE
:
757 if (Z_TYPE_P(value
) == IS_ARRAY
) {
759 if (zend_hash_index_find(Z_ARRVAL_P(value
), 0, (void**)&tmp
) == SUCCESS
&& Z_TYPE_PP(tmp
) == IS_LONG
) {
760 if (Z_LVAL_PP(tmp
) == PDO_FETCH_INTO
|| Z_LVAL_PP(tmp
) == PDO_FETCH_CLASS
) {
761 pdo_raise_impl_error(dbh
, NULL
, "HY000", "FETCH_INTO and FETCH_CLASS are not yet supported as default fetch modes" TSRMLS_CC
);
766 PDO_LONG_PARAM_CHECK
;
768 convert_to_long(value
);
769 if (Z_LVAL_P(value
) == PDO_FETCH_USE_DEFAULT
) {
770 pdo_raise_impl_error(dbh
, NULL
, "HY000", "invalid fetch mode type" TSRMLS_CC
);
773 dbh
->default_fetch_type
= Z_LVAL_P(value
);
776 case PDO_ATTR_STRINGIFY_FETCHES
:
777 PDO_LONG_PARAM_CHECK
;
778 convert_to_long(value
);
779 dbh
->stringify
= Z_LVAL_P(value
) ? 1 : 0;
782 case PDO_ATTR_STATEMENT_CLASS
: {
783 /* array(string classname, array(mixed ctor_args)) */
784 zend_class_entry
**pce
;
787 if (dbh
->is_persistent
) {
788 pdo_raise_impl_error(dbh
, NULL
, "HY000",
789 "PDO::ATTR_STATEMENT_CLASS cannot be used with persistent PDO instances"
791 PDO_HANDLE_DBH_ERR();
794 if (Z_TYPE_P(value
) != IS_ARRAY
795 || zend_hash_index_find(Z_ARRVAL_P(value
), 0, (void**)&item
) == FAILURE
796 || Z_TYPE_PP(item
) != IS_STRING
797 || zend_lookup_class(Z_STRVAL_PP(item
), Z_STRLEN_PP(item
), &pce TSRMLS_CC
) == FAILURE
799 pdo_raise_impl_error(dbh
, NULL
, "HY000",
800 "PDO::ATTR_STATEMENT_CLASS requires format array(classname, array(ctor_args)); "
801 "the classname must be a string specifying an existing class"
803 PDO_HANDLE_DBH_ERR();
806 if (!instanceof_function(*pce
, pdo_dbstmt_ce TSRMLS_CC
)) {
807 pdo_raise_impl_error(dbh
, NULL
, "HY000",
808 "user-supplied statement class must be derived from PDOStatement" TSRMLS_CC
);
809 PDO_HANDLE_DBH_ERR();
812 if ((*pce
)->constructor
&& !((*pce
)->constructor
->common
.fn_flags
& (ZEND_ACC_PRIVATE
|ZEND_ACC_PROTECTED
))) {
813 pdo_raise_impl_error(dbh
, NULL
, "HY000",
814 "user-supplied statement class cannot have a public constructor" TSRMLS_CC
);
815 PDO_HANDLE_DBH_ERR();
818 dbh
->def_stmt_ce
= *pce
;
819 if (dbh
->def_stmt_ctor_args
) {
820 zval_ptr_dtor(&dbh
->def_stmt_ctor_args
);
821 dbh
->def_stmt_ctor_args
= NULL
;
823 if (zend_hash_index_find(Z_ARRVAL_P(value
), 1, (void**)&item
) == SUCCESS
) {
824 if (Z_TYPE_PP(item
) != IS_ARRAY
) {
825 pdo_raise_impl_error(dbh
, NULL
, "HY000",
826 "PDO::ATTR_STATEMENT_CLASS requires format array(classname, array(ctor_args)); "
827 "ctor_args must be an array"
829 PDO_HANDLE_DBH_ERR();
833 dbh
->def_stmt_ctor_args
= *item
;
842 if (!dbh
->methods
->set_attribute
) {
847 if (dbh
->methods
->set_attribute(dbh
, attr
, value TSRMLS_CC
)) {
852 if (attr
== PDO_ATTR_AUTOCOMMIT
) {
853 zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC
, "The auto-commit mode cannot be changed for this driver");
854 } else if (!dbh
->methods
->set_attribute
) {
855 pdo_raise_impl_error(dbh
, NULL
, "IM001", "driver does not support setting attributes" TSRMLS_CC
);
857 PDO_HANDLE_DBH_ERR();
863 /* {{{ proto bool PDO::setAttribute(long attribute, mixed value)
865 static PHP_METHOD(PDO
, setAttribute
)
867 pdo_dbh_t
*dbh
= zend_object_store_get_object(getThis() TSRMLS_CC
);
871 if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "lz", &attr
, &value
)) {
878 if (pdo_dbh_attribute_set(dbh
, attr
, value TSRMLS_CC
) != FAILURE
) {
885 /* {{{ proto mixed PDO::getAttribute(long attribute)
887 static PHP_METHOD(PDO
, getAttribute
)
889 pdo_dbh_t
*dbh
= zend_object_store_get_object(getThis() TSRMLS_CC
);
892 if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &attr
)) {
899 /* handle generic PDO-level atributes */
901 case PDO_ATTR_PERSISTENT
:
902 RETURN_BOOL(dbh
->is_persistent
);
905 RETURN_LONG(dbh
->desired_case
);
907 case PDO_ATTR_ORACLE_NULLS
:
908 RETURN_LONG(dbh
->oracle_nulls
);
910 case PDO_ATTR_ERRMODE
:
911 RETURN_LONG(dbh
->error_mode
);
913 case PDO_ATTR_DRIVER_NAME
:
914 RETURN_STRINGL((char*)dbh
->driver
->driver_name
, dbh
->driver
->driver_name_len
, 1);
916 case PDO_ATTR_STATEMENT_CLASS
:
917 array_init(return_value
);
918 add_next_index_string(return_value
, dbh
->def_stmt_ce
->name
, 1);
919 if (dbh
->def_stmt_ctor_args
) {
920 Z_ADDREF_P(dbh
->def_stmt_ctor_args
);
921 add_next_index_zval(return_value
, dbh
->def_stmt_ctor_args
);
924 case PDO_ATTR_DEFAULT_FETCH_MODE
:
925 RETURN_LONG(dbh
->default_fetch_type
);
929 if (!dbh
->methods
->get_attribute
) {
930 pdo_raise_impl_error(dbh
, NULL
, "IM001", "driver does not support getting attributes" TSRMLS_CC
);
934 switch (dbh
->methods
->get_attribute(dbh
, attr
, return_value TSRMLS_CC
)) {
936 PDO_HANDLE_DBH_ERR();
940 pdo_raise_impl_error(dbh
, NULL
, "IM001", "driver does not support that attribute" TSRMLS_CC
);
949 /* {{{ proto long PDO::exec(string query)
950 Execute a query that does not return a row set, returning the number of affected rows */
951 static PHP_METHOD(PDO
, exec
)
953 pdo_dbh_t
*dbh
= zend_object_store_get_object(getThis() TSRMLS_CC
);
958 if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &statement
, &statement_len
)) {
962 if (!statement_len
) {
963 pdo_raise_impl_error(dbh
, NULL
, "HY000", "trying to execute an empty query" TSRMLS_CC
);
968 ret
= dbh
->methods
->doer(dbh
, statement
, statement_len TSRMLS_CC
);
970 PDO_HANDLE_DBH_ERR();
979 /* {{{ proto string PDO::lastInsertId([string seqname])
980 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. */
981 static PHP_METHOD(PDO
, lastInsertId
)
983 pdo_dbh_t
*dbh
= zend_object_store_get_object(getThis() TSRMLS_CC
);
987 if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s!", &name
, &namelen
)) {
993 if (!dbh
->methods
->last_id
) {
994 pdo_raise_impl_error(dbh
, NULL
, "IM001", "driver does not support lastInsertId()" TSRMLS_CC
);
997 Z_STRVAL_P(return_value
) = dbh
->methods
->last_id(dbh
, name
, &Z_STRLEN_P(return_value
) TSRMLS_CC
);
998 if (!Z_STRVAL_P(return_value
)) {
999 PDO_HANDLE_DBH_ERR();
1002 Z_TYPE_P(return_value
) = IS_STRING
;
1008 /* {{{ proto string PDO::errorCode()
1009 Fetch the error code associated with the last operation on the database handle */
1010 static PHP_METHOD(PDO
, errorCode
)
1012 pdo_dbh_t
*dbh
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1014 if (zend_parse_parameters_none() == FAILURE
) {
1017 PDO_CONSTRUCT_CHECK
;
1019 if (dbh
->query_stmt
) {
1020 RETURN_STRING(dbh
->query_stmt
->error_code
, 1);
1023 if (dbh
->error_code
[0] == '\0') {
1028 * Making sure that we fallback to the default implementation
1029 * if the dbh->error_code is not null.
1031 RETURN_STRING(dbh
->error_code
, 1);
1035 /* {{{ proto int PDO::errorInfo()
1036 Fetch extended error information associated with the last operation on the database handle */
1037 static PHP_METHOD(PDO
, errorInfo
)
1040 int error_count_diff
= 0;
1041 int error_expected_count
= 3;
1043 pdo_dbh_t
*dbh
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1045 if (zend_parse_parameters_none() == FAILURE
) {
1049 PDO_CONSTRUCT_CHECK
;
1051 array_init(return_value
);
1053 if (dbh
->query_stmt
) {
1054 add_next_index_string(return_value
, dbh
->query_stmt
->error_code
, 1);
1056 add_next_index_string(return_value
, dbh
->error_code
, 1);
1059 if (dbh
->methods
->fetch_err
) {
1060 dbh
->methods
->fetch_err(dbh
, dbh
->query_stmt
, return_value TSRMLS_CC
);
1064 * In order to be consistent, we have to make sure we add the good amount
1065 * of nulls depending on the current number of elements. We make a simple
1066 * difference and add the needed elements
1068 error_count
= zend_hash_num_elements(Z_ARRVAL_P(return_value
));
1070 if (error_expected_count
> error_count
) {
1073 error_count_diff
= error_expected_count
- error_count
;
1074 for (current_index
= 0; current_index
< error_count_diff
; current_index
++) {
1075 add_next_index_null(return_value
);
1081 /* {{{ proto object PDO::query(string sql [, PDOStatement::setFetchMode() args])
1082 Prepare and execute $sql; returns the statement object for iteration */
1083 static PHP_METHOD(PDO
, query
)
1085 pdo_dbh_t
*dbh
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1090 /* Return a meaningful error when no parameters were passed */
1091 if (!ZEND_NUM_ARGS()) {
1092 zend_parse_parameters(0 TSRMLS_CC
, "z|z", NULL
, NULL
);
1096 if (FAILURE
== zend_parse_parameters(1 TSRMLS_CC
, "s", &statement
,
1101 PDO_DBH_CLEAR_ERR();
1102 PDO_CONSTRUCT_CHECK
;
1104 if (!pdo_stmt_instantiate(dbh
, return_value
, dbh
->def_stmt_ce
, dbh
->def_stmt_ctor_args TSRMLS_CC
)) {
1105 pdo_raise_impl_error(dbh
, NULL
, "HY000", "failed to instantiate user supplied statement class" TSRMLS_CC
);
1108 stmt
= (pdo_stmt_t
*)zend_object_store_get_object(return_value TSRMLS_CC
);
1110 /* unconditionally keep this for later reference */
1111 stmt
->query_string
= estrndup(statement
, statement_len
);
1112 stmt
->query_stringlen
= statement_len
;
1114 stmt
->default_fetch_type
= dbh
->default_fetch_type
;
1115 stmt
->active_query_string
= stmt
->query_string
;
1116 stmt
->active_query_stringlen
= statement_len
;
1118 /* give it a reference to me */
1119 zend_objects_store_add_ref(getThis() TSRMLS_CC
);
1120 php_pdo_dbh_addref(dbh TSRMLS_CC
);
1121 stmt
->database_object_handle
= *getThis();
1122 /* we haven't created a lazy object yet */
1123 ZVAL_NULL(&stmt
->lazy_object_ref
);
1125 if (dbh
->methods
->preparer(dbh
, statement
, statement_len
, stmt
, NULL TSRMLS_CC
)) {
1126 PDO_STMT_CLEAR_ERR();
1127 if (ZEND_NUM_ARGS() == 1 || SUCCESS
== pdo_stmt_setup_fetch_mode(INTERNAL_FUNCTION_PARAM_PASSTHRU
, stmt
, 1)) {
1129 /* now execute the statement */
1130 PDO_STMT_CLEAR_ERR();
1131 if (stmt
->methods
->executer(stmt TSRMLS_CC
)) {
1133 if (!stmt
->executed
) {
1134 if (stmt
->dbh
->alloc_own_columns
) {
1135 ret
= pdo_stmt_describe_columns(stmt TSRMLS_CC
);
1140 pdo_stmt_construct(stmt
, return_value
, dbh
->def_stmt_ce
, dbh
->def_stmt_ctor_args TSRMLS_CC
);
1145 /* something broke */
1146 dbh
->query_stmt
= stmt
;
1147 dbh
->query_stmt_zval
= *return_value
;
1148 PDO_HANDLE_STMT_ERR();
1150 PDO_HANDLE_DBH_ERR();
1151 zval_dtor(return_value
);
1158 /* {{{ proto string PDO::quote(string string [, int paramtype])
1159 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 */
1160 static PHP_METHOD(PDO
, quote
)
1162 pdo_dbh_t
*dbh
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1165 long paramtype
= PDO_PARAM_STR
;
1169 if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", &str
, &str_len
, ¶mtype
)) {
1173 PDO_DBH_CLEAR_ERR();
1174 PDO_CONSTRUCT_CHECK
;
1175 if (!dbh
->methods
->quoter
) {
1176 pdo_raise_impl_error(dbh
, NULL
, "IM001", "driver does not support quoting" TSRMLS_CC
);
1180 if (dbh
->methods
->quoter(dbh
, str
, str_len
, &qstr
, &qlen
, paramtype TSRMLS_CC
)) {
1181 RETURN_STRINGL(qstr
, qlen
, 0);
1183 PDO_HANDLE_DBH_ERR();
1188 /* {{{ proto int PDO::__wakeup()
1189 Prevents use of a PDO instance that has been unserialized */
1190 static PHP_METHOD(PDO
, __wakeup
)
1192 zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC
, "You cannot serialize or unserialize PDO instances");
1196 /* {{{ proto int PDO::__sleep()
1197 Prevents serialization of a PDO instance */
1198 static PHP_METHOD(PDO
, __sleep
)
1200 zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC
, "You cannot serialize or unserialize PDO instances");
1204 /* {{{ proto array PDO::getAvailableDrivers()
1205 Return array of available PDO drivers */
1206 static PHP_METHOD(PDO
, getAvailableDrivers
)
1209 pdo_driver_t
**pdriver
;
1211 if (zend_parse_parameters_none() == FAILURE
) {
1215 array_init(return_value
);
1217 zend_hash_internal_pointer_reset_ex(&pdo_driver_hash
, &pos
);
1218 while (SUCCESS
== zend_hash_get_current_data_ex(&pdo_driver_hash
, (void**)&pdriver
, &pos
)) {
1219 add_next_index_stringl(return_value
, (char*)(*pdriver
)->driver_name
, (*pdriver
)->driver_name_len
, 1);
1220 zend_hash_move_forward_ex(&pdo_driver_hash
, &pos
);
1226 ZEND_BEGIN_ARG_INFO_EX(arginfo_pdo___construct
, 0, 0, 3)
1227 ZEND_ARG_INFO(0, dsn
)
1228 ZEND_ARG_INFO(0, username
)
1229 ZEND_ARG_INFO(0, passwd
)
1230 ZEND_ARG_INFO(0, options
) /* array */
1233 ZEND_BEGIN_ARG_INFO_EX(arginfo_pdo_prepare
, 0, 0, 1)
1234 ZEND_ARG_INFO(0, statement
)
1235 ZEND_ARG_INFO(0, options
) /* array */
1238 ZEND_BEGIN_ARG_INFO(arginfo_pdo_setattribute
, 0)
1239 ZEND_ARG_INFO(0, attribute
)
1240 ZEND_ARG_INFO(0, value
)
1243 ZEND_BEGIN_ARG_INFO(arginfo_pdo_getattribute
, 0)
1244 ZEND_ARG_INFO(0, attribute
)
1247 ZEND_BEGIN_ARG_INFO(arginfo_pdo_exec
, 0)
1248 ZEND_ARG_INFO(0, query
)
1251 ZEND_BEGIN_ARG_INFO_EX(arginfo_pdo_lastinsertid
, 0, 0, 0)
1252 ZEND_ARG_INFO(0, seqname
)
1255 ZEND_BEGIN_ARG_INFO_EX(arginfo_pdo_quote
, 0, 0, 1)
1256 ZEND_ARG_INFO(0, string
)
1257 ZEND_ARG_INFO(0, paramtype
)
1260 ZEND_BEGIN_ARG_INFO(arginfo_pdo__void
, 0)
1264 const zend_function_entry pdo_dbh_functions
[] = {
1265 ZEND_MALIAS(PDO
, __construct
, dbh_constructor
, arginfo_pdo___construct
, ZEND_ACC_PUBLIC
)
1266 PHP_ME(PDO
, prepare
, arginfo_pdo_prepare
, ZEND_ACC_PUBLIC
)
1267 PHP_ME(PDO
, beginTransaction
, arginfo_pdo__void
, ZEND_ACC_PUBLIC
)
1268 PHP_ME(PDO
, commit
, arginfo_pdo__void
, ZEND_ACC_PUBLIC
)
1269 PHP_ME(PDO
, rollBack
, arginfo_pdo__void
, ZEND_ACC_PUBLIC
)
1270 PHP_ME(PDO
, inTransaction
, arginfo_pdo__void
, ZEND_ACC_PUBLIC
)
1271 PHP_ME(PDO
, setAttribute
, arginfo_pdo_setattribute
, ZEND_ACC_PUBLIC
)
1272 PHP_ME(PDO
, exec
, arginfo_pdo_exec
, ZEND_ACC_PUBLIC
)
1273 PHP_ME(PDO
, query
, NULL
, ZEND_ACC_PUBLIC
)
1274 PHP_ME(PDO
, lastInsertId
, arginfo_pdo_lastinsertid
, ZEND_ACC_PUBLIC
)
1275 PHP_ME(PDO
, errorCode
, arginfo_pdo__void
, ZEND_ACC_PUBLIC
)
1276 PHP_ME(PDO
, errorInfo
, arginfo_pdo__void
, ZEND_ACC_PUBLIC
)
1277 PHP_ME(PDO
, getAttribute
, arginfo_pdo_getattribute
, ZEND_ACC_PUBLIC
)
1278 PHP_ME(PDO
, quote
, arginfo_pdo_quote
, ZEND_ACC_PUBLIC
)
1279 PHP_ME(PDO
, __wakeup
, arginfo_pdo__void
, ZEND_ACC_PUBLIC
|ZEND_ACC_FINAL
)
1280 PHP_ME(PDO
, __sleep
, arginfo_pdo__void
, ZEND_ACC_PUBLIC
|ZEND_ACC_FINAL
)
1281 PHP_ME(PDO
, getAvailableDrivers
, arginfo_pdo__void
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
1285 /* {{{ overloaded object handlers for PDO class */
1286 int pdo_hash_methods(pdo_dbh_t
*dbh
, int kind TSRMLS_DC
)
1288 const zend_function_entry
*funcs
;
1290 zend_internal_function
*ifunc
= (zend_internal_function
*)&func
;
1294 if (!dbh
|| !dbh
->methods
|| !dbh
->methods
->get_driver_methods
) {
1297 funcs
= dbh
->methods
->get_driver_methods(dbh
, kind TSRMLS_CC
);
1302 if (!(dbh
->cls_methods
[kind
] = pemalloc(sizeof(HashTable
), dbh
->is_persistent
))) {
1303 php_error_docref(NULL TSRMLS_CC
, E_ERROR
, "out of memory while allocating PDO methods.");
1305 zend_hash_init_ex(dbh
->cls_methods
[kind
], 8, NULL
, NULL
, dbh
->is_persistent
, 0);
1307 while (funcs
->fname
) {
1308 ifunc
->type
= ZEND_INTERNAL_FUNCTION
;
1309 ifunc
->handler
= funcs
->handler
;
1310 ifunc
->function_name
= (char*)funcs
->fname
;
1311 ifunc
->scope
= dbh
->std
.ce
;
1312 ifunc
->prototype
= NULL
;
1314 ifunc
->fn_flags
= funcs
->flags
| ZEND_ACC_NEVER_CACHE
;
1316 ifunc
->fn_flags
= ZEND_ACC_PUBLIC
| ZEND_ACC_NEVER_CACHE
;
1318 if (funcs
->arg_info
) {
1319 zend_internal_function_info
*info
= (zend_internal_function_info
*)funcs
->arg_info
;
1321 ifunc
->arg_info
= (zend_arg_info
*)funcs
->arg_info
+ 1;
1322 ifunc
->num_args
= funcs
->num_args
;
1323 if (info
->required_num_args
== -1) {
1324 ifunc
->required_num_args
= funcs
->num_args
;
1326 ifunc
->required_num_args
= info
->required_num_args
;
1328 if (info
->pass_rest_by_reference
) {
1329 if (info
->pass_rest_by_reference
== ZEND_SEND_PREFER_REF
) {
1330 ifunc
->fn_flags
|= ZEND_ACC_PASS_REST_PREFER_REF
;
1332 ifunc
->fn_flags
|= ZEND_ACC_PASS_REST_BY_REFERENCE
;
1335 if (info
->return_reference
) {
1336 ifunc
->fn_flags
|= ZEND_ACC_RETURN_REFERENCE
;
1339 ifunc
->arg_info
= NULL
;
1340 ifunc
->num_args
= 0;
1341 ifunc
->required_num_args
= 0;
1343 namelen
= strlen(funcs
->fname
);
1344 lc_name
= emalloc(namelen
+1);
1345 zend_str_tolower_copy(lc_name
, funcs
->fname
, namelen
);
1346 zend_hash_add(dbh
->cls_methods
[kind
], lc_name
, namelen
+1, &func
, sizeof(func
), NULL
);
1354 static union _zend_function
*dbh_method_get(
1355 #if PHP_API_VERSION >= 20041225
1360 char *method_name
, int method_len
, const zend_literal
*key TSRMLS_DC
)
1362 zend_function
*fbc
= NULL
;
1363 char *lc_method_name
;
1364 #if PHP_API_VERSION >= 20041225
1365 zval
*object
= *object_pp
;
1367 pdo_dbh_t
*dbh
= zend_object_store_get_object(object TSRMLS_CC
);
1369 lc_method_name
= emalloc(method_len
+ 1);
1370 zend_str_tolower_copy(lc_method_name
, method_name
, method_len
);
1372 if ((fbc
= std_object_handlers
.get_method(object_pp
, method_name
, method_len
, key TSRMLS_CC
)) == NULL
) {
1373 /* not a pre-defined method, nor a user-defined method; check
1374 * the driver specific methods */
1375 if (!dbh
->cls_methods
[PDO_DBH_DRIVER_METHOD_KIND_DBH
]) {
1376 if (!pdo_hash_methods(dbh
,
1377 PDO_DBH_DRIVER_METHOD_KIND_DBH TSRMLS_CC
)
1378 || !dbh
->cls_methods
[PDO_DBH_DRIVER_METHOD_KIND_DBH
]) {
1383 if (zend_hash_find(dbh
->cls_methods
[PDO_DBH_DRIVER_METHOD_KIND_DBH
],
1384 lc_method_name
, method_len
+1, (void**)&fbc
) == FAILURE
) {
1392 efree(lc_method_name
);
1396 static int dbh_compare(zval
*object1
, zval
*object2 TSRMLS_DC
)
1401 static zend_object_handlers pdo_dbh_object_handlers
;
1403 void pdo_dbh_init(TSRMLS_D
)
1405 zend_class_entry ce
;
1407 INIT_CLASS_ENTRY(ce
, "PDO", pdo_dbh_functions
);
1408 pdo_dbh_ce
= zend_register_internal_class(&ce TSRMLS_CC
);
1409 pdo_dbh_ce
->create_object
= pdo_dbh_new
;
1411 memcpy(&pdo_dbh_object_handlers
, &std_object_handlers
, sizeof(zend_object_handlers
));
1412 pdo_dbh_object_handlers
.get_method
= dbh_method_get
;
1413 pdo_dbh_object_handlers
.compare_objects
= dbh_compare
;
1415 REGISTER_PDO_CLASS_CONST_LONG("PARAM_BOOL", (long)PDO_PARAM_BOOL
);
1416 REGISTER_PDO_CLASS_CONST_LONG("PARAM_NULL", (long)PDO_PARAM_NULL
);
1417 REGISTER_PDO_CLASS_CONST_LONG("PARAM_INT", (long)PDO_PARAM_INT
);
1418 REGISTER_PDO_CLASS_CONST_LONG("PARAM_STR", (long)PDO_PARAM_STR
);
1419 REGISTER_PDO_CLASS_CONST_LONG("PARAM_LOB", (long)PDO_PARAM_LOB
);
1420 REGISTER_PDO_CLASS_CONST_LONG("PARAM_STMT", (long)PDO_PARAM_STMT
);
1421 REGISTER_PDO_CLASS_CONST_LONG("PARAM_INPUT_OUTPUT", (long)PDO_PARAM_INPUT_OUTPUT
);
1423 REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_ALLOC", (long)PDO_PARAM_EVT_ALLOC
);
1424 REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_FREE", (long)PDO_PARAM_EVT_FREE
);
1425 REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_EXEC_PRE", (long)PDO_PARAM_EVT_EXEC_PRE
);
1426 REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_EXEC_POST", (long)PDO_PARAM_EVT_EXEC_POST
);
1427 REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_FETCH_PRE", (long)PDO_PARAM_EVT_FETCH_PRE
);
1428 REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_FETCH_POST", (long)PDO_PARAM_EVT_FETCH_POST
);
1429 REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_NORMALIZE", (long)PDO_PARAM_EVT_NORMALIZE
);
1431 REGISTER_PDO_CLASS_CONST_LONG("FETCH_LAZY", (long)PDO_FETCH_LAZY
);
1432 REGISTER_PDO_CLASS_CONST_LONG("FETCH_ASSOC",(long)PDO_FETCH_ASSOC
);
1433 REGISTER_PDO_CLASS_CONST_LONG("FETCH_NUM", (long)PDO_FETCH_NUM
);
1434 REGISTER_PDO_CLASS_CONST_LONG("FETCH_BOTH", (long)PDO_FETCH_BOTH
);
1435 REGISTER_PDO_CLASS_CONST_LONG("FETCH_OBJ", (long)PDO_FETCH_OBJ
);
1436 REGISTER_PDO_CLASS_CONST_LONG("FETCH_BOUND",(long)PDO_FETCH_BOUND
);
1437 REGISTER_PDO_CLASS_CONST_LONG("FETCH_COLUMN",(long)PDO_FETCH_COLUMN
);
1438 REGISTER_PDO_CLASS_CONST_LONG("FETCH_CLASS",(long)PDO_FETCH_CLASS
);
1439 REGISTER_PDO_CLASS_CONST_LONG("FETCH_INTO", (long)PDO_FETCH_INTO
);
1440 REGISTER_PDO_CLASS_CONST_LONG("FETCH_FUNC", (long)PDO_FETCH_FUNC
);
1441 REGISTER_PDO_CLASS_CONST_LONG("FETCH_GROUP",(long)PDO_FETCH_GROUP
);
1442 REGISTER_PDO_CLASS_CONST_LONG("FETCH_UNIQUE",(long)PDO_FETCH_UNIQUE
);
1443 REGISTER_PDO_CLASS_CONST_LONG("FETCH_KEY_PAIR",(long)PDO_FETCH_KEY_PAIR
);
1444 REGISTER_PDO_CLASS_CONST_LONG("FETCH_CLASSTYPE",(long)PDO_FETCH_CLASSTYPE
);
1445 #if PHP_MAJOR_VERSION > 5 || PHP_MINOR_VERSION >= 1
1446 REGISTER_PDO_CLASS_CONST_LONG("FETCH_SERIALIZE",(long)PDO_FETCH_SERIALIZE
);
1448 REGISTER_PDO_CLASS_CONST_LONG("FETCH_PROPS_LATE",(long)PDO_FETCH_PROPS_LATE
);
1449 REGISTER_PDO_CLASS_CONST_LONG("FETCH_NAMED",(long)PDO_FETCH_NAMED
);
1451 REGISTER_PDO_CLASS_CONST_LONG("ATTR_AUTOCOMMIT", (long)PDO_ATTR_AUTOCOMMIT
);
1452 REGISTER_PDO_CLASS_CONST_LONG("ATTR_PREFETCH", (long)PDO_ATTR_PREFETCH
);
1453 REGISTER_PDO_CLASS_CONST_LONG("ATTR_TIMEOUT", (long)PDO_ATTR_TIMEOUT
);
1454 REGISTER_PDO_CLASS_CONST_LONG("ATTR_ERRMODE", (long)PDO_ATTR_ERRMODE
);
1455 REGISTER_PDO_CLASS_CONST_LONG("ATTR_SERVER_VERSION", (long)PDO_ATTR_SERVER_VERSION
);
1456 REGISTER_PDO_CLASS_CONST_LONG("ATTR_CLIENT_VERSION", (long)PDO_ATTR_CLIENT_VERSION
);
1457 REGISTER_PDO_CLASS_CONST_LONG("ATTR_SERVER_INFO", (long)PDO_ATTR_SERVER_INFO
);
1458 REGISTER_PDO_CLASS_CONST_LONG("ATTR_CONNECTION_STATUS", (long)PDO_ATTR_CONNECTION_STATUS
);
1459 REGISTER_PDO_CLASS_CONST_LONG("ATTR_CASE", (long)PDO_ATTR_CASE
);
1460 REGISTER_PDO_CLASS_CONST_LONG("ATTR_CURSOR_NAME", (long)PDO_ATTR_CURSOR_NAME
);
1461 REGISTER_PDO_CLASS_CONST_LONG("ATTR_CURSOR", (long)PDO_ATTR_CURSOR
);
1462 REGISTER_PDO_CLASS_CONST_LONG("ATTR_ORACLE_NULLS", (long)PDO_ATTR_ORACLE_NULLS
);
1463 REGISTER_PDO_CLASS_CONST_LONG("ATTR_PERSISTENT", (long)PDO_ATTR_PERSISTENT
);
1464 REGISTER_PDO_CLASS_CONST_LONG("ATTR_STATEMENT_CLASS", (long)PDO_ATTR_STATEMENT_CLASS
);
1465 REGISTER_PDO_CLASS_CONST_LONG("ATTR_FETCH_TABLE_NAMES", (long)PDO_ATTR_FETCH_TABLE_NAMES
);
1466 REGISTER_PDO_CLASS_CONST_LONG("ATTR_FETCH_CATALOG_NAMES", (long)PDO_ATTR_FETCH_CATALOG_NAMES
);
1467 REGISTER_PDO_CLASS_CONST_LONG("ATTR_DRIVER_NAME", (long)PDO_ATTR_DRIVER_NAME
);
1468 REGISTER_PDO_CLASS_CONST_LONG("ATTR_STRINGIFY_FETCHES",(long)PDO_ATTR_STRINGIFY_FETCHES
);
1469 REGISTER_PDO_CLASS_CONST_LONG("ATTR_MAX_COLUMN_LEN",(long)PDO_ATTR_MAX_COLUMN_LEN
);
1470 REGISTER_PDO_CLASS_CONST_LONG("ATTR_EMULATE_PREPARES",(long)PDO_ATTR_EMULATE_PREPARES
);
1471 REGISTER_PDO_CLASS_CONST_LONG("ATTR_DEFAULT_FETCH_MODE",(long)PDO_ATTR_DEFAULT_FETCH_MODE
);
1473 REGISTER_PDO_CLASS_CONST_LONG("ERRMODE_SILENT", (long)PDO_ERRMODE_SILENT
);
1474 REGISTER_PDO_CLASS_CONST_LONG("ERRMODE_WARNING", (long)PDO_ERRMODE_WARNING
);
1475 REGISTER_PDO_CLASS_CONST_LONG("ERRMODE_EXCEPTION", (long)PDO_ERRMODE_EXCEPTION
);
1477 REGISTER_PDO_CLASS_CONST_LONG("CASE_NATURAL", (long)PDO_CASE_NATURAL
);
1478 REGISTER_PDO_CLASS_CONST_LONG("CASE_LOWER", (long)PDO_CASE_LOWER
);
1479 REGISTER_PDO_CLASS_CONST_LONG("CASE_UPPER", (long)PDO_CASE_UPPER
);
1481 REGISTER_PDO_CLASS_CONST_LONG("NULL_NATURAL", (long)PDO_NULL_NATURAL
);
1482 REGISTER_PDO_CLASS_CONST_LONG("NULL_EMPTY_STRING", (long)PDO_NULL_EMPTY_STRING
);
1483 REGISTER_PDO_CLASS_CONST_LONG("NULL_TO_STRING", (long)PDO_NULL_TO_STRING
);
1485 REGISTER_PDO_CLASS_CONST_STRING("ERR_NONE", PDO_ERR_NONE
);
1487 REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_NEXT", (long)PDO_FETCH_ORI_NEXT
);
1488 REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_PRIOR", (long)PDO_FETCH_ORI_PRIOR
);
1489 REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_FIRST", (long)PDO_FETCH_ORI_FIRST
);
1490 REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_LAST", (long)PDO_FETCH_ORI_LAST
);
1491 REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_ABS", (long)PDO_FETCH_ORI_ABS
);
1492 REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_REL", (long)PDO_FETCH_ORI_REL
);
1494 REGISTER_PDO_CLASS_CONST_LONG("CURSOR_FWDONLY", (long)PDO_CURSOR_FWDONLY
);
1495 REGISTER_PDO_CLASS_CONST_LONG("CURSOR_SCROLL", (long)PDO_CURSOR_SCROLL
);
1498 REGISTER_PDO_CLASS_CONST_LONG("ERR_CANT_MAP", (long)PDO_ERR_CANT_MAP
);
1499 REGISTER_PDO_CLASS_CONST_LONG("ERR_SYNTAX", (long)PDO_ERR_SYNTAX
);
1500 REGISTER_PDO_CLASS_CONST_LONG("ERR_CONSTRAINT", (long)PDO_ERR_CONSTRAINT
);
1501 REGISTER_PDO_CLASS_CONST_LONG("ERR_NOT_FOUND", (long)PDO_ERR_NOT_FOUND
);
1502 REGISTER_PDO_CLASS_CONST_LONG("ERR_ALREADY_EXISTS", (long)PDO_ERR_ALREADY_EXISTS
);
1503 REGISTER_PDO_CLASS_CONST_LONG("ERR_NOT_IMPLEMENTED", (long)PDO_ERR_NOT_IMPLEMENTED
);
1504 REGISTER_PDO_CLASS_CONST_LONG("ERR_MISMATCH", (long)PDO_ERR_MISMATCH
);
1505 REGISTER_PDO_CLASS_CONST_LONG("ERR_TRUNCATED", (long)PDO_ERR_TRUNCATED
);
1506 REGISTER_PDO_CLASS_CONST_LONG("ERR_DISCONNECTED", (long)PDO_ERR_DISCONNECTED
);
1507 REGISTER_PDO_CLASS_CONST_LONG("ERR_NO_PERM", (long)PDO_ERR_NO_PERM
);
1512 static void dbh_free(pdo_dbh_t
*dbh TSRMLS_DC
)
1516 if (--dbh
->refcount
)
1519 if (dbh
->query_stmt
) {
1520 zval_dtor(&dbh
->query_stmt_zval
);
1521 dbh
->query_stmt
= NULL
;
1525 dbh
->methods
->closer(dbh TSRMLS_CC
);
1528 if (dbh
->data_source
) {
1529 pefree((char *)dbh
->data_source
, dbh
->is_persistent
);
1531 if (dbh
->username
) {
1532 pefree(dbh
->username
, dbh
->is_persistent
);
1534 if (dbh
->password
) {
1535 pefree(dbh
->password
, dbh
->is_persistent
);
1538 if (dbh
->persistent_id
) {
1539 pefree((char *)dbh
->persistent_id
, dbh
->is_persistent
);
1542 if (dbh
->def_stmt_ctor_args
) {
1543 zval_ptr_dtor(&dbh
->def_stmt_ctor_args
);
1546 for (i
= 0; i
< PDO_DBH_DRIVER_METHOD_KIND__MAX
; i
++) {
1547 if (dbh
->cls_methods
[i
]) {
1548 zend_hash_destroy(dbh
->cls_methods
[i
]);
1549 pefree(dbh
->cls_methods
[i
], dbh
->is_persistent
);
1553 pefree(dbh
, dbh
->is_persistent
);
1556 PDO_API
void php_pdo_dbh_addref(pdo_dbh_t
*dbh TSRMLS_DC
)
1561 PDO_API
void php_pdo_dbh_delref(pdo_dbh_t
*dbh TSRMLS_DC
)
1563 dbh_free(dbh TSRMLS_CC
);
1566 static void pdo_dbh_free_storage(pdo_dbh_t
*dbh TSRMLS_DC
)
1568 if (dbh
->in_txn
&& dbh
->methods
&& dbh
->methods
->rollback
) {
1569 dbh
->methods
->rollback(dbh TSRMLS_CC
);
1573 if (dbh
->is_persistent
&& dbh
->methods
&& dbh
->methods
->persistent_shutdown
) {
1574 dbh
->methods
->persistent_shutdown(dbh TSRMLS_CC
);
1576 zend_object_std_dtor(&dbh
->std TSRMLS_CC
);
1577 dbh
->std
.properties
= NULL
;
1578 dbh_free(dbh TSRMLS_CC
);
1581 zend_object_value
pdo_dbh_new(zend_class_entry
*ce TSRMLS_DC
)
1583 zend_object_value retval
;
1586 dbh
= emalloc(sizeof(*dbh
));
1587 memset(dbh
, 0, sizeof(*dbh
));
1588 zend_object_std_init(&dbh
->std
, ce TSRMLS_CC
);
1589 object_properties_init(&dbh
->std
, ce
);
1590 rebuild_object_properties(&dbh
->std
);
1592 dbh
->def_stmt_ce
= pdo_dbstmt_ce
;
1594 retval
.handle
= zend_objects_store_put(dbh
, (zend_objects_store_dtor_t
)zend_objects_destroy_object
, (zend_objects_free_object_storage_t
)pdo_dbh_free_storage
, NULL TSRMLS_CC
);
1595 retval
.handlers
= &pdo_dbh_object_handlers
;
1602 ZEND_RSRC_DTOR_FUNC(php_pdo_pdbh_dtor
)
1605 pdo_dbh_t
*dbh
= (pdo_dbh_t
*)rsrc
->ptr
;
1606 dbh_free(dbh TSRMLS_CC
);
1616 * vim600: noet sw=4 ts=4 fdm=marker
1617 * vim<600: noet sw=4 ts=4