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-06-25 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          45 : int php_pdo_list_entry(void) /* {{{ */
      54             : {
      55          45 :         return le_ppdo;
      56             : }
      57             : /* }}} */
      58             : 
      59     2213526 : PDO_API zend_class_entry *php_pdo_get_dbh_ce(void) /* {{{ */
      60             : {
      61     2213526 :         return pdo_dbh_ce;
      62             : }
      63             : /* }}} */
      64             : 
      65         112 : PDO_API zend_class_entry *php_pdo_get_exception(void) /* {{{ */
      66             : {
      67         112 :         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       22644 : PDO_API zend_class_entry *php_pdo_get_exception_base(int root) /* {{{ */
      80             : {
      81       22644 :         if (!root) {
      82       22587 :                 return spl_ce_RuntimeException;
      83             :         }
      84          57 :         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       22587 : static PHP_GINIT_FUNCTION(pdo)
     154             : {
     155       22587 :         pdo_globals->global_value = 0;
     156       22587 : }
     157             : /* }}} */
     158             : 
     159      158109 : PDO_API int php_pdo_register_driver(pdo_driver_t *driver) /* {{{ */
     160             : {
     161      158109 :         if (driver->api_version != PDO_DRIVER_API) {
     162           0 :                 zend_error(E_ERROR, "PDO: driver %s requires PDO API version %pd; this is PDO version %d",
     163             :                         driver->driver_name, driver->api_version, PDO_DRIVER_API);
     164           0 :                 return FAILURE;
     165             :         }
     166      158109 :         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      316218 :         return zend_hash_str_add_ptr(&pdo_driver_hash, (char*)driver->driver_name, driver->driver_name_len, driver) != NULL;
     172             : }
     173             : /* }}} */
     174             : 
     175      158361 : PDO_API void php_pdo_unregister_driver(pdo_driver_t *driver) /* {{{ */
     176             : {
     177      158361 :         if (!zend_hash_str_exists(&module_registry, "pdo", sizeof("pdo") - 1)) {
     178           0 :                 return;
     179             :         }
     180             : 
     181      158361 :         zend_hash_str_del(&pdo_driver_hash, (char*)driver->driver_name, driver->driver_name_len);
     182             : }
     183             : /* }}} */
     184             : 
     185        1273 : pdo_driver_t *pdo_find_driver(const char *name, int namelen) /* {{{ */
     186             : {
     187        2546 :         return zend_hash_str_find_ptr(&pdo_driver_hash, (char*)name, namelen);
     188             : }
     189             : /* }}} */
     190             : 
     191         789 : 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             :         int i, j;
     194         789 :         int valstart = -1;
     195         789 :         int semi = -1;
     196         789 :         int optstart = 0;
     197             :         int nlen;
     198         789 :         int n_matches = 0;
     199         789 :         int n_semicolumns = 0;
     200             : 
     201         789 :         i = 0;
     202      124972 :         while (i < data_source_len) {
     203             :                 /* looking for NAME= */
     204             : 
     205      123394 :                 if (data_source[i] == '\0') {
     206           0 :                         break;
     207             :                 }
     208             : 
     209      123394 :                 if (data_source[i] != '=') {
     210      120919 :                         ++i;
     211      120919 :                         continue;
     212             :                 }
     213             : 
     214        2475 :                 valstart = ++i;
     215             : 
     216             :                 /* now we're looking for VALUE; or just VALUE<NUL> */
     217        2475 :                 semi = -1;
     218        2475 :                 n_semicolumns = 0;
     219       36968 :                 while (i < data_source_len) {
     220       33706 :                         if (data_source[i] == '\0') {
     221           0 :                                 semi = i++;
     222           0 :                                 break;
     223             :                         }
     224       33706 :                         if (data_source[i] == ';') {
     225        1688 :                                 if ((i + 1 >= data_source_len) || data_source[i+1] != ';') {
     226        1688 :                                         semi = i++;
     227        1688 :                                         break;
     228             :                                 } else {
     229           0 :                                         n_semicolumns++;
     230           0 :                                         i += 2;
     231           0 :                                         continue;
     232             :                                 }
     233             :                         }
     234       32018 :                         ++i;
     235             :                 }
     236             : 
     237        2475 :                 if (semi == -1) {
     238         787 :                         semi = i;
     239             :                 }
     240             : 
     241             :                 /* find the entry in the array */
     242        2475 :                 nlen = valstart - optstart - 1;
     243       10219 :                 for (j = 0; j < nparams; j++) {
     244        9217 :                         if (0 == strncmp(data_source + optstart, parsed[j].optname, nlen) && parsed[j].optname[nlen] == '\0') {
     245             :                                 /* got a match */
     246        1473 :                                 if (parsed[j].freeme) {
     247           6 :                                         efree(parsed[j].optval);
     248             :                                 }
     249             : 
     250        1473 :                                 if (n_semicolumns == 0) {
     251        1473 :                                         parsed[j].optval = estrndup(data_source + valstart, semi - valstart - n_semicolumns);
     252             :                                 } else {
     253           0 :                                         int vlen = semi - valstart;
     254           0 :                                         const char *orig_val = data_source + valstart;
     255           0 :                                         char *new_val  = (char *) emalloc(vlen - n_semicolumns + 1);
     256             : 
     257           0 :                                         parsed[j].optval = new_val;
     258             : 
     259           0 :                                         while (vlen && *orig_val) {
     260           0 :                                                 *new_val = *orig_val;
     261           0 :                                                 new_val++;
     262             : 
     263           0 :                                                 if (*orig_val == ';') {
     264           0 :                                                         orig_val+=2;
     265           0 :                                                         vlen-=2;
     266             :                                                 } else {
     267           0 :                                                         orig_val++;
     268           0 :                                                         vlen--;
     269             :                                                 }
     270             :                                         }
     271           0 :                                         *new_val = '\0';
     272             :                                 }
     273             : 
     274        1473 :                                 parsed[j].freeme = 1;
     275        1473 :                                 ++n_matches;
     276        1473 :                                 break;
     277             :                         }
     278             :                 }
     279             : 
     280        4950 :                 while (i < data_source_len && isspace(data_source[i])) {
     281           0 :                         i++;
     282             :                 }
     283             : 
     284        2475 :                 optstart = i;
     285             :         }
     286             : 
     287         789 :         return n_matches;
     288             : }
     289             : /* }}} */
     290             : 
     291             : static const char digit_vec[] = "0123456789";
     292          14 : PDO_API char *php_pdo_int64_to_str(pdo_int64_t i64) /* {{{ */
     293             : {
     294             :         char buffer[65];
     295          14 :         char outbuf[65] = "";
     296             :         register char *p;
     297             :         zend_long long_val;
     298          14 :         char *dst = outbuf;
     299             : 
     300          14 :         if (i64 < 0) {
     301           0 :                 i64 = -i64;
     302           0 :                 *dst++ = '-';
     303             :         }
     304             : 
     305          14 :         if (i64 == 0) {
     306           8 :                 *dst++ = '0';
     307           8 :                 *dst++ = '\0';
     308           8 :                 return estrdup(outbuf);
     309             :         }
     310             : 
     311           6 :         p = &buffer[sizeof(buffer)-1];
     312           6 :         *p = '\0';
     313             : 
     314          12 :         while ((pdo_uint64_t)i64 > (pdo_uint64_t)ZEND_LONG_MAX) {
     315           0 :                 pdo_uint64_t quo = (pdo_uint64_t)i64 / (unsigned int)10;
     316           0 :                 unsigned int rem = (unsigned int)(i64 - quo*10U);
     317           0 :                 *--p = digit_vec[rem];
     318           0 :                 i64 = (pdo_int64_t)quo;
     319             :         }
     320           6 :         long_val = (zend_long)i64;
     321          27 :         while (long_val != 0) {
     322          15 :                 zend_long quo = long_val / 10;
     323          15 :                 *--p = digit_vec[(unsigned int)(long_val - quo * 10)];
     324          15 :                 long_val = quo;
     325             :         }
     326          21 :         while ((*dst++ = *p++) != 0)
     327             :                 ;
     328           6 :         *dst = '\0';
     329           6 :         return estrdup(outbuf);
     330             : }
     331             : /* }}} */
     332             : 
     333             : /* {{{ PHP_MINIT_FUNCTION */
     334       22587 : PHP_MINIT_FUNCTION(pdo)
     335             : {
     336             :         zend_class_entry ce;
     337             : 
     338       22587 :         if (FAILURE == pdo_sqlstate_init_error_table()) {
     339           0 :                 return FAILURE;
     340             :         }
     341             : 
     342       22587 :         zend_hash_init(&pdo_driver_hash, 0, NULL, NULL, 1);
     343             : 
     344       22587 :         le_ppdo = zend_register_list_destructors_ex(NULL, php_pdo_pdbh_dtor,
     345             :                 "PDO persistent database", module_number);
     346             : 
     347       22587 :         INIT_CLASS_ENTRY(ce, "PDOException", NULL);
     348             : 
     349       22587 :         pdo_exception_ce = zend_register_internal_class_ex(&ce, php_pdo_get_exception_base(0));
     350             : 
     351       22587 :         zend_declare_property_null(pdo_exception_ce, "errorInfo", sizeof("errorInfo")-1, ZEND_ACC_PUBLIC);
     352             : 
     353       22587 :         pdo_dbh_init();
     354       22587 :         pdo_stmt_init();
     355             : 
     356       22587 :         return SUCCESS;
     357             : }
     358             : /* }}} */
     359             : 
     360             : /* {{{ PHP_MSHUTDOWN_FUNCTION */
     361       22623 : PHP_MSHUTDOWN_FUNCTION(pdo)
     362             : {
     363       22623 :         zend_hash_destroy(&pdo_driver_hash);
     364       22623 :         pdo_sqlstate_fini_error_table();
     365       22623 :         return SUCCESS;
     366             : }
     367             : /* }}} */
     368             : 
     369             : /* {{{ PHP_MINFO_FUNCTION */
     370         149 : PHP_MINFO_FUNCTION(pdo)
     371             : {
     372         149 :         char *drivers = NULL, *ldrivers = estrdup("");
     373             :         pdo_driver_t *pdriver;
     374             : 
     375         149 :         php_info_print_table_start();
     376         149 :         php_info_print_table_header(2, "PDO support", "enabled");
     377             : 
     378        2235 :         ZEND_HASH_FOREACH_PTR(&pdo_driver_hash, pdriver) {
     379        1043 :                 spprintf(&drivers, 0, "%s, %s", ldrivers, pdriver->driver_name);
     380        1043 :                 efree(ldrivers);
     381        1043 :                 ldrivers = drivers;
     382             :         } ZEND_HASH_FOREACH_END();
     383             : 
     384         149 :         php_info_print_table_row(2, "PDO drivers", drivers ? drivers + 2 : "");
     385             : 
     386         149 :         if (drivers) {
     387         149 :                 efree(drivers);
     388             :         } else {
     389           0 :                 efree(ldrivers);
     390             :         }
     391             : 
     392         149 :         php_info_print_table_end();
     393             : 
     394         149 : }
     395             : /* }}} */
     396             : 
     397             : /*
     398             :  * Local variables:
     399             :  * tab-width: 4
     400             :  * c-basic-offset: 4
     401             :  * End:
     402             :  * vim600: noet sw=4 ts=4 fdm=marker
     403             :  * vim<600: noet sw=4 ts=4
     404             :  */

Generated by: LCOV version 1.10

Generated at Sat, 25 Jun 2016 07:09:01 +0000 (5 days ago)

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