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: 127 164 77.4 %
Date: 2016-09-27 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-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             :   +----------------------------------------------------------------------+
      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         191 : static int pdo_sqlite_stmt_dtor(pdo_stmt_t *stmt)
      35             : {
      36         191 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
      37             : 
      38         191 :         if (S->stmt) {
      39         187 :                 sqlite3_finalize(S->stmt);
      40         187 :                 S->stmt = NULL;
      41             :         }
      42         191 :         efree(S);
      43         191 :         return 1;
      44             : }
      45             : 
      46         256 : static int pdo_sqlite_stmt_execute(pdo_stmt_t *stmt)
      47             : {
      48         256 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
      49             : 
      50         256 :         if (stmt->executed && !S->done) {
      51           1 :                 sqlite3_reset(S->stmt);
      52             :         }
      53             : 
      54         256 :         S->done = 0;
      55         256 :         switch (sqlite3_step(S->stmt)) {
      56             :                 case SQLITE_ROW:
      57         177 :                         S->pre_fetched = 1;
      58         177 :                         stmt->column_count = sqlite3_data_count(S->stmt);
      59         177 :                         return 1;
      60             : 
      61             :                 case SQLITE_DONE:
      62          78 :                         stmt->column_count = sqlite3_column_count(S->stmt);
      63          78 :                         stmt->row_count = sqlite3_changes(S->H->db);
      64          78 :                         sqlite3_reset(S->stmt);
      65          78 :                         S->done = 1;
      66          78 :                         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        1039 : 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        1039 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
      82             :         zval *parameter;
      83             : 
      84        1039 :         switch (event_type) {
      85             :                 case PDO_PARAM_EVT_EXEC_PRE:
      86         196 :                         if (stmt->executed && !S->done) {
      87           5 :                                 sqlite3_reset(S->stmt);
      88           5 :                                 S->done = 1;
      89             :                         }
      90             : 
      91         196 :                         if (param->is_param) {
      92             : 
      93         163 :                                 if (param->paramno == -1) {
      94          50 :                                         param->paramno = sqlite3_bind_parameter_index(S->stmt, ZSTR_VAL(param->name)) - 1;
      95             :                                 }
      96             : 
      97         163 :                                 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           4 :                                                 if (Z_ISREF(param->parameter)) {
     136           1 :                                                         parameter = Z_REFVAL(param->parameter);
     137             :                                                 } else {
     138           1 :                                                         parameter = &param->parameter;
     139             :                                                 }
     140           2 :                                                 if (Z_TYPE_P(parameter) == IS_RESOURCE) {
     141           2 :                                                         php_stream *stm = NULL;
     142           2 :                                                         php_stream_from_zval_no_verify(stm, parameter);
     143           2 :                                                         if (stm) {
     144           2 :                                                                 zend_string *mem = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
     145           2 :                                                                 zval_ptr_dtor(parameter);
     146           2 :                                                                 ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC());
     147             :                                                         } else {
     148           0 :                                                                 pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource");
     149           0 :                                                                 return 0;
     150             :                                                         }
     151           0 :                                                 } else if (Z_TYPE_P(parameter) == IS_NULL) {
     152           0 :                                                         if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
     153           0 :                                                                 return 1;
     154             :                                                         }
     155           0 :                                                         pdo_sqlite_error_stmt(stmt);
     156           0 :                                                         return 0;
     157             :                                                 } else {
     158           0 :                                                         convert_to_string(parameter);
     159             :                                                 }
     160             : 
     161           4 :                                                 if (SQLITE_OK == sqlite3_bind_blob(S->stmt, param->paramno + 1,
     162           2 :                                                                 Z_STRVAL_P(parameter),
     163           2 :                                                                 Z_STRLEN_P(parameter),
     164             :                                                                 SQLITE_STATIC)) {
     165           2 :                                                         return 1;
     166             :                                                 }
     167           0 :                                                 return 0;
     168             : 
     169             :                                         case PDO_PARAM_STR:
     170             :                                         default:
     171         308 :                                                 if (Z_ISREF(param->parameter)) {
     172          43 :                                                         parameter = Z_REFVAL(param->parameter);
     173             :                                                 } else {
     174         111 :                                                         parameter = &param->parameter;
     175             :                                                 }
     176         154 :                                                 if (Z_TYPE_P(parameter) == IS_NULL) {
     177           2 :                                                         if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
     178           2 :                                                                 return 1;
     179             :                                                         }
     180             :                                                 } else {
     181         152 :                                                         convert_to_string(parameter);
     182         304 :                                                         if (SQLITE_OK == sqlite3_bind_text(S->stmt, param->paramno + 1,
     183         152 :                                                                         Z_STRVAL_P(parameter),
     184         152 :                                                                         Z_STRLEN_P(parameter),
     185             :                                                                         SQLITE_STATIC)) {
     186         151 :                                                                 return 1;
     187             :                                                         }
     188             :                                                 }
     189           1 :                                                 pdo_sqlite_error_stmt(stmt);
     190           1 :                                                 return 0;
     191             :                                 }
     192             :                         }
     193             :                         break;
     194             : 
     195             :                 default:
     196             :                         ;
     197             :         }
     198         876 :         return 1;
     199             : }
     200             : 
     201         393 : static int pdo_sqlite_stmt_fetch(pdo_stmt_t *stmt,
     202             :         enum pdo_fetch_orientation ori, zend_long offset)
     203             : {
     204         393 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     205             :         int i;
     206         393 :         if (!S->stmt) {
     207           0 :                 return 0;
     208             :         }
     209         393 :         if (S->pre_fetched) {
     210         159 :                 S->pre_fetched = 0;
     211         159 :                 return 1;
     212             :         }
     213         234 :         if (S->done) {
     214           2 :                 return 0;
     215             :         }
     216         232 :         i = sqlite3_step(S->stmt);
     217         232 :         switch (i) {
     218             :                 case SQLITE_ROW:
     219         142 :                         return 1;
     220             : 
     221             :                 case SQLITE_DONE:
     222          90 :                         S->done = 1;
     223          90 :                         sqlite3_reset(S->stmt);
     224          90 :                         return 0;
     225             : 
     226             :                 case SQLITE_ERROR:
     227           0 :                         sqlite3_reset(S->stmt);
     228             :                 default:
     229           0 :                         pdo_sqlite_error_stmt(stmt);
     230           0 :                         return 0;
     231             :         }
     232             : }
     233             : 
     234         222 : static int pdo_sqlite_stmt_describe(pdo_stmt_t *stmt, int colno)
     235             : {
     236         222 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     237             :         const char *str;
     238             : 
     239         222 :         if(colno >= sqlite3_column_count(S->stmt)) {
     240             :                 /* error invalid column */
     241           0 :                 pdo_sqlite_error_stmt(stmt);
     242           0 :                 return 0;
     243             :         }
     244             : 
     245         222 :         str = sqlite3_column_name(S->stmt, colno);
     246         444 :         stmt->columns[colno].name = zend_string_init(str, strlen(str), 0);
     247         222 :         stmt->columns[colno].maxlen = 0xffffffff;
     248         222 :         stmt->columns[colno].precision = 0;
     249             : 
     250         222 :         switch (sqlite3_column_type(S->stmt, colno)) {
     251             :                 case SQLITE_INTEGER:
     252             :                 case SQLITE_FLOAT:
     253             :                 case SQLITE3_TEXT:
     254             :                 case SQLITE_BLOB:
     255             :                 case SQLITE_NULL:
     256             :                 default:
     257         222 :                         stmt->columns[colno].param_type = PDO_PARAM_STR;
     258             :                         break;
     259             :         }
     260             : 
     261         222 :         return 1;
     262             : }
     263             : 
     264         563 : static int pdo_sqlite_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, zend_ulong *len, int *caller_frees)
     265             : {
     266         563 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     267         563 :         if (!S->stmt) {
     268           0 :                 return 0;
     269             :         }
     270         563 :         if(colno >= sqlite3_data_count(S->stmt)) {
     271             :                 /* error invalid column */
     272           0 :                 pdo_sqlite_error_stmt(stmt);
     273           0 :                 return 0;
     274             :         }
     275         563 :         switch (sqlite3_column_type(S->stmt, colno)) {
     276             :                 case SQLITE_NULL:
     277           9 :                         *ptr = NULL;
     278           9 :                         *len = 0;
     279           9 :                         return 1;
     280             : 
     281             :                 case SQLITE_BLOB:
     282           1 :                         *ptr = (char*)sqlite3_column_blob(S->stmt, colno);
     283           1 :                         *len = sqlite3_column_bytes(S->stmt, colno);
     284           1 :                         return 1;
     285             : 
     286             :                 default:
     287         553 :                         *ptr = (char*)sqlite3_column_text(S->stmt, colno);
     288         553 :                         *len = sqlite3_column_bytes(S->stmt, colno);
     289         553 :                         return 1;
     290             :         }
     291             : }
     292             : 
     293           2 : static int pdo_sqlite_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
     294             : {
     295           2 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     296             :         const char *str;
     297             :         zval flags;
     298             : 
     299           2 :         if (!S->stmt) {
     300           0 :                 return FAILURE;
     301             :         }
     302           2 :         if(colno >= sqlite3_data_count(S->stmt)) {
     303             :                 /* error invalid column */
     304           0 :                 pdo_sqlite_error_stmt(stmt);
     305           0 :                 return FAILURE;
     306             :         }
     307             : 
     308           2 :         array_init(return_value);
     309           2 :         array_init(&flags);
     310             : 
     311           2 :         switch (sqlite3_column_type(S->stmt, colno)) {
     312             :                 case SQLITE_NULL:
     313           0 :                         add_assoc_string(return_value, "native_type", "null");
     314           0 :                         break;
     315             : 
     316             :                 case SQLITE_FLOAT:
     317           0 :                         add_assoc_string(return_value, "native_type", "double");
     318           0 :                         break;
     319             : 
     320             :                 case SQLITE_BLOB:
     321           0 :                         add_next_index_string(&flags, "blob");
     322             :                 case SQLITE_TEXT:
     323           2 :                         add_assoc_string(return_value, "native_type", "string");
     324           2 :                         break;
     325             : 
     326             :                 case SQLITE_INTEGER:
     327           0 :                         add_assoc_string(return_value, "native_type", "integer");
     328             :                         break;
     329             :         }
     330             : 
     331           2 :         str = sqlite3_column_decltype(S->stmt, colno);
     332           2 :         if (str) {
     333           2 :                 add_assoc_string(return_value, "sqlite:decl_type", (char *)str);
     334             :         }
     335             : 
     336             : #ifdef SQLITE_ENABLE_COLUMN_METADATA
     337           2 :         str = sqlite3_column_table_name(S->stmt, colno);
     338           2 :         if (str) {
     339           2 :                 add_assoc_string(return_value, "table", (char *)str);
     340             :         }
     341             : #endif
     342             : 
     343           2 :         add_assoc_zval(return_value, "flags", &flags);
     344             : 
     345           2 :         return SUCCESS;
     346             : }
     347             : 
     348          27 : static int pdo_sqlite_stmt_cursor_closer(pdo_stmt_t *stmt)
     349             : {
     350          27 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     351          27 :         sqlite3_reset(S->stmt);
     352          27 :         return 1;
     353             : }
     354             : 
     355             : struct pdo_stmt_methods sqlite_stmt_methods = {
     356             :         pdo_sqlite_stmt_dtor,
     357             :         pdo_sqlite_stmt_execute,
     358             :         pdo_sqlite_stmt_fetch,
     359             :         pdo_sqlite_stmt_describe,
     360             :         pdo_sqlite_stmt_get_col,
     361             :         pdo_sqlite_stmt_param_hook,
     362             :         NULL, /* set_attr */
     363             :         NULL, /* get_attr */
     364             :         pdo_sqlite_stmt_col_meta,
     365             :         NULL, /* next_rowset */
     366             :         pdo_sqlite_stmt_cursor_closer
     367             : };
     368             : 
     369             : /*
     370             :  * Local variables:
     371             :  * tab-width: 4
     372             :  * c-basic-offset: 4
     373             :  * End:
     374             :  * vim600: noet sw=4 ts=4 fdm=marker
     375             :  * vim<600: noet sw=4 ts=4
     376             :  */

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:26:04 +0000 (2 days ago)

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