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_dblib - dblib_driver.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 46 191 24.1 %
Date: 2016-09-18 Functions: 2 13 15.4 %
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             :   |         Frank M. Kromann <frank@kromann.info>                        |
      17             :   +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #ifdef HAVE_CONFIG_H
      23             : # include "config.h"
      24             : #endif
      25             : 
      26             : #include "php.h"
      27             : #include "php_ini.h"
      28             : #include "ext/standard/info.h"
      29             : #include "pdo/php_pdo.h"
      30             : #include "pdo/php_pdo_driver.h"
      31             : #include "php_pdo_dblib.h"
      32             : #include "php_pdo_dblib_int.h"
      33             : #include "zend_exceptions.h"
      34             : 
      35             : /* Cache of the server supported datatypes, initialized in handle_factory */
      36             : zval* pdo_dblib_datatypes;
      37             : 
      38           0 : static int dblib_fetch_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info)
      39             : {
      40           0 :         pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
      41           0 :         pdo_dblib_err *einfo = &H->err;
      42           0 :         pdo_dblib_stmt *S = NULL;
      43             :         char *message;
      44             :         char *msg;
      45             : 
      46           0 :         if (stmt) {
      47           0 :                 S = (pdo_dblib_stmt*)stmt->driver_data;
      48           0 :                 einfo = &S->err;
      49             :         }
      50             : 
      51           0 :         if (einfo->dberr == SYBESMSG && einfo->lastmsg) {
      52           0 :                 msg = einfo->lastmsg;
      53           0 :         } else if (einfo->dberr == SYBESMSG && DBLIB_G(err).lastmsg) {
      54           0 :                 msg = DBLIB_G(err).lastmsg;
      55           0 :                 DBLIB_G(err).lastmsg = NULL;
      56             :         } else {
      57           0 :                 msg = einfo->dberrstr;
      58             :         }
      59             : 
      60             :         /* don't return anything if there's nothing to return */
      61           0 :         if (msg == NULL && einfo->dberr == 0 && einfo->oserr == 0 && einfo->severity == 0) {
      62           0 :                 return 0;
      63             :         }
      64             : 
      65           0 :         spprintf(&message, 0, "%s [%d] (severity %d) [%s]",
      66             :                 msg, einfo->dberr, einfo->severity, stmt ? stmt->active_query_string : "");
      67             : 
      68           0 :         add_next_index_long(info, einfo->dberr);
      69           0 :         add_next_index_string(info, message);
      70           0 :         efree(message);
      71           0 :         add_next_index_long(info, einfo->oserr);
      72           0 :         add_next_index_long(info, einfo->severity);
      73           0 :         if (einfo->oserrstr) {
      74           0 :                 add_next_index_string(info, einfo->oserrstr);
      75             :         }
      76             : 
      77           0 :         return 1;
      78             : }
      79             : 
      80             : 
      81          73 : static int dblib_handle_closer(pdo_dbh_t *dbh)
      82             : {
      83          73 :         pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
      84             : 
      85          73 :         if (H) {
      86          73 :                 if (H->link) {
      87           0 :                         dbclose(H->link);
      88           0 :                         H->link = NULL;
      89             :                 }
      90          73 :                 if (H->login) {
      91          73 :                         dbfreelogin(H->login);
      92          73 :                         H->login = NULL;
      93             :                 }
      94          73 :                 pefree(H, dbh->is_persistent);
      95          73 :                 dbh->driver_data = NULL;
      96          73 :                 pdo_dblib_err_dtor(&H->err);
      97             :         }
      98          73 :         return 0;
      99             : }
     100             : 
     101           0 : static int dblib_handle_preparer(pdo_dbh_t *dbh, const char *sql, size_t sql_len, pdo_stmt_t *stmt, zval *driver_options)
     102             : {
     103           0 :         pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
     104           0 :         pdo_dblib_stmt *S = ecalloc(1, sizeof(*S));
     105             : 
     106           0 :         S->H = H;
     107           0 :         stmt->driver_data = S;
     108           0 :         stmt->methods = &dblib_stmt_methods;
     109           0 :         stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
     110           0 :         S->computed_column_name_count = 0;
     111           0 :         S->err.sqlstate = stmt->error_code;
     112             : 
     113           0 :         return 1;
     114             : }
     115             : 
     116           0 : static zend_long dblib_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
     117             : {
     118           0 :         pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
     119             :         RETCODE ret, resret;
     120             : 
     121           0 :         dbsetuserdata(H->link, (BYTE*)&H->err);
     122             : 
     123           0 :         if (FAIL == dbcmd(H->link, sql)) {
     124           0 :                 return -1;
     125             :         }
     126             : 
     127           0 :         if (FAIL == dbsqlexec(H->link)) {
     128           0 :                 return -1;
     129             :         }
     130             : 
     131           0 :         resret = dbresults(H->link);
     132             : 
     133           0 :         if (resret == FAIL) {
     134           0 :                 return -1;
     135             :         }
     136             : 
     137           0 :         ret = dbnextrow(H->link);
     138           0 :         if (ret == FAIL) {
     139           0 :                 return -1;
     140             :         }
     141             : 
     142           0 :         if (dbnumcols(H->link) <= 0) {
     143           0 :                 return DBCOUNT(H->link);
     144             :         }
     145             : 
     146             :         /* throw away any rows it might have returned */
     147           0 :         dbcanquery(H->link);
     148             : 
     149           0 :         return DBCOUNT(H->link);
     150             : }
     151             : 
     152           0 : static int dblib_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype)
     153             : {
     154             : 
     155             :         size_t i;
     156             :         char * q;
     157           0 :         *quotedlen = 0;
     158             : 
     159             :         /* Detect quoted length, adding extra char for doubled single quotes */
     160           0 :         for(i=0;i<unquotedlen;i++) {
     161           0 :                 if(unquoted[i] == '\'') ++*quotedlen;
     162           0 :                 ++*quotedlen;
     163             :         }
     164             : 
     165           0 :         *quotedlen += 2; /* +2 for opening, closing quotes */
     166           0 :         q  = *quoted = emalloc(*quotedlen+1); /* Add byte for terminal null */
     167           0 :         *q++ = '\'';
     168             : 
     169           0 :         for (i=0;i<unquotedlen;i++) {
     170           0 :                 if (unquoted[i] == '\'') {
     171           0 :                         *q++ = '\'';
     172           0 :                         *q++ = '\'';
     173             :                 } else {
     174           0 :                         *q++ = unquoted[i];
     175             :                 }
     176             :         }
     177           0 :         *q++ = '\'';
     178             : 
     179           0 :         *q = 0;
     180             : 
     181           0 :         return 1;
     182             : }
     183             : 
     184           0 : static int pdo_dblib_transaction_cmd(const char *cmd, pdo_dbh_t *dbh)
     185             : {
     186           0 :         pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
     187             : 
     188           0 :         if (FAIL == dbcmd(H->link, cmd)) {
     189           0 :                 return 0;
     190             :         }
     191             : 
     192           0 :         if (FAIL == dbsqlexec(H->link)) {
     193           0 :                 return 0;
     194             :         }
     195             : 
     196           0 :         return 1;
     197             : }
     198             : 
     199           0 : static int dblib_handle_begin(pdo_dbh_t *dbh)
     200             : {
     201           0 :         return pdo_dblib_transaction_cmd("BEGIN TRANSACTION", dbh);
     202             : }
     203             : 
     204           0 : static int dblib_handle_commit(pdo_dbh_t *dbh)
     205             : {
     206           0 :         return pdo_dblib_transaction_cmd("COMMIT TRANSACTION", dbh);
     207             : }
     208             : 
     209           0 : static int dblib_handle_rollback(pdo_dbh_t *dbh)
     210             : {
     211           0 :         return pdo_dblib_transaction_cmd("ROLLBACK TRANSACTION", dbh);
     212             : }
     213             : 
     214           0 : char *dblib_handle_last_id(pdo_dbh_t *dbh, const char *name, size_t *len)
     215             : {
     216           0 :         pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
     217             : 
     218             :         RETCODE ret;
     219           0 :         char *id = NULL;
     220             : 
     221             :         /*
     222             :          * Would use scope_identity() but it's not implemented on Sybase
     223             :          */
     224             : 
     225           0 :         if (FAIL == dbcmd(H->link, "SELECT @@IDENTITY")) {
     226           0 :                 return NULL;
     227             :         }
     228             : 
     229           0 :         if (FAIL == dbsqlexec(H->link)) {
     230           0 :                 return NULL;
     231             :         }
     232             : 
     233           0 :         ret = dbresults(H->link);
     234           0 :         if (ret == FAIL || ret == NO_MORE_RESULTS) {
     235           0 :                 dbcancel(H->link);
     236           0 :                 return NULL;
     237             :         }
     238             : 
     239           0 :         ret = dbnextrow(H->link);
     240             : 
     241           0 :         if (ret == FAIL || ret == NO_MORE_ROWS) {
     242           0 :                 dbcancel(H->link);
     243           0 :                 return NULL;
     244             :         }
     245             : 
     246           0 :         if (dbdatlen(H->link, 1) == 0) {
     247           0 :                 dbcancel(H->link);
     248           0 :                 return NULL;
     249             :         }
     250             : 
     251           0 :         id = emalloc(32);
     252           0 :         *len = dbconvert(NULL, (dbcoltype(H->link, 1)) , (dbdata(H->link, 1)) , (dbdatlen(H->link, 1)), SQLCHAR, (BYTE *)id, (DBINT)-1);
     253             : 
     254           0 :         dbcancel(H->link);
     255           0 :         return id;
     256             : }
     257             : 
     258           0 : static int dblib_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
     259             : {
     260           0 :         switch(attr) {
     261             :                 case PDO_ATTR_TIMEOUT:
     262             :                 case PDO_DBLIB_ATTR_QUERY_TIMEOUT:
     263           0 :                         return SUCCEED == dbsettime(zval_get_long(val)) ? 1 : 0;
     264             :                 case PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER:
     265           0 :                         ((pdo_dblib_db_handle *)dbh->driver_data)->stringify_uniqueidentifier = zval_get_long(val);
     266           0 :                         return 1;
     267             :                 default:
     268           0 :                         return 0;
     269             :         }
     270             : }
     271             : 
     272           0 : static int dblib_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
     273             : {
     274           0 :         switch (attr) {
     275             :                 case PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER:
     276           0 :                         ZVAL_BOOL(return_value, ((pdo_dblib_db_handle *)dbh->driver_data)->stringify_uniqueidentifier);
     277           0 :                         break;
     278             : 
     279             :                 default:
     280           0 :                         return 0;
     281             :         }
     282             : 
     283           0 :         return 1;
     284             : }
     285             : 
     286             : static struct pdo_dbh_methods dblib_methods = {
     287             :         dblib_handle_closer,
     288             :         dblib_handle_preparer,
     289             :         dblib_handle_doer,
     290             :         dblib_handle_quoter,
     291             :         dblib_handle_begin, /* begin */
     292             :         dblib_handle_commit, /* commit */
     293             :         dblib_handle_rollback, /* rollback */
     294             :         dblib_set_attr, /*set attr */
     295             :         dblib_handle_last_id, /* last insert id */
     296             :         dblib_fetch_error, /* fetch error */
     297             :         dblib_get_attribute, /* get attr */
     298             :         NULL, /* check liveness */
     299             :         NULL, /* get driver methods */
     300             :         NULL, /* request shutdown */
     301             :         NULL  /* in transaction */
     302             : };
     303             : 
     304          73 : static int pdo_dblib_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
     305             : {
     306             :         pdo_dblib_db_handle *H;
     307          73 :         int i, nvars, nvers, ret = 0;
     308             : 
     309             :         const pdo_dblib_keyval tdsver[] = {
     310             :                  {"4.2",DBVERSION_42}
     311             :                 ,{"4.6",DBVERSION_46}
     312             :                 ,{"5.0",DBVERSION_70} /* FIXME: This does not work with Sybase, but environ will */
     313             :                 ,{"6.0",DBVERSION_70}
     314             :                 ,{"7.0",DBVERSION_70}
     315             : #ifdef DBVERSION_71
     316             :                 ,{"7.1",DBVERSION_71}
     317             : #endif
     318             : #ifdef DBVERSION_72
     319             :                 ,{"7.2",DBVERSION_72}
     320             :                 ,{"8.0",DBVERSION_72}
     321             : #endif
     322             : #ifdef DBVERSION_73
     323             :                 ,{"7.3",DBVERSION_73}
     324             : #endif
     325             : #ifdef DBVERSION_74
     326             :                 ,{"7.4",DBVERSION_74}
     327             : #endif
     328             :                 ,{"10.0",DBVERSION_100}
     329             :                 ,{"auto",0} /* Only works with FreeTDS. Other drivers will bork */
     330             : 
     331          73 :         };
     332             :         
     333             :         struct pdo_data_src_parser vars[] = {
     334             :                 { "charset",  NULL,   0 }
     335             :                 ,{ "appname", "PHP " PDO_DBLIB_FLAVOUR,     0 }
     336             :                 ,{ "host",            "127.0.0.1", 0 }
     337             :                 ,{ "dbname",  NULL,   0 }
     338             :                 ,{ "secure",  NULL,   0 } /* DBSETLSECURE */
     339             :                 ,{ "version", NULL,   0 } /* DBSETLVERSION */
     340          73 :         };
     341             : 
     342          73 :         nvars = sizeof(vars)/sizeof(vars[0]);
     343          73 :         nvers = sizeof(tdsver)/sizeof(tdsver[0]);
     344             :         
     345          73 :         php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, nvars);
     346             : 
     347          73 :         H = pecalloc(1, sizeof(*H), dbh->is_persistent);
     348          73 :         H->login = dblogin();
     349          73 :         H->err.sqlstate = dbh->error_code;
     350          73 :         H->stringify_uniqueidentifier = 0;
     351             : 
     352          73 :         if (!H->login) {
     353           0 :                 goto cleanup;
     354             :         }
     355             : 
     356          73 :         if (driver_options) {
     357           0 :                 int connect_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_CONNECTION_TIMEOUT, -1);
     358           0 :                 int query_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_QUERY_TIMEOUT, -1);
     359           0 :                 int timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
     360             : 
     361           0 :                 if (connect_timeout == -1) {
     362           0 :                         connect_timeout = timeout;
     363             :                 }
     364           0 :                 if (query_timeout == -1) {
     365           0 :                         query_timeout = timeout;
     366             :                 }
     367             : 
     368           0 :                 dbsetlogintime(connect_timeout); /* Connection/Login Timeout */
     369           0 :                 dbsettime(query_timeout); /* Statement Timeout */
     370             : 
     371           0 :                 H->stringify_uniqueidentifier = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER, 0);
     372             :         }
     373             : 
     374          73 :         DBERRHANDLE(H->login, (EHANDLEFUNC) pdo_dblib_error_handler);
     375          73 :         DBMSGHANDLE(H->login, (MHANDLEFUNC) pdo_dblib_msg_handler);
     376             : 
     377          73 :         if(vars[5].optval) {
     378           0 :                 for(i=0;i<nvers;i++) {
     379           0 :                         if(strcmp(vars[5].optval,tdsver[i].key) == 0) {
     380           0 :                                 if(FAIL==dbsetlversion(H->login, tdsver[i].value)) {
     381           0 :                                         pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Failed to set version specified in connection string.");
     382           0 :                                         goto cleanup;
     383             :                                 }
     384           0 :                                 break;
     385             :                         }
     386             :                 }
     387             : 
     388           0 :                 if (i==nvers) {
     389           0 :                         printf("Invalid version '%s'\n", vars[5].optval);
     390           0 :                         pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Invalid version specified in connection string.");
     391           0 :                         goto cleanup; /* unknown version specified */
     392             :                 }
     393             :         }
     394             : 
     395          73 :         if (dbh->username) {
     396          73 :                 if(FAIL == DBSETLUSER(H->login, dbh->username)) {
     397           0 :                         goto cleanup;
     398             :                 }
     399             :         }
     400             : 
     401          73 :         if (dbh->password) {
     402          73 :                 if(FAIL == DBSETLPWD(H->login, dbh->password)) {
     403           0 :                         goto cleanup;
     404             :                 }
     405             :         }
     406             : 
     407             : #if !PHP_DBLIB_IS_MSSQL
     408          73 :         if (vars[0].optval) {
     409           0 :                 DBSETLCHARSET(H->login, vars[0].optval);
     410             :         }
     411             : #endif
     412             : 
     413          73 :         DBSETLAPP(H->login, vars[1].optval);
     414             : 
     415             : /* DBSETLDBNAME is only available in FreeTDS 0.92 or above */
     416             : #ifdef DBSETLDBNAME
     417          73 :         if (vars[3].optval) {
     418          73 :                 if(FAIL == DBSETLDBNAME(H->login, vars[3].optval)) goto cleanup;
     419             :         }
     420             : #endif
     421             : 
     422          73 :         H->link = dbopen(H->login, vars[2].optval);
     423             : 
     424          73 :         if (!H->link) {
     425          73 :                 goto cleanup;
     426             :         }
     427             : 
     428             : /*
     429             :  * FreeTDS < 0.92 does not support the DBSETLDBNAME option
     430             :  * Send use database here after login (Will not work with SQL Azure)
     431             :  */
     432             : #ifndef DBSETLDBNAME
     433             :         if (vars[3].optval) {
     434             :                 if(FAIL == dbuse(H->link, vars[3].optval)) goto cleanup;
     435             :         }
     436             : #endif
     437             : 
     438             : #if PHP_DBLIB_IS_MSSQL
     439             :         /* dblib do not return more than this length from text/image */
     440             :         DBSETOPT(H->link, DBTEXTLIMIT, "2147483647");
     441             : #endif
     442             : 
     443             :         /* limit text/image from network */
     444           0 :         DBSETOPT(H->link, DBTEXTSIZE, "2147483647");
     445             : 
     446             :         /* allow double quoted indentifiers */
     447           0 :         DBSETOPT(H->link, DBQUOTEDIDENT, "1");
     448             : 
     449           0 :         ret = 1;
     450           0 :         dbh->max_escaped_char_length = 2;
     451           0 :         dbh->alloc_own_columns = 1;
     452             : 
     453             : cleanup:
     454         511 :         for (i = 0; i < nvars; i++) {
     455         438 :                 if (vars[i].freeme) {
     456         146 :                         efree(vars[i].optval);
     457             :                 }
     458             :         }
     459             : 
     460          73 :         dbh->methods = &dblib_methods;
     461          73 :         dbh->driver_data = H;
     462             : 
     463          73 :         if (!ret) {
     464          73 :                 zend_throw_exception_ex(php_pdo_get_exception(), DBLIB_G(err).dberr,
     465             :                         "SQLSTATE[%s] %s (severity %d)",
     466             :                         DBLIB_G(err).sqlstate,
     467             :                         DBLIB_G(err).dberrstr,
     468             :                         DBLIB_G(err).severity);
     469             :         }
     470             : 
     471          73 :         return ret;
     472             : }
     473             : 
     474             : pdo_driver_t pdo_dblib_driver = {
     475             : #if PDO_DBLIB_IS_MSSQL
     476             :         PDO_DRIVER_HEADER(mssql),
     477             : #elif defined(PHP_WIN32)
     478             : #define PDO_DBLIB_IS_SYBASE
     479             :         PDO_DRIVER_HEADER(sybase),
     480             : #else
     481             :         PDO_DRIVER_HEADER(dblib),
     482             : #endif
     483             :         pdo_dblib_handle_factory
     484             : };
     485             : 

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.