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: 750 956 78.5 %
Date: 2016-04-27 Functions: 57 65 87.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Wed, 27 Apr 2016 15:51:49 +0000 (2 days ago)

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