Back to index

php5  5.3.10
sqlite_statement.c
Go to the documentation of this file.
00001 /*
00002   +----------------------------------------------------------------------+
00003   | PHP Version 5                                                        |
00004   +----------------------------------------------------------------------+
00005   | Copyright (c) 1997-2012 The PHP Group                                |
00006   +----------------------------------------------------------------------+
00007   | This source file is subject to version 3.01 of the PHP license,      |
00008   | that is bundled with this package in the file LICENSE, and is        |
00009   | available through the world-wide-web at the following url:           |
00010   | http://www.php.net/license/3_01.txt                                  |
00011   | If you did not receive a copy of the PHP license and are unable to   |
00012   | obtain it through the world-wide-web, please send a note to          |
00013   | license@php.net so we can mail you a copy immediately.               |
00014   +----------------------------------------------------------------------+
00015   | Author: Wez Furlong <wez@php.net>                                    |
00016   +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id: sqlite_statement.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 
00025 #include "php.h"
00026 #include "php_ini.h"
00027 #include "ext/standard/info.h"
00028 #include "pdo/php_pdo.h"
00029 #include "pdo/php_pdo_driver.h"
00030 #include "php_pdo_sqlite.h"
00031 #include "php_pdo_sqlite_int.h"
00032 
00033 
00034 static int pdo_sqlite_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)
00035 {
00036        pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
00037 
00038        if (S->stmt) {
00039               sqlite3_finalize(S->stmt);
00040               S->stmt = NULL;
00041        }
00042        efree(S);
00043        return 1;
00044 }
00045 
00046 static int pdo_sqlite_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
00047 {
00048        pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
00049 
00050        if (stmt->executed && !S->done) {
00051               sqlite3_reset(S->stmt);
00052        }
00053 
00054        S->done = 0;
00055        switch (sqlite3_step(S->stmt)) {
00056               case SQLITE_ROW:
00057                      S->pre_fetched = 1;
00058                      stmt->column_count = sqlite3_data_count(S->stmt);
00059                      return 1;
00060 
00061               case SQLITE_DONE:
00062                      stmt->column_count = sqlite3_column_count(S->stmt);
00063                      stmt->row_count = sqlite3_changes(S->H->db);
00064                      sqlite3_reset(S->stmt);
00065                      S->done = 1;
00066                      return 1;
00067 
00068               case SQLITE_ERROR:
00069                      sqlite3_reset(S->stmt);
00070               case SQLITE_MISUSE:
00071               case SQLITE_BUSY:
00072               default:
00073                      pdo_sqlite_error_stmt(stmt);
00074                      return 0;
00075        }
00076 }
00077 
00078 static int pdo_sqlite_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
00079               enum pdo_param_event event_type TSRMLS_DC)
00080 {
00081        pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
00082 
00083        switch (event_type) {
00084               case PDO_PARAM_EVT_EXEC_PRE:
00085                      if (stmt->executed && !S->done) {
00086                             sqlite3_reset(S->stmt);
00087                             S->done = 1;
00088                      }
00089                      
00090                      if (param->is_param) {
00091                             
00092                             if (param->paramno == -1) {
00093                                    param->paramno = sqlite3_bind_parameter_index(S->stmt, param->name) - 1;
00094                             }
00095 
00096                             switch (PDO_PARAM_TYPE(param->param_type)) {
00097                                    case PDO_PARAM_STMT:
00098                                           return 0;
00099 
00100                                    case PDO_PARAM_NULL:
00101                                           if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
00102                                                  return 1;
00103                                           }
00104                                           pdo_sqlite_error_stmt(stmt);
00105                                           return 0;
00106                                    
00107                                    case PDO_PARAM_INT:
00108                                    case PDO_PARAM_BOOL:
00109                                           if (Z_TYPE_P(param->parameter) == IS_NULL) {
00110                                                  if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
00111                                                         return 1;
00112                                                  }
00113                                           } else {
00114                                                  convert_to_long(param->parameter);
00115                                                  if (SQLITE_OK == sqlite3_bind_int(S->stmt, param->paramno + 1, Z_LVAL_P(param->parameter))) {
00116                                                         return 1;
00117                                                  }
00118                                           }
00119                                           pdo_sqlite_error_stmt(stmt);
00120                                           return 0;
00121                                    
00122                                    case PDO_PARAM_LOB:
00123                                           if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
00124                                                  php_stream *stm;
00125                                                  php_stream_from_zval_no_verify(stm, &param->parameter);
00126                                                  if (stm) {
00127                                                         SEPARATE_ZVAL(&param->parameter);
00128                                                         Z_TYPE_P(param->parameter) = IS_STRING;
00129                                                         Z_STRLEN_P(param->parameter) = php_stream_copy_to_mem(stm,
00130                                                                &Z_STRVAL_P(param->parameter), PHP_STREAM_COPY_ALL, 0);
00131                                                  } else {
00132                                                         pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource" TSRMLS_CC);
00133                                                         return 0;
00134                                                  }
00135                                           } else if (Z_TYPE_P(param->parameter) == IS_NULL) {
00136                                                  if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
00137                                                         return 1;
00138                                                  }
00139                                                  pdo_sqlite_error_stmt(stmt);
00140                                                  return 0;
00141                                           } else {
00142                                                  convert_to_string(param->parameter);
00143                                           }
00144                                           
00145                                           if (SQLITE_OK == sqlite3_bind_blob(S->stmt, param->paramno + 1,
00146                                                         Z_STRVAL_P(param->parameter),
00147                                                         Z_STRLEN_P(param->parameter),
00148                                                         SQLITE_STATIC)) {
00149                                                  return 1;     
00150                                           }
00151                                           pdo_sqlite_error_stmt(stmt);
00152                                           return 0;
00153                                                  
00154                                    case PDO_PARAM_STR:
00155                                    default:
00156                                           if (Z_TYPE_P(param->parameter) == IS_NULL) {
00157                                                  if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
00158                                                         return 1;
00159                                                  }
00160                                           } else {
00161                                                  convert_to_string(param->parameter);
00162                                                  if(SQLITE_OK == sqlite3_bind_text(S->stmt, param->paramno + 1,
00163                                                                Z_STRVAL_P(param->parameter),
00164                                                                Z_STRLEN_P(param->parameter),
00165                                                                SQLITE_STATIC)) {
00166                                                         return 1;     
00167                                                  }
00168                                           }
00169                                           pdo_sqlite_error_stmt(stmt);
00170                                           return 0;
00171                             }
00172                      }
00173                      break;
00174 
00175               default:
00176                      ;
00177        }
00178        return 1;
00179 }
00180 
00181 static int pdo_sqlite_stmt_fetch(pdo_stmt_t *stmt,
00182        enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
00183 {
00184        pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
00185        int i;
00186        if (!S->stmt) {
00187               return 0;     
00188        }
00189        if (S->pre_fetched) {
00190               S->pre_fetched = 0;
00191               return 1;
00192        }
00193        if (S->done) {
00194               return 0;
00195        }
00196        i = sqlite3_step(S->stmt);
00197        switch (i) {
00198               case SQLITE_ROW:
00199                      return 1;
00200 
00201               case SQLITE_DONE:
00202                      S->done = 1;
00203                      sqlite3_reset(S->stmt);
00204                      return 0;
00205 
00206               case SQLITE_ERROR:
00207                      sqlite3_reset(S->stmt);
00208               default:
00209                      pdo_sqlite_error_stmt(stmt);
00210                      return 0;
00211        }
00212 }
00213 
00214 static int pdo_sqlite_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
00215 {
00216        pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
00217 
00218        if(colno >= sqlite3_column_count(S->stmt)) {
00219               /* error invalid column */
00220               pdo_sqlite_error_stmt(stmt);
00221               return 0;
00222        }
00223 
00224        stmt->columns[colno].name = estrdup(sqlite3_column_name(S->stmt, colno));
00225        stmt->columns[colno].namelen = strlen(stmt->columns[colno].name);
00226        stmt->columns[colno].maxlen = 0xffffffff;
00227        stmt->columns[colno].precision = 0;
00228        
00229        switch (sqlite3_column_type(S->stmt, colno)) {
00230               case SQLITE_INTEGER:
00231               case SQLITE_FLOAT:
00232               case SQLITE3_TEXT:
00233               case SQLITE_BLOB:
00234               case SQLITE_NULL:
00235               default:
00236                      stmt->columns[colno].param_type = PDO_PARAM_STR;
00237                      break;
00238        }
00239 
00240        return 1;
00241 }
00242 
00243 static int pdo_sqlite_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC)
00244 {
00245        pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
00246        if (!S->stmt) {
00247               return 0;
00248        }
00249        if(colno >= sqlite3_data_count(S->stmt)) {
00250               /* error invalid column */
00251               pdo_sqlite_error_stmt(stmt);
00252               return 0;
00253        }
00254        switch (sqlite3_column_type(S->stmt, colno)) {
00255               case SQLITE_NULL:
00256                      *ptr = NULL;
00257                      *len = 0;
00258                      return 1;
00259 
00260               case SQLITE_BLOB:
00261                      *ptr = (char*)sqlite3_column_blob(S->stmt, colno);
00262                      *len = sqlite3_column_bytes(S->stmt, colno);
00263                      return 1;
00264 
00265               default:
00266                      *ptr = (char*)sqlite3_column_text(S->stmt, colno);
00267                      *len = sqlite3_column_bytes(S->stmt, colno);
00268                      return 1;
00269        }
00270 }
00271 
00272 static int pdo_sqlite_stmt_col_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC)
00273 {
00274        pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
00275        const char *str;
00276        zval *flags;
00277        
00278        if (!S->stmt) {
00279               return FAILURE;
00280        }
00281        if(colno >= sqlite3_data_count(S->stmt)) {
00282               /* error invalid column */
00283               pdo_sqlite_error_stmt(stmt);
00284               return FAILURE;
00285        }
00286 
00287        array_init(return_value);
00288        MAKE_STD_ZVAL(flags);
00289        array_init(flags);
00290 
00291        switch (sqlite3_column_type(S->stmt, colno)) {
00292               case SQLITE_NULL:
00293                      add_assoc_string(return_value, "native_type", "null", 1);
00294                      break;
00295 
00296               case SQLITE_FLOAT:
00297                      add_assoc_string(return_value, "native_type", "double", 1);
00298                      break;
00299 
00300               case SQLITE_BLOB:
00301                      add_next_index_string(flags, "blob", 1);
00302               case SQLITE_TEXT:
00303                      add_assoc_string(return_value, "native_type", "string", 1);
00304                      break;
00305 
00306               case SQLITE_INTEGER:
00307                      add_assoc_string(return_value, "native_type", "integer", 1);
00308                      break;
00309        }
00310 
00311        str = sqlite3_column_decltype(S->stmt, colno);
00312        if (str) {
00313               add_assoc_string(return_value, "sqlite:decl_type", (char *)str, 1);
00314        }
00315 
00316 #ifdef SQLITE_ENABLE_COLUMN_METADATA
00317        str = sqlite3_column_table_name(S->stmt, colno);
00318        if (str) {
00319               add_assoc_string(return_value, "table", (char *)str, 1);
00320        }
00321 #endif
00322 
00323        add_assoc_zval(return_value, "flags", flags);
00324 
00325        return SUCCESS;
00326 }
00327 
00328 static int pdo_sqlite_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
00329 {
00330        pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
00331        sqlite3_reset(S->stmt);
00332        return 1;
00333 }
00334 
00335 struct pdo_stmt_methods sqlite_stmt_methods = {
00336        pdo_sqlite_stmt_dtor,
00337        pdo_sqlite_stmt_execute,
00338        pdo_sqlite_stmt_fetch,
00339        pdo_sqlite_stmt_describe,
00340        pdo_sqlite_stmt_get_col,
00341        pdo_sqlite_stmt_param_hook,
00342        NULL, /* set_attr */
00343        NULL, /* get_attr */
00344        pdo_sqlite_stmt_col_meta,
00345        NULL, /* next_rowset */
00346        pdo_sqlite_stmt_cursor_closer
00347 };
00348 
00349 /*
00350  * Local variables:
00351  * tab-width: 4
00352  * c-basic-offset: 4
00353  * End:
00354  * vim600: noet sw=4 ts=4 fdm=marker
00355  * vim<600: noet sw=4 ts=4
00356  */