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

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:20 +0000 (28 hours ago)

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