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

Generated by: LCOV version 1.10

Generated at Fri, 19 Sep 2014 17:11:16 +0000 (2 days ago)

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