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

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:54 +0000 (2 days ago)

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