Annotation of embedaddon/php/ext/sqlite3/sqlite3.c, revision 1.1.1.2

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>