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_sqlite - sqlite_statement.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 124 162 76.5 %
Date: 2014-10-30 Functions: 8 8 100.0 %
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             :   +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #ifdef HAVE_CONFIG_H
      22             : #include "config.h"
      23             : #endif
      24             : 
      25             : #include "php.h"
      26             : #include "php_ini.h"
      27             : #include "ext/standard/info.h"
      28             : #include "pdo/php_pdo.h"
      29             : #include "pdo/php_pdo_driver.h"
      30             : #include "php_pdo_sqlite.h"
      31             : #include "php_pdo_sqlite_int.h"
      32             : 
      33             : 
      34         173 : static int pdo_sqlite_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)
      35             : {
      36         173 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
      37             : 
      38         173 :         if (S->stmt) {
      39         172 :                 sqlite3_finalize(S->stmt);
      40         172 :                 S->stmt = NULL;
      41             :         }
      42         173 :         efree(S);
      43         173 :         return 1;
      44             : }
      45             : 
      46         241 : static int pdo_sqlite_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
      47             : {
      48         241 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
      49             : 
      50         241 :         if (stmt->executed && !S->done) {
      51           1 :                 sqlite3_reset(S->stmt);
      52             :         }
      53             : 
      54         241 :         S->done = 0;
      55         241 :         switch (sqlite3_step(S->stmt)) {
      56             :                 case SQLITE_ROW:
      57         163 :                         S->pre_fetched = 1;
      58         163 :                         stmt->column_count = sqlite3_data_count(S->stmt);
      59         163 :                         return 1;
      60             : 
      61             :                 case SQLITE_DONE:
      62          77 :                         stmt->column_count = sqlite3_column_count(S->stmt);
      63          77 :                         stmt->row_count = sqlite3_changes(S->H->db);
      64          77 :                         sqlite3_reset(S->stmt);
      65          77 :                         S->done = 1;
      66          77 :                         return 1;
      67             : 
      68             :                 case SQLITE_ERROR:
      69           1 :                         sqlite3_reset(S->stmt);
      70             :                 case SQLITE_MISUSE:
      71             :                 case SQLITE_BUSY:
      72             :                 default:
      73           1 :                         pdo_sqlite_error_stmt(stmt);
      74           1 :                         return 0;
      75             :         }
      76             : }
      77             : 
      78        1020 : static int pdo_sqlite_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
      79             :                 enum pdo_param_event event_type TSRMLS_DC)
      80             : {
      81        1020 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
      82             :         zval *parameter;
      83             : 
      84        1020 :         switch (event_type) {
      85             :                 case PDO_PARAM_EVT_EXEC_PRE:
      86         193 :                         if (stmt->executed && !S->done) {
      87           5 :                                 sqlite3_reset(S->stmt);
      88           5 :                                 S->done = 1;
      89             :                         }
      90             :                         
      91         193 :                         if (param->is_param) {
      92             :                                 
      93         160 :                                 if (param->paramno == -1) {
      94          48 :                                         param->paramno = sqlite3_bind_parameter_index(S->stmt, param->name->val) - 1;
      95             :                                 }
      96             : 
      97         160 :                                 switch (PDO_PARAM_TYPE(param->param_type)) {
      98             :                                         case PDO_PARAM_STMT:
      99           0 :                                                 return 0;
     100             : 
     101             :                                         case PDO_PARAM_NULL:
     102           0 :                                                 if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
     103           0 :                                                         return 1;
     104             :                                                 }
     105           0 :                                                 pdo_sqlite_error_stmt(stmt);
     106           0 :                                                 return 0;
     107             :                                         
     108             :                                         case PDO_PARAM_INT:
     109             :                                         case PDO_PARAM_BOOL:
     110          14 :                                                 if (Z_ISREF(param->parameter)) {
     111           0 :                                                         parameter = Z_REFVAL(param->parameter);
     112             :                                                 } else {
     113           7 :                                                         parameter = &param->parameter;
     114             :                                                 }
     115           7 :                                                 if (Z_TYPE_P(parameter) == IS_NULL) {
     116           0 :                                                         if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
     117           0 :                                                                 return 1;
     118             :                                                         }
     119             :                                                 } else {
     120           7 :                                                         convert_to_long(parameter);
     121             : #if ZEND_LONG_MAX > 2147483647
     122           7 :                                                         if (SQLITE_OK == sqlite3_bind_int64(S->stmt, param->paramno + 1, Z_LVAL_P(parameter))) {
     123           7 :                                                                 return 1;
     124             :                                                         }
     125             : #else
     126             :                                                         if (SQLITE_OK == sqlite3_bind_int(S->stmt, param->paramno + 1, Z_LVAL_P(parameter))) {
     127             :                                                                 return 1;
     128             :                                                         }
     129             : #endif
     130             :                                                 }
     131           0 :                                                 pdo_sqlite_error_stmt(stmt);
     132           0 :                                                 return 0;
     133             :                                         
     134             :                                         case PDO_PARAM_LOB:
     135           2 :                                                 if (Z_ISREF(param->parameter)) {
     136           0 :                                                         parameter = Z_REFVAL(param->parameter);
     137             :                                                 } else {
     138           1 :                                                         parameter = &param->parameter;
     139             :                                                 }
     140           1 :                                                 if (Z_TYPE_P(parameter) == IS_RESOURCE) {
     141             :                                                         php_stream *stm;
     142           1 :                                                         php_stream_from_zval_no_verify(stm, parameter);
     143           1 :                                                         if (stm) {
     144           1 :                                                                 zval_ptr_dtor(parameter);
     145           1 :                                                                 ZVAL_STR(parameter, php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0));
     146             :                                                         } else {
     147           0 :                                                                 pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource" TSRMLS_CC);
     148           0 :                                                                 return 0;
     149             :                                                         }
     150           0 :                                                 } else if (Z_TYPE_P(parameter) == IS_NULL) {
     151           0 :                                                         if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
     152           0 :                                                                 return 1;
     153             :                                                         }
     154           0 :                                                         pdo_sqlite_error_stmt(stmt);
     155           0 :                                                         return 0;
     156             :                                                 } else {
     157           0 :                                                         convert_to_string(parameter);
     158             :                                                 }
     159             :                                                 
     160           2 :                                                 if (SQLITE_OK == sqlite3_bind_blob(S->stmt, param->paramno + 1,
     161           1 :                                                                 Z_STRVAL_P(parameter),
     162           1 :                                                                 Z_STRLEN_P(parameter),
     163             :                                                                 SQLITE_STATIC)) {
     164           1 :                                                         return 1;       
     165             :                                                 }
     166           0 :                                                 return 0;
     167             :                                                         
     168             :                                         case PDO_PARAM_STR:
     169             :                                         default:
     170         304 :                                                 if (Z_ISREF(param->parameter)) {
     171          43 :                                                         parameter = Z_REFVAL(param->parameter);
     172             :                                                 } else {
     173         109 :                                                         parameter = &param->parameter;
     174             :                                                 }
     175         152 :                                                 if (Z_TYPE_P(parameter) == IS_NULL) {
     176           2 :                                                         if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
     177           2 :                                                                 return 1;
     178             :                                                         }
     179             :                                                 } else {
     180         150 :                                                         convert_to_string(parameter);
     181         300 :                                                         if (SQLITE_OK == sqlite3_bind_text(S->stmt, param->paramno + 1,
     182         150 :                                                                         Z_STRVAL_P(parameter),
     183         150 :                                                                         Z_STRLEN_P(parameter),
     184             :                                                                         SQLITE_STATIC)) {
     185         149 :                                                                 return 1;       
     186             :                                                         }
     187             :                                                 }
     188           1 :                                                 pdo_sqlite_error_stmt(stmt);
     189           1 :                                                 return 0;
     190             :                                 }
     191             :                         }
     192             :                         break;
     193             : 
     194             :                 default:
     195             :                         ;
     196             :         }
     197         860 :         return 1;
     198             : }
     199             : 
     200         377 : static int pdo_sqlite_stmt_fetch(pdo_stmt_t *stmt,
     201             :         enum pdo_fetch_orientation ori, zend_long offset TSRMLS_DC)
     202             : {
     203         377 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     204             :         int i;
     205         377 :         if (!S->stmt) {
     206           0 :                 return 0;       
     207             :         }
     208         377 :         if (S->pre_fetched) {
     209         146 :                 S->pre_fetched = 0;
     210         146 :                 return 1;
     211             :         }
     212         231 :         if (S->done) {
     213           2 :                 return 0;
     214             :         }
     215         229 :         i = sqlite3_step(S->stmt);
     216         229 :         switch (i) {
     217             :                 case SQLITE_ROW:
     218         142 :                         return 1;
     219             : 
     220             :                 case SQLITE_DONE:
     221          87 :                         S->done = 1;
     222          87 :                         sqlite3_reset(S->stmt);
     223          87 :                         return 0;
     224             : 
     225             :                 case SQLITE_ERROR:
     226           0 :                         sqlite3_reset(S->stmt);
     227             :                 default:
     228           0 :                         pdo_sqlite_error_stmt(stmt);
     229           0 :                         return 0;
     230             :         }
     231             : }
     232             : 
     233         206 : static int pdo_sqlite_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
     234             : {
     235         206 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     236             : 
     237         206 :         if(colno >= sqlite3_column_count(S->stmt)) {
     238             :                 /* error invalid column */
     239           0 :                 pdo_sqlite_error_stmt(stmt);
     240           0 :                 return 0;
     241             :         }
     242             : 
     243         206 :         stmt->columns[colno].name = estrdup(sqlite3_column_name(S->stmt, colno));
     244         206 :         stmt->columns[colno].namelen = strlen(stmt->columns[colno].name);
     245         206 :         stmt->columns[colno].maxlen = 0xffffffff;
     246         206 :         stmt->columns[colno].precision = 0;
     247             :         
     248         206 :         switch (sqlite3_column_type(S->stmt, colno)) {
     249             :                 case SQLITE_INTEGER:
     250             :                 case SQLITE_FLOAT:
     251             :                 case SQLITE3_TEXT:
     252             :                 case SQLITE_BLOB:
     253             :                 case SQLITE_NULL:
     254             :                 default:
     255         206 :                         stmt->columns[colno].param_type = PDO_PARAM_STR;
     256             :                         break;
     257             :         }
     258             : 
     259         206 :         return 1;
     260             : }
     261             : 
     262         545 : static int pdo_sqlite_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, zend_ulong *len, int *caller_frees TSRMLS_DC)
     263             : {
     264         545 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     265         545 :         if (!S->stmt) {
     266           0 :                 return 0;
     267             :         }
     268         545 :         if(colno >= sqlite3_data_count(S->stmt)) {
     269             :                 /* error invalid column */
     270           0 :                 pdo_sqlite_error_stmt(stmt);
     271           0 :                 return 0;
     272             :         }
     273         545 :         switch (sqlite3_column_type(S->stmt, colno)) {
     274             :                 case SQLITE_NULL:
     275           7 :                         *ptr = NULL;
     276           7 :                         *len = 0;
     277           7 :                         return 1;
     278             : 
     279             :                 case SQLITE_BLOB:
     280           1 :                         *ptr = (char*)sqlite3_column_blob(S->stmt, colno);
     281           1 :                         *len = sqlite3_column_bytes(S->stmt, colno);
     282           1 :                         return 1;
     283             : 
     284             :                 default:
     285         537 :                         *ptr = (char*)sqlite3_column_text(S->stmt, colno);
     286         537 :                         *len = sqlite3_column_bytes(S->stmt, colno);
     287         537 :                         return 1;
     288             :         }
     289             : }
     290             : 
     291           2 : static int pdo_sqlite_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value TSRMLS_DC)
     292             : {
     293           2 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     294             :         const char *str;
     295             :         zval flags;
     296             :         
     297           2 :         if (!S->stmt) {
     298           0 :                 return FAILURE;
     299             :         }
     300           2 :         if(colno >= sqlite3_data_count(S->stmt)) {
     301             :                 /* error invalid column */
     302           0 :                 pdo_sqlite_error_stmt(stmt);
     303           0 :                 return FAILURE;
     304             :         }
     305             : 
     306           2 :         array_init(return_value);
     307           2 :         array_init(&flags);
     308             : 
     309           2 :         switch (sqlite3_column_type(S->stmt, colno)) {
     310             :                 case SQLITE_NULL:
     311           0 :                         add_assoc_string(return_value, "native_type", "null");
     312           0 :                         break;
     313             : 
     314             :                 case SQLITE_FLOAT:
     315           0 :                         add_assoc_string(return_value, "native_type", "double");
     316           0 :                         break;
     317             : 
     318             :                 case SQLITE_BLOB:
     319           0 :                         add_next_index_string(&flags, "blob");
     320             :                 case SQLITE_TEXT:
     321           2 :                         add_assoc_string(return_value, "native_type", "string");
     322           2 :                         break;
     323             : 
     324             :                 case SQLITE_INTEGER:
     325           0 :                         add_assoc_string(return_value, "native_type", "integer");
     326             :                         break;
     327             :         }
     328             : 
     329           2 :         str = sqlite3_column_decltype(S->stmt, colno);
     330           2 :         if (str) {
     331           2 :                 add_assoc_string(return_value, "sqlite:decl_type", (char *)str);
     332             :         }
     333             : 
     334             : #ifdef SQLITE_ENABLE_COLUMN_METADATA
     335           2 :         str = sqlite3_column_table_name(S->stmt, colno);
     336           2 :         if (str) {
     337           2 :                 add_assoc_string(return_value, "table", (char *)str);
     338             :         }
     339             : #endif
     340             : 
     341           2 :         add_assoc_zval(return_value, "flags", &flags);
     342             : 
     343           2 :         return SUCCESS;
     344             : }
     345             : 
     346          27 : static int pdo_sqlite_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
     347             : {
     348          27 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     349          27 :         sqlite3_reset(S->stmt);
     350          27 :         return 1;
     351             : }
     352             : 
     353             : struct pdo_stmt_methods sqlite_stmt_methods = {
     354             :         pdo_sqlite_stmt_dtor,
     355             :         pdo_sqlite_stmt_execute,
     356             :         pdo_sqlite_stmt_fetch,
     357             :         pdo_sqlite_stmt_describe,
     358             :         pdo_sqlite_stmt_get_col,
     359             :         pdo_sqlite_stmt_param_hook,
     360             :         NULL, /* set_attr */
     361             :         NULL, /* get_attr */
     362             :         pdo_sqlite_stmt_col_meta,
     363             :         NULL, /* next_rowset */
     364             :         pdo_sqlite_stmt_cursor_closer
     365             : };
     366             : 
     367             : /*
     368             :  * Local variables:
     369             :  * tab-width: 4
     370             :  * c-basic-offset: 4
     371             :  * End:
     372             :  * vim600: noet sw=4 ts=4 fdm=marker
     373             :  * vim<600: noet sw=4 ts=4
     374             :  */

Generated by: LCOV version 1.10

Generated at Thu, 30 Oct 2014 07:41:36 +0000 (30 hours ago)

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