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 163 26.4 %
Date: 2014-04-19 Functions: 2 11 18.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 20 Apr 2014 03:52:12 +0000 (4 days ago)

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