Back to index

php5  5.3.10
sqlite3.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Scott MacVicar <scottmac@php.net>                           |
00016    +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id: sqlite3.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 
00025 #include "php.h"
00026 #include "php_ini.h"
00027 #include "ext/standard/info.h"
00028 #include "php_sqlite3.h"
00029 #include "php_sqlite3_structs.h"
00030 #include "main/SAPI.h"
00031 
00032 #include <sqlite3.h>
00033 
00034 #include "zend_exceptions.h"
00035 #include "zend_interfaces.h"
00036 #include "SAPI.h"
00037 
00038 ZEND_DECLARE_MODULE_GLOBALS(sqlite3)
00039 
00040 static PHP_GINIT_FUNCTION(sqlite3);
00041 static int php_sqlite3_authorizer(void *autharg, int access_type, const char *arg3, const char *arg4, const char *arg5, const char *arg6);
00042 static void sqlite3_param_dtor(void *data);
00043 static int php_sqlite3_compare_stmt_zval_free(php_sqlite3_free_list **free_list, zval *statement);
00044 
00045 /* {{{ Error Handler
00046 */
00047 static void php_sqlite3_error(php_sqlite3_db_object *db_obj, char *format, ...)
00048 {
00049        va_list arg;
00050        char   *message;
00051        TSRMLS_FETCH();
00052 
00053        va_start(arg, format); 
00054        vspprintf(&message, 0, format, arg);
00055        va_end(arg);
00056 
00057        if (db_obj->exception) {
00058               zend_throw_exception(zend_exception_get_default(TSRMLS_C), message, 0 TSRMLS_CC);
00059        } else {
00060               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", message);
00061        }
00062        
00063        if (message) {
00064               efree(message);
00065        }
00066 }
00067 /* }}} */
00068 
00069 #define SQLITE3_CHECK_INITIALIZED(db_obj, member, class_name) \
00070        if (!(member)) { \
00071               php_sqlite3_error(db_obj, "The " #class_name " object has not been correctly initialised"); \
00072               RETURN_FALSE; \
00073        }
00074 
00075 /* {{{ PHP_INI
00076 */
00077 PHP_INI_BEGIN()
00078        STD_PHP_INI_ENTRY("sqlite3.extension_dir",  NULL, PHP_INI_SYSTEM, OnUpdateString, extension_dir, zend_sqlite3_globals, sqlite3_globals)
00079 PHP_INI_END()
00080 /* }}} */
00081 
00082 /* Handlers */
00083 static zend_object_handlers sqlite3_object_handlers;
00084 static zend_object_handlers sqlite3_stmt_object_handlers;
00085 static zend_object_handlers sqlite3_result_object_handlers;
00086 
00087 /* Class entries */
00088 zend_class_entry *php_sqlite3_sc_entry;
00089 zend_class_entry *php_sqlite3_stmt_entry;
00090 zend_class_entry *php_sqlite3_result_entry;
00091 
00092 /* {{{ proto void SQLite3::open(String filename [, int Flags [, string Encryption Key]])
00093    Opens a SQLite 3 Database, if the build includes encryption then it will attempt to use the key. */
00094 PHP_METHOD(sqlite3, open)
00095 {
00096        php_sqlite3_db_object *db_obj;
00097        zval *object = getThis();
00098        char *filename, *encryption_key, *fullpath;
00099        int filename_len, encryption_key_len = 0;
00100        long flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
00101        zend_error_handling error_handling;
00102 
00103        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
00104        zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
00105 
00106        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls", &filename, &filename_len, &flags, &encryption_key, &encryption_key_len)) {
00107               zend_restore_error_handling(&error_handling TSRMLS_CC);
00108               return;
00109        }
00110 
00111        zend_restore_error_handling(&error_handling TSRMLS_CC);
00112 
00113        if (db_obj->initialised) {
00114               zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Already initialised DB Object", 0 TSRMLS_CC);
00115        }
00116 
00117        if (strlen(filename) != filename_len) {
00118               return;
00119        }
00120        if (strncmp(filename, ":memory:", 8) != 0) {
00121               if (!(fullpath = expand_filepath(filename, NULL TSRMLS_CC))) {
00122                      zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Unable to expand filepath", 0 TSRMLS_CC);
00123                      return;
00124               }
00125 
00126 #if PHP_API_VERSION < 20100412
00127               if (PG(safe_mode) && (!php_checkuid(fullpath, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
00128                      zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "safe_mode prohibits opening %s", fullpath);
00129                      efree(fullpath);
00130                      return;
00131               }
00132 #endif
00133 
00134               if (php_check_open_basedir(fullpath TSRMLS_CC)) {
00135                      zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "open_basedir prohibits opening %s", fullpath);
00136                      efree(fullpath);
00137                      return;
00138               }
00139        } else {
00140               fullpath = estrdup(filename);
00141        }
00142 
00143 #if SQLITE_VERSION_NUMBER >= 3005000
00144        if (sqlite3_open_v2(fullpath, &(db_obj->db), flags, NULL) != SQLITE_OK) {
00145 #else
00146        if (sqlite3_open(fullpath, &(db_obj->db)) != SQLITE_OK) {
00147 #endif
00148               zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
00149               if (fullpath) {
00150                      efree(fullpath);
00151               }
00152               return;
00153        }
00154 
00155 #if SQLITE_HAS_CODEC
00156        if (encryption_key_len > 0) {
00157               if (sqlite3_key(db_obj->db, encryption_key, encryption_key_len) != SQLITE_OK) {
00158                      zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
00159                      return;
00160               }
00161        }
00162 #endif
00163 
00164        db_obj->initialised = 1;
00165 
00166 #if PHP_API_VERSION < 20100412
00167        if (PG(safe_mode) || (PG(open_basedir) && *PG(open_basedir))) {
00168 #else
00169        if (PG(open_basedir) && *PG(open_basedir)) {
00170 #endif
00171               sqlite3_set_authorizer(db_obj->db, php_sqlite3_authorizer, NULL);
00172        }
00173 
00174        if (fullpath) {
00175               efree(fullpath);
00176        }
00177 }
00178 /* }}} */
00179 
00180 /* {{{ proto bool SQLite3::close()
00181    Close a SQLite 3 Database. */
00182 PHP_METHOD(sqlite3, close)
00183 {
00184        php_sqlite3_db_object *db_obj;
00185        zval *object = getThis();
00186        int errcode;
00187        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
00188 
00189        if (zend_parse_parameters_none() == FAILURE) {
00190               return;
00191        }
00192 
00193        if (db_obj->initialised) {
00194               zend_llist_clean(&(db_obj->free_list));
00195               errcode = sqlite3_close(db_obj->db);
00196               if (errcode != SQLITE_OK) {
00197                      php_sqlite3_error(db_obj, "Unable to close database: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
00198                      RETURN_FALSE;
00199               }
00200               db_obj->initialised = 0;
00201        }
00202 
00203        RETURN_TRUE;
00204 }
00205 /* }}} */
00206 
00207 /* {{{ proto bool SQLite3::exec(String Query)
00208    Executes a result-less query against a given database. */
00209 PHP_METHOD(sqlite3, exec)
00210 {
00211        php_sqlite3_db_object *db_obj;
00212        zval *object = getThis();
00213        char *sql, *errtext = NULL;
00214        int sql_len;
00215        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
00216 
00217        SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
00218 
00219        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
00220               return;
00221        }
00222 
00223        if (sqlite3_exec(db_obj->db, sql, NULL, NULL, &errtext) != SQLITE_OK) {
00224               php_sqlite3_error(db_obj, "%s", errtext);
00225               sqlite3_free(errtext);
00226               RETURN_FALSE;
00227        }
00228 
00229        RETURN_TRUE;
00230 }
00231 /* }}} */
00232 
00233 /* {{{ proto Array SQLite3::version()
00234    Returns the SQLite3 Library version as a string constant and as a number. */
00235 PHP_METHOD(sqlite3, version)
00236 {
00237        if (zend_parse_parameters_none() == FAILURE) {
00238               return;
00239        }
00240 
00241        array_init(return_value);
00242 
00243        add_assoc_string(return_value, "versionString", (char*)sqlite3_libversion(), 1);
00244        add_assoc_long(return_value, "versionNumber", sqlite3_libversion_number());
00245 
00246        return;
00247 }
00248 /* }}} */
00249 
00250 /* {{{ proto int SQLite3::lastInsertRowID()
00251    Returns the rowid of the most recent INSERT into the database from the database connection. */
00252 PHP_METHOD(sqlite3, lastInsertRowID)
00253 {
00254        php_sqlite3_db_object *db_obj;
00255        zval *object = getThis();
00256        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
00257 
00258        SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
00259 
00260        if (zend_parse_parameters_none() == FAILURE) {
00261               return;
00262        }
00263 
00264        RETURN_LONG(sqlite3_last_insert_rowid(db_obj->db));
00265 }
00266 /* }}} */
00267 
00268 /* {{{ proto int SQLite3::lastErrorCode()
00269    Returns the numeric result code of the most recent failed sqlite API call for the database connection. */
00270 PHP_METHOD(sqlite3, lastErrorCode)
00271 {
00272        php_sqlite3_db_object *db_obj;
00273        zval *object = getThis();
00274        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
00275 
00276        SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
00277 
00278        if (zend_parse_parameters_none() == FAILURE) {
00279               return;
00280        }
00281 
00282        RETURN_LONG(sqlite3_errcode(db_obj->db));
00283 }
00284 /* }}} */
00285 
00286 /* {{{ proto string SQLite3::lastErrorMsg()
00287    Returns english text describing the most recent failed sqlite API call for the database connection. */
00288 PHP_METHOD(sqlite3, lastErrorMsg)
00289 {
00290        php_sqlite3_db_object *db_obj;
00291        zval *object = getThis();
00292        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
00293 
00294        SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
00295 
00296        if (zend_parse_parameters_none() == FAILURE) {
00297               return;
00298        }
00299 
00300        RETVAL_STRING((char *)sqlite3_errmsg(db_obj->db), 1);
00301 }
00302 /* }}} */
00303 
00304 /* {{{ proto bool SQLite3::busyTimeout(int msecs)
00305    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. */
00306 PHP_METHOD(sqlite3, busyTimeout)
00307 {
00308        php_sqlite3_db_object *db_obj;
00309        zval *object = getThis();
00310        long ms;
00311        int return_code;
00312        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
00313 
00314        SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
00315 
00316        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ms)) {
00317               return;
00318        }
00319 
00320        return_code = sqlite3_busy_timeout(db_obj->db, ms);
00321        if (return_code != SQLITE_OK) {
00322               php_sqlite3_error(db_obj, "Unable to set busy timeout: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
00323               RETURN_FALSE;
00324        }
00325 
00326        RETURN_TRUE;
00327 }
00328 /* }}} */
00329 
00330 
00331 #ifndef SQLITE_OMIT_LOAD_EXTENSION
00332 /* {{{ proto bool SQLite3::loadExtension(String Shared Library)
00333    Attempts to load an SQLite extension library. */
00334 PHP_METHOD(sqlite3, loadExtension)
00335 {
00336        php_sqlite3_db_object *db_obj;
00337        zval *object = getThis();
00338        char *extension, *lib_path, *extension_dir, *errtext = NULL;
00339        char fullpath[MAXPATHLEN];
00340        int extension_len, extension_dir_len;
00341        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
00342 
00343        SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
00344 
00345        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &extension, &extension_len)) {
00346               return;
00347        }
00348 
00349 #ifdef ZTS
00350        if ((strncmp(sapi_module.name, "cgi", 3) != 0) &&
00351               (strcmp(sapi_module.name, "cli") != 0) &&
00352               (strncmp(sapi_module.name, "embed", 5) != 0)
00353        ) {           php_sqlite3_error(db_obj, "Not supported in multithreaded Web servers");
00354               RETURN_FALSE;
00355        }
00356 #endif
00357 
00358        if (!SQLITE3G(extension_dir)) {
00359               php_sqlite3_error(db_obj, "SQLite Extension are disabled");
00360               RETURN_FALSE;
00361        }
00362 
00363        if (extension_len == 0) {
00364               php_sqlite3_error(db_obj, "Empty string as an extension");
00365               RETURN_FALSE;
00366        }
00367 
00368        extension_dir = SQLITE3G(extension_dir);
00369        extension_dir_len = strlen(SQLITE3G(extension_dir));
00370 
00371        if (IS_SLASH(extension_dir[extension_dir_len-1])) {
00372               spprintf(&lib_path, 0, "%s%s", extension_dir, extension);
00373        } else {
00374               spprintf(&lib_path, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, extension);
00375        }
00376 
00377        if (!VCWD_REALPATH(lib_path, fullpath)) {
00378               php_sqlite3_error(db_obj, "Unable to load extension at '%s'", lib_path);
00379               efree(lib_path);
00380               RETURN_FALSE;
00381        }
00382 
00383        efree(lib_path);
00384 
00385        if (strncmp(fullpath, extension_dir, extension_dir_len) != 0) {
00386               php_sqlite3_error(db_obj, "Unable to open extensions outside the defined directory");
00387               RETURN_FALSE;
00388        }
00389 
00390        /* Extension loading should only be enabled for when we attempt to load */
00391        sqlite3_enable_load_extension(db_obj->db, 1);
00392        if (sqlite3_load_extension(db_obj->db, fullpath, 0, &errtext) != SQLITE_OK) {
00393               php_sqlite3_error(db_obj, "%s", errtext);
00394               sqlite3_free(errtext);
00395               sqlite3_enable_load_extension(db_obj->db, 0);
00396               RETURN_FALSE;
00397        }
00398        sqlite3_enable_load_extension(db_obj->db, 0);
00399 
00400        RETURN_TRUE;
00401 }
00402 /* }}} */
00403 #endif
00404 
00405 /* {{{ proto int SQLite3::changes()
00406   Returns the number of database rows that were changed (or inserted or deleted) by the most recent SQL statement. */
00407 PHP_METHOD(sqlite3, changes)
00408 {
00409        php_sqlite3_db_object *db_obj;
00410        zval *object = getThis();
00411        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
00412 
00413        SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
00414 
00415        if (zend_parse_parameters_none() == FAILURE) {
00416               return;
00417        }
00418 
00419        RETURN_LONG(sqlite3_changes(db_obj->db));
00420 }
00421 /* }}} */
00422 
00423 /* {{{ proto String SQLite3::escapeString(String value)
00424    Returns a string that has been properly escaped. */
00425 PHP_METHOD(sqlite3, escapeString)
00426 {
00427        char *sql, *ret;
00428        int sql_len;
00429 
00430        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
00431               return;
00432        }
00433 
00434        if (sql_len) {
00435               ret = sqlite3_mprintf("%q", sql);
00436               if (ret) {
00437                      RETVAL_STRING(ret, 1);
00438                      sqlite3_free(ret);
00439               }
00440        } else {
00441               RETURN_EMPTY_STRING();
00442        }
00443 }
00444 /* }}} */
00445 
00446 /* {{{ proto SQLite3Stmt SQLite3::prepare(String Query)
00447    Returns a prepared SQL statement for execution. */
00448 PHP_METHOD(sqlite3, prepare)
00449 {
00450        php_sqlite3_db_object *db_obj;
00451        php_sqlite3_stmt *stmt_obj;
00452        zval *object = getThis();
00453        char *sql;
00454        int sql_len, errcode;
00455        php_sqlite3_free_list *free_item;
00456 
00457        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
00458 
00459        SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
00460 
00461        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
00462               return;
00463        }
00464 
00465        if (!sql_len) {
00466               RETURN_FALSE;
00467        }
00468 
00469        object_init_ex(return_value, php_sqlite3_stmt_entry);
00470        stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(return_value TSRMLS_CC);
00471        stmt_obj->db_obj = db_obj;
00472        stmt_obj->db_obj_zval = getThis();
00473 
00474        Z_ADDREF_P(object);
00475 
00476        errcode = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &(stmt_obj->stmt), NULL);
00477        if (errcode != SQLITE_OK) {
00478               php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
00479               zval_dtor(return_value);
00480               RETURN_FALSE;
00481        }
00482 
00483        stmt_obj->initialised = 1;
00484 
00485        free_item = emalloc(sizeof(php_sqlite3_free_list));
00486        free_item->stmt_obj = stmt_obj;
00487        free_item->stmt_obj_zval = return_value;
00488 
00489        zend_llist_add_element(&(db_obj->free_list), &free_item);
00490 }
00491 /* }}} */
00492 
00493 /* {{{ proto SQLite3Result SQLite3::query(String Query)
00494    Returns true or false, for queries that return data it will return a SQLite3Result object. */
00495 PHP_METHOD(sqlite3, query)
00496 {
00497        php_sqlite3_db_object *db_obj;
00498        php_sqlite3_result *result;
00499        php_sqlite3_stmt *stmt_obj;
00500        zval *object = getThis();
00501        zval *stmt = NULL;
00502        char *sql, *errtext = NULL;
00503        int sql_len, return_code;
00504        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
00505 
00506        SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
00507 
00508        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &sql, &sql_len)) {
00509               return;
00510        }
00511 
00512        if (!sql_len) {
00513               RETURN_FALSE;
00514        }
00515 
00516        /* If there was no return value then just execute the query */
00517        if (!return_value_used) {
00518               if (sqlite3_exec(db_obj->db, sql, NULL, NULL, &errtext) != SQLITE_OK) {
00519                      php_sqlite3_error(db_obj, "%s", errtext);
00520                      sqlite3_free(errtext);
00521               }
00522               return;
00523        }
00524 
00525        MAKE_STD_ZVAL(stmt);
00526 
00527        object_init_ex(stmt, php_sqlite3_stmt_entry);
00528        stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(stmt TSRMLS_CC);
00529        stmt_obj->db_obj = db_obj;
00530        stmt_obj->db_obj_zval = getThis();
00531 
00532        Z_ADDREF_P(object);
00533 
00534        return_code = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &(stmt_obj->stmt), NULL);
00535        if (return_code != SQLITE_OK) {
00536               php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
00537               zval_ptr_dtor(&stmt);
00538               RETURN_FALSE;
00539        }
00540 
00541        stmt_obj->initialised = 1;
00542 
00543        object_init_ex(return_value, php_sqlite3_result_entry);
00544        result = (php_sqlite3_result *)zend_object_store_get_object(return_value TSRMLS_CC);
00545        result->db_obj = db_obj;
00546        result->stmt_obj = stmt_obj;
00547        result->stmt_obj_zval = stmt;
00548 
00549        return_code = sqlite3_step(result->stmt_obj->stmt);
00550 
00551        switch (return_code) {
00552               case SQLITE_ROW: /* Valid Row */
00553               case SQLITE_DONE: /* Valid but no results */
00554               {
00555                      php_sqlite3_free_list *free_item;
00556                      free_item = emalloc(sizeof(php_sqlite3_free_list));
00557                      free_item->stmt_obj = stmt_obj;
00558                      free_item->stmt_obj_zval = stmt;
00559                      zend_llist_add_element(&(db_obj->free_list), &free_item);
00560                      sqlite3_reset(result->stmt_obj->stmt);
00561                      break;
00562               }
00563               default:
00564                      php_sqlite3_error(db_obj, "Unable to execute statement: %s", sqlite3_errmsg(db_obj->db));
00565                      sqlite3_finalize(stmt_obj->stmt);
00566                      stmt_obj->initialised = 0;
00567                      zval_dtor(return_value);
00568                      RETURN_FALSE;
00569        }
00570 }
00571 /* }}} */
00572 
00573 static zval* sqlite_value_to_zval(sqlite3_stmt *stmt, int column) /* {{{ */
00574 {
00575        zval *data;
00576        MAKE_STD_ZVAL(data);
00577        switch (sqlite3_column_type(stmt, column)) {
00578               case SQLITE_INTEGER:
00579                      if ((sqlite3_column_int64(stmt, column)) >= INT_MAX || sqlite3_column_int64(stmt, column) <= INT_MIN) {
00580                             ZVAL_STRINGL(data, (char *)sqlite3_column_text(stmt, column), sqlite3_column_bytes(stmt, column), 1);
00581                      } else {
00582                             ZVAL_LONG(data, sqlite3_column_int64(stmt, column));
00583                      }
00584                      break;
00585 
00586               case SQLITE_FLOAT:
00587                      ZVAL_DOUBLE(data, sqlite3_column_double(stmt, column));
00588                      break;
00589 
00590               case SQLITE_NULL:
00591                      ZVAL_NULL(data);
00592                      break;
00593 
00594               case SQLITE3_TEXT:
00595                      ZVAL_STRING(data, (char*)sqlite3_column_text(stmt, column), 1);
00596                      break;
00597 
00598               case SQLITE_BLOB:
00599               default:
00600                      ZVAL_STRINGL(data, (char*)sqlite3_column_blob(stmt, column), sqlite3_column_bytes(stmt, column), 1);
00601        }
00602        return data;
00603 }
00604 /* }}} */
00605 
00606 /* {{{ proto SQLite3Result SQLite3::querySingle(String Query [, bool entire_row = false])
00607    Returns a string of the first column, or an array of the entire row. */
00608 PHP_METHOD(sqlite3, querySingle)
00609 {
00610        php_sqlite3_db_object *db_obj;
00611        zval *object = getThis();
00612        char *sql, *errtext = NULL;
00613        int sql_len, return_code;
00614        zend_bool entire_row = 0;
00615        sqlite3_stmt *stmt;
00616        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
00617 
00618        SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
00619 
00620        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &sql, &sql_len, &entire_row)) {
00621               return;
00622        }
00623 
00624        if (!sql_len) {
00625               RETURN_FALSE;
00626        }
00627 
00628        /* If there was no return value then just execute the query */
00629        if (!return_value_used) {
00630               if (sqlite3_exec(db_obj->db, sql, NULL, NULL, &errtext) != SQLITE_OK) {
00631                      php_sqlite3_error(db_obj, "%s", errtext);
00632                      sqlite3_free(errtext);
00633               }
00634               return;
00635        }
00636 
00637        return_code = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &stmt, NULL);
00638        if (return_code != SQLITE_OK) {
00639               php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
00640               RETURN_FALSE;
00641        }
00642 
00643        return_code = sqlite3_step(stmt);
00644 
00645        switch (return_code) {
00646               case SQLITE_ROW: /* Valid Row */
00647               {
00648                      if (!entire_row) {
00649                             zval *data;
00650                             data = sqlite_value_to_zval(stmt, 0);
00651                             *return_value = *data;
00652                             zval_copy_ctor(return_value);
00653                             zval_dtor(data);
00654                             FREE_ZVAL(data);
00655                      } else {
00656                             int i = 0;
00657                             array_init(return_value);
00658                             for (i = 0; i < sqlite3_data_count(stmt); i++) {
00659                                    zval *data;
00660                                    data = sqlite_value_to_zval(stmt, i);
00661                                    add_assoc_zval(return_value, (char*)sqlite3_column_name(stmt, i), data);
00662                             }
00663                      }
00664                      break;
00665               }
00666               case SQLITE_DONE: /* Valid but no results */
00667               {
00668                      if (!entire_row) {
00669                             RETVAL_NULL();
00670                      } else {
00671                             array_init(return_value);
00672                      }
00673                      break;
00674               }
00675               default:
00676                      php_sqlite3_error(db_obj, "Unable to execute statement: %s", sqlite3_errmsg(db_obj->db));
00677                      RETVAL_FALSE;
00678        }
00679        sqlite3_finalize(stmt);
00680 }
00681 /* }}} */
00682 
00683 static int sqlite3_do_callback(struct php_sqlite3_fci *fc, zval *cb, int argc, sqlite3_value **argv, sqlite3_context *context, int is_agg TSRMLS_DC) /* {{{ */
00684 {
00685        zval ***zargs = NULL;
00686        zval *retval = NULL;
00687        int i;
00688        int ret;
00689        int fake_argc;
00690        php_sqlite3_agg_context *agg_context = NULL;
00691 
00692        if (is_agg) {
00693               is_agg = 2;
00694        }
00695 
00696        fake_argc = argc + is_agg;
00697 
00698        fc->fci.size = sizeof(fc->fci);
00699        fc->fci.function_table = EG(function_table);
00700        fc->fci.function_name = cb;
00701        fc->fci.symbol_table = NULL;
00702        fc->fci.object_ptr = NULL;
00703        fc->fci.retval_ptr_ptr = &retval;
00704        fc->fci.param_count = fake_argc;
00705 
00706        /* build up the params */
00707 
00708        if (fake_argc) {
00709               zargs = (zval ***)safe_emalloc(fake_argc, sizeof(zval **), 0);
00710        }
00711 
00712        if (is_agg) {
00713               /* summon the aggregation context */
00714               agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
00715 
00716               if (!agg_context->zval_context) {
00717                      MAKE_STD_ZVAL(agg_context->zval_context);
00718                      ZVAL_NULL(agg_context->zval_context);
00719               }
00720               zargs[0] = &agg_context->zval_context;
00721 
00722               zargs[1] = emalloc(sizeof(zval*));
00723               MAKE_STD_ZVAL(*zargs[1]);
00724               ZVAL_LONG(*zargs[1], agg_context->row_count);
00725        }
00726 
00727        for (i = 0; i < argc; i++) {
00728               zargs[i + is_agg] = emalloc(sizeof(zval *));
00729               MAKE_STD_ZVAL(*zargs[i + is_agg]);
00730 
00731               switch (sqlite3_value_type(argv[i])) {
00732                      case SQLITE_INTEGER:
00733                             ZVAL_LONG(*zargs[i + is_agg], sqlite3_value_int(argv[i]));
00734                             break;
00735 
00736                      case SQLITE_FLOAT:
00737                             ZVAL_DOUBLE(*zargs[i + is_agg], sqlite3_value_double(argv[i]));
00738                             break;
00739 
00740                      case SQLITE_NULL:
00741                             ZVAL_NULL(*zargs[i + is_agg]);
00742                             break;
00743 
00744                      case SQLITE_BLOB:
00745                      case SQLITE3_TEXT:
00746                      default:
00747                             ZVAL_STRINGL(*zargs[i + is_agg], (char*)sqlite3_value_text(argv[i]), sqlite3_value_bytes(argv[i]), 1);
00748                             break;
00749               }
00750        }
00751 
00752        fc->fci.params = zargs;
00753 
00754        if ((ret = zend_call_function(&fc->fci, &fc->fcc TSRMLS_CC)) == FAILURE) {
00755               php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the callback");
00756        }
00757 
00758        /* clean up the params */
00759        if (fake_argc) {
00760               for (i = is_agg; i < argc + is_agg; i++) {
00761                      zval_ptr_dtor(zargs[i]);
00762                      efree(zargs[i]);
00763               }
00764               if (is_agg) {
00765                      zval_ptr_dtor(zargs[1]);
00766                      efree(zargs[1]);
00767               }
00768               efree(zargs);
00769        }
00770 
00771        if (!is_agg || !argv) {
00772               /* only set the sqlite return value if we are a scalar function,
00773                * or if we are finalizing an aggregate */
00774               if (retval) {
00775                      switch (Z_TYPE_P(retval)) {
00776                             case IS_LONG:
00777                                    sqlite3_result_int(context, Z_LVAL_P(retval));
00778                                    break;
00779 
00780                             case IS_NULL:
00781                                    sqlite3_result_null(context);
00782                                    break;
00783 
00784                             case IS_DOUBLE:
00785                                    sqlite3_result_double(context, Z_DVAL_P(retval));
00786                                    break;
00787 
00788                             default:
00789                                    convert_to_string_ex(&retval);
00790                                    sqlite3_result_text(context, Z_STRVAL_P(retval), Z_STRLEN_P(retval), SQLITE_TRANSIENT);
00791                                    break;
00792                      }
00793               } else {
00794                      sqlite3_result_error(context, "failed to invoke callback", 0);
00795               }
00796 
00797               if (agg_context && agg_context->zval_context) {
00798                      zval_ptr_dtor(&agg_context->zval_context);
00799               }
00800        } else {
00801               /* we're stepping in an aggregate; the return value goes into
00802                * the context */
00803               if (agg_context && agg_context->zval_context) {
00804                      zval_ptr_dtor(&agg_context->zval_context);
00805               }
00806               if (retval) {
00807                      agg_context->zval_context = retval;
00808                      retval = NULL;
00809               } else {
00810                      agg_context->zval_context = NULL;
00811               }
00812        }
00813 
00814        if (retval) {
00815               zval_ptr_dtor(&retval);
00816        }
00817        return ret;
00818 }
00819 /* }}}*/
00820 
00821 static void php_sqlite3_callback_func(sqlite3_context *context, int argc, sqlite3_value **argv) /* {{{ */
00822 {
00823        php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
00824        TSRMLS_FETCH();
00825 
00826        sqlite3_do_callback(&func->afunc, func->func, argc, argv, context, 0 TSRMLS_CC);
00827 }
00828 /* }}}*/
00829 
00830 static void php_sqlite3_callback_step(sqlite3_context *context, int argc, sqlite3_value **argv) /* {{{ */
00831 {
00832        php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
00833        php_sqlite3_agg_context *agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
00834 
00835        TSRMLS_FETCH();
00836        agg_context->row_count++;
00837 
00838        sqlite3_do_callback(&func->astep, func->step, argc, argv, context, 1 TSRMLS_CC);
00839 }
00840 /* }}} */
00841 
00842 static void php_sqlite3_callback_final(sqlite3_context *context) /* {{{ */
00843 {
00844        php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
00845        php_sqlite3_agg_context *agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
00846 
00847        TSRMLS_FETCH();
00848        agg_context->row_count = 0;
00849 
00850        sqlite3_do_callback(&func->afini, func->fini, 0, NULL, context, 1 TSRMLS_CC);
00851 }
00852 /* }}} */
00853 
00854 /* {{{ proto bool SQLite3::createFunction(string name, mixed callback [, int argcount])
00855    Allows registration of a PHP function as a SQLite UDF that can be called within SQL statements. */
00856 PHP_METHOD(sqlite3, createFunction)
00857 {
00858        php_sqlite3_db_object *db_obj;
00859        zval *object = getThis();
00860        php_sqlite3_func *func;
00861        char *sql_func, *callback_name;
00862        int sql_func_len;
00863        zval *callback_func;
00864        long sql_func_num_args = -1;
00865        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
00866 
00867        SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
00868 
00869        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &sql_func, &sql_func_len, &callback_func, &sql_func_num_args) == FAILURE) {
00870               return;
00871        }
00872 
00873        if (!sql_func_len) {
00874               RETURN_FALSE;
00875        }
00876 
00877        if (!zend_is_callable(callback_func, 0, &callback_name TSRMLS_CC)) {
00878               php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
00879               efree(callback_name);
00880               RETURN_FALSE;
00881        }
00882        efree(callback_name);
00883 
00884        func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
00885 
00886        if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, SQLITE_UTF8, func, php_sqlite3_callback_func, NULL, NULL) == SQLITE_OK) {
00887               func->func_name = estrdup(sql_func);
00888 
00889               MAKE_STD_ZVAL(func->func);
00890               MAKE_COPY_ZVAL(&callback_func, func->func);
00891 
00892               func->argc = sql_func_num_args;
00893               func->next = db_obj->funcs;
00894               db_obj->funcs = func;
00895 
00896               RETURN_TRUE;
00897        }
00898        efree(func);
00899 
00900        RETURN_FALSE;
00901 }
00902 /* }}} */
00903 
00904 /* {{{ proto bool SQLite3::createAggregate(string name, mixed step, mixed final [, int argcount])
00905    Allows registration of a PHP function for use as an aggregate. */
00906 PHP_METHOD(sqlite3, createAggregate)
00907 {
00908        php_sqlite3_db_object *db_obj;
00909        zval *object = getThis();
00910        php_sqlite3_func *func;
00911        char *sql_func, *callback_name;
00912        int sql_func_len;
00913        zval *step_callback, *fini_callback;
00914        long sql_func_num_args = -1;
00915        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
00916 
00917        SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
00918 
00919        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szz|l", &sql_func, &sql_func_len, &step_callback, &fini_callback, &sql_func_num_args) == FAILURE) {
00920               return;
00921        }
00922 
00923        if (!sql_func_len) {
00924               RETURN_FALSE;
00925        }
00926 
00927        if (!zend_is_callable(step_callback, 0, &callback_name TSRMLS_CC)) {
00928               php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
00929               efree(callback_name);
00930               RETURN_FALSE;
00931        }
00932        efree(callback_name);
00933 
00934        if (!zend_is_callable(fini_callback, 0, &callback_name TSRMLS_CC)) {
00935               php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
00936               efree(callback_name);
00937               RETURN_FALSE;
00938        }
00939        efree(callback_name);
00940 
00941        func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
00942 
00943        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) {
00944               func->func_name = estrdup(sql_func);
00945 
00946               MAKE_STD_ZVAL(func->step);
00947               MAKE_COPY_ZVAL(&step_callback, func->step);
00948 
00949               MAKE_STD_ZVAL(func->fini);
00950               MAKE_COPY_ZVAL(&fini_callback, func->fini);
00951 
00952               func->argc = sql_func_num_args;
00953               func->next = db_obj->funcs;
00954               db_obj->funcs = func;
00955 
00956               RETURN_TRUE;
00957        }
00958        efree(func);
00959 
00960        RETURN_FALSE;
00961 }
00962 /* }}} */
00963 
00964 typedef struct {
00965        sqlite3_blob *blob;
00966        size_t         position;
00967        size_t       size;
00968 } php_stream_sqlite3_data;
00969 
00970 static size_t php_sqlite3_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
00971 {
00972 /*     php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract; */
00973        
00974        return 0;
00975 }
00976 
00977 static size_t php_sqlite3_stream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
00978 {
00979        php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
00980 
00981        if (sqlite3_stream->position + count >= sqlite3_stream->size) {
00982               count = sqlite3_stream->size - sqlite3_stream->position;
00983               stream->eof = 1;
00984        }
00985        if (count) {
00986               if (sqlite3_blob_read(sqlite3_stream->blob, buf, count, sqlite3_stream->position) != SQLITE_OK) {
00987                      return 0;
00988               }
00989               sqlite3_stream->position += count;
00990        }
00991        return count;
00992 }
00993 
00994 static int php_sqlite3_stream_close(php_stream *stream, int close_handle TSRMLS_DC)
00995 {
00996        php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
00997        
00998        if (sqlite3_blob_close(sqlite3_stream->blob) != SQLITE_OK) {
00999               /* Error occured, but it still closed */
01000        }
01001 
01002        efree(sqlite3_stream);
01003        
01004        return 0;
01005 }
01006 
01007 static int php_sqlite3_stream_flush(php_stream *stream TSRMLS_DC)
01008 {
01009        /* do nothing */
01010        return 0;
01011 }
01012 
01013 /* {{{ */
01014 static int php_sqlite3_stream_seek(php_stream *stream, off_t offset, int whence, off_t *newoffs TSRMLS_DC)
01015 {
01016        php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
01017 
01018        switch(whence) {
01019               case SEEK_CUR:
01020                      if (offset < 0) {
01021                             if (sqlite3_stream->position < (size_t)(-offset)) {
01022                                    sqlite3_stream->position = 0;
01023                                    *newoffs = -1;
01024                                    return -1;
01025                             } else {
01026                                    sqlite3_stream->position = sqlite3_stream->position + offset;
01027                                    *newoffs = sqlite3_stream->position;
01028                                    stream->eof = 0;
01029                                    return 0;
01030                             }
01031                      } else {
01032                             if (sqlite3_stream->position + (size_t)(offset) > sqlite3_stream->size) {
01033                                    sqlite3_stream->position = sqlite3_stream->size;
01034                                    *newoffs = -1;
01035                                    return -1;
01036                             } else {
01037                                    sqlite3_stream->position = sqlite3_stream->position + offset;
01038                                    *newoffs = sqlite3_stream->position;
01039                                    stream->eof = 0;
01040                                    return 0;
01041                             }
01042                      }
01043               case SEEK_SET:
01044                      if (sqlite3_stream->size < (size_t)(offset)) {
01045                             sqlite3_stream->position = sqlite3_stream->size;
01046                             *newoffs = -1;
01047                             return -1;
01048                      } else {
01049                             sqlite3_stream->position = offset;
01050                             *newoffs = sqlite3_stream->position;
01051                             stream->eof = 0;
01052                             return 0;
01053                      }
01054               case SEEK_END:
01055                      if (offset > 0) {
01056                             sqlite3_stream->position = sqlite3_stream->size;
01057                             *newoffs = -1;
01058                             return -1;
01059                      } else if (sqlite3_stream->size < (size_t)(-offset)) {
01060                             sqlite3_stream->position = 0;
01061                             *newoffs = -1;
01062                             return -1;
01063                      } else {
01064                             sqlite3_stream->position = sqlite3_stream->size + offset;
01065                             *newoffs = sqlite3_stream->position;
01066                             stream->eof = 0;
01067                             return 0;
01068                      }
01069               default:
01070                      *newoffs = sqlite3_stream->position;
01071                      return -1;
01072        }
01073 }
01074 /* }}} */
01075 
01076 
01077 static int php_sqlite3_stream_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
01078 {
01079        return FAILURE;
01080 }
01081 
01082 static int php_sqlite3_stream_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
01083 {
01084        php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
01085        ssb->sb.st_size = sqlite3_stream->size;
01086        return 0;
01087 }
01088 
01089 static php_stream_ops php_stream_sqlite3_ops = {
01090        php_sqlite3_stream_write,
01091        php_sqlite3_stream_read,
01092        php_sqlite3_stream_close,
01093        php_sqlite3_stream_flush,
01094        "SQLite3",
01095        php_sqlite3_stream_seek,
01096        php_sqlite3_stream_cast,
01097        php_sqlite3_stream_stat
01098 };
01099 
01100 /* {{{ proto resource SQLite3::openBlob(string table, string column, int rowid [, string dbname])
01101    Open a blob as a stream which we can read / write to. */
01102 PHP_METHOD(sqlite3, openBlob)
01103 {
01104        php_sqlite3_db_object *db_obj;
01105        zval *object = getThis();
01106        char *table, *column, *dbname = "main";
01107        int table_len, column_len, dbname_len;
01108        long rowid, flags = 0;
01109        sqlite3_blob *blob = NULL;
01110        php_stream_sqlite3_data *sqlite3_stream;
01111        php_stream *stream;
01112 
01113        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
01114 
01115        SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
01116 
01117        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssl|s", &table, &table_len, &column, &column_len, &rowid, &dbname, &dbname_len) == FAILURE) {
01118               return;
01119        }
01120 
01121        if (sqlite3_blob_open(db_obj->db, dbname, table, column, rowid, flags, &blob) != SQLITE_OK) {
01122               php_sqlite3_error(db_obj, "Unable to open blob: %s", sqlite3_errmsg(db_obj->db));
01123               RETURN_FALSE;
01124        }
01125 
01126        sqlite3_stream = emalloc(sizeof(php_stream_sqlite3_data));
01127        sqlite3_stream->blob = blob;
01128        sqlite3_stream->position = 0;
01129        sqlite3_stream->size = sqlite3_blob_bytes(blob);
01130        
01131        stream = php_stream_alloc(&php_stream_sqlite3_ops, sqlite3_stream, 0, "rb");
01132 
01133        if (stream) {
01134               php_stream_to_zval(stream, return_value);
01135        } else {
01136               RETURN_FALSE;
01137        }
01138 }
01139 /* }}} */
01140 
01141 /* {{{ proto bool SQLite3::enableExceptions([bool enableExceptions = false])
01142    Enables an exception error mode. */
01143 PHP_METHOD(sqlite3, enableExceptions)
01144 {
01145        php_sqlite3_db_object *db_obj;
01146        zval *object = getThis();
01147        zend_bool enableExceptions = 0;
01148 
01149        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
01150 
01151        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enableExceptions) == FAILURE) {
01152               return;
01153        }
01154 
01155        RETVAL_BOOL(db_obj->exception);
01156 
01157        db_obj->exception = enableExceptions;
01158 }
01159 /* }}} */
01160 
01161 /* {{{ proto int SQLite3Stmt::paramCount()
01162    Returns the number of parameters within the prepared statement. */
01163 PHP_METHOD(sqlite3stmt, paramCount)
01164 {
01165        php_sqlite3_stmt *stmt_obj;
01166        zval *object = getThis();
01167        stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
01168 
01169        if (zend_parse_parameters_none() == FAILURE) {
01170               return;
01171        }
01172 
01173        RETURN_LONG(sqlite3_bind_parameter_count(stmt_obj->stmt));
01174 }
01175 /* }}} */
01176 
01177 /* {{{ proto bool SQLite3Stmt::close()
01178    Closes the prepared statement. */
01179 PHP_METHOD(sqlite3stmt, close)
01180 {
01181        php_sqlite3_stmt *stmt_obj;
01182        zval *object = getThis();
01183        stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
01184 
01185        if (zend_parse_parameters_none() == FAILURE) {
01186               return;
01187        }
01188 
01189        zend_llist_del_element(&(stmt_obj->db_obj->free_list), object, (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
01190 
01191        RETURN_TRUE;
01192 }
01193 /* }}} */
01194 
01195 /* {{{ proto bool SQLite3Stmt::reset()
01196    Reset the prepared statement to the state before it was executed, bindings still remain. */
01197 PHP_METHOD(sqlite3stmt, reset)
01198 {
01199        php_sqlite3_stmt *stmt_obj;
01200        zval *object = getThis();
01201        stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
01202 
01203        if (zend_parse_parameters_none() == FAILURE) {
01204               return;
01205        }
01206 
01207        if (sqlite3_reset(stmt_obj->stmt) != SQLITE_OK) {
01208               php_sqlite3_error(stmt_obj->db_obj, "Unable to reset statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
01209               RETURN_FALSE;
01210        }
01211        RETURN_TRUE;
01212 }
01213 /* }}} */
01214 
01215 /* {{{ proto bool SQLite3Stmt::clear()
01216    Clear all current bound parameters. */
01217 PHP_METHOD(sqlite3stmt, clear)
01218 {
01219        php_sqlite3_stmt *stmt_obj;
01220        zval *object = getThis();
01221        stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
01222 
01223        if (zend_parse_parameters_none() == FAILURE) {
01224               return;
01225        }
01226 
01227        if (sqlite3_clear_bindings(stmt_obj->stmt) != SQLITE_OK) {
01228               php_sqlite3_error(stmt_obj->db_obj, "Unable to clear statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
01229               RETURN_FALSE;
01230        }
01231 
01232        RETURN_TRUE;
01233 }
01234 /* }}} */
01235 
01236 /* {{{ proto bool SQLite3Stmt::readOnly()
01237    Returns true if a statement is definitely read only */
01238 PHP_METHOD(sqlite3stmt, readOnly)
01239 {
01240        php_sqlite3_stmt *stmt_obj;
01241        zval *object = getThis();
01242        stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
01243 
01244        if (zend_parse_parameters_none() == FAILURE) {
01245               return;
01246        }
01247 
01248 #if SQLITE_VERSION_NUMBER >= 3007004
01249        if (sqlite3_stmt_readonly(stmt_obj->stmt)) {
01250               RETURN_TRUE;
01251        }
01252 #endif
01253        RETURN_FALSE;
01254 }
01255 /* }}} */
01256 
01257 static int register_bound_parameter_to_sqlite(struct php_sqlite3_bound_param *param, php_sqlite3_stmt *stmt TSRMLS_DC) /* {{{ */
01258 {
01259        HashTable *hash;
01260        hash = stmt->bound_params;
01261 
01262        if (!hash) {
01263               ALLOC_HASHTABLE(hash);
01264               zend_hash_init(hash, 13, NULL, sqlite3_param_dtor, 0);
01265               stmt->bound_params = hash;
01266        }
01267 
01268        /* We need a : prefix to resolve a name to a parameter number */
01269        if (param->name) {
01270               if (param->name[0] != ':') {
01271                      /* pre-increment for character + 1 for null */
01272                      char *temp = emalloc(++param->name_len + 1);
01273                      temp[0] = ':';
01274                      memmove(temp+1, param->name, param->name_len);
01275                      param->name = temp;
01276               } else {
01277                      param->name = estrndup(param->name, param->name_len);
01278               }
01279               /* do lookup*/
01280               param->param_number = sqlite3_bind_parameter_index(stmt->stmt, param->name);
01281        }
01282 
01283        if (param->param_number < 1) {
01284               efree(param->name);
01285               return 0;
01286        }
01287 
01288        if (param->param_number >= 1) {
01289               zend_hash_index_del(hash, param->param_number);
01290        }
01291 
01292        if (param->name) {
01293               zend_hash_update(hash, param->name, param->name_len, param, sizeof(*param), NULL);
01294        } else {
01295               zend_hash_index_update(hash, param->param_number, param, sizeof(*param), NULL);
01296        }
01297 
01298        return 1;
01299 }
01300 /* }}} */
01301 
01302 /* {{{ proto bool SQLite3Stmt::bindParam(int parameter_number, mixed parameter [, int type])
01303    Bind Paramater to a stmt variable. */
01304 PHP_METHOD(sqlite3stmt, bindParam)
01305 {
01306        php_sqlite3_stmt *stmt_obj;
01307        zval *object = getThis();
01308        struct php_sqlite3_bound_param param = {0};
01309        stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
01310 
01311        param.param_number = -1;
01312        param.type = SQLITE3_TEXT;
01313 
01314        if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "lz|l", &param.param_number, &param.parameter, &param.type) == FAILURE) {
01315               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &param.name, &param.name_len, &param.parameter, &param.type) == FAILURE) {
01316                      return;
01317               }
01318        }
01319 
01320        Z_ADDREF_P(param.parameter);
01321 
01322        if (!register_bound_parameter_to_sqlite(&param, stmt_obj TSRMLS_CC)) {
01323               if (param.parameter) {
01324                      zval_ptr_dtor(&(param.parameter));
01325                      param.parameter = NULL;
01326               }
01327               RETURN_FALSE;
01328        }
01329        RETURN_TRUE;
01330 }
01331 /* }}} */
01332 
01333 /* {{{ proto bool SQLite3Stmt::bindValue(int parameter_number, mixed parameter [, int type])
01334    Bind Value of a parameter to a stmt variable. */
01335 PHP_METHOD(sqlite3stmt, bindValue)
01336 {
01337        php_sqlite3_stmt *stmt_obj;
01338        zval *object = getThis();
01339        struct php_sqlite3_bound_param param = {0};
01340        stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
01341 
01342        param.param_number = -1;
01343        param.type = SQLITE3_TEXT;
01344 
01345        if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "lz/|l", &param.param_number, &param.parameter, &param.type) == FAILURE) {
01346               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz/|l", &param.name, &param.name_len, &param.parameter, &param.type) == FAILURE) {
01347                      return;
01348               }
01349        }
01350 
01351        Z_ADDREF_P(param.parameter);
01352 
01353        if (!register_bound_parameter_to_sqlite(&param, stmt_obj TSRMLS_CC)) {
01354               if (param.parameter) {
01355                      zval_ptr_dtor(&(param.parameter));
01356                      param.parameter = NULL;
01357               }
01358               RETURN_FALSE;
01359        }
01360        RETURN_TRUE;
01361 }
01362 /* }}} */
01363 
01364 /* {{{ proto SQLite3Result SQLite3Stmt::execute()
01365    Executes a prepared statement and returns a result set object. */
01366 PHP_METHOD(sqlite3stmt, execute)
01367 {
01368        php_sqlite3_stmt *stmt_obj;
01369        php_sqlite3_result *result;
01370        zval *object = getThis();
01371        int return_code = 0;
01372        struct php_sqlite3_bound_param *param;
01373 
01374        stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
01375 
01376        if (zend_parse_parameters_none() == FAILURE) {
01377               return;
01378        }
01379 
01380        SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3)
01381 
01382        if (stmt_obj->bound_params) {
01383               zend_hash_internal_pointer_reset(stmt_obj->bound_params);
01384               while (zend_hash_get_current_data(stmt_obj->bound_params, (void **)&param) == SUCCESS) {
01385                      /* If the ZVAL is null then it should be bound as that */
01386                      if (Z_TYPE_P(param->parameter) == IS_NULL) {
01387                             sqlite3_bind_null(stmt_obj->stmt, param->param_number);
01388                             zend_hash_move_forward(stmt_obj->bound_params);
01389                             continue;
01390                      }
01391 
01392                      switch (param->type) {
01393                             case SQLITE_INTEGER:
01394                                    convert_to_long(param->parameter);
01395                                    sqlite3_bind_int(stmt_obj->stmt, param->param_number, Z_LVAL_P(param->parameter));
01396                                    break;
01397 
01398                             case SQLITE_FLOAT:
01399                                    /* convert_to_double(param->parameter);*/
01400                                    sqlite3_bind_double(stmt_obj->stmt, param->param_number, Z_DVAL_P(param->parameter));
01401                                    break;
01402 
01403                             case SQLITE_BLOB:
01404                             {
01405                                    php_stream *stream = NULL;
01406                                    int blength;
01407                                    char *buffer = NULL;
01408                                    if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
01409                                           php_stream_from_zval_no_verify(stream, &param->parameter);
01410                                           if (stream == NULL) {
01411                                                  php_sqlite3_error(stmt_obj->db_obj, "Unable to read stream for parameter %ld", param->param_number);
01412                                                  RETURN_FALSE;
01413                                           }
01414                                           blength = php_stream_copy_to_mem(stream, (void *)&buffer, PHP_STREAM_COPY_ALL, 0);
01415                                    } else {
01416                                           convert_to_string(param->parameter);
01417                                           blength =  Z_STRLEN_P(param->parameter);
01418                                           buffer = Z_STRVAL_P(param->parameter);
01419                                    }
01420 
01421                                    sqlite3_bind_blob(stmt_obj->stmt, param->param_number, buffer, blength, SQLITE_TRANSIENT);
01422 
01423                                    if (stream) {
01424                                           pefree(buffer, 0);
01425                                    }
01426                                    break;
01427                             }
01428 
01429                             case SQLITE3_TEXT:
01430                                    convert_to_string(param->parameter);
01431                                    sqlite3_bind_text(stmt_obj->stmt, param->param_number, Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter), SQLITE_STATIC);
01432                                    break;
01433 
01434                             case SQLITE_NULL:
01435                                    sqlite3_bind_null(stmt_obj->stmt, param->param_number);
01436                                    break;
01437 
01438                             default:
01439                                    php_sqlite3_error(stmt_obj->db_obj, "Unknown parameter type: %ld for parameter %ld", param->type, param->param_number);
01440                                    RETURN_FALSE;
01441                      }
01442                      zend_hash_move_forward(stmt_obj->bound_params);
01443               }
01444        }
01445 
01446        return_code = sqlite3_step(stmt_obj->stmt);
01447 
01448        switch (return_code) {
01449               case SQLITE_ROW: /* Valid Row */
01450               case SQLITE_DONE: /* Valid but no results */
01451               {
01452                      sqlite3_reset(stmt_obj->stmt);
01453                      object_init_ex(return_value, php_sqlite3_result_entry);
01454                      result = (php_sqlite3_result *)zend_object_store_get_object(return_value TSRMLS_CC);
01455 
01456                      Z_ADDREF_P(object);
01457        
01458                      result->is_prepared_statement = 1;
01459                      result->db_obj = stmt_obj->db_obj;
01460                      result->stmt_obj = stmt_obj;
01461                      result->stmt_obj_zval = getThis();
01462 
01463                      break;
01464               }
01465               case SQLITE_ERROR:
01466                      sqlite3_reset(stmt_obj->stmt);
01467 
01468               default:
01469                      php_sqlite3_error(stmt_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
01470                      zval_dtor(return_value);
01471                      RETURN_FALSE;
01472        }
01473 
01474        return;
01475 }
01476 /* }}} */
01477 
01478 /* {{{ proto int SQLite3Stmt::__construct(SQLite3 dbobject, String Statement)
01479    __constructor for SQLite3Stmt. */
01480 PHP_METHOD(sqlite3stmt, __construct)
01481 {
01482        php_sqlite3_stmt *stmt_obj;
01483        php_sqlite3_db_object *db_obj;
01484        zval *object = getThis();
01485        zval *db_zval;
01486        char *sql;
01487        int sql_len, errcode;
01488        zend_error_handling error_handling;
01489        php_sqlite3_free_list *free_item;
01490 
01491        stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
01492        zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
01493 
01494        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &db_zval, php_sqlite3_sc_entry, &sql, &sql_len) == FAILURE) {
01495               zend_restore_error_handling(&error_handling TSRMLS_CC);
01496               return;
01497        }
01498 
01499        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(db_zval TSRMLS_CC);
01500 
01501        SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
01502 
01503        zend_restore_error_handling(&error_handling TSRMLS_CC);
01504 
01505        if (!sql_len) {
01506               RETURN_FALSE;
01507        }
01508 
01509        stmt_obj->db_obj = db_obj;
01510        stmt_obj->db_obj_zval = db_zval;
01511 
01512        Z_ADDREF_P(db_zval);
01513        
01514        errcode = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &(stmt_obj->stmt), NULL);
01515        if (errcode != SQLITE_OK) {
01516               php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
01517               zval_dtor(return_value);
01518               RETURN_FALSE;
01519        }
01520        stmt_obj->initialised = 1;
01521 
01522        free_item = emalloc(sizeof(php_sqlite3_free_list));
01523        free_item->stmt_obj = stmt_obj;
01524        free_item->stmt_obj_zval = getThis();
01525 
01526        zend_llist_add_element(&(db_obj->free_list), &free_item);
01527 }
01528 /* }}} */
01529 
01530 /* {{{ proto int SQLite3Result::numColumns()
01531    Number of columns in the result set. */
01532 PHP_METHOD(sqlite3result, numColumns)
01533 {
01534        php_sqlite3_result *result_obj;
01535        zval *object = getThis();
01536        result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
01537 
01538        SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
01539 
01540        if (zend_parse_parameters_none() == FAILURE) {
01541               return;
01542        }
01543 
01544        RETURN_LONG(sqlite3_column_count(result_obj->stmt_obj->stmt));
01545 }
01546 /* }}} */
01547 
01548 /* {{{ proto string SQLite3Result::columnName(int column)
01549    Returns the name of the nth column. */
01550 PHP_METHOD(sqlite3result, columnName)
01551 {
01552        php_sqlite3_result *result_obj;
01553        zval *object = getThis();
01554        long column = 0;
01555        char *column_name;
01556        result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
01557 
01558        SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
01559 
01560        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &column) == FAILURE) {
01561               return;
01562        }
01563        column_name = (char*) sqlite3_column_name(result_obj->stmt_obj->stmt, column);
01564 
01565        if (column_name == NULL) {
01566               RETURN_FALSE;
01567        }
01568               
01569        RETVAL_STRING(column_name, 1);
01570 }
01571 /* }}} */
01572 
01573 /* {{{ proto int SQLite3Result::columnType(int column)
01574    Returns the type of the nth column. */
01575 PHP_METHOD(sqlite3result, columnType)
01576 {
01577        php_sqlite3_result *result_obj;
01578        zval *object = getThis();
01579        long column = 0;
01580        result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
01581 
01582        SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
01583 
01584        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &column) == FAILURE) {
01585               return;
01586        }
01587 
01588        if (result_obj->complete) {
01589               RETURN_FALSE;
01590        }
01591 
01592        RETURN_LONG(sqlite3_column_type(result_obj->stmt_obj->stmt, column));
01593 }
01594 /* }}} */
01595 
01596 /* {{{ proto array SQLite3Result::fetchArray([int mode])
01597    Fetch a result row as both an associative or numerically indexed array or both. */
01598 PHP_METHOD(sqlite3result, fetchArray)
01599 {
01600        php_sqlite3_result *result_obj;
01601        zval *object = getThis();
01602        int i, ret;
01603        long mode = PHP_SQLITE3_BOTH;
01604        result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
01605 
01606        SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
01607 
01608        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &mode) == FAILURE) {
01609               return;
01610        }
01611 
01612        ret = sqlite3_step(result_obj->stmt_obj->stmt);
01613        switch (ret) {
01614               case SQLITE_ROW:
01615                      /* If there was no return value then just skip fetching */
01616                      if (!return_value_used) {
01617                             return;
01618                      }
01619 
01620                      array_init(return_value);
01621 
01622                      for (i = 0; i < sqlite3_data_count(result_obj->stmt_obj->stmt); i++) {
01623                             zval *data;
01624 
01625                             data = sqlite_value_to_zval(result_obj->stmt_obj->stmt, i);
01626 
01627                             if (mode & PHP_SQLITE3_NUM) {
01628                                    add_index_zval(return_value, i, data);
01629                             }
01630 
01631                             if (mode & PHP_SQLITE3_ASSOC) {
01632                                    if (mode & PHP_SQLITE3_NUM) {
01633                                           Z_ADDREF_P(data);
01634                                    }
01635                                    add_assoc_zval(return_value, (char*)sqlite3_column_name(result_obj->stmt_obj->stmt, i), data);
01636                             }
01637                      }
01638                      break;
01639 
01640               case SQLITE_DONE:
01641                      result_obj->complete = 1;
01642                      RETURN_FALSE;
01643                      break;
01644 
01645               default:
01646                      php_sqlite3_error(result_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(result_obj->stmt_obj->stmt)));
01647        }
01648 }
01649 /* }}} */
01650 
01651 /* {{{ proto bool SQLite3Result::reset()
01652    Resets the result set back to the first row. */
01653 PHP_METHOD(sqlite3result, reset)
01654 {
01655        php_sqlite3_result *result_obj;
01656        zval *object = getThis();
01657        result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
01658 
01659        SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
01660 
01661        if (zend_parse_parameters_none() == FAILURE) {
01662               return;
01663        }
01664 
01665        if (sqlite3_reset(result_obj->stmt_obj->stmt) != SQLITE_OK) {
01666               RETURN_FALSE;
01667        }
01668 
01669        result_obj->complete = 0;
01670 
01671        RETURN_TRUE;
01672 }
01673 /* }}} */
01674 
01675 /* {{{ proto bool SQLite3Result::finalize()
01676    Closes the result set. */
01677 PHP_METHOD(sqlite3result, finalize)
01678 {
01679        php_sqlite3_result *result_obj;
01680        zval *object = getThis();
01681        result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
01682 
01683        SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
01684 
01685        if (zend_parse_parameters_none() == FAILURE) {
01686               return;
01687        }
01688 
01689        /* We need to finalize an internal statement */
01690        if (result_obj->is_prepared_statement == 0) {
01691               zend_llist_del_element(&(result_obj->db_obj->free_list), result_obj->stmt_obj_zval,
01692                      (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
01693        } else {
01694               sqlite3_reset(result_obj->stmt_obj->stmt);
01695        }
01696 
01697        RETURN_TRUE;
01698 }
01699 /* }}} */
01700 
01701 /* {{{ proto int SQLite3Result::__construct()
01702    __constructor for SQLite3Result. */
01703 PHP_METHOD(sqlite3result, __construct)
01704 {
01705        zend_throw_exception(zend_exception_get_default(TSRMLS_C), "SQLite3Result cannot be directly instantiated", 0 TSRMLS_CC);
01706 }
01707 /* }}} */
01708 
01709 /* {{{ arginfo */
01710 ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_open, 0)
01711        ZEND_ARG_INFO(0, filename)
01712        ZEND_ARG_INFO(0, flags)
01713        ZEND_ARG_INFO(0, encryption_key)
01714 ZEND_END_ARG_INFO()
01715 
01716 ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_busytimeout, 0)
01717        ZEND_ARG_INFO(0, ms)
01718 ZEND_END_ARG_INFO()
01719 
01720 #ifndef SQLITE_OMIT_LOAD_EXTENSION
01721 ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_loadextension, 0)
01722        ZEND_ARG_INFO(0, shared_library)
01723 ZEND_END_ARG_INFO()
01724 #endif
01725 
01726 ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_escapestring, 0, 0, 1)
01727        ZEND_ARG_INFO(0, value)
01728 ZEND_END_ARG_INFO()
01729 
01730 ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_query, 0, 0, 1)
01731        ZEND_ARG_INFO(0, query)
01732 ZEND_END_ARG_INFO()
01733 
01734 ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_querysingle, 0, 0, 1)
01735        ZEND_ARG_INFO(0, query)
01736        ZEND_ARG_INFO(0, entire_row)
01737 ZEND_END_ARG_INFO()
01738 
01739 ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createfunction, 0, 0, 2)
01740        ZEND_ARG_INFO(0, name)
01741        ZEND_ARG_INFO(0, callback)
01742        ZEND_ARG_INFO(0, argument_count)
01743 ZEND_END_ARG_INFO()
01744 
01745 ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createaggregate, 0, 0, 3)
01746        ZEND_ARG_INFO(0, name)
01747        ZEND_ARG_INFO(0, step_callback)
01748        ZEND_ARG_INFO(0, final_callback)
01749        ZEND_ARG_INFO(0, argument_count)
01750 ZEND_END_ARG_INFO()
01751 
01752 ZEND_BEGIN_ARG_INFO_EX(argingo_sqlite3_openblob, 0, 0, 3)
01753        ZEND_ARG_INFO(0, table)
01754        ZEND_ARG_INFO(0, column)
01755        ZEND_ARG_INFO(0, rowid)
01756        ZEND_ARG_INFO(0, dbname)
01757 ZEND_END_ARG_INFO()
01758 
01759 ZEND_BEGIN_ARG_INFO_EX(argingo_sqlite3_enableexceptions, 0, 0, 1)
01760        ZEND_ARG_INFO(0, enableExceptions)
01761 ZEND_END_ARG_INFO()
01762 
01763 ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3stmt_bindparam, 0, 0, 2)
01764        ZEND_ARG_INFO(0, param_number)
01765        ZEND_ARG_INFO(1, param)
01766        ZEND_ARG_INFO(0, type)
01767 ZEND_END_ARG_INFO()
01768 
01769 ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3stmt_bindvalue, 0, 0, 2)
01770        ZEND_ARG_INFO(0, param_number)
01771        ZEND_ARG_INFO(0, param)
01772        ZEND_ARG_INFO(0, type)
01773 ZEND_END_ARG_INFO()
01774 
01775 ZEND_BEGIN_ARG_INFO(arginfo_sqlite3stmt_construct, 1)
01776        ZEND_ARG_INFO(0, sqlite3)
01777 ZEND_END_ARG_INFO()
01778 
01779 ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_columnname, 0, 0, 1)
01780        ZEND_ARG_INFO(0, column_number)
01781 ZEND_END_ARG_INFO()
01782 
01783 ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_columntype, 0, 0, 1)
01784        ZEND_ARG_INFO(0, column_number)
01785 ZEND_END_ARG_INFO()
01786 
01787 ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_fetcharray, 0, 0, 1)
01788        ZEND_ARG_INFO(0, mode)
01789 ZEND_END_ARG_INFO()
01790 
01791 ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_void, 0)
01792 ZEND_END_ARG_INFO()
01793 /* }}} */
01794 
01795 /* {{{ php_sqlite3_class_methods */
01796 static zend_function_entry php_sqlite3_class_methods[] = {
01797        PHP_ME(sqlite3,             open,                       arginfo_sqlite3_open, ZEND_ACC_PUBLIC)
01798        PHP_ME(sqlite3,             close,                      arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
01799        PHP_ME(sqlite3,             exec,                       arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
01800        PHP_ME(sqlite3,             version,                    arginfo_sqlite3_void, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
01801        PHP_ME(sqlite3,             lastInsertRowID,     arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
01802        PHP_ME(sqlite3,             lastErrorCode,              arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
01803        PHP_ME(sqlite3,             lastErrorMsg,        arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
01804        PHP_ME(sqlite3,             busyTimeout,         arginfo_sqlite3_busytimeout, ZEND_ACC_PUBLIC)
01805 #ifndef SQLITE_OMIT_LOAD_EXTENSION
01806        PHP_ME(sqlite3,             loadExtension,              arginfo_sqlite3_loadextension, ZEND_ACC_PUBLIC)
01807 #endif
01808        PHP_ME(sqlite3,             changes,                    arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
01809        PHP_ME(sqlite3,             escapeString,        arginfo_sqlite3_escapestring, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
01810        PHP_ME(sqlite3,             prepare,                    arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
01811        PHP_ME(sqlite3,             query,                      arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
01812        PHP_ME(sqlite3,             querySingle,         arginfo_sqlite3_querysingle, ZEND_ACC_PUBLIC)
01813        PHP_ME(sqlite3,             createFunction,             arginfo_sqlite3_createfunction, ZEND_ACC_PUBLIC)
01814        PHP_ME(sqlite3,             createAggregate,     arginfo_sqlite3_createaggregate, ZEND_ACC_PUBLIC)
01815        PHP_ME(sqlite3,             openBlob,                   argingo_sqlite3_openblob, ZEND_ACC_PUBLIC)
01816        PHP_ME(sqlite3,             enableExceptions,    argingo_sqlite3_enableexceptions, ZEND_ACC_PUBLIC)
01817        /* Aliases */
01818        PHP_MALIAS(sqlite3,  __construct, open, arginfo_sqlite3_open, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
01819        PHP_FE_END
01820 };
01821 /* }}} */
01822 
01823 /* {{{ php_sqlite3_stmt_class_methods */
01824 static zend_function_entry php_sqlite3_stmt_class_methods[] = {
01825        PHP_ME(sqlite3stmt, paramCount,    arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
01826        PHP_ME(sqlite3stmt, close,         arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
01827        PHP_ME(sqlite3stmt, reset,         arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
01828        PHP_ME(sqlite3stmt, clear,         arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
01829        PHP_ME(sqlite3stmt, execute,       arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
01830        PHP_ME(sqlite3stmt, bindParam,     arginfo_sqlite3stmt_bindparam, ZEND_ACC_PUBLIC)
01831        PHP_ME(sqlite3stmt, bindValue,     arginfo_sqlite3stmt_bindvalue, ZEND_ACC_PUBLIC)
01832        PHP_ME(sqlite3stmt, readOnly,      arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
01833        PHP_ME(sqlite3stmt, __construct, arginfo_sqlite3stmt_construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
01834        PHP_FE_END
01835 };
01836 /* }}} */
01837 
01838 /* {{{ php_sqlite3_result_class_methods */
01839 static zend_function_entry php_sqlite3_result_class_methods[] = {
01840        PHP_ME(sqlite3result, numColumns,         arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
01841        PHP_ME(sqlite3result, columnName,         arginfo_sqlite3result_columnname, ZEND_ACC_PUBLIC)
01842        PHP_ME(sqlite3result, columnType,         arginfo_sqlite3result_columntype, ZEND_ACC_PUBLIC)
01843        PHP_ME(sqlite3result, fetchArray,         arginfo_sqlite3result_fetcharray, ZEND_ACC_PUBLIC)
01844        PHP_ME(sqlite3result, reset,                     arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
01845        PHP_ME(sqlite3result, finalize,                  arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
01846        PHP_ME(sqlite3result, __construct,               arginfo_sqlite3_void, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
01847        PHP_FE_END
01848 };
01849 /* }}} */
01850 
01851 /* {{{ Authorization Callback 
01852 */
01853 static int php_sqlite3_authorizer(void *autharg, int access_type, const char *arg3, const char *arg4, const char *arg5, const char *arg6)
01854 {
01855        switch (access_type) {
01856               case SQLITE_ATTACH:
01857               {
01858                      if (strncmp(arg3, ":memory:", sizeof(":memory:")-1) && *arg3) {
01859                             TSRMLS_FETCH();
01860 
01861 #if PHP_API_VERSION < 20100412
01862                             if (PG(safe_mode) && (!php_checkuid(arg3, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
01863                                    return SQLITE_DENY;
01864                             }
01865 #endif
01866 
01867                             if (php_check_open_basedir(arg3 TSRMLS_CC)) {
01868                                    return SQLITE_DENY;
01869                             }
01870                      }
01871                      return SQLITE_OK;
01872               }
01873 
01874               default:
01875                      /* access allowed */
01876                      return SQLITE_OK;
01877        }
01878 }
01879 /* }}} */
01880 
01881 /* {{{ php_sqlite3_free_list_dtor
01882 */
01883 static void php_sqlite3_free_list_dtor(void **item)
01884 {
01885        php_sqlite3_free_list *free_item = (php_sqlite3_free_list *)*item;
01886 
01887        if (free_item->stmt_obj && free_item->stmt_obj->initialised) {
01888               sqlite3_finalize(free_item->stmt_obj->stmt);
01889               free_item->stmt_obj->initialised = 0;
01890        }
01891        efree(*item);
01892 }
01893 /* }}} */
01894 
01895 static int php_sqlite3_compare_stmt_zval_free( php_sqlite3_free_list **free_list, zval *statement ) /* {{{ */
01896 {
01897        return ((*free_list)->stmt_obj->initialised && statement == (*free_list)->stmt_obj_zval);
01898 }
01899 /* }}} */
01900 
01901 static int php_sqlite3_compare_stmt_free( php_sqlite3_free_list **free_list, sqlite3_stmt *statement ) /* {{{ */
01902 {
01903        return ((*free_list)->stmt_obj->initialised && statement == (*free_list)->stmt_obj->stmt);
01904 }
01905 /* }}} */
01906 
01907 static void php_sqlite3_object_free_storage(void *object TSRMLS_DC) /* {{{ */
01908 {
01909        php_sqlite3_db_object *intern = (php_sqlite3_db_object *)object;
01910        php_sqlite3_func *func;
01911 
01912        if (!intern) {
01913               return;
01914        }
01915 
01916        while (intern->funcs) {
01917               func = intern->funcs;
01918               intern->funcs = func->next;
01919               if (intern->initialised && intern->db) {
01920                      sqlite3_create_function(intern->db, func->func_name, func->argc, SQLITE_UTF8, func, NULL, NULL, NULL);
01921               }
01922 
01923               efree((char*)func->func_name);
01924 
01925               if (func->func) {
01926                      zval_ptr_dtor(&func->func);
01927               }
01928               if (func->step) {
01929                      zval_ptr_dtor(&func->step);
01930               }
01931               if (func->fini) {
01932                      zval_ptr_dtor(&func->fini);
01933               }
01934               efree(func);
01935        }
01936 
01937        if (intern->initialised && intern->db) {
01938               sqlite3_close(intern->db);
01939               intern->initialised = 0;
01940        }
01941 
01942        zend_object_std_dtor(&intern->zo TSRMLS_CC);
01943        efree(intern);
01944 }
01945 /* }}} */
01946 
01947 static void php_sqlite3_stmt_object_free_storage(void *object TSRMLS_DC) /* {{{ */
01948 {
01949        php_sqlite3_stmt *intern = (php_sqlite3_stmt *)object;
01950 
01951        if (!intern) {
01952               return;
01953        }
01954 
01955        if (intern->bound_params) {
01956               zend_hash_destroy(intern->bound_params);
01957               FREE_HASHTABLE(intern->bound_params);
01958               intern->bound_params = NULL;
01959        }
01960 
01961        if (intern->initialised) {
01962               zend_llist_del_element(&(intern->db_obj->free_list), intern->stmt,
01963                      (int (*)(void *, void *)) php_sqlite3_compare_stmt_free);
01964        }
01965 
01966        if (intern->db_obj_zval) {
01967               zval_ptr_dtor(&intern->db_obj_zval);
01968        }
01969 
01970        zend_object_std_dtor(&intern->zo TSRMLS_CC);
01971        efree(intern);
01972 }
01973 /* }}} */
01974 
01975 static void php_sqlite3_result_object_free_storage(void *object TSRMLS_DC) /* {{{ */
01976 {
01977        php_sqlite3_result *intern = (php_sqlite3_result *)object;
01978 
01979        if (!intern) {
01980               return;
01981        }
01982 
01983        if (intern->stmt_obj_zval) {
01984               if (intern->stmt_obj->initialised) {
01985                      sqlite3_reset(intern->stmt_obj->stmt);
01986               }
01987 
01988               if (intern->is_prepared_statement == 0) {
01989                      zval_dtor(intern->stmt_obj_zval);
01990                      FREE_ZVAL(intern->stmt_obj_zval);
01991               } else {
01992                      zval_ptr_dtor(&intern->stmt_obj_zval);
01993               }
01994        }
01995 
01996        zend_object_std_dtor(&intern->zo TSRMLS_CC);
01997        efree(intern);
01998 }
01999 /* }}} */
02000 
02001 static zend_object_value php_sqlite3_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
02002 {
02003        zval *tmp;
02004        zend_object_value retval;
02005        php_sqlite3_db_object *intern;
02006 
02007        /* Allocate memory for it */
02008        intern = emalloc(sizeof(php_sqlite3_db_object));
02009        memset(intern, 0, sizeof(php_sqlite3_db_object));
02010        intern->exception = 0;
02011 
02012        /* Need to keep track of things to free */
02013        zend_llist_init(&(intern->free_list),   sizeof(php_sqlite3_free_list *), (llist_dtor_func_t)php_sqlite3_free_list_dtor, 0);
02014 
02015        zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
02016        zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref,(void *) &tmp, sizeof(zval *));
02017 
02018        retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_sqlite3_object_free_storage, NULL TSRMLS_CC);
02019        retval.handlers = (zend_object_handlers *) &sqlite3_object_handlers;
02020 
02021        return retval;
02022 }
02023 /* }}} */
02024 
02025 static zend_object_value php_sqlite3_stmt_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
02026 {
02027        zval *tmp;
02028        zend_object_value retval;
02029        php_sqlite3_stmt *intern;
02030 
02031        /* Allocate memory for it */
02032        intern = emalloc(sizeof(php_sqlite3_stmt));
02033        memset(intern, 0, sizeof(php_sqlite3_stmt));
02034 
02035        intern->db_obj_zval = NULL;
02036 
02037        zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
02038        zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref,(void *) &tmp, sizeof(zval *));
02039 
02040        retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_sqlite3_stmt_object_free_storage, NULL TSRMLS_CC);
02041        retval.handlers = (zend_object_handlers *) &sqlite3_stmt_object_handlers;
02042 
02043        return retval;
02044 }
02045 /* }}} */
02046 
02047 static zend_object_value php_sqlite3_result_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
02048 {
02049        zval *tmp;
02050        zend_object_value retval;
02051        php_sqlite3_result *intern;
02052 
02053        /* Allocate memory for it */
02054        intern = emalloc(sizeof(php_sqlite3_result));
02055        memset(intern, 0, sizeof(php_sqlite3_result));
02056 
02057        intern->complete = 0;
02058        intern->is_prepared_statement = 0;
02059        intern->stmt_obj_zval = NULL;
02060 
02061        zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
02062        zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref,(void *) &tmp, sizeof(zval *));
02063 
02064        retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_sqlite3_result_object_free_storage, NULL TSRMLS_CC);
02065        retval.handlers = (zend_object_handlers *) &sqlite3_result_object_handlers;
02066 
02067        return retval;
02068 }
02069 /* }}} */
02070 
02071 static void sqlite3_param_dtor(void *data) /* {{{ */
02072 {
02073        struct php_sqlite3_bound_param *param = (struct php_sqlite3_bound_param*)data;
02074 
02075        if (param->name) {
02076               efree(param->name);
02077        }
02078 
02079        if (param->parameter) {
02080               zval_ptr_dtor(&(param->parameter));
02081               param->parameter = NULL;
02082        }
02083 }
02084 /* }}} */
02085 
02086 /* {{{ PHP_MINIT_FUNCTION
02087 */
02088 PHP_MINIT_FUNCTION(sqlite3)
02089 {
02090        zend_class_entry ce;
02091 
02092 #if defined(ZTS)
02093        /* Refuse to load if this wasn't a threasafe library loaded */
02094        if (!sqlite3_threadsafe()) {
02095               php_error_docref(NULL TSRMLS_CC, E_WARNING, "A thread safe version of SQLite is required when using a thread safe version of PHP.");
02096               return FAILURE;
02097        }
02098 #endif
02099 
02100        memcpy(&sqlite3_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
02101        memcpy(&sqlite3_stmt_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
02102        memcpy(&sqlite3_result_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
02103 
02104        /* Register SQLite 3 Class */
02105        INIT_CLASS_ENTRY(ce, "SQLite3", php_sqlite3_class_methods);
02106        ce.create_object = php_sqlite3_object_new;
02107        sqlite3_object_handlers.clone_obj = NULL;
02108        php_sqlite3_sc_entry = zend_register_internal_class(&ce TSRMLS_CC);
02109 
02110        /* Register SQLite 3 Prepared Statement Class */
02111        INIT_CLASS_ENTRY(ce, "SQLite3Stmt", php_sqlite3_stmt_class_methods);
02112        ce.create_object = php_sqlite3_stmt_object_new;
02113        sqlite3_stmt_object_handlers.clone_obj = NULL;
02114        php_sqlite3_stmt_entry = zend_register_internal_class(&ce TSRMLS_CC);
02115 
02116        /* Register SQLite 3 Result Class */
02117        INIT_CLASS_ENTRY(ce, "SQLite3Result", php_sqlite3_result_class_methods);
02118        ce.create_object = php_sqlite3_result_object_new;
02119        sqlite3_result_object_handlers.clone_obj = NULL;
02120        php_sqlite3_result_entry = zend_register_internal_class(&ce TSRMLS_CC);
02121 
02122        REGISTER_INI_ENTRIES();
02123 
02124        REGISTER_LONG_CONSTANT("SQLITE3_ASSOC", PHP_SQLITE3_ASSOC, CONST_CS | CONST_PERSISTENT);
02125        REGISTER_LONG_CONSTANT("SQLITE3_NUM", PHP_SQLITE3_NUM, CONST_CS | CONST_PERSISTENT);
02126        REGISTER_LONG_CONSTANT("SQLITE3_BOTH", PHP_SQLITE3_BOTH, CONST_CS | CONST_PERSISTENT);
02127 
02128        REGISTER_LONG_CONSTANT("SQLITE3_INTEGER", SQLITE_INTEGER, CONST_CS | CONST_PERSISTENT);
02129        REGISTER_LONG_CONSTANT("SQLITE3_FLOAT", SQLITE_FLOAT, CONST_CS | CONST_PERSISTENT);
02130        REGISTER_LONG_CONSTANT("SQLITE3_TEXT", SQLITE3_TEXT, CONST_CS | CONST_PERSISTENT);
02131        REGISTER_LONG_CONSTANT("SQLITE3_BLOB", SQLITE_BLOB, CONST_CS | CONST_PERSISTENT);
02132        REGISTER_LONG_CONSTANT("SQLITE3_NULL", SQLITE_NULL, CONST_CS | CONST_PERSISTENT);
02133 
02134        REGISTER_LONG_CONSTANT("SQLITE3_OPEN_READONLY", SQLITE_OPEN_READONLY, CONST_CS | CONST_PERSISTENT);
02135        REGISTER_LONG_CONSTANT("SQLITE3_OPEN_READWRITE", SQLITE_OPEN_READWRITE, CONST_CS | CONST_PERSISTENT);
02136        REGISTER_LONG_CONSTANT("SQLITE3_OPEN_CREATE", SQLITE_OPEN_CREATE, CONST_CS | CONST_PERSISTENT);
02137 
02138        return SUCCESS;
02139 }
02140 /* }}} */
02141 
02142 /* {{{ PHP_MSHUTDOWN_FUNCTION
02143 */
02144 PHP_MSHUTDOWN_FUNCTION(sqlite3)
02145 {
02146        UNREGISTER_INI_ENTRIES();
02147 
02148        return SUCCESS;
02149 }
02150 /* }}} */
02151 
02152 /* {{{ PHP_MINFO_FUNCTION
02153 */
02154 PHP_MINFO_FUNCTION(sqlite3)
02155 {
02156        php_info_print_table_start();
02157        php_info_print_table_header(2, "SQLite3 support", "enabled");
02158        php_info_print_table_row(2, "SQLite3 module version", PHP_SQLITE3_VERSION);
02159        php_info_print_table_row(2, "SQLite Library", sqlite3_libversion());
02160        php_info_print_table_end();
02161 
02162        DISPLAY_INI_ENTRIES();
02163 }
02164 /* }}} */
02165 
02166 /* {{{ PHP_GINIT_FUNCTION
02167 */
02168 static PHP_GINIT_FUNCTION(sqlite3)
02169 {
02170        memset(sqlite3_globals, 0, sizeof(*sqlite3_globals));
02171 }
02172 /* }}} */
02173 
02174 /* {{{ sqlite3_module_entry
02175 */
02176 zend_module_entry sqlite3_module_entry = {
02177        STANDARD_MODULE_HEADER,
02178        "sqlite3",
02179        NULL,
02180        PHP_MINIT(sqlite3),
02181        PHP_MSHUTDOWN(sqlite3),
02182        NULL,
02183        NULL,
02184        PHP_MINFO(sqlite3),
02185        PHP_SQLITE3_VERSION,
02186        PHP_MODULE_GLOBALS(sqlite3),
02187        PHP_GINIT(sqlite3),
02188        NULL,
02189        NULL,
02190        STANDARD_MODULE_PROPERTIES_EX
02191 };
02192 /* }}} */
02193 
02194 #ifdef COMPILE_DL_SQLITE3
02195 ZEND_GET_MODULE(sqlite3)
02196 #endif
02197 
02198 /*
02199  * Local variables:
02200  * tab-width: 4
02201  * c-basic-offset: 4
02202  * End:
02203  * vim600: sw=4 ts=4 fdm=marker
02204  * vim<600: sw=4 ts=4
02205  */