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/sqlite3 - sqlite3.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 764 984 77.6 %
Date: 2014-07-27 Functions: 58 65 89.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2013 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             :    | Authors: Scott MacVicar <scottmac@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 "php_sqlite3.h"
      29             : #include "php_sqlite3_structs.h"
      30             : #include "main/SAPI.h"
      31             : 
      32             : #include <sqlite3.h>
      33             : 
      34             : #include "zend_exceptions.h"
      35             : #include "zend_interfaces.h"
      36             : #include "SAPI.h"
      37             : 
      38             : ZEND_DECLARE_MODULE_GLOBALS(sqlite3)
      39             : 
      40             : static PHP_GINIT_FUNCTION(sqlite3);
      41             : static int php_sqlite3_authorizer(void *autharg, int access_type, const char *arg3, const char *arg4, const char *arg5, const char *arg6);
      42             : static void sqlite3_param_dtor(void *data);
      43             : static int php_sqlite3_compare_stmt_zval_free(php_sqlite3_free_list **free_list, zval *statement);
      44             : 
      45             : /* {{{ Error Handler
      46             : */
      47          11 : static void php_sqlite3_error(php_sqlite3_db_object *db_obj, char *format, ...)
      48             : {
      49             :         va_list arg;
      50             :         char    *message;
      51             :         TSRMLS_FETCH();
      52             : 
      53          11 :         va_start(arg, format); 
      54          11 :         vspprintf(&message, 0, format, arg);
      55          11 :         va_end(arg);
      56             : 
      57          11 :         if (db_obj->exception) {
      58           1 :                 zend_throw_exception(zend_exception_get_default(TSRMLS_C), message, 0 TSRMLS_CC);
      59             :         } else {
      60          10 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", message);
      61             :         }
      62             :         
      63          11 :         if (message) {
      64          11 :                 efree(message);
      65             :         }
      66          11 : }
      67             : /* }}} */
      68             : 
      69             : #define SQLITE3_CHECK_INITIALIZED(db_obj, member, class_name) \
      70             :         if (!(member)) { \
      71             :                 php_sqlite3_error(db_obj, "The " #class_name " object has not been correctly initialised"); \
      72             :                 RETURN_FALSE; \
      73             :         }
      74             : 
      75             : /* {{{ PHP_INI
      76             : */
      77             : PHP_INI_BEGIN()
      78             :         STD_PHP_INI_ENTRY("sqlite3.extension_dir",  NULL, PHP_INI_SYSTEM, OnUpdateString, extension_dir, zend_sqlite3_globals, sqlite3_globals)
      79             : PHP_INI_END()
      80             : /* }}} */
      81             : 
      82             : /* Handlers */
      83             : static zend_object_handlers sqlite3_object_handlers;
      84             : static zend_object_handlers sqlite3_stmt_object_handlers;
      85             : static zend_object_handlers sqlite3_result_object_handlers;
      86             : 
      87             : /* Class entries */
      88             : zend_class_entry *php_sqlite3_sc_entry;
      89             : zend_class_entry *php_sqlite3_stmt_entry;
      90             : zend_class_entry *php_sqlite3_result_entry;
      91             : 
      92             : /* {{{ proto void SQLite3::open(String filename [, int Flags [, string Encryption Key]])
      93             :    Opens a SQLite 3 Database, if the build includes encryption then it will attempt to use the key. */
      94          67 : PHP_METHOD(sqlite3, open)
      95             : {
      96             :         php_sqlite3_db_object *db_obj;
      97          67 :         zval *object = getThis();
      98             :         char *filename, *encryption_key, *fullpath;
      99          67 :         int filename_len, encryption_key_len = 0;
     100          67 :         long flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
     101             :         zend_error_handling error_handling;
     102             : 
     103          67 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
     104          67 :         zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
     105             : 
     106          67 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls", &filename, &filename_len, &flags, &encryption_key, &encryption_key_len)) {
     107           1 :                 zend_restore_error_handling(&error_handling TSRMLS_CC);
     108           1 :                 return;
     109             :         }
     110             : 
     111          66 :         zend_restore_error_handling(&error_handling TSRMLS_CC);
     112             : 
     113          66 :         if (db_obj->initialised) {
     114           1 :                 zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Already initialised DB Object", 0 TSRMLS_CC);
     115             :         }
     116             : 
     117          66 :         if (strlen(filename) != filename_len) {
     118           0 :                 return;
     119             :         }
     120          66 :         if (memcmp(filename, ":memory:", sizeof(":memory:")) != 0) {
     121           6 :                 if (!(fullpath = expand_filepath(filename, NULL TSRMLS_CC))) {
     122           1 :                         zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Unable to expand filepath", 0 TSRMLS_CC);
     123           1 :                         return;
     124             :                 }
     125             : 
     126             : #if PHP_API_VERSION < 20100412
     127           5 :                 if (PG(safe_mode) && (!php_checkuid(fullpath, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
     128           0 :                         zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "safe_mode prohibits opening %s", fullpath);
     129           0 :                         efree(fullpath);
     130           0 :                         return;
     131             :                 }
     132             : #endif
     133             : 
     134           5 :                 if (php_check_open_basedir(fullpath TSRMLS_CC)) {
     135           1 :                         zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "open_basedir prohibits opening %s", fullpath);
     136           1 :                         efree(fullpath);
     137           1 :                         return;
     138             :                 }
     139             :         } else {
     140          60 :                 fullpath = estrdup(filename);
     141             :         }
     142             : 
     143             : #if SQLITE_VERSION_NUMBER >= 3005000
     144          64 :         if (sqlite3_open_v2(fullpath, &(db_obj->db), flags, NULL) != SQLITE_OK) {
     145             : #else
     146             :         if (sqlite3_open(fullpath, &(db_obj->db)) != SQLITE_OK) {
     147             : #endif
     148           1 :                 zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
     149           1 :                 if (fullpath) {
     150           1 :                         efree(fullpath);
     151             :                 }
     152           1 :                 return;
     153             :         }
     154             : 
     155             : #if SQLITE_HAS_CODEC
     156             :         if (encryption_key_len > 0) {
     157             :                 if (sqlite3_key(db_obj->db, encryption_key, encryption_key_len) != SQLITE_OK) {
     158             :                         zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
     159             :                         return;
     160             :                 }
     161             :         }
     162             : #endif
     163             : 
     164          63 :         db_obj->initialised = 1;
     165             : 
     166             : #if PHP_API_VERSION < 20100412
     167          63 :         if (PG(safe_mode) || (PG(open_basedir) && *PG(open_basedir))) {
     168             : #else
     169             :         if (PG(open_basedir) && *PG(open_basedir)) {
     170             : #endif
     171           2 :                 sqlite3_set_authorizer(db_obj->db, php_sqlite3_authorizer, NULL);
     172             :         }
     173             : 
     174          63 :         if (fullpath) {
     175          63 :                 efree(fullpath);
     176             :         }
     177             : }
     178             : /* }}} */
     179             : 
     180             : /* {{{ proto bool SQLite3::close()
     181             :    Close a SQLite 3 Database. */
     182          39 : PHP_METHOD(sqlite3, close)
     183             : {
     184             :         php_sqlite3_db_object *db_obj;
     185          39 :         zval *object = getThis();
     186             :         int errcode;
     187          39 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
     188             : 
     189          39 :         if (zend_parse_parameters_none() == FAILURE) {
     190           2 :                 return;
     191             :         }
     192             : 
     193          37 :         if (db_obj->initialised) {
     194          37 :                 zend_llist_clean(&(db_obj->free_list));
     195          37 :                 errcode = sqlite3_close(db_obj->db);
     196          37 :                 if (errcode != SQLITE_OK) {
     197           0 :                         php_sqlite3_error(db_obj, "Unable to close database: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
     198           0 :                         RETURN_FALSE;
     199             :                 }
     200          37 :                 db_obj->initialised = 0;
     201             :         }
     202             : 
     203          37 :         RETURN_TRUE;
     204             : }
     205             : /* }}} */
     206             : 
     207             : /* {{{ proto bool SQLite3::exec(String Query)
     208             :    Executes a result-less query against a given database. */
     209         105 : PHP_METHOD(sqlite3, exec)
     210             : {
     211             :         php_sqlite3_db_object *db_obj;
     212         105 :         zval *object = getThis();
     213         105 :         char *sql, *errtext = NULL;
     214             :         int sql_len;
     215         105 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
     216             : 
     217         105 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
     218             : 
     219         105 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
     220           1 :                 return;
     221             :         }
     222             : 
     223         104 :         if (sqlite3_exec(db_obj->db, sql, NULL, NULL, &errtext) != SQLITE_OK) {
     224           1 :                 php_sqlite3_error(db_obj, "%s", errtext);
     225           1 :                 sqlite3_free(errtext);
     226           1 :                 RETURN_FALSE;
     227             :         }
     228             : 
     229         103 :         RETURN_TRUE;
     230             : }
     231             : /* }}} */
     232             : 
     233             : /* {{{ proto Array SQLite3::version()
     234             :    Returns the SQLite3 Library version as a string constant and as a number. */
     235           3 : PHP_METHOD(sqlite3, version)
     236             : {
     237           3 :         if (zend_parse_parameters_none() == FAILURE) {
     238           1 :                 return;
     239             :         }
     240             : 
     241           2 :         array_init(return_value);
     242             : 
     243           2 :         add_assoc_string(return_value, "versionString", (char*)sqlite3_libversion(), 1);
     244           2 :         add_assoc_long(return_value, "versionNumber", sqlite3_libversion_number());
     245             : 
     246           2 :         return;
     247             : }
     248             : /* }}} */
     249             : 
     250             : /* {{{ proto int SQLite3::lastInsertRowID()
     251             :    Returns the rowid of the most recent INSERT into the database from the database connection. */
     252           3 : PHP_METHOD(sqlite3, lastInsertRowID)
     253             : {
     254             :         php_sqlite3_db_object *db_obj;
     255           3 :         zval *object = getThis();
     256           3 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
     257             : 
     258           3 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
     259             : 
     260           3 :         if (zend_parse_parameters_none() == FAILURE) {
     261           1 :                 return;
     262             :         }
     263             : 
     264           2 :         RETURN_LONG(sqlite3_last_insert_rowid(db_obj->db));
     265             : }
     266             : /* }}} */
     267             : 
     268             : /* {{{ proto int SQLite3::lastErrorCode()
     269             :    Returns the numeric result code of the most recent failed sqlite API call for the database connection. */
     270           2 : PHP_METHOD(sqlite3, lastErrorCode)
     271             : {
     272             :         php_sqlite3_db_object *db_obj;
     273           2 :         zval *object = getThis();
     274           2 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
     275             : 
     276           2 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
     277             : 
     278           2 :         if (zend_parse_parameters_none() == FAILURE) {
     279           1 :                 return;
     280             :         }
     281             : 
     282           1 :         RETURN_LONG(sqlite3_errcode(db_obj->db));
     283             : }
     284             : /* }}} */
     285             : 
     286             : /* {{{ proto string SQLite3::lastErrorMsg()
     287             :    Returns english text describing the most recent failed sqlite API call for the database connection. */
     288           2 : PHP_METHOD(sqlite3, lastErrorMsg)
     289             : {
     290             :         php_sqlite3_db_object *db_obj;
     291           2 :         zval *object = getThis();
     292           2 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
     293             : 
     294           2 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
     295             : 
     296           2 :         if (zend_parse_parameters_none() == FAILURE) {
     297           1 :                 return;
     298             :         }
     299             : 
     300           1 :         RETVAL_STRING((char *)sqlite3_errmsg(db_obj->db), 1);
     301             : }
     302             : /* }}} */
     303             : 
     304             : /* {{{ proto bool SQLite3::busyTimeout(int msecs)
     305             :    Sets a busy handler that will sleep until database is not locked or timeout is reached. Passing a value less than or equal to zero turns off all busy handlers. */
     306           0 : PHP_METHOD(sqlite3, busyTimeout)
     307             : {
     308             :         php_sqlite3_db_object *db_obj;
     309           0 :         zval *object = getThis();
     310             :         long ms;
     311             :         int return_code;
     312           0 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
     313             : 
     314           0 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
     315             : 
     316           0 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ms)) {
     317           0 :                 return;
     318             :         }
     319             : 
     320           0 :         return_code = sqlite3_busy_timeout(db_obj->db, ms);
     321           0 :         if (return_code != SQLITE_OK) {
     322           0 :                 php_sqlite3_error(db_obj, "Unable to set busy timeout: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
     323           0 :                 RETURN_FALSE;
     324             :         }
     325             : 
     326           0 :         RETURN_TRUE;
     327             : }
     328             : /* }}} */
     329             : 
     330             : 
     331             : #ifndef SQLITE_OMIT_LOAD_EXTENSION
     332             : /* {{{ proto bool SQLite3::loadExtension(String Shared Library)
     333             :    Attempts to load an SQLite extension library. */
     334           4 : PHP_METHOD(sqlite3, loadExtension)
     335             : {
     336             :         php_sqlite3_db_object *db_obj;
     337           4 :         zval *object = getThis();
     338           4 :         char *extension, *lib_path, *extension_dir, *errtext = NULL;
     339             :         char fullpath[MAXPATHLEN];
     340             :         int extension_len, extension_dir_len;
     341           4 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
     342             : 
     343           4 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
     344             : 
     345           4 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &extension, &extension_len)) {
     346           1 :                 return;
     347             :         }
     348             : 
     349             : #ifdef ZTS
     350             :         if ((strncmp(sapi_module.name, "cgi", 3) != 0) &&
     351             :                 (strcmp(sapi_module.name, "cli") != 0) &&
     352             :                 (strncmp(sapi_module.name, "embed", 5) != 0)
     353             :         ) {             php_sqlite3_error(db_obj, "Not supported in multithreaded Web servers");
     354             :                 RETURN_FALSE;
     355             :         }
     356             : #endif
     357             : 
     358           3 :         if (!SQLITE3G(extension_dir)) {
     359           1 :                 php_sqlite3_error(db_obj, "SQLite Extension are disabled");
     360           1 :                 RETURN_FALSE;
     361             :         }
     362             : 
     363           2 :         if (extension_len == 0) {
     364           1 :                 php_sqlite3_error(db_obj, "Empty string as an extension");
     365           1 :                 RETURN_FALSE;
     366             :         }
     367             : 
     368           1 :         extension_dir = SQLITE3G(extension_dir);
     369           1 :         extension_dir_len = strlen(SQLITE3G(extension_dir));
     370             : 
     371           1 :         if (IS_SLASH(extension_dir[extension_dir_len-1])) {
     372           0 :                 spprintf(&lib_path, 0, "%s%s", extension_dir, extension);
     373             :         } else {
     374           1 :                 spprintf(&lib_path, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, extension);
     375             :         }
     376             : 
     377           1 :         if (!VCWD_REALPATH(lib_path, fullpath)) {
     378           1 :                 php_sqlite3_error(db_obj, "Unable to load extension at '%s'", lib_path);
     379           1 :                 efree(lib_path);
     380           1 :                 RETURN_FALSE;
     381             :         }
     382             : 
     383           0 :         efree(lib_path);
     384             : 
     385           0 :         if (strncmp(fullpath, extension_dir, extension_dir_len) != 0) {
     386           0 :                 php_sqlite3_error(db_obj, "Unable to open extensions outside the defined directory");
     387           0 :                 RETURN_FALSE;
     388             :         }
     389             : 
     390             :         /* Extension loading should only be enabled for when we attempt to load */
     391           0 :         sqlite3_enable_load_extension(db_obj->db, 1);
     392           0 :         if (sqlite3_load_extension(db_obj->db, fullpath, 0, &errtext) != SQLITE_OK) {
     393           0 :                 php_sqlite3_error(db_obj, "%s", errtext);
     394           0 :                 sqlite3_free(errtext);
     395           0 :                 sqlite3_enable_load_extension(db_obj->db, 0);
     396           0 :                 RETURN_FALSE;
     397             :         }
     398           0 :         sqlite3_enable_load_extension(db_obj->db, 0);
     399             : 
     400           0 :         RETURN_TRUE;
     401             : }
     402             : /* }}} */
     403             : #endif
     404             : 
     405             : /* {{{ proto int SQLite3::changes()
     406             :   Returns the number of database rows that were changed (or inserted or deleted) by the most recent SQL statement. */
     407           3 : PHP_METHOD(sqlite3, changes)
     408             : {
     409             :         php_sqlite3_db_object *db_obj;
     410           3 :         zval *object = getThis();
     411           3 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
     412             : 
     413           3 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
     414             : 
     415           3 :         if (zend_parse_parameters_none() == FAILURE) {
     416           1 :                 return;
     417             :         }
     418             : 
     419           2 :         RETURN_LONG(sqlite3_changes(db_obj->db));
     420             : }
     421             : /* }}} */
     422             : 
     423             : /* {{{ proto String SQLite3::escapeString(String value)
     424             :    Returns a string that has been properly escaped. */
     425           1 : PHP_METHOD(sqlite3, escapeString)
     426             : {
     427             :         char *sql, *ret;
     428             :         int sql_len;
     429             : 
     430           1 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
     431           0 :                 return;
     432             :         }
     433             : 
     434           1 :         if (sql_len) {
     435           1 :                 ret = sqlite3_mprintf("%q", sql);
     436           1 :                 if (ret) {
     437           1 :                         RETVAL_STRING(ret, 1);
     438           1 :                         sqlite3_free(ret);
     439             :                 }
     440             :         } else {
     441           0 :                 RETURN_EMPTY_STRING();
     442             :         }
     443             : }
     444             : /* }}} */
     445             : 
     446             : /* {{{ proto SQLite3Stmt SQLite3::prepare(String Query)
     447             :    Returns a prepared SQL statement for execution. */
     448          27 : PHP_METHOD(sqlite3, prepare)
     449             : {
     450             :         php_sqlite3_db_object *db_obj;
     451             :         php_sqlite3_stmt *stmt_obj;
     452          27 :         zval *object = getThis();
     453             :         char *sql;
     454             :         int sql_len, errcode;
     455             :         php_sqlite3_free_list *free_item;
     456             : 
     457          27 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
     458             : 
     459          27 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
     460             : 
     461          27 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
     462           1 :                 return;
     463             :         }
     464             : 
     465          26 :         if (!sql_len) {
     466           1 :                 RETURN_FALSE;
     467             :         }
     468             : 
     469          25 :         object_init_ex(return_value, php_sqlite3_stmt_entry);
     470          25 :         stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(return_value TSRMLS_CC);
     471          25 :         stmt_obj->db_obj = db_obj;
     472          25 :         stmt_obj->db_obj_zval = getThis();
     473             : 
     474             :         Z_ADDREF_P(object);
     475             : 
     476          25 :         errcode = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &(stmt_obj->stmt), NULL);
     477          25 :         if (errcode != SQLITE_OK) {
     478           1 :                 php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
     479           1 :                 zval_dtor(return_value);
     480           1 :                 RETURN_FALSE;
     481             :         }
     482             : 
     483          24 :         stmt_obj->initialised = 1;
     484             : 
     485          24 :         free_item = emalloc(sizeof(php_sqlite3_free_list));
     486          24 :         free_item->stmt_obj = stmt_obj;
     487          24 :         free_item->stmt_obj_zval = return_value;
     488             : 
     489          24 :         zend_llist_add_element(&(db_obj->free_list), &free_item);
     490             : }
     491             : /* }}} */
     492             : 
     493             : /* {{{ proto SQLite3Result SQLite3::query(String Query)
     494             :    Returns true or false, for queries that return data it will return a SQLite3Result object. */
     495          24 : PHP_METHOD(sqlite3, query)
     496             : {
     497             :         php_sqlite3_db_object *db_obj;
     498             :         php_sqlite3_result *result;
     499             :         php_sqlite3_stmt *stmt_obj;
     500          24 :         zval *object = getThis();
     501          24 :         zval *stmt = NULL;
     502          24 :         char *sql, *errtext = NULL;
     503             :         int sql_len, return_code;
     504          24 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
     505             : 
     506          24 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
     507             : 
     508          24 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
     509           2 :                 return;
     510             :         }
     511             : 
     512          22 :         if (!sql_len) {
     513           1 :                 RETURN_FALSE;
     514             :         }
     515             : 
     516             :         /* If there was no return value then just execute the query */
     517          21 :         if (!return_value_used) {
     518           2 :                 if (sqlite3_exec(db_obj->db, sql, NULL, NULL, &errtext) != SQLITE_OK) {
     519           2 :                         php_sqlite3_error(db_obj, "%s", errtext);
     520           2 :                         sqlite3_free(errtext);
     521             :                 }
     522           2 :                 return;
     523             :         }
     524             : 
     525          19 :         MAKE_STD_ZVAL(stmt);
     526             : 
     527          19 :         object_init_ex(stmt, php_sqlite3_stmt_entry);
     528          19 :         stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(stmt TSRMLS_CC);
     529          19 :         stmt_obj->db_obj = db_obj;
     530          19 :         stmt_obj->db_obj_zval = getThis();
     531             : 
     532             :         Z_ADDREF_P(object);
     533             : 
     534          19 :         return_code = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &(stmt_obj->stmt), NULL);
     535          19 :         if (return_code != SQLITE_OK) {
     536           1 :                 php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
     537           1 :                 zval_ptr_dtor(&stmt);
     538           1 :                 RETURN_FALSE;
     539             :         }
     540             : 
     541          18 :         stmt_obj->initialised = 1;
     542             : 
     543          18 :         object_init_ex(return_value, php_sqlite3_result_entry);
     544          18 :         result = (php_sqlite3_result *)zend_object_store_get_object(return_value TSRMLS_CC);
     545          18 :         result->db_obj = db_obj;
     546          18 :         result->stmt_obj = stmt_obj;
     547          18 :         result->stmt_obj_zval = stmt;
     548             : 
     549          18 :         return_code = sqlite3_step(result->stmt_obj->stmt);
     550             : 
     551          18 :         switch (return_code) {
     552             :                 case SQLITE_ROW: /* Valid Row */
     553             :                 case SQLITE_DONE: /* Valid but no results */
     554             :                 {
     555             :                         php_sqlite3_free_list *free_item;
     556          18 :                         free_item = emalloc(sizeof(php_sqlite3_free_list));
     557          18 :                         free_item->stmt_obj = stmt_obj;
     558          18 :                         free_item->stmt_obj_zval = stmt;
     559          18 :                         zend_llist_add_element(&(db_obj->free_list), &free_item);
     560          18 :                         sqlite3_reset(result->stmt_obj->stmt);
     561          18 :                         break;
     562             :                 }
     563             :                 default:
     564           0 :                         php_sqlite3_error(db_obj, "Unable to execute statement: %s", sqlite3_errmsg(db_obj->db));
     565           0 :                         sqlite3_finalize(stmt_obj->stmt);
     566           0 :                         stmt_obj->initialised = 0;
     567           0 :                         zval_dtor(return_value);
     568           0 :                         RETURN_FALSE;
     569             :         }
     570             : }
     571             : /* }}} */
     572             : 
     573          69 : static zval* sqlite_value_to_zval(sqlite3_stmt *stmt, int column) /* {{{ */
     574             : {
     575             :         zval *data;
     576          69 :         MAKE_STD_ZVAL(data);
     577          69 :         switch (sqlite3_column_type(stmt, column)) {
     578             :                 case SQLITE_INTEGER:
     579          27 :                         if ((sqlite3_column_int64(stmt, column)) >= INT_MAX || sqlite3_column_int64(stmt, column) <= INT_MIN) {
     580           0 :                                 ZVAL_STRINGL(data, (char *)sqlite3_column_text(stmt, column), sqlite3_column_bytes(stmt, column), 1);
     581             :                         } else {
     582          27 :                                 ZVAL_LONG(data, sqlite3_column_int64(stmt, column));
     583             :                         }
     584          27 :                         break;
     585             : 
     586             :                 case SQLITE_FLOAT:
     587           0 :                         ZVAL_DOUBLE(data, sqlite3_column_double(stmt, column));
     588           0 :                         break;
     589             : 
     590             :                 case SQLITE_NULL:
     591           0 :                         ZVAL_NULL(data);
     592           0 :                         break;
     593             : 
     594             :                 case SQLITE3_TEXT:
     595          42 :                         ZVAL_STRING(data, (char*)sqlite3_column_text(stmt, column), 1);
     596          42 :                         break;
     597             : 
     598             :                 case SQLITE_BLOB:
     599             :                 default:
     600           0 :                         ZVAL_STRINGL(data, (char*)sqlite3_column_blob(stmt, column), sqlite3_column_bytes(stmt, column), 1);
     601             :         }
     602          69 :         return data;
     603             : }
     604             : /* }}} */
     605             : 
     606             : /* {{{ proto SQLite3Result SQLite3::querySingle(String Query [, bool entire_row = false])
     607             :    Returns a string of the first column, or an array of the entire row. */
     608           9 : PHP_METHOD(sqlite3, querySingle)
     609             : {
     610             :         php_sqlite3_db_object *db_obj;
     611           9 :         zval *object = getThis();
     612           9 :         char *sql, *errtext = NULL;
     613             :         int sql_len, return_code;
     614           9 :         zend_bool entire_row = 0;
     615             :         sqlite3_stmt *stmt;
     616           9 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
     617             : 
     618           9 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
     619             : 
     620           9 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &sql, &sql_len, &entire_row)) {
     621           2 :                 return;
     622             :         }
     623             : 
     624           7 :         if (!sql_len) {
     625           1 :                 RETURN_FALSE;
     626             :         }
     627             : 
     628             :         /* If there was no return value then just execute the query */
     629           6 :         if (!return_value_used) {
     630           0 :                 if (sqlite3_exec(db_obj->db, sql, NULL, NULL, &errtext) != SQLITE_OK) {
     631           0 :                         php_sqlite3_error(db_obj, "%s", errtext);
     632           0 :                         sqlite3_free(errtext);
     633             :                 }
     634           0 :                 return;
     635             :         }
     636             : 
     637           6 :         return_code = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &stmt, NULL);
     638           6 :         if (return_code != SQLITE_OK) {
     639           0 :                 php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
     640           0 :                 RETURN_FALSE;
     641             :         }
     642             : 
     643           6 :         return_code = sqlite3_step(stmt);
     644             : 
     645           6 :         switch (return_code) {
     646             :                 case SQLITE_ROW: /* Valid Row */
     647             :                 {
     648           6 :                         if (!entire_row) {
     649             :                                 zval *data;
     650           4 :                                 data = sqlite_value_to_zval(stmt, 0);
     651           4 :                                 *return_value = *data;
     652           4 :                                 zval_copy_ctor(return_value);
     653           4 :                                 zval_dtor(data);
     654           4 :                                 FREE_ZVAL(data);
     655             :                         } else {
     656           2 :                                 int i = 0;
     657           2 :                                 array_init(return_value);
     658           6 :                                 for (i = 0; i < sqlite3_data_count(stmt); i++) {
     659             :                                         zval *data;
     660           4 :                                         data = sqlite_value_to_zval(stmt, i);
     661           4 :                                         add_assoc_zval(return_value, (char*)sqlite3_column_name(stmt, i), data);
     662             :                                 }
     663             :                         }
     664           6 :                         break;
     665             :                 }
     666             :                 case SQLITE_DONE: /* Valid but no results */
     667             :                 {
     668           0 :                         if (!entire_row) {
     669           0 :                                 RETVAL_NULL();
     670             :                         } else {
     671           0 :                                 array_init(return_value);
     672             :                         }
     673           0 :                         break;
     674             :                 }
     675             :                 default:
     676           0 :                         php_sqlite3_error(db_obj, "Unable to execute statement: %s", sqlite3_errmsg(db_obj->db));
     677           0 :                         RETVAL_FALSE;
     678             :         }
     679           6 :         sqlite3_finalize(stmt);
     680             : }
     681             : /* }}} */
     682             : 
     683          19 : static int sqlite3_do_callback(struct php_sqlite3_fci *fc, zval *cb, int argc, sqlite3_value **argv, sqlite3_context *context, int is_agg TSRMLS_DC) /* {{{ */
     684             : {
     685          19 :         zval ***zargs = NULL;
     686          19 :         zval *retval = NULL;
     687             :         int i;
     688             :         int ret;
     689             :         int fake_argc;
     690          19 :         php_sqlite3_agg_context *agg_context = NULL;
     691             : 
     692          19 :         if (is_agg) {
     693          12 :                 is_agg = 2;
     694             :         }
     695             : 
     696          19 :         fake_argc = argc + is_agg;
     697             : 
     698          19 :         fc->fci.size = sizeof(fc->fci);
     699          19 :         fc->fci.function_table = EG(function_table);
     700          19 :         fc->fci.function_name = cb;
     701          19 :         fc->fci.symbol_table = NULL;
     702          19 :         fc->fci.object_ptr = NULL;
     703          19 :         fc->fci.retval_ptr_ptr = &retval;
     704          19 :         fc->fci.param_count = fake_argc;
     705             : 
     706             :         /* build up the params */
     707             : 
     708          19 :         if (fake_argc) {
     709          19 :                 zargs = (zval ***)safe_emalloc(fake_argc, sizeof(zval **), 0);
     710             :         }
     711             : 
     712          19 :         if (is_agg) {
     713             :                 /* summon the aggregation context */
     714          12 :                 agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
     715             : 
     716          12 :                 if (!agg_context->zval_context) {
     717           2 :                         MAKE_STD_ZVAL(agg_context->zval_context);
     718           2 :                         ZVAL_NULL(agg_context->zval_context);
     719             :                 }
     720          12 :                 zargs[0] = &agg_context->zval_context;
     721             : 
     722          12 :                 zargs[1] = emalloc(sizeof(zval*));
     723          12 :                 MAKE_STD_ZVAL(*zargs[1]);
     724          12 :                 ZVAL_LONG(*zargs[1], agg_context->row_count);
     725             :         }
     726             : 
     727          36 :         for (i = 0; i < argc; i++) {
     728          17 :                 zargs[i + is_agg] = emalloc(sizeof(zval *));
     729          17 :                 MAKE_STD_ZVAL(*zargs[i + is_agg]);
     730             : 
     731          17 :                 switch (sqlite3_value_type(argv[i])) {
     732             :                         case SQLITE_INTEGER:
     733          10 :                                 ZVAL_LONG(*zargs[i + is_agg], sqlite3_value_int(argv[i]));
     734          10 :                                 break;
     735             : 
     736             :                         case SQLITE_FLOAT:
     737           0 :                                 ZVAL_DOUBLE(*zargs[i + is_agg], sqlite3_value_double(argv[i]));
     738           0 :                                 break;
     739             : 
     740             :                         case SQLITE_NULL:
     741           0 :                                 ZVAL_NULL(*zargs[i + is_agg]);
     742           0 :                                 break;
     743             : 
     744             :                         case SQLITE_BLOB:
     745             :                         case SQLITE3_TEXT:
     746             :                         default:
     747           7 :                                 ZVAL_STRINGL(*zargs[i + is_agg], (char*)sqlite3_value_text(argv[i]), sqlite3_value_bytes(argv[i]), 1);
     748             :                                 break;
     749             :                 }
     750             :         }
     751             : 
     752          19 :         fc->fci.params = zargs;
     753             : 
     754          19 :         if ((ret = zend_call_function(&fc->fci, &fc->fcc TSRMLS_CC)) == FAILURE) {
     755           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the callback");
     756             :         }
     757             : 
     758             :         /* clean up the params */
     759          19 :         if (fake_argc) {
     760          36 :                 for (i = is_agg; i < argc + is_agg; i++) {
     761          17 :                         zval_ptr_dtor(zargs[i]);
     762          17 :                         efree(zargs[i]);
     763             :                 }
     764          19 :                 if (is_agg) {
     765          12 :                         zval_ptr_dtor(zargs[1]);
     766          12 :                         efree(zargs[1]);
     767             :                 }
     768          19 :                 efree(zargs);
     769             :         }
     770             : 
     771          28 :         if (!is_agg || !argv) {
     772             :                 /* only set the sqlite return value if we are a scalar function,
     773             :                  * or if we are finalizing an aggregate */
     774           9 :                 if (retval) {
     775           9 :                         switch (Z_TYPE_P(retval)) {
     776             :                                 case IS_LONG:
     777           0 :                                         sqlite3_result_int(context, Z_LVAL_P(retval));
     778           0 :                                         break;
     779             : 
     780             :                                 case IS_NULL:
     781           0 :                                         sqlite3_result_null(context);
     782           0 :                                         break;
     783             : 
     784             :                                 case IS_DOUBLE:
     785           0 :                                         sqlite3_result_double(context, Z_DVAL_P(retval));
     786           0 :                                         break;
     787             : 
     788             :                                 default:
     789           9 :                                         convert_to_string_ex(&retval);
     790           9 :                                         sqlite3_result_text(context, Z_STRVAL_P(retval), Z_STRLEN_P(retval), SQLITE_TRANSIENT);
     791             :                                         break;
     792             :                         }
     793             :                 } else {
     794           0 :                         sqlite3_result_error(context, "failed to invoke callback", 0);
     795             :                 }
     796             : 
     797           9 :                 if (agg_context && agg_context->zval_context) {
     798           2 :                         zval_ptr_dtor(&agg_context->zval_context);
     799             :                 }
     800             :         } else {
     801             :                 /* we're stepping in an aggregate; the return value goes into
     802             :                  * the context */
     803          10 :                 if (agg_context && agg_context->zval_context) {
     804          10 :                         zval_ptr_dtor(&agg_context->zval_context);
     805             :                 }
     806          10 :                 if (retval) {
     807          10 :                         agg_context->zval_context = retval;
     808          10 :                         retval = NULL;
     809             :                 } else {
     810           0 :                         agg_context->zval_context = NULL;
     811             :                 }
     812             :         }
     813             : 
     814          19 :         if (retval) {
     815           9 :                 zval_ptr_dtor(&retval);
     816             :         }
     817          19 :         return ret;
     818             : }
     819             : /* }}}*/
     820             : 
     821           7 : static void php_sqlite3_callback_func(sqlite3_context *context, int argc, sqlite3_value **argv) /* {{{ */
     822             : {
     823           7 :         php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
     824             :         TSRMLS_FETCH();
     825             : 
     826           7 :         sqlite3_do_callback(&func->afunc, func->func, argc, argv, context, 0 TSRMLS_CC);
     827           7 : }
     828             : /* }}}*/
     829             : 
     830          10 : static void php_sqlite3_callback_step(sqlite3_context *context, int argc, sqlite3_value **argv) /* {{{ */
     831             : {
     832          10 :         php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
     833          10 :         php_sqlite3_agg_context *agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
     834             : 
     835             :         TSRMLS_FETCH();
     836          10 :         agg_context->row_count++;
     837             : 
     838          10 :         sqlite3_do_callback(&func->astep, func->step, argc, argv, context, 1 TSRMLS_CC);
     839          10 : }
     840             : /* }}} */
     841             : 
     842           2 : static void php_sqlite3_callback_final(sqlite3_context *context) /* {{{ */
     843             : {
     844           2 :         php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
     845           2 :         php_sqlite3_agg_context *agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
     846             : 
     847             :         TSRMLS_FETCH();
     848           2 :         agg_context->row_count = 0;
     849             : 
     850           2 :         sqlite3_do_callback(&func->afini, func->fini, 0, NULL, context, 1 TSRMLS_CC);
     851           2 : }
     852             : /* }}} */
     853             : 
     854           6 : static int php_sqlite3_callback_compare(void *coll, int a_len, const void *a, int b_len, const void* b) /* {{{ */
     855             : {
     856           6 :         php_sqlite3_collation *collation = (php_sqlite3_collation*)coll;
     857           6 :         zval ***zargs = NULL;
     858           6 :         zval *retval = NULL;
     859             :         int ret;
     860             : 
     861             :         TSRMLS_FETCH();
     862             : 
     863           6 :         collation->fci.fci.size = (sizeof(collation->fci.fci));
     864           6 :         collation->fci.fci.function_table = EG(function_table);
     865           6 :         collation->fci.fci.function_name = collation->cmp_func;
     866           6 :         collation->fci.fci.symbol_table = NULL;
     867           6 :         collation->fci.fci.object_ptr = NULL;
     868           6 :         collation->fci.fci.retval_ptr_ptr = &retval;
     869           6 :         collation->fci.fci.param_count = 2;
     870             : 
     871           6 :         zargs = (zval***)safe_emalloc(2, sizeof(zval**), 0);
     872           6 :         zargs[0] = emalloc(sizeof(zval*));
     873           6 :         zargs[1] = emalloc(sizeof(zval*));
     874             : 
     875           6 :         MAKE_STD_ZVAL(*zargs[0]);
     876           6 :         ZVAL_STRINGL(*zargs[0], a, a_len, 1);
     877             : 
     878           6 :         MAKE_STD_ZVAL(*zargs[1]);
     879           6 :         ZVAL_STRINGL(*zargs[1], b, b_len, 1);
     880             :  
     881           6 :         collation->fci.fci.params = zargs;
     882             : 
     883           6 :         if ((ret = zend_call_function(&collation->fci.fci, &collation->fci.fcc TSRMLS_CC)) == FAILURE) {
     884           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the compare callback");
     885             :         }
     886             : 
     887           6 :         zval_ptr_dtor(zargs[0]);
     888           6 :         zval_ptr_dtor(zargs[1]);
     889           6 :         efree(zargs[0]);
     890           6 :         efree(zargs[1]);
     891           6 :         efree(zargs);
     892             : 
     893             :         //retval ought to contain a ZVAL_LONG by now
     894             :         // (the result of a comparison, i.e. most likely -1, 0, or 1)
     895             :         //I suppose we could accept any scalar return type, though.
     896           6 :         if (Z_TYPE_P(retval) != IS_LONG){
     897           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the compare callback (invalid return type).  Collation behaviour is undefined.");
     898             :         }else{
     899           6 :                 ret = Z_LVAL_P(retval);
     900             :         }
     901             : 
     902           6 :         zval_ptr_dtor(&retval);
     903             : 
     904           6 :         return ret;
     905             : }
     906             : /* }}} */
     907             : 
     908             : /* {{{ proto bool SQLite3::createFunction(string name, mixed callback [, int argcount])
     909             :    Allows registration of a PHP function as a SQLite UDF that can be called within SQL statements. */
     910           4 : PHP_METHOD(sqlite3, createFunction)
     911             : {
     912             :         php_sqlite3_db_object *db_obj;
     913           4 :         zval *object = getThis();
     914             :         php_sqlite3_func *func;
     915             :         char *sql_func, *callback_name;
     916             :         int sql_func_len;
     917             :         zval *callback_func;
     918           4 :         long sql_func_num_args = -1;
     919           4 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
     920             : 
     921           4 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
     922             : 
     923           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &sql_func, &sql_func_len, &callback_func, &sql_func_num_args) == FAILURE) {
     924           0 :                 return;
     925             :         }
     926             : 
     927           4 :         if (!sql_func_len) {
     928           0 :                 RETURN_FALSE;
     929             :         }
     930             : 
     931           4 :         if (!zend_is_callable(callback_func, 0, &callback_name TSRMLS_CC)) {
     932           0 :                 php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
     933           0 :                 efree(callback_name);
     934           0 :                 RETURN_FALSE;
     935             :         }
     936           4 :         efree(callback_name);
     937             : 
     938           4 :         func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
     939             : 
     940           4 :         if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, SQLITE_UTF8, func, php_sqlite3_callback_func, NULL, NULL) == SQLITE_OK) {
     941           4 :                 func->func_name = estrdup(sql_func);
     942             : 
     943           4 :                 MAKE_STD_ZVAL(func->func);
     944           4 :                 MAKE_COPY_ZVAL(&callback_func, func->func);
     945             : 
     946           4 :                 func->argc = sql_func_num_args;
     947           4 :                 func->next = db_obj->funcs;
     948           4 :                 db_obj->funcs = func;
     949             : 
     950           4 :                 RETURN_TRUE;
     951             :         }
     952           0 :         efree(func);
     953             : 
     954           0 :         RETURN_FALSE;
     955             : }
     956             : /* }}} */
     957             : 
     958             : /* {{{ proto bool SQLite3::createAggregate(string name, mixed step, mixed final [, int argcount])
     959             :    Allows registration of a PHP function for use as an aggregate. */
     960           5 : PHP_METHOD(sqlite3, createAggregate)
     961             : {
     962             :         php_sqlite3_db_object *db_obj;
     963           5 :         zval *object = getThis();
     964             :         php_sqlite3_func *func;
     965             :         char *sql_func, *callback_name;
     966             :         int sql_func_len;
     967             :         zval *step_callback, *fini_callback;
     968           5 :         long sql_func_num_args = -1;
     969           5 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
     970             : 
     971           5 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
     972             : 
     973           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szz|l", &sql_func, &sql_func_len, &step_callback, &fini_callback, &sql_func_num_args) == FAILURE) {
     974           1 :                 return;
     975             :         }
     976             : 
     977           4 :         if (!sql_func_len) {
     978           0 :                 RETURN_FALSE;
     979             :         }
     980             : 
     981           4 :         if (!zend_is_callable(step_callback, 0, &callback_name TSRMLS_CC)) {
     982           1 :                 php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
     983           1 :                 efree(callback_name);
     984           1 :                 RETURN_FALSE;
     985             :         }
     986           3 :         efree(callback_name);
     987             : 
     988           3 :         if (!zend_is_callable(fini_callback, 0, &callback_name TSRMLS_CC)) {
     989           1 :                 php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
     990           1 :                 efree(callback_name);
     991           1 :                 RETURN_FALSE;
     992             :         }
     993           2 :         efree(callback_name);
     994             : 
     995           2 :         func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
     996             : 
     997           2 :         if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, SQLITE_UTF8, func, NULL, php_sqlite3_callback_step, php_sqlite3_callback_final) == SQLITE_OK) {
     998           2 :                 func->func_name = estrdup(sql_func);
     999             : 
    1000           2 :                 MAKE_STD_ZVAL(func->step);
    1001           2 :                 MAKE_COPY_ZVAL(&step_callback, func->step);
    1002             : 
    1003           2 :                 MAKE_STD_ZVAL(func->fini);
    1004           2 :                 MAKE_COPY_ZVAL(&fini_callback, func->fini);
    1005             : 
    1006           2 :                 func->argc = sql_func_num_args;
    1007           2 :                 func->next = db_obj->funcs;
    1008           2 :                 db_obj->funcs = func;
    1009             : 
    1010           2 :                 RETURN_TRUE;
    1011             :         }
    1012           0 :         efree(func);
    1013             : 
    1014           0 :         RETURN_FALSE;
    1015             : }
    1016             : /* }}} */
    1017             : 
    1018             : /* {{{ proto bool SQLite3::createCollation(string name, mixed callback)
    1019             :    Registers a PHP function as a comparator that can be used with the SQL COLLATE operator. Callback must accept two strings and return an integer (as strcmp()). */
    1020           1 : PHP_METHOD(sqlite3, createCollation)
    1021             : {
    1022             :         php_sqlite3_db_object *db_obj;
    1023           1 :         zval *object = getThis();
    1024             :         php_sqlite3_collation *collation;
    1025             :         char *collation_name, *callback_name;
    1026             :         int collation_name_len;
    1027             :         zval *callback_func;
    1028           1 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
    1029             : 
    1030           1 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
    1031             : 
    1032           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &collation_name, &collation_name_len, &callback_func) == FAILURE) {
    1033           0 :                 RETURN_FALSE;
    1034             :         }
    1035             : 
    1036           1 :         if (!collation_name_len) {
    1037           0 :                 RETURN_FALSE;
    1038             :         }
    1039             : 
    1040           1 :         if (!zend_is_callable(callback_func, 0, &callback_name TSRMLS_CC)) {
    1041           0 :                 php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
    1042           0 :                 efree(callback_name);
    1043           0 :                 RETURN_FALSE;
    1044             :         }
    1045           1 :         efree(callback_name);
    1046             : 
    1047           1 :         collation = (php_sqlite3_collation *)ecalloc(1, sizeof(*collation));
    1048           1 :         if (sqlite3_create_collation(db_obj->db, collation_name, SQLITE_UTF8, collation, php_sqlite3_callback_compare) == SQLITE_OK) {
    1049           1 :                 collation->collation_name = estrdup(collation_name);
    1050             : 
    1051           1 :                 MAKE_STD_ZVAL(collation->cmp_func);
    1052           1 :                 MAKE_COPY_ZVAL(&callback_func, collation->cmp_func);
    1053             : 
    1054           1 :                 collation->next = db_obj->collations;
    1055           1 :                 db_obj->collations = collation;
    1056             : 
    1057           1 :                 RETURN_TRUE;
    1058             :         }
    1059           0 :         efree(collation);
    1060             : 
    1061           0 :         RETURN_FALSE;
    1062             : }
    1063             : /* }}} */
    1064             : 
    1065             : typedef struct {
    1066             :         sqlite3_blob *blob;
    1067             :         size_t           position;
    1068             :         size_t       size;
    1069             : } php_stream_sqlite3_data;
    1070             : 
    1071           0 : static size_t php_sqlite3_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
    1072             : {
    1073             : /*      php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract; */
    1074             :         
    1075           0 :         return 0;
    1076             : }
    1077             : 
    1078           2 : static size_t php_sqlite3_stream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
    1079             : {
    1080           2 :         php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
    1081             : 
    1082           2 :         if (sqlite3_stream->position + count >= sqlite3_stream->size) {
    1083           2 :                 count = sqlite3_stream->size - sqlite3_stream->position;
    1084           2 :                 stream->eof = 1;
    1085             :         }
    1086           2 :         if (count) {
    1087           1 :                 if (sqlite3_blob_read(sqlite3_stream->blob, buf, count, sqlite3_stream->position) != SQLITE_OK) {
    1088           0 :                         return 0;
    1089             :                 }
    1090           1 :                 sqlite3_stream->position += count;
    1091             :         }
    1092           2 :         return count;
    1093             : }
    1094             : 
    1095           1 : static int php_sqlite3_stream_close(php_stream *stream, int close_handle TSRMLS_DC)
    1096             : {
    1097           1 :         php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
    1098             :         
    1099           1 :         if (sqlite3_blob_close(sqlite3_stream->blob) != SQLITE_OK) {
    1100             :                 /* Error occured, but it still closed */
    1101             :         }
    1102             : 
    1103           1 :         efree(sqlite3_stream);
    1104             :         
    1105           1 :         return 0;
    1106             : }
    1107             : 
    1108           1 : static int php_sqlite3_stream_flush(php_stream *stream TSRMLS_DC)
    1109             : {
    1110             :         /* do nothing */
    1111           1 :         return 0;
    1112             : }
    1113             : 
    1114             : /* {{{ */
    1115           0 : static int php_sqlite3_stream_seek(php_stream *stream, off_t offset, int whence, off_t *newoffs TSRMLS_DC)
    1116             : {
    1117           0 :         php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
    1118             : 
    1119           0 :         switch(whence) {
    1120             :                 case SEEK_CUR:
    1121           0 :                         if (offset < 0) {
    1122           0 :                                 if (sqlite3_stream->position < (size_t)(-offset)) {
    1123           0 :                                         sqlite3_stream->position = 0;
    1124           0 :                                         *newoffs = -1;
    1125           0 :                                         return -1;
    1126             :                                 } else {
    1127           0 :                                         sqlite3_stream->position = sqlite3_stream->position + offset;
    1128           0 :                                         *newoffs = sqlite3_stream->position;
    1129           0 :                                         stream->eof = 0;
    1130           0 :                                         return 0;
    1131             :                                 }
    1132             :                         } else {
    1133           0 :                                 if (sqlite3_stream->position + (size_t)(offset) > sqlite3_stream->size) {
    1134           0 :                                         sqlite3_stream->position = sqlite3_stream->size;
    1135           0 :                                         *newoffs = -1;
    1136           0 :                                         return -1;
    1137             :                                 } else {
    1138           0 :                                         sqlite3_stream->position = sqlite3_stream->position + offset;
    1139           0 :                                         *newoffs = sqlite3_stream->position;
    1140           0 :                                         stream->eof = 0;
    1141           0 :                                         return 0;
    1142             :                                 }
    1143             :                         }
    1144             :                 case SEEK_SET:
    1145           0 :                         if (sqlite3_stream->size < (size_t)(offset)) {
    1146           0 :                                 sqlite3_stream->position = sqlite3_stream->size;
    1147           0 :                                 *newoffs = -1;
    1148           0 :                                 return -1;
    1149             :                         } else {
    1150           0 :                                 sqlite3_stream->position = offset;
    1151           0 :                                 *newoffs = sqlite3_stream->position;
    1152           0 :                                 stream->eof = 0;
    1153           0 :                                 return 0;
    1154             :                         }
    1155             :                 case SEEK_END:
    1156           0 :                         if (offset > 0) {
    1157           0 :                                 sqlite3_stream->position = sqlite3_stream->size;
    1158           0 :                                 *newoffs = -1;
    1159           0 :                                 return -1;
    1160           0 :                         } else if (sqlite3_stream->size < (size_t)(-offset)) {
    1161           0 :                                 sqlite3_stream->position = 0;
    1162           0 :                                 *newoffs = -1;
    1163           0 :                                 return -1;
    1164             :                         } else {
    1165           0 :                                 sqlite3_stream->position = sqlite3_stream->size + offset;
    1166           0 :                                 *newoffs = sqlite3_stream->position;
    1167           0 :                                 stream->eof = 0;
    1168           0 :                                 return 0;
    1169             :                         }
    1170             :                 default:
    1171           0 :                         *newoffs = sqlite3_stream->position;
    1172           0 :                         return -1;
    1173             :         }
    1174             : }
    1175             : /* }}} */
    1176             : 
    1177             : 
    1178           0 : static int php_sqlite3_stream_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
    1179             : {
    1180           0 :         return FAILURE;
    1181             : }
    1182             : 
    1183           1 : static int php_sqlite3_stream_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
    1184             : {
    1185           1 :         php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
    1186           1 :         ssb->sb.st_size = sqlite3_stream->size;
    1187           1 :         return 0;
    1188             : }
    1189             : 
    1190             : static php_stream_ops php_stream_sqlite3_ops = {
    1191             :         php_sqlite3_stream_write,
    1192             :         php_sqlite3_stream_read,
    1193             :         php_sqlite3_stream_close,
    1194             :         php_sqlite3_stream_flush,
    1195             :         "SQLite3",
    1196             :         php_sqlite3_stream_seek,
    1197             :         php_sqlite3_stream_cast,
    1198             :         php_sqlite3_stream_stat
    1199             : };
    1200             : 
    1201             : /* {{{ proto resource SQLite3::openBlob(string table, string column, int rowid [, string dbname])
    1202             :    Open a blob as a stream which we can read / write to. */
    1203           2 : PHP_METHOD(sqlite3, openBlob)
    1204             : {
    1205             :         php_sqlite3_db_object *db_obj;
    1206           2 :         zval *object = getThis();
    1207           2 :         char *table, *column, *dbname = "main";
    1208             :         int table_len, column_len, dbname_len;
    1209           2 :         long rowid, flags = 0;
    1210           2 :         sqlite3_blob *blob = NULL;
    1211             :         php_stream_sqlite3_data *sqlite3_stream;
    1212             :         php_stream *stream;
    1213             : 
    1214           2 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
    1215             : 
    1216           2 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
    1217             : 
    1218           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssl|s", &table, &table_len, &column, &column_len, &rowid, &dbname, &dbname_len) == FAILURE) {
    1219           1 :                 return;
    1220             :         }
    1221             : 
    1222           1 :         if (sqlite3_blob_open(db_obj->db, dbname, table, column, rowid, flags, &blob) != SQLITE_OK) {
    1223           0 :                 php_sqlite3_error(db_obj, "Unable to open blob: %s", sqlite3_errmsg(db_obj->db));
    1224           0 :                 RETURN_FALSE;
    1225             :         }
    1226             : 
    1227           1 :         sqlite3_stream = emalloc(sizeof(php_stream_sqlite3_data));
    1228           1 :         sqlite3_stream->blob = blob;
    1229           1 :         sqlite3_stream->position = 0;
    1230           1 :         sqlite3_stream->size = sqlite3_blob_bytes(blob);
    1231             :         
    1232           1 :         stream = php_stream_alloc(&php_stream_sqlite3_ops, sqlite3_stream, 0, "rb");
    1233             : 
    1234           1 :         if (stream) {
    1235           1 :                 php_stream_to_zval(stream, return_value);
    1236             :         } else {
    1237           0 :                 RETURN_FALSE;
    1238             :         }
    1239             : }
    1240             : /* }}} */
    1241             : 
    1242             : /* {{{ proto bool SQLite3::enableExceptions([bool enableExceptions = false])
    1243             :    Enables an exception error mode. */
    1244           3 : PHP_METHOD(sqlite3, enableExceptions)
    1245             : {
    1246             :         php_sqlite3_db_object *db_obj;
    1247           3 :         zval *object = getThis();
    1248           3 :         zend_bool enableExceptions = 0;
    1249             : 
    1250           3 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
    1251             : 
    1252           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enableExceptions) == FAILURE) {
    1253           1 :                 return;
    1254             :         }
    1255             : 
    1256           2 :         RETVAL_BOOL(db_obj->exception);
    1257             : 
    1258           2 :         db_obj->exception = enableExceptions;
    1259             : }
    1260             : /* }}} */
    1261             : 
    1262             : /* {{{ proto int SQLite3Stmt::paramCount()
    1263             :    Returns the number of parameters within the prepared statement. */
    1264           5 : PHP_METHOD(sqlite3stmt, paramCount)
    1265             : {
    1266             :         php_sqlite3_stmt *stmt_obj;
    1267           5 :         zval *object = getThis();
    1268           5 :         stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
    1269             : 
    1270           5 :         if (zend_parse_parameters_none() == FAILURE) {
    1271           1 :                 return;
    1272             :         }
    1273             : 
    1274           4 :         RETURN_LONG(sqlite3_bind_parameter_count(stmt_obj->stmt));
    1275             : }
    1276             : /* }}} */
    1277             : 
    1278             : /* {{{ proto bool SQLite3Stmt::close()
    1279             :    Closes the prepared statement. */
    1280           4 : PHP_METHOD(sqlite3stmt, close)
    1281             : {
    1282             :         php_sqlite3_stmt *stmt_obj;
    1283           4 :         zval *object = getThis();
    1284           4 :         stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
    1285             : 
    1286           4 :         if (zend_parse_parameters_none() == FAILURE) {
    1287           0 :                 return;
    1288             :         }
    1289             : 
    1290           4 :         zend_llist_del_element(&(stmt_obj->db_obj->free_list), object, (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
    1291             : 
    1292           4 :         RETURN_TRUE;
    1293             : }
    1294             : /* }}} */
    1295             : 
    1296             : /* {{{ proto bool SQLite3Stmt::reset()
    1297             :    Reset the prepared statement to the state before it was executed, bindings still remain. */
    1298           5 : PHP_METHOD(sqlite3stmt, reset)
    1299             : {
    1300             :         php_sqlite3_stmt *stmt_obj;
    1301           5 :         zval *object = getThis();
    1302           5 :         stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
    1303             : 
    1304           5 :         if (zend_parse_parameters_none() == FAILURE) {
    1305           2 :                 return;
    1306             :         }
    1307             : 
    1308           3 :         if (sqlite3_reset(stmt_obj->stmt) != SQLITE_OK) {
    1309           0 :                 php_sqlite3_error(stmt_obj->db_obj, "Unable to reset statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
    1310           0 :                 RETURN_FALSE;
    1311             :         }
    1312           3 :         RETURN_TRUE;
    1313             : }
    1314             : /* }}} */
    1315             : 
    1316             : /* {{{ proto bool SQLite3Stmt::clear()
    1317             :    Clear all current bound parameters. */
    1318           2 : PHP_METHOD(sqlite3stmt, clear)
    1319             : {
    1320             :         php_sqlite3_stmt *stmt_obj;
    1321           2 :         zval *object = getThis();
    1322           2 :         stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
    1323             : 
    1324           2 :         if (zend_parse_parameters_none() == FAILURE) {
    1325           1 :                 return;
    1326             :         }
    1327             : 
    1328           1 :         if (sqlite3_clear_bindings(stmt_obj->stmt) != SQLITE_OK) {
    1329           0 :                 php_sqlite3_error(stmt_obj->db_obj, "Unable to clear statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
    1330           0 :                 RETURN_FALSE;
    1331             :         }
    1332             : 
    1333           1 :         RETURN_TRUE;
    1334             : }
    1335             : /* }}} */
    1336             : 
    1337             : /* {{{ proto bool SQLite3Stmt::readOnly()
    1338             :    Returns true if a statement is definitely read only */
    1339           3 : PHP_METHOD(sqlite3stmt, readOnly)
    1340             : {
    1341             :         php_sqlite3_stmt *stmt_obj;
    1342           3 :         zval *object = getThis();
    1343           3 :         stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
    1344             : 
    1345           3 :         if (zend_parse_parameters_none() == FAILURE) {
    1346           0 :                 return;
    1347             :         }
    1348             : 
    1349             : #if SQLITE_VERSION_NUMBER >= 3007004
    1350           3 :         if (sqlite3_stmt_readonly(stmt_obj->stmt)) {
    1351           1 :                 RETURN_TRUE;
    1352             :         }
    1353             : #endif
    1354           2 :         RETURN_FALSE;
    1355             : }
    1356             : /* }}} */
    1357             : 
    1358          19 : static int register_bound_parameter_to_sqlite(struct php_sqlite3_bound_param *param, php_sqlite3_stmt *stmt TSRMLS_DC) /* {{{ */
    1359             : {
    1360             :         HashTable *hash;
    1361          19 :         hash = stmt->bound_params;
    1362             : 
    1363          19 :         if (!hash) {
    1364          12 :                 ALLOC_HASHTABLE(hash);
    1365          12 :                 zend_hash_init(hash, 13, NULL, sqlite3_param_dtor, 0);
    1366          12 :                 stmt->bound_params = hash;
    1367             :         }
    1368             : 
    1369             :         /* We need a : prefix to resolve a name to a parameter number */
    1370          19 :         if (param->name) {
    1371           3 :                 if (param->name[0] != ':') {
    1372             :                         /* pre-increment for character + 1 for null */
    1373           1 :                         char *temp = emalloc(++param->name_len + 1);
    1374           1 :                         temp[0] = ':';
    1375           1 :                         memmove(temp+1, param->name, param->name_len);
    1376           1 :                         param->name = temp;
    1377             :                 } else {
    1378           2 :                         param->name = estrndup(param->name, param->name_len);
    1379             :                 }
    1380             :                 /* do lookup*/
    1381           3 :                 param->param_number = sqlite3_bind_parameter_index(stmt->stmt, param->name);
    1382             :         }
    1383             : 
    1384          19 :         if (param->param_number < 1) {
    1385           0 :                 efree(param->name);
    1386           0 :                 return 0;
    1387             :         }
    1388             : 
    1389          19 :         if (param->param_number >= 1) {
    1390          19 :                 zend_hash_index_del(hash, param->param_number);
    1391             :         }
    1392             : 
    1393          19 :         if (param->name) {
    1394           3 :                 zend_hash_update(hash, param->name, param->name_len, param, sizeof(*param), NULL);
    1395             :         } else {
    1396          16 :                 zend_hash_index_update(hash, param->param_number, param, sizeof(*param), NULL);
    1397             :         }
    1398             : 
    1399          19 :         return 1;
    1400             : }
    1401             : /* }}} */
    1402             : 
    1403             : /* {{{ proto bool SQLite3Stmt::bindParam(int parameter_number, mixed parameter [, int type])
    1404             :    Bind Paramater to a stmt variable. */
    1405           9 : PHP_METHOD(sqlite3stmt, bindParam)
    1406             : {
    1407             :         php_sqlite3_stmt *stmt_obj;
    1408           9 :         zval *object = getThis();
    1409           9 :         struct php_sqlite3_bound_param param = {0};
    1410           9 :         stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
    1411             : 
    1412           9 :         param.param_number = -1;
    1413           9 :         param.type = SQLITE3_TEXT;
    1414             : 
    1415           9 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "lz|l", &param.param_number, &param.parameter, &param.type) == FAILURE) {
    1416           0 :                 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &param.name, &param.name_len, &param.parameter, &param.type) == FAILURE) {
    1417           0 :                         return;
    1418             :                 }
    1419             :         }
    1420             : 
    1421           9 :         Z_ADDREF_P(param.parameter);
    1422             : 
    1423           9 :         if (!register_bound_parameter_to_sqlite(&param, stmt_obj TSRMLS_CC)) {
    1424           0 :                 if (param.parameter) {
    1425           0 :                         zval_ptr_dtor(&(param.parameter));
    1426           0 :                         param.parameter = NULL;
    1427             :                 }
    1428           0 :                 RETURN_FALSE;
    1429             :         }
    1430           9 :         RETURN_TRUE;
    1431             : }
    1432             : /* }}} */
    1433             : 
    1434             : /* {{{ proto bool SQLite3Stmt::bindValue(int parameter_number, mixed parameter [, int type])
    1435             :    Bind Value of a parameter to a stmt variable. */
    1436          10 : PHP_METHOD(sqlite3stmt, bindValue)
    1437             : {
    1438             :         php_sqlite3_stmt *stmt_obj;
    1439          10 :         zval *object = getThis();
    1440          10 :         struct php_sqlite3_bound_param param = {0};
    1441          10 :         stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
    1442             : 
    1443          10 :         param.param_number = -1;
    1444          10 :         param.type = SQLITE3_TEXT;
    1445             : 
    1446          10 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "lz/|l", &param.param_number, &param.parameter, &param.type) == FAILURE) {
    1447           3 :                 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz/|l", &param.name, &param.name_len, &param.parameter, &param.type) == FAILURE) {
    1448           0 :                         return;
    1449             :                 }
    1450             :         }
    1451             : 
    1452          10 :         Z_ADDREF_P(param.parameter);
    1453             : 
    1454          10 :         if (!register_bound_parameter_to_sqlite(&param, stmt_obj TSRMLS_CC)) {
    1455           0 :                 if (param.parameter) {
    1456           0 :                         zval_ptr_dtor(&(param.parameter));
    1457           0 :                         param.parameter = NULL;
    1458             :                 }
    1459           0 :                 RETURN_FALSE;
    1460             :         }
    1461          10 :         RETURN_TRUE;
    1462             : }
    1463             : /* }}} */
    1464             : 
    1465             : /* {{{ proto SQLite3Result SQLite3Stmt::execute()
    1466             :    Executes a prepared statement and returns a result set object. */
    1467          19 : PHP_METHOD(sqlite3stmt, execute)
    1468             : {
    1469             :         php_sqlite3_stmt *stmt_obj;
    1470             :         php_sqlite3_result *result;
    1471          19 :         zval *object = getThis();
    1472          19 :         int return_code = 0;
    1473             :         struct php_sqlite3_bound_param *param;
    1474             : 
    1475          19 :         stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
    1476             : 
    1477          19 :         if (zend_parse_parameters_none() == FAILURE) {
    1478           0 :                 return;
    1479             :         }
    1480             : 
    1481          19 :         SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3)
    1482             : 
    1483          19 :         if (stmt_obj->bound_params) {
    1484          14 :                 zend_hash_internal_pointer_reset(stmt_obj->bound_params);
    1485          45 :                 while (zend_hash_get_current_data(stmt_obj->bound_params, (void **)&param) == SUCCESS) {
    1486             :                         /* If the ZVAL is null then it should be bound as that */
    1487          17 :                         if (Z_TYPE_P(param->parameter) == IS_NULL) {
    1488           1 :                                 sqlite3_bind_null(stmt_obj->stmt, param->param_number);
    1489           1 :                                 zend_hash_move_forward(stmt_obj->bound_params);
    1490           1 :                                 continue;
    1491             :                         }
    1492             : 
    1493          16 :                         switch (param->type) {
    1494             :                                 case SQLITE_INTEGER:
    1495           0 :                                         convert_to_long(param->parameter);
    1496           0 :                                         sqlite3_bind_int(stmt_obj->stmt, param->param_number, Z_LVAL_P(param->parameter));
    1497           0 :                                         break;
    1498             : 
    1499             :                                 case SQLITE_FLOAT:
    1500             :                                         /* convert_to_double(param->parameter);*/
    1501           0 :                                         sqlite3_bind_double(stmt_obj->stmt, param->param_number, Z_DVAL_P(param->parameter));
    1502           0 :                                         break;
    1503             : 
    1504             :                                 case SQLITE_BLOB:
    1505             :                                 {
    1506           3 :                                         php_stream *stream = NULL;
    1507             :                                         int blength;
    1508           3 :                                         char *buffer = NULL;
    1509           3 :                                         if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
    1510           1 :                                                 php_stream_from_zval_no_verify(stream, &param->parameter);
    1511           1 :                                                 if (stream == NULL) {
    1512           0 :                                                         php_sqlite3_error(stmt_obj->db_obj, "Unable to read stream for parameter %ld", param->param_number);
    1513           0 :                                                         RETURN_FALSE;
    1514             :                                                 }
    1515           1 :                                                 blength = php_stream_copy_to_mem(stream, (void *)&buffer, PHP_STREAM_COPY_ALL, 0);
    1516             :                                         } else {
    1517           2 :                                                 convert_to_string(param->parameter);
    1518           2 :                                                 blength =  Z_STRLEN_P(param->parameter);
    1519           2 :                                                 buffer = Z_STRVAL_P(param->parameter);
    1520             :                                         }
    1521             : 
    1522           3 :                                         sqlite3_bind_blob(stmt_obj->stmt, param->param_number, buffer, blength, SQLITE_TRANSIENT);
    1523             : 
    1524           3 :                                         if (stream) {
    1525           1 :                                                 pefree(buffer, 0);
    1526             :                                         }
    1527           3 :                                         break;
    1528             :                                 }
    1529             : 
    1530             :                                 case SQLITE3_TEXT:
    1531          13 :                                         convert_to_string(param->parameter);
    1532          13 :                                         sqlite3_bind_text(stmt_obj->stmt, param->param_number, Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter), SQLITE_STATIC);
    1533          13 :                                         break;
    1534             : 
    1535             :                                 case SQLITE_NULL:
    1536           0 :                                         sqlite3_bind_null(stmt_obj->stmt, param->param_number);
    1537           0 :                                         break;
    1538             : 
    1539             :                                 default:
    1540           0 :                                         php_sqlite3_error(stmt_obj->db_obj, "Unknown parameter type: %ld for parameter %ld", param->type, param->param_number);
    1541           0 :                                         RETURN_FALSE;
    1542             :                         }
    1543          16 :                         zend_hash_move_forward(stmt_obj->bound_params);
    1544             :                 }
    1545             :         }
    1546             : 
    1547          19 :         return_code = sqlite3_step(stmt_obj->stmt);
    1548             : 
    1549          19 :         switch (return_code) {
    1550             :                 case SQLITE_ROW: /* Valid Row */
    1551             :                 case SQLITE_DONE: /* Valid but no results */
    1552             :                 {
    1553          19 :                         sqlite3_reset(stmt_obj->stmt);
    1554          19 :                         object_init_ex(return_value, php_sqlite3_result_entry);
    1555          19 :                         result = (php_sqlite3_result *)zend_object_store_get_object(return_value TSRMLS_CC);
    1556             : 
    1557             :                         Z_ADDREF_P(object);
    1558             :         
    1559          19 :                         result->is_prepared_statement = 1;
    1560          19 :                         result->db_obj = stmt_obj->db_obj;
    1561          19 :                         result->stmt_obj = stmt_obj;
    1562          19 :                         result->stmt_obj_zval = getThis();
    1563             : 
    1564          19 :                         break;
    1565             :                 }
    1566             :                 case SQLITE_ERROR:
    1567           0 :                         sqlite3_reset(stmt_obj->stmt);
    1568             : 
    1569             :                 default:
    1570           0 :                         php_sqlite3_error(stmt_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
    1571           0 :                         zval_dtor(return_value);
    1572           0 :                         RETURN_FALSE;
    1573             :         }
    1574             : 
    1575          19 :         return;
    1576             : }
    1577             : /* }}} */
    1578             : 
    1579             : /* {{{ proto int SQLite3Stmt::__construct(SQLite3 dbobject, String Statement)
    1580             :    __constructor for SQLite3Stmt. */
    1581           0 : PHP_METHOD(sqlite3stmt, __construct)
    1582             : {
    1583             :         php_sqlite3_stmt *stmt_obj;
    1584             :         php_sqlite3_db_object *db_obj;
    1585           0 :         zval *object = getThis();
    1586             :         zval *db_zval;
    1587             :         char *sql;
    1588             :         int sql_len, errcode;
    1589             :         zend_error_handling error_handling;
    1590             :         php_sqlite3_free_list *free_item;
    1591             : 
    1592           0 :         stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
    1593           0 :         zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
    1594             : 
    1595           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &db_zval, php_sqlite3_sc_entry, &sql, &sql_len) == FAILURE) {
    1596           0 :                 zend_restore_error_handling(&error_handling TSRMLS_CC);
    1597           0 :                 return;
    1598             :         }
    1599             : 
    1600           0 :         db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(db_zval TSRMLS_CC);
    1601             : 
    1602           0 :         SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
    1603             : 
    1604           0 :         zend_restore_error_handling(&error_handling TSRMLS_CC);
    1605             : 
    1606           0 :         if (!sql_len) {
    1607           0 :                 RETURN_FALSE;
    1608             :         }
    1609             : 
    1610           0 :         stmt_obj->db_obj = db_obj;
    1611           0 :         stmt_obj->db_obj_zval = db_zval;
    1612             : 
    1613           0 :         Z_ADDREF_P(db_zval);
    1614             :         
    1615           0 :         errcode = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &(stmt_obj->stmt), NULL);
    1616           0 :         if (errcode != SQLITE_OK) {
    1617           0 :                 php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
    1618           0 :                 zval_dtor(return_value);
    1619           0 :                 RETURN_FALSE;
    1620             :         }
    1621           0 :         stmt_obj->initialised = 1;
    1622             : 
    1623           0 :         free_item = emalloc(sizeof(php_sqlite3_free_list));
    1624           0 :         free_item->stmt_obj = stmt_obj;
    1625           0 :         free_item->stmt_obj_zval = getThis();
    1626             : 
    1627           0 :         zend_llist_add_element(&(db_obj->free_list), &free_item);
    1628             : }
    1629             : /* }}} */
    1630             : 
    1631             : /* {{{ proto int SQLite3Result::numColumns()
    1632             :    Number of columns in the result set. */
    1633           4 : PHP_METHOD(sqlite3result, numColumns)
    1634             : {
    1635             :         php_sqlite3_result *result_obj;
    1636           4 :         zval *object = getThis();
    1637           4 :         result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
    1638             : 
    1639           4 :         SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
    1640             : 
    1641           3 :         if (zend_parse_parameters_none() == FAILURE) {
    1642           1 :                 return;
    1643             :         }
    1644             : 
    1645           2 :         RETURN_LONG(sqlite3_column_count(result_obj->stmt_obj->stmt));
    1646             : }
    1647             : /* }}} */
    1648             : 
    1649             : /* {{{ proto string SQLite3Result::columnName(int column)
    1650             :    Returns the name of the nth column. */
    1651           6 : PHP_METHOD(sqlite3result, columnName)
    1652             : {
    1653             :         php_sqlite3_result *result_obj;
    1654           6 :         zval *object = getThis();
    1655           6 :         long column = 0;
    1656             :         char *column_name;
    1657           6 :         result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
    1658             : 
    1659           6 :         SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
    1660             : 
    1661           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &column) == FAILURE) {
    1662           0 :                 return;
    1663             :         }
    1664           6 :         column_name = (char*) sqlite3_column_name(result_obj->stmt_obj->stmt, column);
    1665             : 
    1666           6 :         if (column_name == NULL) {
    1667           1 :                 RETURN_FALSE;
    1668             :         }
    1669             :                 
    1670           5 :         RETVAL_STRING(column_name, 1);
    1671             : }
    1672             : /* }}} */
    1673             : 
    1674             : /* {{{ proto int SQLite3Result::columnType(int column)
    1675             :    Returns the type of the nth column. */
    1676           4 : PHP_METHOD(sqlite3result, columnType)
    1677             : {
    1678             :         php_sqlite3_result *result_obj;
    1679           4 :         zval *object = getThis();
    1680           4 :         long column = 0;
    1681           4 :         result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
    1682             : 
    1683           4 :         SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
    1684             : 
    1685           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &column) == FAILURE) {
    1686           0 :                 return;
    1687             :         }
    1688             : 
    1689           4 :         if (result_obj->complete) {
    1690           0 :                 RETURN_FALSE;
    1691             :         }
    1692             : 
    1693           4 :         RETURN_LONG(sqlite3_column_type(result_obj->stmt_obj->stmt, column));
    1694             : }
    1695             : /* }}} */
    1696             : 
    1697             : /* {{{ proto array SQLite3Result::fetchArray([int mode])
    1698             :    Fetch a result row as both an associative or numerically indexed array or both. */
    1699          60 : PHP_METHOD(sqlite3result, fetchArray)
    1700             : {
    1701             :         php_sqlite3_result *result_obj;
    1702          60 :         zval *object = getThis();
    1703             :         int i, ret;
    1704          60 :         long mode = PHP_SQLITE3_BOTH;
    1705          60 :         result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
    1706             : 
    1707          60 :         SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
    1708             : 
    1709          60 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &mode) == FAILURE) {
    1710           1 :                 return;
    1711             :         }
    1712             : 
    1713          59 :         ret = sqlite3_step(result_obj->stmt_obj->stmt);
    1714          59 :         switch (ret) {
    1715             :                 case SQLITE_ROW:
    1716             :                         /* If there was no return value then just skip fetching */
    1717          35 :                         if (!return_value_used) {
    1718           0 :                                 return;
    1719             :                         }
    1720             : 
    1721          35 :                         array_init(return_value);
    1722             : 
    1723          96 :                         for (i = 0; i < sqlite3_data_count(result_obj->stmt_obj->stmt); i++) {
    1724             :                                 zval *data;
    1725             : 
    1726          61 :                                 data = sqlite_value_to_zval(result_obj->stmt_obj->stmt, i);
    1727             : 
    1728          61 :                                 if (mode & PHP_SQLITE3_NUM) {
    1729          61 :                                         add_index_zval(return_value, i, data);
    1730             :                                 }
    1731             : 
    1732          61 :                                 if (mode & PHP_SQLITE3_ASSOC) {
    1733           6 :                                         if (mode & PHP_SQLITE3_NUM) {
    1734             :                                                 Z_ADDREF_P(data);
    1735             :                                         }
    1736           6 :                                         add_assoc_zval(return_value, (char*)sqlite3_column_name(result_obj->stmt_obj->stmt, i), data);
    1737             :                                 }
    1738             :                         }
    1739          35 :                         break;
    1740             : 
    1741             :                 case SQLITE_DONE:
    1742          24 :                         result_obj->complete = 1;
    1743          24 :                         RETURN_FALSE;
    1744             :                         break;
    1745             : 
    1746             :                 default:
    1747           0 :                         php_sqlite3_error(result_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(result_obj->stmt_obj->stmt)));
    1748             :         }
    1749             : }
    1750             : /* }}} */
    1751             : 
    1752             : /* {{{ proto bool SQLite3Result::reset()
    1753             :    Resets the result set back to the first row. */
    1754           2 : PHP_METHOD(sqlite3result, reset)
    1755             : {
    1756             :         php_sqlite3_result *result_obj;
    1757           2 :         zval *object = getThis();
    1758           2 :         result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
    1759             : 
    1760           2 :         SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
    1761             : 
    1762           2 :         if (zend_parse_parameters_none() == FAILURE) {
    1763           1 :                 return;
    1764             :         }
    1765             : 
    1766           1 :         if (sqlite3_reset(result_obj->stmt_obj->stmt) != SQLITE_OK) {
    1767           0 :                 RETURN_FALSE;
    1768             :         }
    1769             : 
    1770           1 :         result_obj->complete = 0;
    1771             : 
    1772           1 :         RETURN_TRUE;
    1773             : }
    1774             : /* }}} */
    1775             : 
    1776             : /* {{{ proto bool SQLite3Result::finalize()
    1777             :    Closes the result set. */
    1778          18 : PHP_METHOD(sqlite3result, finalize)
    1779             : {
    1780             :         php_sqlite3_result *result_obj;
    1781          18 :         zval *object = getThis();
    1782          18 :         result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
    1783             : 
    1784          18 :         SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
    1785             : 
    1786          18 :         if (zend_parse_parameters_none() == FAILURE) {
    1787           0 :                 return;
    1788             :         }
    1789             : 
    1790             :         /* We need to finalize an internal statement */
    1791          18 :         if (result_obj->is_prepared_statement == 0) {
    1792          11 :                 zend_llist_del_element(&(result_obj->db_obj->free_list), result_obj->stmt_obj_zval,
    1793             :                         (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
    1794             :         } else {
    1795           7 :                 sqlite3_reset(result_obj->stmt_obj->stmt);
    1796             :         }
    1797             : 
    1798          18 :         RETURN_TRUE;
    1799             : }
    1800             : /* }}} */
    1801             : 
    1802             : /* {{{ proto int SQLite3Result::__construct()
    1803             :    __constructor for SQLite3Result. */
    1804           0 : PHP_METHOD(sqlite3result, __construct)
    1805             : {
    1806           0 :         zend_throw_exception(zend_exception_get_default(TSRMLS_C), "SQLite3Result cannot be directly instantiated", 0 TSRMLS_CC);
    1807           0 : }
    1808             : /* }}} */
    1809             : 
    1810             : /* {{{ arginfo */
    1811             : ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_open, 0)
    1812             :         ZEND_ARG_INFO(0, filename)
    1813             :         ZEND_ARG_INFO(0, flags)
    1814             :         ZEND_ARG_INFO(0, encryption_key)
    1815             : ZEND_END_ARG_INFO()
    1816             : 
    1817             : ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_busytimeout, 0)
    1818             :         ZEND_ARG_INFO(0, ms)
    1819             : ZEND_END_ARG_INFO()
    1820             : 
    1821             : #ifndef SQLITE_OMIT_LOAD_EXTENSION
    1822             : ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_loadextension, 0)
    1823             :         ZEND_ARG_INFO(0, shared_library)
    1824             : ZEND_END_ARG_INFO()
    1825             : #endif
    1826             : 
    1827             : ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_escapestring, 0, 0, 1)
    1828             :         ZEND_ARG_INFO(0, value)
    1829             : ZEND_END_ARG_INFO()
    1830             : 
    1831             : ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_query, 0, 0, 1)
    1832             :         ZEND_ARG_INFO(0, query)
    1833             : ZEND_END_ARG_INFO()
    1834             : 
    1835             : ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_querysingle, 0, 0, 1)
    1836             :         ZEND_ARG_INFO(0, query)
    1837             :         ZEND_ARG_INFO(0, entire_row)
    1838             : ZEND_END_ARG_INFO()
    1839             : 
    1840             : ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createfunction, 0, 0, 2)
    1841             :         ZEND_ARG_INFO(0, name)
    1842             :         ZEND_ARG_INFO(0, callback)
    1843             :         ZEND_ARG_INFO(0, argument_count)
    1844             : ZEND_END_ARG_INFO()
    1845             : 
    1846             : ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createaggregate, 0, 0, 3)
    1847             :         ZEND_ARG_INFO(0, name)
    1848             :         ZEND_ARG_INFO(0, step_callback)
    1849             :         ZEND_ARG_INFO(0, final_callback)
    1850             :         ZEND_ARG_INFO(0, argument_count)
    1851             : ZEND_END_ARG_INFO()
    1852             : 
    1853             : ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createcollation, 0, 0, 2)
    1854             :         ZEND_ARG_INFO(0, name)
    1855             :         ZEND_ARG_INFO(0, callback)
    1856             : ZEND_END_ARG_INFO()
    1857             : 
    1858             : ZEND_BEGIN_ARG_INFO_EX(argingo_sqlite3_openblob, 0, 0, 3)
    1859             :         ZEND_ARG_INFO(0, table)
    1860             :         ZEND_ARG_INFO(0, column)
    1861             :         ZEND_ARG_INFO(0, rowid)
    1862             :         ZEND_ARG_INFO(0, dbname)
    1863             : ZEND_END_ARG_INFO()
    1864             : 
    1865             : ZEND_BEGIN_ARG_INFO_EX(argingo_sqlite3_enableexceptions, 0, 0, 1)
    1866             :         ZEND_ARG_INFO(0, enableExceptions)
    1867             : ZEND_END_ARG_INFO()
    1868             : 
    1869             : ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3stmt_bindparam, 0, 0, 2)
    1870             :         ZEND_ARG_INFO(0, param_number)
    1871             :         ZEND_ARG_INFO(1, param)
    1872             :         ZEND_ARG_INFO(0, type)
    1873             : ZEND_END_ARG_INFO()
    1874             : 
    1875             : ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3stmt_bindvalue, 0, 0, 2)
    1876             :         ZEND_ARG_INFO(0, param_number)
    1877             :         ZEND_ARG_INFO(0, param)
    1878             :         ZEND_ARG_INFO(0, type)
    1879             : ZEND_END_ARG_INFO()
    1880             : 
    1881             : ZEND_BEGIN_ARG_INFO(arginfo_sqlite3stmt_construct, 1)
    1882             :         ZEND_ARG_INFO(0, sqlite3)
    1883             : ZEND_END_ARG_INFO()
    1884             : 
    1885             : ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_columnname, 0, 0, 1)
    1886             :         ZEND_ARG_INFO(0, column_number)
    1887             : ZEND_END_ARG_INFO()
    1888             : 
    1889             : ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_columntype, 0, 0, 1)
    1890             :         ZEND_ARG_INFO(0, column_number)
    1891             : ZEND_END_ARG_INFO()
    1892             : 
    1893             : ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_fetcharray, 0, 0, 1)
    1894             :         ZEND_ARG_INFO(0, mode)
    1895             : ZEND_END_ARG_INFO()
    1896             : 
    1897             : ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_void, 0)
    1898             : ZEND_END_ARG_INFO()
    1899             : /* }}} */
    1900             : 
    1901             : /* {{{ php_sqlite3_class_methods */
    1902             : static zend_function_entry php_sqlite3_class_methods[] = {
    1903             :         PHP_ME(sqlite3,         open,                           arginfo_sqlite3_open, ZEND_ACC_PUBLIC)
    1904             :         PHP_ME(sqlite3,         close,                          arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
    1905             :         PHP_ME(sqlite3,         exec,                           arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
    1906             :         PHP_ME(sqlite3,         version,                        arginfo_sqlite3_void, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
    1907             :         PHP_ME(sqlite3,         lastInsertRowID,        arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
    1908             :         PHP_ME(sqlite3,         lastErrorCode,          arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
    1909             :         PHP_ME(sqlite3,         lastErrorMsg,           arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
    1910             :         PHP_ME(sqlite3,         busyTimeout,            arginfo_sqlite3_busytimeout, ZEND_ACC_PUBLIC)
    1911             : #ifndef SQLITE_OMIT_LOAD_EXTENSION
    1912             :         PHP_ME(sqlite3,         loadExtension,          arginfo_sqlite3_loadextension, ZEND_ACC_PUBLIC)
    1913             : #endif
    1914             :         PHP_ME(sqlite3,         changes,                        arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
    1915             :         PHP_ME(sqlite3,         escapeString,           arginfo_sqlite3_escapestring, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
    1916             :         PHP_ME(sqlite3,         prepare,                        arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
    1917             :         PHP_ME(sqlite3,         query,                          arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
    1918             :         PHP_ME(sqlite3,         querySingle,            arginfo_sqlite3_querysingle, ZEND_ACC_PUBLIC)
    1919             :         PHP_ME(sqlite3,         createFunction,         arginfo_sqlite3_createfunction, ZEND_ACC_PUBLIC)
    1920             :         PHP_ME(sqlite3,         createAggregate,        arginfo_sqlite3_createaggregate, ZEND_ACC_PUBLIC)
    1921             :         PHP_ME(sqlite3,         createCollation,        arginfo_sqlite3_createcollation, ZEND_ACC_PUBLIC)
    1922             :         PHP_ME(sqlite3,         openBlob,                       argingo_sqlite3_openblob, ZEND_ACC_PUBLIC)
    1923             :         PHP_ME(sqlite3,         enableExceptions,       argingo_sqlite3_enableexceptions, ZEND_ACC_PUBLIC)
    1924             :         /* Aliases */
    1925             :         PHP_MALIAS(sqlite3,     __construct, open, arginfo_sqlite3_open, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
    1926             :         PHP_FE_END
    1927             : };
    1928             : /* }}} */
    1929             : 
    1930             : /* {{{ php_sqlite3_stmt_class_methods */
    1931             : static zend_function_entry php_sqlite3_stmt_class_methods[] = {
    1932             :         PHP_ME(sqlite3stmt, paramCount, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
    1933             :         PHP_ME(sqlite3stmt, close,              arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
    1934             :         PHP_ME(sqlite3stmt, reset,              arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
    1935             :         PHP_ME(sqlite3stmt, clear,              arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
    1936             :         PHP_ME(sqlite3stmt, execute,    arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
    1937             :         PHP_ME(sqlite3stmt, bindParam,  arginfo_sqlite3stmt_bindparam, ZEND_ACC_PUBLIC)
    1938             :         PHP_ME(sqlite3stmt, bindValue,  arginfo_sqlite3stmt_bindvalue, ZEND_ACC_PUBLIC)
    1939             :         PHP_ME(sqlite3stmt, readOnly,   arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
    1940             :         PHP_ME(sqlite3stmt, __construct, arginfo_sqlite3stmt_construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
    1941             :         PHP_FE_END
    1942             : };
    1943             : /* }}} */
    1944             : 
    1945             : /* {{{ php_sqlite3_result_class_methods */
    1946             : static zend_function_entry php_sqlite3_result_class_methods[] = {
    1947             :         PHP_ME(sqlite3result, numColumns,               arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
    1948             :         PHP_ME(sqlite3result, columnName,               arginfo_sqlite3result_columnname, ZEND_ACC_PUBLIC)
    1949             :         PHP_ME(sqlite3result, columnType,               arginfo_sqlite3result_columntype, ZEND_ACC_PUBLIC)
    1950             :         PHP_ME(sqlite3result, fetchArray,               arginfo_sqlite3result_fetcharray, ZEND_ACC_PUBLIC)
    1951             :         PHP_ME(sqlite3result, reset,                    arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
    1952             :         PHP_ME(sqlite3result, finalize,                 arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
    1953             :         PHP_ME(sqlite3result, __construct,              arginfo_sqlite3_void, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
    1954             :         PHP_FE_END
    1955             : };
    1956             : /* }}} */
    1957             : 
    1958             : /* {{{ Authorization Callback 
    1959             : */
    1960           0 : static int php_sqlite3_authorizer(void *autharg, int access_type, const char *arg3, const char *arg4, const char *arg5, const char *arg6)
    1961             : {
    1962           0 :         switch (access_type) {
    1963             :                 case SQLITE_ATTACH:
    1964             :                 {
    1965           0 :                         if (memcmp(arg3, ":memory:", sizeof(":memory:")) && *arg3) {
    1966             :                                 TSRMLS_FETCH();
    1967             : 
    1968             : #if PHP_API_VERSION < 20100412
    1969           0 :                                 if (PG(safe_mode) && (!php_checkuid(arg3, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
    1970           0 :                                         return SQLITE_DENY;
    1971             :                                 }
    1972             : #endif
    1973             : 
    1974           0 :                                 if (php_check_open_basedir(arg3 TSRMLS_CC)) {
    1975           0 :                                         return SQLITE_DENY;
    1976             :                                 }
    1977             :                         }
    1978           0 :                         return SQLITE_OK;
    1979             :                 }
    1980             : 
    1981             :                 default:
    1982             :                         /* access allowed */
    1983           0 :                         return SQLITE_OK;
    1984             :         }
    1985             : }
    1986             : /* }}} */
    1987             : 
    1988             : /* {{{ php_sqlite3_free_list_dtor
    1989             : */
    1990          42 : static void php_sqlite3_free_list_dtor(void **item)
    1991             : {
    1992          42 :         php_sqlite3_free_list *free_item = (php_sqlite3_free_list *)*item;
    1993             : 
    1994          42 :         if (free_item->stmt_obj && free_item->stmt_obj->initialised) {
    1995          42 :                 sqlite3_finalize(free_item->stmt_obj->stmt);
    1996          42 :                 free_item->stmt_obj->initialised = 0;
    1997             :         }
    1998          42 :         efree(*item);
    1999          42 : }
    2000             : /* }}} */
    2001             : 
    2002          15 : static int php_sqlite3_compare_stmt_zval_free( php_sqlite3_free_list **free_list, zval *statement ) /* {{{ */
    2003             : {
    2004          15 :         return ((*free_list)->stmt_obj->initialised && statement == (*free_list)->stmt_obj_zval);
    2005             : }
    2006             : /* }}} */
    2007             : 
    2008          14 : static int php_sqlite3_compare_stmt_free( php_sqlite3_free_list **free_list, sqlite3_stmt *statement ) /* {{{ */
    2009             : {
    2010          14 :         return ((*free_list)->stmt_obj->initialised && statement == (*free_list)->stmt_obj->stmt);
    2011             : }
    2012             : /* }}} */
    2013             : 
    2014          66 : static void php_sqlite3_object_free_storage(void *object TSRMLS_DC) /* {{{ */
    2015             : {
    2016          66 :         php_sqlite3_db_object *intern = (php_sqlite3_db_object *)object;
    2017             :         php_sqlite3_func *func;
    2018             :         php_sqlite3_collation *collation;
    2019             : 
    2020          66 :         if (!intern) {
    2021           0 :                 return;
    2022             :         }
    2023             : 
    2024         138 :         while (intern->funcs) {
    2025           6 :                 func = intern->funcs;
    2026           6 :                 intern->funcs = func->next;
    2027           6 :                 if (intern->initialised && intern->db) {
    2028           3 :                         sqlite3_create_function(intern->db, func->func_name, func->argc, SQLITE_UTF8, func, NULL, NULL, NULL);
    2029             :                 }
    2030             : 
    2031           6 :                 efree((char*)func->func_name);
    2032             : 
    2033           6 :                 if (func->func) {
    2034           4 :                         zval_ptr_dtor(&func->func);
    2035             :                 }
    2036           6 :                 if (func->step) {
    2037           2 :                         zval_ptr_dtor(&func->step);
    2038             :                 }
    2039           6 :                 if (func->fini) {
    2040           2 :                         zval_ptr_dtor(&func->fini);
    2041             :                 }
    2042           6 :                 efree(func);
    2043             :         }
    2044             : 
    2045         133 :         while (intern->collations){
    2046           1 :                 collation = intern->collations;
    2047           1 :                 intern->collations = collation->next;
    2048           1 :                 if (intern->initialised && intern->db){
    2049           0 :                         sqlite3_create_collation(intern->db, collation->collation_name, SQLITE_UTF8, NULL, NULL);
    2050             :                 }
    2051           1 :                 efree((char*)collation->collation_name);
    2052           1 :                 if (collation->cmp_func){
    2053           1 :                         zval_ptr_dtor(&collation->cmp_func);
    2054             :                 }
    2055           1 :                 efree(collation);
    2056             :         }
    2057             : 
    2058          66 :         if (intern->initialised && intern->db) {
    2059          25 :                 sqlite3_close(intern->db);
    2060          25 :                 intern->initialised = 0;
    2061             :         }
    2062             : 
    2063          66 :         zend_object_std_dtor(&intern->zo TSRMLS_CC);
    2064          66 :         efree(intern);
    2065             : }
    2066             : /* }}} */
    2067             : 
    2068          44 : static void php_sqlite3_stmt_object_free_storage(void *object TSRMLS_DC) /* {{{ */
    2069             : {
    2070          44 :         php_sqlite3_stmt *intern = (php_sqlite3_stmt *)object;
    2071             : 
    2072          44 :         if (!intern) {
    2073           0 :                 return;
    2074             :         }
    2075             : 
    2076          44 :         if (intern->bound_params) {
    2077          12 :                 zend_hash_destroy(intern->bound_params);
    2078          12 :                 FREE_HASHTABLE(intern->bound_params);
    2079          12 :                 intern->bound_params = NULL;
    2080             :         }
    2081             : 
    2082          44 :         if (intern->initialised) {
    2083          14 :                 zend_llist_del_element(&(intern->db_obj->free_list), intern->stmt,
    2084             :                         (int (*)(void *, void *)) php_sqlite3_compare_stmt_free);
    2085             :         }
    2086             : 
    2087          44 :         if (intern->db_obj_zval) {
    2088          44 :                 zval_ptr_dtor(&intern->db_obj_zval);
    2089             :         }
    2090             : 
    2091          44 :         zend_object_std_dtor(&intern->zo TSRMLS_CC);
    2092          44 :         efree(intern);
    2093             : }
    2094             : /* }}} */
    2095             : 
    2096          37 : static void php_sqlite3_result_object_free_storage(void *object TSRMLS_DC) /* {{{ */
    2097             : {
    2098          37 :         php_sqlite3_result *intern = (php_sqlite3_result *)object;
    2099             : 
    2100          37 :         if (!intern) {
    2101           0 :                 return;
    2102             :         }
    2103             : 
    2104          37 :         if (intern->stmt_obj_zval) {
    2105          37 :                 if (intern->stmt_obj->initialised) {
    2106          16 :                         sqlite3_reset(intern->stmt_obj->stmt);
    2107             :                 }
    2108             : 
    2109          37 :                 if (intern->is_prepared_statement == 0) {
    2110          18 :                         zval_dtor(intern->stmt_obj_zval);
    2111          18 :                         FREE_ZVAL(intern->stmt_obj_zval);
    2112             :                 } else {
    2113          19 :                         zval_ptr_dtor(&intern->stmt_obj_zval);
    2114             :                 }
    2115             :         }
    2116             : 
    2117          37 :         zend_object_std_dtor(&intern->zo TSRMLS_CC);
    2118          37 :         efree(intern);
    2119             : }
    2120             : /* }}} */
    2121             : 
    2122          66 : static zend_object_value php_sqlite3_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
    2123             : {
    2124             :         zval *tmp;
    2125             :         zend_object_value retval;
    2126             :         php_sqlite3_db_object *intern;
    2127             : 
    2128             :         /* Allocate memory for it */
    2129          66 :         intern = emalloc(sizeof(php_sqlite3_db_object));
    2130          66 :         memset(intern, 0, sizeof(php_sqlite3_db_object));
    2131          66 :         intern->exception = 0;
    2132             : 
    2133             :         /* Need to keep track of things to free */
    2134          66 :         zend_llist_init(&(intern->free_list),   sizeof(php_sqlite3_free_list *), (llist_dtor_func_t)php_sqlite3_free_list_dtor, 0);
    2135             : 
    2136          66 :         zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
    2137          66 :         zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_property_ctor,(void *) &tmp, sizeof(zval *));
    2138             : 
    2139          66 :         retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_sqlite3_object_free_storage, NULL TSRMLS_CC);
    2140          66 :         retval.handlers = (zend_object_handlers *) &sqlite3_object_handlers;
    2141             : 
    2142          66 :         return retval;
    2143             : }
    2144             : /* }}} */
    2145             : 
    2146          44 : static zend_object_value php_sqlite3_stmt_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
    2147             : {
    2148             :         zval *tmp;
    2149             :         zend_object_value retval;
    2150             :         php_sqlite3_stmt *intern;
    2151             : 
    2152             :         /* Allocate memory for it */
    2153          44 :         intern = emalloc(sizeof(php_sqlite3_stmt));
    2154          44 :         memset(intern, 0, sizeof(php_sqlite3_stmt));
    2155             : 
    2156          44 :         intern->db_obj_zval = NULL;
    2157             : 
    2158          44 :         zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
    2159          44 :         zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_property_ctor,(void *) &tmp, sizeof(zval *));
    2160             : 
    2161          44 :         retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_sqlite3_stmt_object_free_storage, NULL TSRMLS_CC);
    2162          44 :         retval.handlers = (zend_object_handlers *) &sqlite3_stmt_object_handlers;
    2163             : 
    2164          44 :         return retval;
    2165             : }
    2166             : /* }}} */
    2167             : 
    2168          37 : static zend_object_value php_sqlite3_result_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
    2169             : {
    2170             :         zval *tmp;
    2171             :         zend_object_value retval;
    2172             :         php_sqlite3_result *intern;
    2173             : 
    2174             :         /* Allocate memory for it */
    2175          37 :         intern = emalloc(sizeof(php_sqlite3_result));
    2176          37 :         memset(intern, 0, sizeof(php_sqlite3_result));
    2177             : 
    2178          37 :         intern->complete = 0;
    2179          37 :         intern->is_prepared_statement = 0;
    2180          37 :         intern->stmt_obj_zval = NULL;
    2181             : 
    2182          37 :         zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
    2183          37 :         zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_property_ctor,(void *) &tmp, sizeof(zval *));
    2184             : 
    2185          37 :         retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_sqlite3_result_object_free_storage, NULL TSRMLS_CC);
    2186          37 :         retval.handlers = (zend_object_handlers *) &sqlite3_result_object_handlers;
    2187             : 
    2188          37 :         return retval;
    2189             : }
    2190             : /* }}} */
    2191             : 
    2192          19 : static void sqlite3_param_dtor(void *data) /* {{{ */
    2193             : {
    2194          19 :         struct php_sqlite3_bound_param *param = (struct php_sqlite3_bound_param*)data;
    2195             : 
    2196          19 :         if (param->name) {
    2197           3 :                 efree(param->name);
    2198             :         }
    2199             : 
    2200          19 :         if (param->parameter) {
    2201          19 :                 zval_ptr_dtor(&(param->parameter));
    2202          19 :                 param->parameter = NULL;
    2203             :         }
    2204          19 : }
    2205             : /* }}} */
    2206             : 
    2207             : /* {{{ PHP_MINIT_FUNCTION
    2208             : */
    2209       19341 : PHP_MINIT_FUNCTION(sqlite3)
    2210             : {
    2211             :         zend_class_entry ce;
    2212             : 
    2213             : #if defined(ZTS)
    2214             :         /* Refuse to load if this wasn't a threasafe library loaded */
    2215             :         if (!sqlite3_threadsafe()) {
    2216             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "A thread safe version of SQLite is required when using a thread safe version of PHP.");
    2217             :                 return FAILURE;
    2218             :         }
    2219             : #endif
    2220             : 
    2221       19341 :         memcpy(&sqlite3_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    2222       19341 :         memcpy(&sqlite3_stmt_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    2223       19341 :         memcpy(&sqlite3_result_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    2224             : 
    2225             :         /* Register SQLite 3 Class */
    2226       19341 :         INIT_CLASS_ENTRY(ce, "SQLite3", php_sqlite3_class_methods);
    2227       19341 :         ce.create_object = php_sqlite3_object_new;
    2228       19341 :         sqlite3_object_handlers.clone_obj = NULL;
    2229       19341 :         php_sqlite3_sc_entry = zend_register_internal_class(&ce TSRMLS_CC);
    2230             : 
    2231             :         /* Register SQLite 3 Prepared Statement Class */
    2232       19341 :         INIT_CLASS_ENTRY(ce, "SQLite3Stmt", php_sqlite3_stmt_class_methods);
    2233       19341 :         ce.create_object = php_sqlite3_stmt_object_new;
    2234       19341 :         sqlite3_stmt_object_handlers.clone_obj = NULL;
    2235       19341 :         php_sqlite3_stmt_entry = zend_register_internal_class(&ce TSRMLS_CC);
    2236             : 
    2237             :         /* Register SQLite 3 Result Class */
    2238       19341 :         INIT_CLASS_ENTRY(ce, "SQLite3Result", php_sqlite3_result_class_methods);
    2239       19341 :         ce.create_object = php_sqlite3_result_object_new;
    2240       19341 :         sqlite3_result_object_handlers.clone_obj = NULL;
    2241       19341 :         php_sqlite3_result_entry = zend_register_internal_class(&ce TSRMLS_CC);
    2242             : 
    2243       19341 :         REGISTER_INI_ENTRIES();
    2244             : 
    2245       19341 :         REGISTER_LONG_CONSTANT("SQLITE3_ASSOC", PHP_SQLITE3_ASSOC, CONST_CS | CONST_PERSISTENT);
    2246       19341 :         REGISTER_LONG_CONSTANT("SQLITE3_NUM", PHP_SQLITE3_NUM, CONST_CS | CONST_PERSISTENT);
    2247       19341 :         REGISTER_LONG_CONSTANT("SQLITE3_BOTH", PHP_SQLITE3_BOTH, CONST_CS | CONST_PERSISTENT);
    2248             : 
    2249       19341 :         REGISTER_LONG_CONSTANT("SQLITE3_INTEGER", SQLITE_INTEGER, CONST_CS | CONST_PERSISTENT);
    2250       19341 :         REGISTER_LONG_CONSTANT("SQLITE3_FLOAT", SQLITE_FLOAT, CONST_CS | CONST_PERSISTENT);
    2251       19341 :         REGISTER_LONG_CONSTANT("SQLITE3_TEXT", SQLITE3_TEXT, CONST_CS | CONST_PERSISTENT);
    2252       19341 :         REGISTER_LONG_CONSTANT("SQLITE3_BLOB", SQLITE_BLOB, CONST_CS | CONST_PERSISTENT);
    2253       19341 :         REGISTER_LONG_CONSTANT("SQLITE3_NULL", SQLITE_NULL, CONST_CS | CONST_PERSISTENT);
    2254             : 
    2255       19341 :         REGISTER_LONG_CONSTANT("SQLITE3_OPEN_READONLY", SQLITE_OPEN_READONLY, CONST_CS | CONST_PERSISTENT);
    2256       19341 :         REGISTER_LONG_CONSTANT("SQLITE3_OPEN_READWRITE", SQLITE_OPEN_READWRITE, CONST_CS | CONST_PERSISTENT);
    2257       19341 :         REGISTER_LONG_CONSTANT("SQLITE3_OPEN_CREATE", SQLITE_OPEN_CREATE, CONST_CS | CONST_PERSISTENT);
    2258             : 
    2259       19341 :         return SUCCESS;
    2260             : }
    2261             : /* }}} */
    2262             : 
    2263             : /* {{{ PHP_MSHUTDOWN_FUNCTION
    2264             : */
    2265       19376 : PHP_MSHUTDOWN_FUNCTION(sqlite3)
    2266             : {
    2267       19376 :         UNREGISTER_INI_ENTRIES();
    2268             : 
    2269       19376 :         return SUCCESS;
    2270             : }
    2271             : /* }}} */
    2272             : 
    2273             : /* {{{ PHP_MINFO_FUNCTION
    2274             : */
    2275         148 : PHP_MINFO_FUNCTION(sqlite3)
    2276             : {
    2277         148 :         php_info_print_table_start();
    2278         148 :         php_info_print_table_header(2, "SQLite3 support", "enabled");
    2279         148 :         php_info_print_table_row(2, "SQLite3 module version", PHP_SQLITE3_VERSION);
    2280         148 :         php_info_print_table_row(2, "SQLite Library", sqlite3_libversion());
    2281         148 :         php_info_print_table_end();
    2282             : 
    2283         148 :         DISPLAY_INI_ENTRIES();
    2284         148 : }
    2285             : /* }}} */
    2286             : 
    2287             : /* {{{ PHP_GINIT_FUNCTION
    2288             : */
    2289       19341 : static PHP_GINIT_FUNCTION(sqlite3)
    2290             : {
    2291       19341 :         memset(sqlite3_globals, 0, sizeof(*sqlite3_globals));
    2292       19341 : }
    2293             : /* }}} */
    2294             : 
    2295             : /* {{{ sqlite3_module_entry
    2296             : */
    2297             : zend_module_entry sqlite3_module_entry = {
    2298             :         STANDARD_MODULE_HEADER,
    2299             :         "sqlite3",
    2300             :         NULL,
    2301             :         PHP_MINIT(sqlite3),
    2302             :         PHP_MSHUTDOWN(sqlite3),
    2303             :         NULL,
    2304             :         NULL,
    2305             :         PHP_MINFO(sqlite3),
    2306             :         PHP_SQLITE3_VERSION,
    2307             :         PHP_MODULE_GLOBALS(sqlite3),
    2308             :         PHP_GINIT(sqlite3),
    2309             :         NULL,
    2310             :         NULL,
    2311             :         STANDARD_MODULE_PROPERTIES_EX
    2312             : };
    2313             : /* }}} */
    2314             : 
    2315             : #ifdef COMPILE_DL_SQLITE3
    2316             : ZEND_GET_MODULE(sqlite3)
    2317             : #endif
    2318             : 
    2319             : /*
    2320             :  * Local variables:
    2321             :  * tab-width: 4
    2322             :  * c-basic-offset: 4
    2323             :  * End:
    2324             :  * vim600: sw=4 ts=4 fdm=marker
    2325             :  * vim<600: sw=4 ts=4
    2326             :  */

Generated by: LCOV version 1.10

Generated at Sun, 27 Jul 2014 12:58:36 +0000 (2 days ago)

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