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.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 105 148 70.9 %
Date: 2016-09-18 Functions: 13 15 86.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2016 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             : #ifdef HAVE_CONFIG_H
      24             : #include "config.h"
      25             : #endif
      26             : 
      27             : #include <ctype.h>
      28             : #include "php.h"
      29             : #include "php_ini.h"
      30             : #include "ext/standard/info.h"
      31             : #include "php_pdo.h"
      32             : #include "php_pdo_driver.h"
      33             : #include "php_pdo_int.h"
      34             : #include "zend_exceptions.h"
      35             : #include "ext/spl/spl_exceptions.h"
      36             : 
      37             : zend_class_entry *pdo_dbh_ce, *pdo_dbstmt_ce, *pdo_row_ce;
      38             : 
      39             : /* for exceptional circumstances */
      40             : zend_class_entry *pdo_exception_ce;
      41             : 
      42             : ZEND_DECLARE_MODULE_GLOBALS(pdo)
      43             : static PHP_GINIT_FUNCTION(pdo);
      44             : 
      45             : /* True global resources - no need for thread safety here */
      46             : 
      47             : /* the registry of PDO drivers */
      48             : HashTable pdo_driver_hash;
      49             : 
      50             : /* we use persistent resources for the driver connection stuff */
      51             : static int le_ppdo;
      52             : 
      53          57 : int php_pdo_list_entry(void) /* {{{ */
      54             : {
      55          57 :         return le_ppdo;
      56             : }
      57             : /* }}} */
      58             : 
      59     2416122 : PDO_API zend_class_entry *php_pdo_get_dbh_ce(void) /* {{{ */
      60             : {
      61     2416122 :         return pdo_dbh_ce;
      62             : }
      63             : /* }}} */
      64             : 
      65         185 : PDO_API zend_class_entry *php_pdo_get_exception(void) /* {{{ */
      66             : {
      67         185 :         return pdo_exception_ce;
      68             : }
      69             : /* }}} */
      70             : 
      71           0 : PDO_API char *php_pdo_str_tolower_dup(const char *src, int len) /* {{{ */
      72             : {
      73           0 :         char *dest = emalloc(len + 1);
      74           0 :         zend_str_tolower_copy(dest, src, len);
      75           0 :         return dest;
      76             : }
      77             : /* }}} */
      78             : 
      79       23982 : PDO_API zend_class_entry *php_pdo_get_exception_base(int root) /* {{{ */
      80             : {
      81       23982 :         if (!root) {
      82       23922 :                 return spl_ce_RuntimeException;
      83             :         }
      84          60 :         return zend_ce_exception;
      85             : }
      86             : /* }}} */
      87             : 
      88             : /* {{{ proto array pdo_drivers()
      89             :  Return array of available PDO drivers */
      90           0 : PHP_FUNCTION(pdo_drivers)
      91             : {
      92             :         pdo_driver_t *pdriver;
      93             : 
      94           0 :         if (zend_parse_parameters_none() == FAILURE) {
      95           0 :                 return;
      96             :         }
      97             : 
      98           0 :         array_init(return_value);
      99             : 
     100           0 :         ZEND_HASH_FOREACH_PTR(&pdo_driver_hash, pdriver) {
     101           0 :                 add_next_index_stringl(return_value, (char*)pdriver->driver_name, pdriver->driver_name_len);
     102             :         } ZEND_HASH_FOREACH_END();
     103             : }
     104             : /* }}} */
     105             : 
     106             : /* {{{ arginfo */
     107             : ZEND_BEGIN_ARG_INFO(arginfo_pdo_drivers, 0)
     108             : ZEND_END_ARG_INFO()
     109             : /* }}} */
     110             : 
     111             : /* {{{ pdo_functions[] */
     112             : const zend_function_entry pdo_functions[] = {
     113             :         PHP_FE(pdo_drivers,             arginfo_pdo_drivers)
     114             :         PHP_FE_END
     115             : };
     116             : /* }}} */
     117             : 
     118             : /* {{{ pdo_functions[] */
     119             : static const zend_module_dep pdo_deps[] = {
     120             :         ZEND_MOD_REQUIRED("spl")
     121             :         ZEND_MOD_END
     122             : };
     123             : /* }}} */
     124             : 
     125             : /* {{{ pdo_module_entry */
     126             : zend_module_entry pdo_module_entry = {
     127             :         STANDARD_MODULE_HEADER_EX, NULL,
     128             :         pdo_deps,
     129             :         "PDO",
     130             :         pdo_functions,
     131             :         PHP_MINIT(pdo),
     132             :         PHP_MSHUTDOWN(pdo),
     133             :         NULL,
     134             :         NULL,
     135             :         PHP_MINFO(pdo),
     136             :         PHP_PDO_VERSION,
     137             :         PHP_MODULE_GLOBALS(pdo),
     138             :         PHP_GINIT(pdo),
     139             :         NULL,
     140             :         NULL,
     141             :         STANDARD_MODULE_PROPERTIES_EX
     142             : };
     143             : /* }}} */
     144             : 
     145             : /* TODO: visit persistent handles: for each persistent statement handle,
     146             :  * remove bound parameter associations */
     147             : 
     148             : #ifdef COMPILE_DL_PDO
     149             : ZEND_GET_MODULE(pdo)
     150             : #endif
     151             : 
     152             : /* {{{ PHP_GINIT_FUNCTION */
     153       23922 : static PHP_GINIT_FUNCTION(pdo)
     154             : {
     155       23922 :         pdo_globals->global_value = 0;
     156       23922 : }
     157             : /* }}} */
     158             : 
     159      167454 : PDO_API int php_pdo_register_driver(pdo_driver_t *driver) /* {{{ */
     160             : {
     161      167454 :         if (driver->api_version != PDO_DRIVER_API) {
     162           0 :                 zend_error(E_ERROR, "PDO: driver %s requires PDO API version " ZEND_ULONG_FMT "; this is PDO version %d",
     163             :                         driver->driver_name, driver->api_version, PDO_DRIVER_API);
     164           0 :                 return FAILURE;
     165             :         }
     166      167454 :         if (!zend_hash_str_exists(&module_registry, "pdo", sizeof("pdo") - 1)) {
     167           0 :                 zend_error(E_ERROR, "You MUST load PDO before loading any PDO drivers");
     168           0 :                 return FAILURE; /* NOTREACHED */
     169             :         }
     170             : 
     171      334908 :         return zend_hash_str_add_ptr(&pdo_driver_hash, (char*)driver->driver_name, driver->driver_name_len, driver) != NULL;
     172             : }
     173             : /* }}} */
     174             : 
     175      167741 : PDO_API void php_pdo_unregister_driver(pdo_driver_t *driver) /* {{{ */
     176             : {
     177      167741 :         if (!zend_hash_str_exists(&module_registry, "pdo", sizeof("pdo") - 1)) {
     178           0 :                 return;
     179             :         }
     180             : 
     181      167741 :         zend_hash_str_del(&pdo_driver_hash, (char*)driver->driver_name, driver->driver_name_len);
     182             : }
     183             : /* }}} */
     184             : 
     185        1423 : pdo_driver_t *pdo_find_driver(const char *name, int namelen) /* {{{ */
     186             : {
     187        2846 :         return zend_hash_str_find_ptr(&pdo_driver_hash, (char*)name, namelen);
     188             : }
     189             : /* }}} */
     190             : 
     191         890 : PDO_API int php_pdo_parse_data_source(const char *data_source, zend_ulong data_source_len, struct pdo_data_src_parser *parsed, int nparams) /* {{{ */
     192             : {
     193             :         zend_ulong i;
     194             :         int j;
     195         890 :         int valstart = -1;
     196         890 :         int semi = -1;
     197         890 :         int optstart = 0;
     198             :         int nlen;
     199         890 :         int n_matches = 0;
     200         890 :         int n_semicolumns = 0;
     201             : 
     202         890 :         i = 0;
     203      126381 :         while (i < data_source_len) {
     204             :                 /* looking for NAME= */
     205             : 
     206      124601 :                 if (data_source[i] == '\0') {
     207           0 :                         break;
     208             :                 }
     209             : 
     210      124601 :                 if (data_source[i] != '=') {
     211      121941 :                         ++i;
     212      121941 :                         continue;
     213             :                 }
     214             : 
     215        2660 :                 valstart = ++i;
     216             : 
     217             :                 /* now we're looking for VALUE; or just VALUE<NUL> */
     218        2660 :                 semi = -1;
     219        2660 :                 n_semicolumns = 0;
     220       39095 :                 while (i < data_source_len) {
     221       35547 :                         if (data_source[i] == '\0') {
     222           0 :                                 semi = i++;
     223           0 :                                 break;
     224             :                         }
     225       35547 :                         if (data_source[i] == ';') {
     226        1772 :                                 if ((i + 1 >= data_source_len) || data_source[i+1] != ';') {
     227        1772 :                                         semi = i++;
     228        1772 :                                         break;
     229             :                                 } else {
     230           0 :                                         n_semicolumns++;
     231           0 :                                         i += 2;
     232           0 :                                         continue;
     233             :                                 }
     234             :                         }
     235       33775 :                         ++i;
     236             :                 }
     237             : 
     238        2660 :                 if (semi == -1) {
     239         888 :                         semi = i;
     240             :                 }
     241             : 
     242             :                 /* find the entry in the array */
     243        2660 :                 nlen = valstart - optstart - 1;
     244       10792 :                 for (j = 0; j < nparams; j++) {
     245        9790 :                         if (0 == strncmp(data_source + optstart, parsed[j].optname, nlen) && parsed[j].optname[nlen] == '\0') {
     246             :                                 /* got a match */
     247        1658 :                                 if (parsed[j].freeme) {
     248           6 :                                         efree(parsed[j].optval);
     249             :                                 }
     250             : 
     251        1658 :                                 if (n_semicolumns == 0) {
     252        1658 :                                         parsed[j].optval = estrndup(data_source + valstart, semi - valstart - n_semicolumns);
     253             :                                 } else {
     254           0 :                                         int vlen = semi - valstart;
     255           0 :                                         const char *orig_val = data_source + valstart;
     256           0 :                                         char *new_val  = (char *) emalloc(vlen - n_semicolumns + 1);
     257             : 
     258           0 :                                         parsed[j].optval = new_val;
     259             : 
     260           0 :                                         while (vlen && *orig_val) {
     261           0 :                                                 *new_val = *orig_val;
     262           0 :                                                 new_val++;
     263             : 
     264           0 :                                                 if (*orig_val == ';') {
     265           0 :                                                         orig_val+=2;
     266           0 :                                                         vlen-=2;
     267             :                                                 } else {
     268           0 :                                                         orig_val++;
     269           0 :                                                         vlen--;
     270             :                                                 }
     271             :                                         }
     272           0 :                                         *new_val = '\0';
     273             :                                 }
     274             : 
     275        1658 :                                 parsed[j].freeme = 1;
     276        1658 :                                 ++n_matches;
     277        1658 :                                 break;
     278             :                         }
     279             :                 }
     280             : 
     281        5320 :                 while (i < data_source_len && isspace(data_source[i])) {
     282           0 :                         i++;
     283             :                 }
     284             : 
     285        2660 :                 optstart = i;
     286             :         }
     287             : 
     288         890 :         return n_matches;
     289             : }
     290             : /* }}} */
     291             : 
     292             : static const char digit_vec[] = "0123456789";
     293          14 : PDO_API char *php_pdo_int64_to_str(pdo_int64_t i64) /* {{{ */
     294             : {
     295             :         char buffer[65];
     296          14 :         char outbuf[65] = "";
     297             :         register char *p;
     298             :         zend_long long_val;
     299          14 :         char *dst = outbuf;
     300             : 
     301          14 :         if (i64 < 0) {
     302           0 :                 i64 = -i64;
     303           0 :                 *dst++ = '-';
     304             :         }
     305             : 
     306          14 :         if (i64 == 0) {
     307           8 :                 *dst++ = '0';
     308           8 :                 *dst++ = '\0';
     309           8 :                 return estrdup(outbuf);
     310             :         }
     311             : 
     312           6 :         p = &buffer[sizeof(buffer)-1];
     313           6 :         *p = '\0';
     314             : 
     315          12 :         while ((pdo_uint64_t)i64 > (pdo_uint64_t)ZEND_LONG_MAX) {
     316           0 :                 pdo_uint64_t quo = (pdo_uint64_t)i64 / (unsigned int)10;
     317           0 :                 unsigned int rem = (unsigned int)(i64 - quo*10U);
     318           0 :                 *--p = digit_vec[rem];
     319           0 :                 i64 = (pdo_int64_t)quo;
     320             :         }
     321           6 :         long_val = (zend_long)i64;
     322          27 :         while (long_val != 0) {
     323          15 :                 zend_long quo = long_val / 10;
     324          15 :                 *--p = digit_vec[(unsigned int)(long_val - quo * 10)];
     325          15 :                 long_val = quo;
     326             :         }
     327          21 :         while ((*dst++ = *p++) != 0)
     328             :                 ;
     329           6 :         *dst = '\0';
     330           6 :         return estrdup(outbuf);
     331             : }
     332             : /* }}} */
     333             : 
     334             : /* {{{ PHP_MINIT_FUNCTION */
     335       23922 : PHP_MINIT_FUNCTION(pdo)
     336             : {
     337             :         zend_class_entry ce;
     338             : 
     339       23922 :         if (FAILURE == pdo_sqlstate_init_error_table()) {
     340           0 :                 return FAILURE;
     341             :         }
     342             : 
     343       23922 :         zend_hash_init(&pdo_driver_hash, 0, NULL, NULL, 1);
     344             : 
     345       23922 :         le_ppdo = zend_register_list_destructors_ex(NULL, php_pdo_pdbh_dtor,
     346             :                 "PDO persistent database", module_number);
     347             : 
     348       23922 :         INIT_CLASS_ENTRY(ce, "PDOException", NULL);
     349             : 
     350       23922 :         pdo_exception_ce = zend_register_internal_class_ex(&ce, php_pdo_get_exception_base(0));
     351             : 
     352       23922 :         zend_declare_property_null(pdo_exception_ce, "errorInfo", sizeof("errorInfo")-1, ZEND_ACC_PUBLIC);
     353             : 
     354       23922 :         pdo_dbh_init();
     355       23922 :         pdo_stmt_init();
     356             : 
     357       23922 :         return SUCCESS;
     358             : }
     359             : /* }}} */
     360             : 
     361             : /* {{{ PHP_MSHUTDOWN_FUNCTION */
     362       23963 : PHP_MSHUTDOWN_FUNCTION(pdo)
     363             : {
     364       23963 :         zend_hash_destroy(&pdo_driver_hash);
     365       23963 :         pdo_sqlstate_fini_error_table();
     366       23963 :         return SUCCESS;
     367             : }
     368             : /* }}} */
     369             : 
     370             : /* {{{ PHP_MINFO_FUNCTION */
     371         150 : PHP_MINFO_FUNCTION(pdo)
     372             : {
     373         150 :         char *drivers = NULL, *ldrivers = estrdup("");
     374             :         pdo_driver_t *pdriver;
     375             : 
     376         150 :         php_info_print_table_start();
     377         150 :         php_info_print_table_header(2, "PDO support", "enabled");
     378             : 
     379        2250 :         ZEND_HASH_FOREACH_PTR(&pdo_driver_hash, pdriver) {
     380        1050 :                 spprintf(&drivers, 0, "%s, %s", ldrivers, pdriver->driver_name);
     381        1050 :                 efree(ldrivers);
     382        1050 :                 ldrivers = drivers;
     383             :         } ZEND_HASH_FOREACH_END();
     384             : 
     385         150 :         php_info_print_table_row(2, "PDO drivers", drivers ? drivers + 2 : "");
     386             : 
     387         150 :         if (drivers) {
     388         150 :                 efree(drivers);
     389             :         } else {
     390           0 :                 efree(ldrivers);
     391             :         }
     392             : 
     393         150 :         php_info_print_table_end();
     394             : 
     395         150 : }
     396             : /* }}} */
     397             : 
     398             : /*
     399             :  * Local variables:
     400             :  * tab-width: 4
     401             :  * c-basic-offset: 4
     402             :  * End:
     403             :  * vim600: noet sw=4 ts=4 fdm=marker
     404             :  * vim<600: noet sw=4 ts=4
     405             :  */

Generated by: LCOV version 1.10

Generated at Sun, 18 Sep 2016 08:20:12 +0000 (7 days ago)

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