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_stmt.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 0 204 0.0 %
Date: 2016-09-18 Functions: 0 10 0.0 %
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/php_string.h"
      29             : #include "ext/standard/info.h"
      30             : #include "pdo/php_pdo.h"
      31             : #include "pdo/php_pdo_driver.h"
      32             : #include "php_pdo_dblib.h"
      33             : #include "php_pdo_dblib_int.h"
      34             : #include "zend_exceptions.h"
      35             : 
      36             : 
      37             : /* {{{ pdo_dblib_get_field_name
      38             :  *
      39             :  * Return the data type name for a given TDS number
      40             :  *
      41             :  */
      42           0 : static char *pdo_dblib_get_field_name(int type)
      43             : {
      44             :         /*
      45             :          * I don't return dbprtype(type) because it does not fully describe the type
      46             :          * (example: varchar is reported as char by dbprtype)
      47             :          *
      48             :          * FIX ME: Cache datatypes from server systypes table in pdo_dblib_handle_factory()
      49             :          *                 to make this future proof.
      50             :          */
      51             : 
      52           0 :         switch (type) {
      53           0 :                 case 31: return "nvarchar";
      54           0 :                 case 34: return "image";
      55           0 :                 case 35: return "text";
      56           0 :                 case 36: return "uniqueidentifier";
      57           0 :                 case 37: return "varbinary"; /* & timestamp - Sybase AS12 */
      58           0 :                 case 38: return "bigint"; /* & bigintn - Sybase AS12 */
      59           0 :                 case 39: return "varchar"; /* & sysname & nvarchar - Sybase AS12 */
      60           0 :                 case 40: return "date";
      61           0 :                 case 41: return "time";
      62           0 :                 case 42: return "datetime2";
      63           0 :                 case 43: return "datetimeoffset";
      64           0 :                 case 45: return "binary"; /* Sybase AS12 */
      65           0 :                 case 47: return "char"; /* & nchar & uniqueidentifierstr Sybase AS12 */
      66           0 :                 case 48: return "tinyint";
      67           0 :                 case 50: return "bit"; /* Sybase AS12 */
      68           0 :                 case 52: return "smallint";
      69           0 :                 case 55: return "decimal"; /* Sybase AS12 */
      70           0 :                 case 56: return "int";
      71           0 :                 case 58: return "smalldatetime";
      72           0 :                 case 59: return "real";
      73           0 :                 case 60: return "money";
      74           0 :                 case 61: return "datetime";
      75           0 :                 case 62: return "float";
      76           0 :                 case 63: return "numeric"; /* or uint, ubigint, usmallint Sybase AS12 */
      77           0 :                 case 98: return "sql_variant";
      78           0 :                 case 99: return "ntext";
      79           0 :                 case 104: return "bit";
      80           0 :                 case 106: return "decimal"; /* decimal n on sybase */
      81           0 :                 case 108: return "numeric"; /* numeric n on sybase */
      82           0 :                 case 122: return "smallmoney";
      83           0 :                 case 127: return "bigint";
      84           0 :                 case 165: return "varbinary";
      85           0 :                 case 167: return "varchar";
      86           0 :                 case 173: return "binary";
      87           0 :                 case 175: return "char";
      88           0 :                 case 189: return "timestamp";
      89           0 :                 case 231: return "nvarchar";
      90           0 :                 case 239: return "nchar";
      91           0 :                 case 240: return "geometry";
      92           0 :                 case 241: return "xml";
      93           0 :                 default: return "unknown";
      94             :         }
      95             : }
      96             : /* }}} */
      97             : 
      98           0 : static int pdo_dblib_stmt_cursor_closer(pdo_stmt_t *stmt)
      99             : {
     100           0 :         pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
     101           0 :         pdo_dblib_db_handle *H = S->H;
     102             : 
     103             :         /* Cancel any pending results */
     104           0 :         dbcancel(H->link);
     105             : 
     106           0 :         pdo_dblib_err_dtor(&H->err);
     107             : 
     108           0 :         return 1;
     109             : }
     110             : 
     111           0 : static int pdo_dblib_stmt_dtor(pdo_stmt_t *stmt)
     112             : {
     113           0 :         pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
     114             : 
     115           0 :         pdo_dblib_err_dtor(&S->err);
     116             : 
     117           0 :         efree(S);
     118             : 
     119           0 :         return 1;
     120             : }
     121             : 
     122           0 : static int pdo_dblib_stmt_next_rowset(pdo_stmt_t *stmt)
     123             : {
     124           0 :         pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
     125           0 :         pdo_dblib_db_handle *H = S->H;
     126             :         RETCODE ret;
     127             : 
     128           0 :         ret = dbresults(H->link);
     129             : 
     130           0 :         if (FAIL == ret) {
     131           0 :                 pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "PDO_DBLIB: dbresults() returned FAIL");
     132           0 :                 return 0;
     133             :         }
     134             : 
     135           0 :         if(NO_MORE_RESULTS == ret) {
     136           0 :                 return 0;
     137             :         }
     138             : 
     139           0 :         stmt->row_count = DBCOUNT(H->link);
     140           0 :         stmt->column_count = dbnumcols(H->link);
     141             : 
     142           0 :         return 1;
     143             : }
     144             : 
     145           0 : static int pdo_dblib_stmt_execute(pdo_stmt_t *stmt)
     146             : {
     147           0 :         pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
     148           0 :         pdo_dblib_db_handle *H = S->H;
     149             :         RETCODE ret;
     150             : 
     151           0 :         dbsetuserdata(H->link, (BYTE*) &S->err);
     152             : 
     153           0 :         pdo_dblib_stmt_cursor_closer(stmt);
     154             : 
     155           0 :         if (FAIL == dbcmd(H->link, stmt->active_query_string)) {
     156           0 :                 return 0;
     157             :         }
     158             : 
     159           0 :         if (FAIL == dbsqlexec(H->link)) {
     160           0 :                 return 0;
     161             :         }
     162             : 
     163           0 :         ret = pdo_dblib_stmt_next_rowset(stmt);
     164             : 
     165           0 :         stmt->row_count = DBCOUNT(H->link);
     166           0 :         stmt->column_count = dbnumcols(H->link);
     167             : 
     168           0 :         return 1;
     169             : }
     170             : 
     171           0 : static int pdo_dblib_stmt_fetch(pdo_stmt_t *stmt,
     172             :         enum pdo_fetch_orientation ori, zend_long offset)
     173             : {
     174             : 
     175             :         RETCODE ret;
     176             : 
     177           0 :         pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
     178           0 :         pdo_dblib_db_handle *H = S->H;
     179             : 
     180           0 :         ret = dbnextrow(H->link);
     181             : 
     182           0 :         if (FAIL == ret) {
     183           0 :                 pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "PDO_DBLIB: dbnextrow() returned FAIL");
     184           0 :                 return 0;
     185             :         }
     186             : 
     187           0 :         if(NO_MORE_ROWS == ret) {
     188           0 :                 return 0;
     189             :         }
     190             : 
     191           0 :         return 1;
     192             : }
     193             : 
     194           0 : static int pdo_dblib_stmt_describe(pdo_stmt_t *stmt, int colno)
     195             : {
     196           0 :         pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
     197           0 :         pdo_dblib_db_handle *H = S->H;
     198             :         struct pdo_column_data *col;
     199             :         char *fname;
     200             : 
     201           0 :         if(colno >= stmt->column_count || colno < 0)  {
     202           0 :                 return FAILURE;
     203             :         }
     204             : 
     205           0 :         if (colno == 0) {
     206           0 :                 S->computed_column_name_count = 0;
     207             :         }
     208             : 
     209           0 :         col = &stmt->columns[colno];
     210           0 :         fname = (char*)dbcolname(H->link, colno+1);
     211             : 
     212           0 :         if (fname && *fname) {
     213           0 :                 col->name =  zend_string_init(fname, strlen(fname), 0);
     214             :         } else {
     215           0 :                 if (S->computed_column_name_count > 0) {
     216             :                         char buf[16];
     217             :                         int len;
     218             : 
     219           0 :                         len = snprintf(buf, sizeof(buf), "computed%d", S->computed_column_name_count);
     220           0 :                         col->name = zend_string_init(buf, len, 0);
     221             :                 } else {
     222           0 :                         col->name = zend_string_init("computed", strlen("computed"), 0);
     223             :                 }
     224             : 
     225           0 :                 S->computed_column_name_count++;
     226             :         }
     227             : 
     228           0 :         col->maxlen = dbcollen(H->link, colno+1);
     229           0 :         col->param_type = PDO_PARAM_ZVAL;
     230             : 
     231           0 :         return 1;
     232             : }
     233             : 
     234           0 : static int pdo_dblib_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr,
     235             :          zend_ulong *len, int *caller_frees)
     236             : {
     237             : 
     238           0 :         pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
     239           0 :         pdo_dblib_db_handle *H = S->H;
     240             : 
     241             :         int coltype;
     242             :         char *data, *tmp_data;
     243             :         unsigned int data_len, tmp_data_len;
     244           0 :         zval *zv = NULL;
     245             : 
     246           0 :         coltype = dbcoltype(H->link, colno+1);
     247           0 :         data = dbdata(H->link, colno+1);
     248           0 :         data_len = dbdatlen(H->link, colno+1);
     249             : 
     250           0 :         if (data_len != 0 || data != NULL) {
     251           0 :                 if (stmt->dbh->stringify) {
     252           0 :                         switch (coltype) {
     253             :                                 case SQLDECIMAL:
     254             :                                 case SQLNUMERIC:
     255             :                                 case SQLMONEY:
     256             :                                 case SQLMONEY4:
     257             :                                 case SQLMONEYN:
     258             :                                 case SQLFLT4:
     259             :                                 case SQLFLT8:
     260             :                                 case SQLINT4:
     261             :                                 case SQLINT2:
     262             :                                 case SQLINT1:
     263             :                                 case SQLBIT: {
     264           0 :                                         if (dbwillconvert(coltype, SQLCHAR)) {
     265           0 :                                                 tmp_data_len = 32 + (2 * (data_len)); /* FIXME: We allocate more than we need here */
     266           0 :                                                 tmp_data = emalloc(tmp_data_len);
     267           0 :                                                 data_len = dbconvert(NULL, coltype, data, data_len, SQLCHAR, tmp_data, -1);
     268             : 
     269           0 :                                                 zv = emalloc(sizeof(zval));
     270           0 :                                                 ZVAL_STRING(zv, tmp_data);
     271             : 
     272           0 :                                                 efree(tmp_data);
     273             :                                         }
     274             :                                         break;
     275             :                                 }
     276             :                         }
     277             :                 }
     278             : 
     279           0 :                 if (!zv) {
     280           0 :                         switch (coltype) {
     281             :                                 case SQLCHAR:
     282             :                                 case SQLVARCHAR:
     283             :                                 case SQLTEXT: {
     284             : #if ilia_0
     285             :                                         while (data_len>0 && data[data_len-1] == ' ') { /* nuke trailing whitespace */
     286             :                                                 data_len--;
     287             :                                         }
     288             : #endif
     289             :                                 }
     290             :                                 case SQLVARBINARY:
     291             :                                 case SQLBINARY:
     292             :                                 case SQLIMAGE: {
     293           0 :                                         zv = emalloc(sizeof(zval));
     294           0 :                                         ZVAL_STRINGL(zv, data, data_len);
     295             : 
     296           0 :                                         break;
     297             :                                 }
     298             :                                 case SQLDATETIME:
     299             :                                 case SQLDATETIM4: {
     300             :                                         int dl;
     301             :                                         DBDATEREC di;
     302             :                                         DBDATEREC dt;
     303             : 
     304           0 :                                         dbconvert(H->link, coltype, data, -1, SQLDATETIME, (LPBYTE) &dt, -1);
     305           0 :                                         dbdatecrack(H->link, &di, (DBDATETIME *) &dt);
     306             : 
     307           0 :                                         dl = spprintf(&tmp_data, 20, "%d-%02d-%02d %02d:%02d:%02d",
     308             : #if defined(PHP_DBLIB_IS_MSSQL) || defined(MSDBLIB)
     309             :                                                         di.year,     di.month,       di.day,        di.hour,     di.minute,     di.second
     310             : #else
     311             :                                                         di.dateyear, di.datemonth+1, di.datedmonth, di.datehour, di.dateminute, di.datesecond
     312             : #endif
     313             :                                         );
     314             : 
     315           0 :                                         zv = emalloc(sizeof(zval));
     316           0 :                                         ZVAL_STRINGL(zv, tmp_data, dl);
     317             : 
     318           0 :                                         efree(tmp_data);
     319             : 
     320           0 :                                         break;
     321             :                                 }
     322             :                                 case SQLFLT4: {
     323           0 :                                         zv = emalloc(sizeof(zval));
     324           0 :                                         ZVAL_DOUBLE(zv, (double) (*(DBFLT4 *) data));
     325             : 
     326           0 :                                         break;
     327             :                                 }
     328             :                                 case SQLFLT8: {
     329           0 :                                         zv = emalloc(sizeof(zval));
     330           0 :                                         ZVAL_DOUBLE(zv, (double) (*(DBFLT8 *) data));
     331             : 
     332           0 :                                         break;
     333             :                                 }
     334             :                                 case SQLINT4: {
     335           0 :                                         zv = emalloc(sizeof(zval));
     336           0 :                                         ZVAL_LONG(zv, (long) ((int) *(DBINT *) data));
     337             : 
     338           0 :                                         break;
     339             :                                 }
     340             :                                 case SQLINT2: {
     341           0 :                                         zv = emalloc(sizeof(zval));
     342           0 :                                         ZVAL_LONG(zv, (long) ((int) *(DBSMALLINT *) data));
     343             : 
     344           0 :                                         break;
     345             :                                 }
     346             :                                 case SQLINT1:
     347             :                                 case SQLBIT: {
     348           0 :                                         zv = emalloc(sizeof(zval));
     349           0 :                                         ZVAL_LONG(zv, (long) ((int) *(DBTINYINT *) data));
     350             : 
     351           0 :                                         break;
     352             :                                 }
     353             :                                 case SQLDECIMAL:
     354             :                                 case SQLNUMERIC:
     355             :                                 case SQLMONEY:
     356             :                                 case SQLMONEY4:
     357             :                                 case SQLMONEYN: {
     358             :                                         DBFLT8 float_value;
     359           0 :                                         dbconvert(NULL, coltype, data, 8, SQLFLT8, (LPBYTE)&float_value, -1);
     360             : 
     361           0 :                                         zv = emalloc(sizeof(zval));
     362           0 :                                         ZVAL_DOUBLE(zv, float_value);
     363             : 
     364           0 :                                         break;
     365             :                                 }
     366             : 
     367             : #ifdef SQLUNIQUE
     368             :                                 case SQLUNIQUE: {
     369             : #else
     370             :                                 case 36: { /* FreeTDS hack */
     371             : #endif
     372           0 :                                         if (H->stringify_uniqueidentifier) { // 36-char hex string representation
     373           0 :                                                 tmp_data_len = 36;
     374           0 :                                                 tmp_data = safe_emalloc(tmp_data_len, sizeof(char), 1);
     375           0 :                                                 data_len = (unsigned int) dbconvert(NULL, SQLUNIQUE, (BYTE*)data, data_len, SQLCHAR, (BYTE*)tmp_data, tmp_data_len);
     376           0 :                                                 php_strtoupper(tmp_data, data_len);
     377           0 :                                                 zv = emalloc(sizeof(zval));
     378           0 :                                                 ZVAL_STRINGL(zv, tmp_data, data_len);
     379           0 :                                                 efree(tmp_data);
     380             : 
     381             :                                         } else { // a 16-byte binary representation
     382           0 :                                                 zv = emalloc(sizeof(zval));
     383           0 :                                                 ZVAL_STRINGL(zv, data, 16);
     384             :                                         }
     385           0 :                                         break;
     386             :                                 }
     387             : 
     388             :                                 default: {
     389           0 :                                         if (dbwillconvert(coltype, SQLCHAR)) {
     390           0 :                                                 tmp_data_len = 32 + (2 * (data_len)); /* FIXME: We allocate more than we need here */
     391           0 :                                                 tmp_data = emalloc(tmp_data_len);
     392           0 :                                                 data_len = dbconvert(NULL, coltype, data, data_len, SQLCHAR, tmp_data, -1);
     393             : 
     394           0 :                                                 zv = emalloc(sizeof(zval));
     395           0 :                                                 ZVAL_STRING(zv, tmp_data);
     396             : 
     397           0 :                                                 efree(tmp_data);
     398             :                                         }
     399             : 
     400             :                                         break;
     401             :                                 }
     402             :                         }
     403             :                 }
     404             :         }
     405             : 
     406           0 :         if (zv != NULL) {
     407           0 :                 *ptr = (char*)zv;
     408           0 :                 *len = sizeof(zval);
     409             :         } else {
     410           0 :                 *ptr = NULL;
     411           0 :                 *len = 0;
     412             :         }
     413             : 
     414           0 :         *caller_frees = 1;
     415             : 
     416           0 :         return 1;
     417             : }
     418             : 
     419           0 : static int pdo_dblib_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
     420             :                 enum pdo_param_event event_type)
     421             : {
     422           0 :         return 1;
     423             : }
     424             : 
     425           0 : static int pdo_dblib_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
     426             : {
     427           0 :         pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
     428           0 :         pdo_dblib_db_handle *H = S->H;
     429             :         DBTYPEINFO* dbtypeinfo;
     430             :         int coltype;
     431             : 
     432           0 :         if(colno >= stmt->column_count || colno < 0)  {
     433           0 :                 return FAILURE;
     434             :         }
     435             : 
     436           0 :         array_init(return_value);
     437             : 
     438           0 :         dbtypeinfo = dbcoltypeinfo(H->link, colno+1);
     439             : 
     440           0 :         if(!dbtypeinfo) return FAILURE;
     441             : 
     442           0 :         coltype = dbcoltype(H->link, colno+1);
     443             : 
     444           0 :         add_assoc_long(return_value, "max_length", dbcollen(H->link, colno+1) );
     445           0 :         add_assoc_long(return_value, "precision", (int) dbtypeinfo->precision );
     446           0 :         add_assoc_long(return_value, "scale", (int) dbtypeinfo->scale );
     447           0 :         add_assoc_string(return_value, "column_source", dbcolsource(H->link, colno+1));
     448           0 :         add_assoc_string(return_value, "native_type", pdo_dblib_get_field_name(coltype));
     449           0 :         add_assoc_long(return_value, "native_type_id", coltype);
     450           0 :         add_assoc_long(return_value, "native_usertype_id", dbcolutype(H->link, colno+1));
     451             : 
     452           0 :         switch (coltype) {
     453             :                 case SQLBIT:
     454             :                 case SQLINT1:
     455             :                 case SQLINT2:
     456             :                 case SQLINT4:
     457           0 :                         add_assoc_long(return_value, "pdo_type", PDO_PARAM_INT);
     458           0 :                         break;
     459             :                 default:
     460           0 :                         add_assoc_long(return_value, "pdo_type", PDO_PARAM_STR);
     461             :                         break;
     462             :         }
     463             : 
     464           0 :         return 1;
     465             : }
     466             : 
     467             : 
     468             : struct pdo_stmt_methods dblib_stmt_methods = {
     469             :         pdo_dblib_stmt_dtor,
     470             :         pdo_dblib_stmt_execute,
     471             :         pdo_dblib_stmt_fetch,
     472             :         pdo_dblib_stmt_describe,
     473             :         pdo_dblib_stmt_get_col,
     474             :         pdo_dblib_stmt_param_hook,
     475             :         NULL, /* set attr */
     476             :         NULL, /* get attr */
     477             :         pdo_dblib_stmt_get_column_meta, /* meta */
     478             :         pdo_dblib_stmt_next_rowset, /* nextrow */
     479             :         pdo_dblib_stmt_cursor_closer
     480             : };

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.