root/ext/sqlite3/sqlite3.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ZEND_DECLARE_MODULE_GLOBALS
  2. PHP_INI_BEGIN
  3. PHP_METHOD
  4. PHP_METHOD
  5. PHP_METHOD
  6. PHP_METHOD
  7. PHP_METHOD
  8. PHP_METHOD
  9. PHP_METHOD
  10. PHP_METHOD
  11. PHP_METHOD
  12. PHP_METHOD
  13. PHP_METHOD
  14. PHP_METHOD
  15. sqlite_value_to_zval
  16. PHP_METHOD
  17. sqlite3_do_callback
  18. php_sqlite3_callback_func
  19. php_sqlite3_callback_step
  20. php_sqlite3_callback_final
  21. php_sqlite3_callback_compare
  22. PHP_METHOD
  23. PHP_METHOD
  24. PHP_METHOD
  25. php_sqlite3_stream_write
  26. php_sqlite3_stream_read
  27. php_sqlite3_stream_close
  28. php_sqlite3_stream_flush
  29. php_sqlite3_stream_seek
  30. php_sqlite3_stream_cast
  31. php_sqlite3_stream_stat
  32. PHP_METHOD
  33. PHP_METHOD
  34. PHP_METHOD
  35. PHP_METHOD
  36. PHP_METHOD
  37. PHP_METHOD
  38. PHP_METHOD
  39. register_bound_parameter_to_sqlite
  40. PHP_METHOD
  41. PHP_METHOD
  42. PHP_METHOD
  43. PHP_METHOD
  44. PHP_METHOD
  45. PHP_METHOD
  46. PHP_METHOD
  47. PHP_METHOD
  48. PHP_METHOD
  49. PHP_METHOD
  50. PHP_METHOD
  51. php_sqlite3_authorizer
  52. php_sqlite3_free_list_dtor
  53. php_sqlite3_compare_stmt_zval_free
  54. php_sqlite3_compare_stmt_free
  55. php_sqlite3_object_free_storage
  56. php_sqlite3_stmt_object_free_storage
  57. php_sqlite3_result_object_free_storage
  58. php_sqlite3_object_new
  59. php_sqlite3_stmt_object_new
  60. php_sqlite3_result_object_new
  61. sqlite3_param_dtor
  62. PHP_MINIT_FUNCTION
  63. PHP_MSHUTDOWN_FUNCTION
  64. PHP_MINFO_FUNCTION
  65. PHP_GINIT_FUNCTION

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

/* [<][>][^][v][top][bottom][index][help] */