PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/pdo - pdo_dbh.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 630 760 82.9 %
Date: 2015-07-26 Functions: 29 33 87.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Mon, 27 Jul 2015 02:32:23 +0000 (36 hours ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.