PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/sqlite3 - sqlite3.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 735 941 78.1 %
Date: 2015-03-01 Functions: 58 65 89.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 01 Mar 2015 23:22:34 +0000 (4 days ago)

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