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

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