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: 2015-05-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-2015 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)
      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)
      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)
      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");
     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)
     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)
     234             : {
     235         206 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     236             :         char *str;
     237             : 
     238         206 :         if(colno >= sqlite3_column_count(S->stmt)) {
     239             :                 /* error invalid column */
     240           0 :                 pdo_sqlite_error_stmt(stmt);
     241           0 :                 return 0;
     242             :         }
     243             : 
     244         206 :         str = sqlite3_column_name(S->stmt, colno);
     245         412 :         stmt->columns[colno].name = zend_string_init(str, strlen(str), 0);
     246         206 :         stmt->columns[colno].maxlen = 0xffffffff;
     247         206 :         stmt->columns[colno].precision = 0;
     248             : 
     249         206 :         switch (sqlite3_column_type(S->stmt, colno)) {
     250             :                 case SQLITE_INTEGER:
     251             :                 case SQLITE_FLOAT:
     252             :                 case SQLITE3_TEXT:
     253             :                 case SQLITE_BLOB:
     254             :                 case SQLITE_NULL:
     255             :                 default:
     256         206 :                         stmt->columns[colno].param_type = PDO_PARAM_STR;
     257             :                         break;
     258             :         }
     259             : 
     260         206 :         return 1;
     261             : }
     262             : 
     263         547 : static int pdo_sqlite_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, zend_ulong *len, int *caller_frees)
     264             : {
     265         547 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     266         547 :         if (!S->stmt) {
     267           0 :                 return 0;
     268             :         }
     269         547 :         if(colno >= sqlite3_data_count(S->stmt)) {
     270             :                 /* error invalid column */
     271           0 :                 pdo_sqlite_error_stmt(stmt);
     272           0 :                 return 0;
     273             :         }
     274         547 :         switch (sqlite3_column_type(S->stmt, colno)) {
     275             :                 case SQLITE_NULL:
     276           7 :                         *ptr = NULL;
     277           7 :                         *len = 0;
     278           7 :                         return 1;
     279             : 
     280             :                 case SQLITE_BLOB:
     281           1 :                         *ptr = (char*)sqlite3_column_blob(S->stmt, colno);
     282           1 :                         *len = sqlite3_column_bytes(S->stmt, colno);
     283           1 :                         return 1;
     284             : 
     285             :                 default:
     286         539 :                         *ptr = (char*)sqlite3_column_text(S->stmt, colno);
     287         539 :                         *len = sqlite3_column_bytes(S->stmt, colno);
     288         539 :                         return 1;
     289             :         }
     290             : }
     291             : 
     292           2 : static int pdo_sqlite_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
     293             : {
     294           2 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     295             :         const char *str;
     296             :         zval flags;
     297             : 
     298           2 :         if (!S->stmt) {
     299           0 :                 return FAILURE;
     300             :         }
     301           2 :         if(colno >= sqlite3_data_count(S->stmt)) {
     302             :                 /* error invalid column */
     303           0 :                 pdo_sqlite_error_stmt(stmt);
     304           0 :                 return FAILURE;
     305             :         }
     306             : 
     307           2 :         array_init(return_value);
     308           2 :         array_init(&flags);
     309             : 
     310           2 :         switch (sqlite3_column_type(S->stmt, colno)) {
     311             :                 case SQLITE_NULL:
     312           0 :                         add_assoc_string(return_value, "native_type", "null");
     313           0 :                         break;
     314             : 
     315             :                 case SQLITE_FLOAT:
     316           0 :                         add_assoc_string(return_value, "native_type", "double");
     317           0 :                         break;
     318             : 
     319             :                 case SQLITE_BLOB:
     320           0 :                         add_next_index_string(&flags, "blob");
     321             :                 case SQLITE_TEXT:
     322           2 :                         add_assoc_string(return_value, "native_type", "string");
     323           2 :                         break;
     324             : 
     325             :                 case SQLITE_INTEGER:
     326           0 :                         add_assoc_string(return_value, "native_type", "integer");
     327             :                         break;
     328             :         }
     329             : 
     330           2 :         str = sqlite3_column_decltype(S->stmt, colno);
     331           2 :         if (str) {
     332           2 :                 add_assoc_string(return_value, "sqlite:decl_type", (char *)str);
     333             :         }
     334             : 
     335             : #ifdef SQLITE_ENABLE_COLUMN_METADATA
     336           2 :         str = sqlite3_column_table_name(S->stmt, colno);
     337           2 :         if (str) {
     338           2 :                 add_assoc_string(return_value, "table", (char *)str);
     339             :         }
     340             : #endif
     341             : 
     342           2 :         add_assoc_zval(return_value, "flags", &flags);
     343             : 
     344           2 :         return SUCCESS;
     345             : }
     346             : 
     347          27 : static int pdo_sqlite_stmt_cursor_closer(pdo_stmt_t *stmt)
     348             : {
     349          27 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     350          27 :         sqlite3_reset(S->stmt);
     351          27 :         return 1;
     352             : }
     353             : 
     354             : struct pdo_stmt_methods sqlite_stmt_methods = {
     355             :         pdo_sqlite_stmt_dtor,
     356             :         pdo_sqlite_stmt_execute,
     357             :         pdo_sqlite_stmt_fetch,
     358             :         pdo_sqlite_stmt_describe,
     359             :         pdo_sqlite_stmt_get_col,
     360             :         pdo_sqlite_stmt_param_hook,
     361             :         NULL, /* set_attr */
     362             :         NULL, /* get_attr */
     363             :         pdo_sqlite_stmt_col_meta,
     364             :         NULL, /* next_rowset */
     365             :         pdo_sqlite_stmt_cursor_closer
     366             : };
     367             : 
     368             : /*
     369             :  * Local variables:
     370             :  * tab-width: 4
     371             :  * c-basic-offset: 4
     372             :  * End:
     373             :  * vim600: noet sw=4 ts=4 fdm=marker
     374             :  * vim<600: noet sw=4 ts=4
     375             :  */

Generated by: LCOV version 1.10

Generated at Sat, 30 May 2015 05:52:53 +0000 (4 hours ago)

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