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: 119 156 76.3 %
Date: 2014-10-16 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 5                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2014 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Author: Wez Furlong <wez@php.net>                                    |
      16             :   +----------------------------------------------------------------------+
      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         242 : static int pdo_sqlite_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
      47             : {
      48         242 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
      49             : 
      50         242 :         if (stmt->executed && !S->done) {
      51           1 :                 sqlite3_reset(S->stmt);
      52             :         }
      53             : 
      54         242 :         S->done = 0;
      55         242 :         switch (sqlite3_step(S->stmt)) {
      56             :                 case SQLITE_ROW:
      57         164 :                         S->pre_fetched = 1;
      58         164 :                         stmt->column_count = sqlite3_data_count(S->stmt);
      59         164 :                         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             : 
      83        1020 :         switch (event_type) {
      84             :                 case PDO_PARAM_EVT_EXEC_PRE:
      85         193 :                         if (stmt->executed && !S->done) {
      86           5 :                                 sqlite3_reset(S->stmt);
      87           5 :                                 S->done = 1;
      88             :                         }
      89             :                         
      90         193 :                         if (param->is_param) {
      91             :                                 
      92         160 :                                 if (param->paramno == -1) {
      93          48 :                                         param->paramno = sqlite3_bind_parameter_index(S->stmt, param->name) - 1;
      94             :                                 }
      95             : 
      96         160 :                                 switch (PDO_PARAM_TYPE(param->param_type)) {
      97             :                                         case PDO_PARAM_STMT:
      98           0 :                                                 return 0;
      99             : 
     100             :                                         case PDO_PARAM_NULL:
     101           0 :                                                 if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
     102           0 :                                                         return 1;
     103             :                                                 }
     104           0 :                                                 pdo_sqlite_error_stmt(stmt);
     105           0 :                                                 return 0;
     106             :                                         
     107             :                                         case PDO_PARAM_INT:
     108             :                                         case PDO_PARAM_BOOL:
     109           7 :                                                 if (Z_TYPE_P(param->parameter) == IS_NULL) {
     110           0 :                                                         if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
     111           0 :                                                                 return 1;
     112             :                                                         }
     113             :                                                 } else {
     114           7 :                                                         convert_to_long(param->parameter);
     115             : #if LONG_MAX > 2147483647
     116           7 :                                                         if (SQLITE_OK == sqlite3_bind_int64(S->stmt, param->paramno + 1, Z_LVAL_P(param->parameter))) {
     117           7 :                                                                 return 1;
     118             :                                                         }
     119             : #else
     120             :                                                         if (SQLITE_OK == sqlite3_bind_int(S->stmt, param->paramno + 1, Z_LVAL_P(param->parameter))) {
     121             :                                                                 return 1;
     122             :                                                         }
     123             : #endif
     124             :                                                 }
     125           0 :                                                 pdo_sqlite_error_stmt(stmt);
     126           0 :                                                 return 0;
     127             :                                         
     128             :                                         case PDO_PARAM_LOB:
     129           1 :                                                 if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
     130             :                                                         php_stream *stm;
     131           1 :                                                         php_stream_from_zval_no_verify(stm, &param->parameter);
     132           1 :                                                         if (stm) {
     133           2 :                                                                 SEPARATE_ZVAL(&param->parameter);
     134           1 :                                                                 Z_TYPE_P(param->parameter) = IS_STRING;
     135           1 :                                                                 Z_STRLEN_P(param->parameter) = php_stream_copy_to_mem(stm,
     136             :                                                                         &Z_STRVAL_P(param->parameter), PHP_STREAM_COPY_ALL, 0);
     137             :                                                         } else {
     138           0 :                                                                 pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource" TSRMLS_CC);
     139           0 :                                                                 return 0;
     140             :                                                         }
     141           0 :                                                 } else if (Z_TYPE_P(param->parameter) == IS_NULL) {
     142           0 :                                                         if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
     143           0 :                                                                 return 1;
     144             :                                                         }
     145           0 :                                                         pdo_sqlite_error_stmt(stmt);
     146           0 :                                                         return 0;
     147             :                                                 } else {
     148           0 :                                                         convert_to_string(param->parameter);
     149             :                                                 }
     150             :                                                 
     151           2 :                                                 if (SQLITE_OK == sqlite3_bind_blob(S->stmt, param->paramno + 1,
     152           1 :                                                                 Z_STRVAL_P(param->parameter),
     153           1 :                                                                 Z_STRLEN_P(param->parameter),
     154             :                                                                 SQLITE_STATIC)) {
     155           1 :                                                         return 1;       
     156             :                                                 }
     157           0 :                                                 pdo_sqlite_error_stmt(stmt);
     158           0 :                                                 return 0;
     159             :                                                         
     160             :                                         case PDO_PARAM_STR:
     161             :                                         default:
     162         152 :                                                 if (Z_TYPE_P(param->parameter) == IS_NULL) {
     163           2 :                                                         if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
     164           2 :                                                                 return 1;
     165             :                                                         }
     166             :                                                 } else {
     167         150 :                                                         convert_to_string(param->parameter);
     168         300 :                                                         if(SQLITE_OK == sqlite3_bind_text(S->stmt, param->paramno + 1,
     169         150 :                                                                         Z_STRVAL_P(param->parameter),
     170         150 :                                                                         Z_STRLEN_P(param->parameter),
     171             :                                                                         SQLITE_STATIC)) {
     172         149 :                                                                 return 1;       
     173             :                                                         }
     174             :                                                 }
     175           1 :                                                 pdo_sqlite_error_stmt(stmt);
     176           1 :                                                 return 0;
     177             :                                 }
     178             :                         }
     179             :                         break;
     180             : 
     181             :                 default:
     182             :                         ;
     183             :         }
     184         860 :         return 1;
     185             : }
     186             : 
     187         378 : static int pdo_sqlite_stmt_fetch(pdo_stmt_t *stmt,
     188             :         enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
     189             : {
     190         378 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     191             :         int i;
     192         378 :         if (!S->stmt) {
     193           0 :                 return 0;       
     194             :         }
     195         378 :         if (S->pre_fetched) {
     196         147 :                 S->pre_fetched = 0;
     197         147 :                 return 1;
     198             :         }
     199         231 :         if (S->done) {
     200           2 :                 return 0;
     201             :         }
     202         229 :         i = sqlite3_step(S->stmt);
     203         229 :         switch (i) {
     204             :                 case SQLITE_ROW:
     205         142 :                         return 1;
     206             : 
     207             :                 case SQLITE_DONE:
     208          87 :                         S->done = 1;
     209          87 :                         sqlite3_reset(S->stmt);
     210          87 :                         return 0;
     211             : 
     212             :                 case SQLITE_ERROR:
     213           0 :                         sqlite3_reset(S->stmt);
     214             :                 default:
     215           0 :                         pdo_sqlite_error_stmt(stmt);
     216           0 :                         return 0;
     217             :         }
     218             : }
     219             : 
     220         207 : static int pdo_sqlite_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
     221             : {
     222         207 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     223             : 
     224         207 :         if(colno >= sqlite3_column_count(S->stmt)) {
     225             :                 /* error invalid column */
     226           0 :                 pdo_sqlite_error_stmt(stmt);
     227           0 :                 return 0;
     228             :         }
     229             : 
     230         207 :         stmt->columns[colno].name = estrdup(sqlite3_column_name(S->stmt, colno));
     231         207 :         stmt->columns[colno].namelen = strlen(stmt->columns[colno].name);
     232         207 :         stmt->columns[colno].maxlen = 0xffffffff;
     233         207 :         stmt->columns[colno].precision = 0;
     234             :         
     235         207 :         switch (sqlite3_column_type(S->stmt, colno)) {
     236             :                 case SQLITE_INTEGER:
     237             :                 case SQLITE_FLOAT:
     238             :                 case SQLITE3_TEXT:
     239             :                 case SQLITE_BLOB:
     240             :                 case SQLITE_NULL:
     241             :                 default:
     242         207 :                         stmt->columns[colno].param_type = PDO_PARAM_STR;
     243             :                         break;
     244             :         }
     245             : 
     246         207 :         return 1;
     247             : }
     248             : 
     249         545 : static int pdo_sqlite_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC)
     250             : {
     251         545 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     252         545 :         if (!S->stmt) {
     253           0 :                 return 0;
     254             :         }
     255         545 :         if(colno >= sqlite3_data_count(S->stmt)) {
     256             :                 /* error invalid column */
     257           0 :                 pdo_sqlite_error_stmt(stmt);
     258           0 :                 return 0;
     259             :         }
     260         545 :         switch (sqlite3_column_type(S->stmt, colno)) {
     261             :                 case SQLITE_NULL:
     262           7 :                         *ptr = NULL;
     263           7 :                         *len = 0;
     264           7 :                         return 1;
     265             : 
     266             :                 case SQLITE_BLOB:
     267           1 :                         *ptr = (char*)sqlite3_column_blob(S->stmt, colno);
     268           1 :                         *len = sqlite3_column_bytes(S->stmt, colno);
     269           1 :                         return 1;
     270             : 
     271             :                 default:
     272         537 :                         *ptr = (char*)sqlite3_column_text(S->stmt, colno);
     273         537 :                         *len = sqlite3_column_bytes(S->stmt, colno);
     274         537 :                         return 1;
     275             :         }
     276             : }
     277             : 
     278           2 : static int pdo_sqlite_stmt_col_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC)
     279             : {
     280           2 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     281             :         const char *str;
     282             :         zval *flags;
     283             :         
     284           2 :         if (!S->stmt) {
     285           0 :                 return FAILURE;
     286             :         }
     287           2 :         if(colno >= sqlite3_data_count(S->stmt)) {
     288             :                 /* error invalid column */
     289           0 :                 pdo_sqlite_error_stmt(stmt);
     290           0 :                 return FAILURE;
     291             :         }
     292             : 
     293           2 :         array_init(return_value);
     294           2 :         MAKE_STD_ZVAL(flags);
     295           2 :         array_init(flags);
     296             : 
     297           2 :         switch (sqlite3_column_type(S->stmt, colno)) {
     298             :                 case SQLITE_NULL:
     299           0 :                         add_assoc_string(return_value, "native_type", "null", 1);
     300           0 :                         break;
     301             : 
     302             :                 case SQLITE_FLOAT:
     303           0 :                         add_assoc_string(return_value, "native_type", "double", 1);
     304           0 :                         break;
     305             : 
     306             :                 case SQLITE_BLOB:
     307           0 :                         add_next_index_string(flags, "blob", 1);
     308             :                 case SQLITE_TEXT:
     309           2 :                         add_assoc_string(return_value, "native_type", "string", 1);
     310           2 :                         break;
     311             : 
     312             :                 case SQLITE_INTEGER:
     313           0 :                         add_assoc_string(return_value, "native_type", "integer", 1);
     314             :                         break;
     315             :         }
     316             : 
     317           2 :         str = sqlite3_column_decltype(S->stmt, colno);
     318           2 :         if (str) {
     319           2 :                 add_assoc_string(return_value, "sqlite:decl_type", (char *)str, 1);
     320             :         }
     321             : 
     322             : #ifdef SQLITE_ENABLE_COLUMN_METADATA
     323           2 :         str = sqlite3_column_table_name(S->stmt, colno);
     324           2 :         if (str) {
     325           2 :                 add_assoc_string(return_value, "table", (char *)str, 1);
     326             :         }
     327             : #endif
     328             : 
     329           2 :         add_assoc_zval(return_value, "flags", flags);
     330             : 
     331           2 :         return SUCCESS;
     332             : }
     333             : 
     334          27 : static int pdo_sqlite_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
     335             : {
     336          27 :         pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
     337          27 :         sqlite3_reset(S->stmt);
     338          27 :         return 1;
     339             : }
     340             : 
     341             : struct pdo_stmt_methods sqlite_stmt_methods = {
     342             :         pdo_sqlite_stmt_dtor,
     343             :         pdo_sqlite_stmt_execute,
     344             :         pdo_sqlite_stmt_fetch,
     345             :         pdo_sqlite_stmt_describe,
     346             :         pdo_sqlite_stmt_get_col,
     347             :         pdo_sqlite_stmt_param_hook,
     348             :         NULL, /* set_attr */
     349             :         NULL, /* get_attr */
     350             :         pdo_sqlite_stmt_col_meta,
     351             :         NULL, /* next_rowset */
     352             :         pdo_sqlite_stmt_cursor_closer
     353             : };
     354             : 
     355             : /*
     356             :  * Local variables:
     357             :  * tab-width: 4
     358             :  * c-basic-offset: 4
     359             :  * End:
     360             :  * vim600: noet sw=4 ts=4 fdm=marker
     361             :  * vim<600: noet sw=4 ts=4
     362             :  */

Generated by: LCOV version 1.10

Generated at Thu, 16 Oct 2014 05:27:07 +0000 (4 days ago)

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