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: 766 961 79.7 %
Date: 2016-07-26 Functions: 57 65 87.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 26 Jul 2016 17:07:45 +0000 (35 hours ago)

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