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