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    if (zend_parse_parameters_none() == FAILURE) {
1292        return;
1293    }
1294
1295    SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
1296
1297    RETURN_LONG(sqlite3_bind_parameter_count(stmt_obj->stmt));
1298}
1299/* }}} */
1300
1301/* {{{ proto bool SQLite3Stmt::close()
1302   Closes the prepared statement. */
1303PHP_METHOD(sqlite3stmt, close)
1304{
1305    php_sqlite3_stmt *stmt_obj;
1306    zval *object = getThis();
1307    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1308
1309    if (zend_parse_parameters_none() == FAILURE) {
1310        return;
1311    }
1312
1313    if(stmt_obj->db_obj) {
1314            zend_llist_del_element(&(stmt_obj->db_obj->free_list), object, (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
1315    }
1316
1317    RETURN_TRUE;
1318}
1319/* }}} */
1320
1321/* {{{ proto bool SQLite3Stmt::reset()
1322   Reset the prepared statement to the state before it was executed, bindings still remain. */
1323PHP_METHOD(sqlite3stmt, reset)
1324{
1325    php_sqlite3_stmt *stmt_obj;
1326    zval *object = getThis();
1327    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1328
1329    if (zend_parse_parameters_none() == FAILURE) {
1330        return;
1331    }
1332
1333    SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
1334
1335    if (sqlite3_reset(stmt_obj->stmt) != SQLITE_OK) {
1336        php_sqlite3_error(stmt_obj->db_obj, "Unable to reset statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
1337        RETURN_FALSE;
1338    }
1339    RETURN_TRUE;
1340}
1341/* }}} */
1342
1343/* {{{ proto bool SQLite3Stmt::clear()
1344   Clear all current bound parameters. */
1345PHP_METHOD(sqlite3stmt, clear)
1346{
1347    php_sqlite3_stmt *stmt_obj;
1348    zval *object = getThis();
1349    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1350
1351    if (zend_parse_parameters_none() == FAILURE) {
1352        return;
1353    }
1354
1355    SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
1356
1357    if (sqlite3_clear_bindings(stmt_obj->stmt) != SQLITE_OK) {
1358        php_sqlite3_error(stmt_obj->db_obj, "Unable to clear statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
1359        RETURN_FALSE;
1360    }
1361
1362    RETURN_TRUE;
1363}
1364/* }}} */
1365
1366/* {{{ proto bool SQLite3Stmt::readOnly()
1367   Returns true if a statement is definitely read only */
1368PHP_METHOD(sqlite3stmt, readOnly)
1369{
1370    php_sqlite3_stmt *stmt_obj;
1371    zval *object = getThis();
1372    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1373
1374    if (zend_parse_parameters_none() == FAILURE) {
1375        return;
1376    }
1377
1378    SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
1379
1380#if SQLITE_VERSION_NUMBER >= 3007004
1381    if (sqlite3_stmt_readonly(stmt_obj->stmt)) {
1382        RETURN_TRUE;
1383    }
1384#endif
1385    RETURN_FALSE;
1386}
1387/* }}} */
1388
1389static int register_bound_parameter_to_sqlite(struct php_sqlite3_bound_param *param, php_sqlite3_stmt *stmt TSRMLS_DC) /* {{{ */
1390{
1391    HashTable *hash;
1392    hash = stmt->bound_params;
1393
1394    if (!hash) {
1395        ALLOC_HASHTABLE(hash);
1396        zend_hash_init(hash, 13, NULL, sqlite3_param_dtor, 0);
1397        stmt->bound_params = hash;
1398    }
1399
1400    /* We need a : prefix to resolve a name to a parameter number */
1401    if (param->name) {
1402        if (param->name[0] != ':') {
1403            /* pre-increment for character + 1 for null */
1404            char *temp = emalloc(++param->name_len + 1);
1405            temp[0] = ':';
1406            memmove(temp+1, param->name, param->name_len);
1407            param->name = temp;
1408        } else {
1409            param->name = estrndup(param->name, param->name_len);
1410        }
1411        /* do lookup*/
1412        param->param_number = sqlite3_bind_parameter_index(stmt->stmt, param->name);
1413    }
1414
1415    if (param->param_number < 1) {
1416        efree(param->name);
1417        return 0;
1418    }
1419
1420    if (param->param_number >= 1) {
1421        zend_hash_index_del(hash, param->param_number);
1422    }
1423
1424    if (param->name) {
1425        zend_hash_update(hash, param->name, param->name_len, param, sizeof(*param), NULL);
1426    } else {
1427        zend_hash_index_update(hash, param->param_number, param, sizeof(*param), NULL);
1428    }
1429
1430    return 1;
1431}
1432/* }}} */
1433
1434/* {{{ proto bool SQLite3Stmt::bindParam(int parameter_number, mixed parameter [, int type])
1435   Bind Parameter to a stmt variable. */
1436PHP_METHOD(sqlite3stmt, bindParam)
1437{
1438    php_sqlite3_stmt *stmt_obj;
1439    zval *object = getThis();
1440    struct php_sqlite3_bound_param param = {0};
1441    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1442
1443    param.param_number = -1;
1444    param.type = SQLITE3_TEXT;
1445
1446    if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "lz|l", &param.param_number, &param.parameter, &param.type) == FAILURE) {
1447        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &param.name, &param.name_len, &param.parameter, &param.type) == FAILURE) {
1448            return;
1449        }
1450    }
1451
1452    SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
1453
1454    Z_ADDREF_P(param.parameter);
1455
1456    if (!register_bound_parameter_to_sqlite(&param, stmt_obj TSRMLS_CC)) {
1457        if (param.parameter) {
1458            zval_ptr_dtor(&(param.parameter));
1459            param.parameter = NULL;
1460        }
1461        RETURN_FALSE;
1462    }
1463    RETURN_TRUE;
1464}
1465/* }}} */
1466
1467/* {{{ proto bool SQLite3Stmt::bindValue(int parameter_number, mixed parameter [, int type])
1468   Bind Value of a parameter to a stmt variable. */
1469PHP_METHOD(sqlite3stmt, bindValue)
1470{
1471    php_sqlite3_stmt *stmt_obj;
1472    zval *object = getThis();
1473    struct php_sqlite3_bound_param param = {0};
1474    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1475
1476    param.param_number = -1;
1477    param.type = SQLITE3_TEXT;
1478
1479    if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "lz/|l", &param.param_number, &param.parameter, &param.type) == FAILURE) {
1480        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz/|l", &param.name, &param.name_len, &param.parameter, &param.type) == FAILURE) {
1481            return;
1482        }
1483    }
1484
1485    SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
1486
1487    Z_ADDREF_P(param.parameter);
1488
1489    if (!register_bound_parameter_to_sqlite(&param, stmt_obj TSRMLS_CC)) {
1490        if (param.parameter) {
1491            zval_ptr_dtor(&(param.parameter));
1492            param.parameter = NULL;
1493        }
1494        RETURN_FALSE;
1495    }
1496    RETURN_TRUE;
1497}
1498/* }}} */
1499
1500/* {{{ proto SQLite3Result SQLite3Stmt::execute()
1501   Executes a prepared statement and returns a result set object. */
1502PHP_METHOD(sqlite3stmt, execute)
1503{
1504    php_sqlite3_stmt *stmt_obj;
1505    php_sqlite3_result *result;
1506    zval *object = getThis();
1507    int return_code = 0;
1508    struct php_sqlite3_bound_param *param;
1509
1510    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1511
1512    if (zend_parse_parameters_none() == FAILURE) {
1513        return;
1514    }
1515
1516    SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3)
1517
1518    if (stmt_obj->bound_params) {
1519        zend_hash_internal_pointer_reset(stmt_obj->bound_params);
1520        while (zend_hash_get_current_data(stmt_obj->bound_params, (void **)&param) == SUCCESS) {
1521            /* If the ZVAL is null then it should be bound as that */
1522            if (Z_TYPE_P(param->parameter) == IS_NULL) {
1523                sqlite3_bind_null(stmt_obj->stmt, param->param_number);
1524                zend_hash_move_forward(stmt_obj->bound_params);
1525                continue;
1526            }
1527
1528            switch (param->type) {
1529                case SQLITE_INTEGER:
1530                    convert_to_long(param->parameter);
1531#if LONG_MAX > 2147483647
1532                    sqlite3_bind_int64(stmt_obj->stmt, param->param_number, Z_LVAL_P(param->parameter));
1533#else
1534                    sqlite3_bind_int(stmt_obj->stmt, param->param_number, Z_LVAL_P(param->parameter));
1535#endif
1536                    break;
1537
1538                case SQLITE_FLOAT:
1539                    /* convert_to_double(param->parameter);*/
1540                    sqlite3_bind_double(stmt_obj->stmt, param->param_number, Z_DVAL_P(param->parameter));
1541                    break;
1542
1543                case SQLITE_BLOB:
1544                {
1545                    php_stream *stream = NULL;
1546                    int blength;
1547                    char *buffer = NULL;
1548                    if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
1549                        php_stream_from_zval_no_verify(stream, &param->parameter);
1550                        if (stream == NULL) {
1551                            php_sqlite3_error(stmt_obj->db_obj, "Unable to read stream for parameter %ld", param->param_number);
1552                            RETURN_FALSE;
1553                        }
1554                        blength = php_stream_copy_to_mem(stream, (void *)&buffer, PHP_STREAM_COPY_ALL, 0);
1555                    } else {
1556                        convert_to_string(param->parameter);
1557                        blength =  Z_STRLEN_P(param->parameter);
1558                        buffer = Z_STRVAL_P(param->parameter);
1559                    }
1560
1561                    sqlite3_bind_blob(stmt_obj->stmt, param->param_number, buffer, blength, SQLITE_TRANSIENT);
1562
1563                    if (stream) {
1564                        pefree(buffer, 0);
1565                    }
1566                    break;
1567                }
1568
1569                case SQLITE3_TEXT:
1570                    convert_to_string(param->parameter);
1571                    sqlite3_bind_text(stmt_obj->stmt, param->param_number, Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter), SQLITE_STATIC);
1572                    break;
1573
1574                case SQLITE_NULL:
1575                    sqlite3_bind_null(stmt_obj->stmt, param->param_number);
1576                    break;
1577
1578                default:
1579                    php_sqlite3_error(stmt_obj->db_obj, "Unknown parameter type: %ld for parameter %ld", param->type, param->param_number);
1580                    RETURN_FALSE;
1581            }
1582            zend_hash_move_forward(stmt_obj->bound_params);
1583        }
1584    }
1585
1586    return_code = sqlite3_step(stmt_obj->stmt);
1587
1588    switch (return_code) {
1589        case SQLITE_ROW: /* Valid Row */
1590        case SQLITE_DONE: /* Valid but no results */
1591        {
1592            sqlite3_reset(stmt_obj->stmt);
1593            object_init_ex(return_value, php_sqlite3_result_entry);
1594            result = (php_sqlite3_result *)zend_object_store_get_object(return_value TSRMLS_CC);
1595
1596            Z_ADDREF_P(object);
1597
1598            result->is_prepared_statement = 1;
1599            result->db_obj = stmt_obj->db_obj;
1600            result->stmt_obj = stmt_obj;
1601            result->stmt_obj_zval = getThis();
1602
1603            break;
1604        }
1605        case SQLITE_ERROR:
1606            sqlite3_reset(stmt_obj->stmt);
1607
1608        default:
1609            php_sqlite3_error(stmt_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
1610            zval_dtor(return_value);
1611            RETURN_FALSE;
1612    }
1613
1614    return;
1615}
1616/* }}} */
1617
1618/* {{{ proto int SQLite3Stmt::__construct(SQLite3 dbobject, String Statement)
1619   __constructor for SQLite3Stmt. */
1620PHP_METHOD(sqlite3stmt, __construct)
1621{
1622    php_sqlite3_stmt *stmt_obj;
1623    php_sqlite3_db_object *db_obj;
1624    zval *object = getThis();
1625    zval *db_zval;
1626    char *sql;
1627    int sql_len, errcode;
1628    zend_error_handling error_handling;
1629    php_sqlite3_free_list *free_item;
1630
1631    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1632    zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
1633
1634    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &db_zval, php_sqlite3_sc_entry, &sql, &sql_len) == FAILURE) {
1635        zend_restore_error_handling(&error_handling TSRMLS_CC);
1636        return;
1637    }
1638
1639    db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(db_zval TSRMLS_CC);
1640
1641    SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
1642
1643    zend_restore_error_handling(&error_handling TSRMLS_CC);
1644
1645    if (!sql_len) {
1646        RETURN_FALSE;
1647    }
1648
1649    stmt_obj->db_obj = db_obj;
1650    stmt_obj->db_obj_zval = db_zval;
1651
1652    Z_ADDREF_P(db_zval);
1653
1654    errcode = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &(stmt_obj->stmt), NULL);
1655    if (errcode != SQLITE_OK) {
1656        php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
1657        zval_dtor(return_value);
1658        RETURN_FALSE;
1659    }
1660    stmt_obj->initialised = 1;
1661
1662    free_item = emalloc(sizeof(php_sqlite3_free_list));
1663    free_item->stmt_obj = stmt_obj;
1664    free_item->stmt_obj_zval = getThis();
1665
1666    zend_llist_add_element(&(db_obj->free_list), &free_item);
1667}
1668/* }}} */
1669
1670/* {{{ proto int SQLite3Result::numColumns()
1671   Number of columns in the result set. */
1672PHP_METHOD(sqlite3result, numColumns)
1673{
1674    php_sqlite3_result *result_obj;
1675    zval *object = getThis();
1676    result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
1677
1678    SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
1679
1680    if (zend_parse_parameters_none() == FAILURE) {
1681        return;
1682    }
1683
1684    RETURN_LONG(sqlite3_column_count(result_obj->stmt_obj->stmt));
1685}
1686/* }}} */
1687
1688/* {{{ proto string SQLite3Result::columnName(int column)
1689   Returns the name of the nth column. */
1690PHP_METHOD(sqlite3result, columnName)
1691{
1692    php_sqlite3_result *result_obj;
1693    zval *object = getThis();
1694    long column = 0;
1695    char *column_name;
1696    result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
1697
1698    SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
1699
1700    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &column) == FAILURE) {
1701        return;
1702    }
1703    column_name = (char*) sqlite3_column_name(result_obj->stmt_obj->stmt, column);
1704
1705    if (column_name == NULL) {
1706        RETURN_FALSE;
1707    }
1708
1709    RETVAL_STRING(column_name, 1);
1710}
1711/* }}} */
1712
1713/* {{{ proto int SQLite3Result::columnType(int column)
1714   Returns the type of the nth column. */
1715PHP_METHOD(sqlite3result, columnType)
1716{
1717    php_sqlite3_result *result_obj;
1718    zval *object = getThis();
1719    long column = 0;
1720    result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
1721
1722    SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
1723
1724    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &column) == FAILURE) {
1725        return;
1726    }
1727
1728    if (result_obj->complete) {
1729        RETURN_FALSE;
1730    }
1731
1732    RETURN_LONG(sqlite3_column_type(result_obj->stmt_obj->stmt, column));
1733}
1734/* }}} */
1735
1736/* {{{ proto array SQLite3Result::fetchArray([int mode])
1737   Fetch a result row as both an associative or numerically indexed array or both. */
1738PHP_METHOD(sqlite3result, fetchArray)
1739{
1740    php_sqlite3_result *result_obj;
1741    zval *object = getThis();
1742    int i, ret;
1743    long mode = PHP_SQLITE3_BOTH;
1744    result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
1745
1746    SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
1747
1748    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &mode) == FAILURE) {
1749        return;
1750    }
1751
1752    ret = sqlite3_step(result_obj->stmt_obj->stmt);
1753    switch (ret) {
1754        case SQLITE_ROW:
1755            /* If there was no return value then just skip fetching */
1756            if (!return_value_used) {
1757                return;
1758            }
1759
1760            array_init(return_value);
1761
1762            for (i = 0; i < sqlite3_data_count(result_obj->stmt_obj->stmt); i++) {
1763                zval *data;
1764
1765                data = sqlite_value_to_zval(result_obj->stmt_obj->stmt, i);
1766
1767                if (mode & PHP_SQLITE3_NUM) {
1768                    add_index_zval(return_value, i, data);
1769                }
1770
1771                if (mode & PHP_SQLITE3_ASSOC) {
1772                    if (mode & PHP_SQLITE3_NUM) {
1773                        Z_ADDREF_P(data);
1774                    }
1775                    add_assoc_zval(return_value, (char*)sqlite3_column_name(result_obj->stmt_obj->stmt, i), data);
1776                }
1777            }
1778            break;
1779
1780        case SQLITE_DONE:
1781            result_obj->complete = 1;
1782            RETURN_FALSE;
1783            break;
1784
1785        default:
1786            php_sqlite3_error(result_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(result_obj->stmt_obj->stmt)));
1787    }
1788}
1789/* }}} */
1790
1791/* {{{ proto bool SQLite3Result::reset()
1792   Resets the result set back to the first row. */
1793PHP_METHOD(sqlite3result, reset)
1794{
1795    php_sqlite3_result *result_obj;
1796    zval *object = getThis();
1797    result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
1798
1799    SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
1800
1801    if (zend_parse_parameters_none() == FAILURE) {
1802        return;
1803    }
1804
1805    if (sqlite3_reset(result_obj->stmt_obj->stmt) != SQLITE_OK) {
1806        RETURN_FALSE;
1807    }
1808
1809    result_obj->complete = 0;
1810
1811    RETURN_TRUE;
1812}
1813/* }}} */
1814
1815/* {{{ proto bool SQLite3Result::finalize()
1816   Closes the result set. */
1817PHP_METHOD(sqlite3result, finalize)
1818{
1819    php_sqlite3_result *result_obj;
1820    zval *object = getThis();
1821    result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
1822
1823    SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
1824
1825    if (zend_parse_parameters_none() == FAILURE) {
1826        return;
1827    }
1828
1829    /* We need to finalize an internal statement */
1830    if (result_obj->is_prepared_statement == 0) {
1831        zend_llist_del_element(&(result_obj->db_obj->free_list), result_obj->stmt_obj_zval,
1832            (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
1833    } else {
1834        sqlite3_reset(result_obj->stmt_obj->stmt);
1835    }
1836
1837    RETURN_TRUE;
1838}
1839/* }}} */
1840
1841/* {{{ proto int SQLite3Result::__construct()
1842   __constructor for SQLite3Result. */
1843PHP_METHOD(sqlite3result, __construct)
1844{
1845    zend_throw_exception(zend_exception_get_default(TSRMLS_C), "SQLite3Result cannot be directly instantiated", 0 TSRMLS_CC);
1846}
1847/* }}} */
1848
1849/* {{{ arginfo */
1850ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_open, 0)
1851    ZEND_ARG_INFO(0, filename)
1852    ZEND_ARG_INFO(0, flags)
1853    ZEND_ARG_INFO(0, encryption_key)
1854ZEND_END_ARG_INFO()
1855
1856ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_busytimeout, 0)
1857    ZEND_ARG_INFO(0, ms)
1858ZEND_END_ARG_INFO()
1859
1860#ifndef SQLITE_OMIT_LOAD_EXTENSION
1861ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_loadextension, 0)
1862    ZEND_ARG_INFO(0, shared_library)
1863ZEND_END_ARG_INFO()
1864#endif
1865
1866ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_escapestring, 0, 0, 1)
1867    ZEND_ARG_INFO(0, value)
1868ZEND_END_ARG_INFO()
1869
1870ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_query, 0, 0, 1)
1871    ZEND_ARG_INFO(0, query)
1872ZEND_END_ARG_INFO()
1873
1874ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_querysingle, 0, 0, 1)
1875    ZEND_ARG_INFO(0, query)
1876    ZEND_ARG_INFO(0, entire_row)
1877ZEND_END_ARG_INFO()
1878
1879ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createfunction, 0, 0, 2)
1880    ZEND_ARG_INFO(0, name)
1881    ZEND_ARG_INFO(0, callback)
1882    ZEND_ARG_INFO(0, argument_count)
1883ZEND_END_ARG_INFO()
1884
1885ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createaggregate, 0, 0, 3)
1886    ZEND_ARG_INFO(0, name)
1887    ZEND_ARG_INFO(0, step_callback)
1888    ZEND_ARG_INFO(0, final_callback)
1889    ZEND_ARG_INFO(0, argument_count)
1890ZEND_END_ARG_INFO()
1891
1892ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createcollation, 0, 0, 2)
1893    ZEND_ARG_INFO(0, name)
1894    ZEND_ARG_INFO(0, callback)
1895ZEND_END_ARG_INFO()
1896
1897ZEND_BEGIN_ARG_INFO_EX(argingo_sqlite3_openblob, 0, 0, 3)
1898    ZEND_ARG_INFO(0, table)
1899    ZEND_ARG_INFO(0, column)
1900    ZEND_ARG_INFO(0, rowid)
1901    ZEND_ARG_INFO(0, dbname)
1902ZEND_END_ARG_INFO()
1903
1904ZEND_BEGIN_ARG_INFO_EX(argingo_sqlite3_enableexceptions, 0, 0, 1)
1905    ZEND_ARG_INFO(0, enableExceptions)
1906ZEND_END_ARG_INFO()
1907
1908ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3stmt_bindparam, 0, 0, 2)
1909    ZEND_ARG_INFO(0, param_number)
1910    ZEND_ARG_INFO(1, param)
1911    ZEND_ARG_INFO(0, type)
1912ZEND_END_ARG_INFO()
1913
1914ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3stmt_bindvalue, 0, 0, 2)
1915    ZEND_ARG_INFO(0, param_number)
1916    ZEND_ARG_INFO(0, param)
1917    ZEND_ARG_INFO(0, type)
1918ZEND_END_ARG_INFO()
1919
1920ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3stmt_construct, 0, 0, 1)
1921    ZEND_ARG_INFO(0, sqlite3)
1922ZEND_END_ARG_INFO()
1923
1924ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_columnname, 0, 0, 1)
1925    ZEND_ARG_INFO(0, column_number)
1926ZEND_END_ARG_INFO()
1927
1928ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_columntype, 0, 0, 1)
1929    ZEND_ARG_INFO(0, column_number)
1930ZEND_END_ARG_INFO()
1931
1932ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_fetcharray, 0, 0, 0)
1933    ZEND_ARG_INFO(0, mode)
1934ZEND_END_ARG_INFO()
1935
1936ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_void, 0)
1937ZEND_END_ARG_INFO()
1938/* }}} */
1939
1940/* {{{ php_sqlite3_class_methods */
1941static zend_function_entry php_sqlite3_class_methods[] = {
1942    PHP_ME(sqlite3,     open,               arginfo_sqlite3_open, ZEND_ACC_PUBLIC)
1943    PHP_ME(sqlite3,     close,              arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1944    PHP_ME(sqlite3,     exec,               arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
1945    PHP_ME(sqlite3,     version,            arginfo_sqlite3_void, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1946    PHP_ME(sqlite3,     lastInsertRowID,    arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1947    PHP_ME(sqlite3,     lastErrorCode,      arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1948    PHP_ME(sqlite3,     lastErrorMsg,       arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1949    PHP_ME(sqlite3,     busyTimeout,        arginfo_sqlite3_busytimeout, ZEND_ACC_PUBLIC)
1950#ifndef SQLITE_OMIT_LOAD_EXTENSION
1951    PHP_ME(sqlite3,     loadExtension,      arginfo_sqlite3_loadextension, ZEND_ACC_PUBLIC)
1952#endif
1953    PHP_ME(sqlite3,     changes,            arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1954    PHP_ME(sqlite3,     escapeString,       arginfo_sqlite3_escapestring, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1955    PHP_ME(sqlite3,     prepare,            arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
1956    PHP_ME(sqlite3,     query,              arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
1957    PHP_ME(sqlite3,     querySingle,        arginfo_sqlite3_querysingle, ZEND_ACC_PUBLIC)
1958    PHP_ME(sqlite3,     createFunction,     arginfo_sqlite3_createfunction, ZEND_ACC_PUBLIC)
1959    PHP_ME(sqlite3,     createAggregate,    arginfo_sqlite3_createaggregate, ZEND_ACC_PUBLIC)
1960    PHP_ME(sqlite3,     createCollation,    arginfo_sqlite3_createcollation, ZEND_ACC_PUBLIC)
1961    PHP_ME(sqlite3,     openBlob,           argingo_sqlite3_openblob, ZEND_ACC_PUBLIC)
1962    PHP_ME(sqlite3,     enableExceptions,   argingo_sqlite3_enableexceptions, ZEND_ACC_PUBLIC)
1963    /* Aliases */
1964    PHP_MALIAS(sqlite3, __construct, open, arginfo_sqlite3_open, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1965    PHP_FE_END
1966};
1967/* }}} */
1968
1969/* {{{ php_sqlite3_stmt_class_methods */
1970static zend_function_entry php_sqlite3_stmt_class_methods[] = {
1971    PHP_ME(sqlite3stmt, paramCount, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1972    PHP_ME(sqlite3stmt, close,      arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1973    PHP_ME(sqlite3stmt, reset,      arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1974    PHP_ME(sqlite3stmt, clear,      arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1975    PHP_ME(sqlite3stmt, execute,    arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1976    PHP_ME(sqlite3stmt, bindParam,  arginfo_sqlite3stmt_bindparam, ZEND_ACC_PUBLIC)
1977    PHP_ME(sqlite3stmt, bindValue,  arginfo_sqlite3stmt_bindvalue, ZEND_ACC_PUBLIC)
1978    PHP_ME(sqlite3stmt, readOnly,   arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1979    PHP_ME(sqlite3stmt, __construct, arginfo_sqlite3stmt_construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
1980    PHP_FE_END
1981};
1982/* }}} */
1983
1984/* {{{ php_sqlite3_result_class_methods */
1985static zend_function_entry php_sqlite3_result_class_methods[] = {
1986    PHP_ME(sqlite3result, numColumns,       arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1987    PHP_ME(sqlite3result, columnName,       arginfo_sqlite3result_columnname, ZEND_ACC_PUBLIC)
1988    PHP_ME(sqlite3result, columnType,       arginfo_sqlite3result_columntype, ZEND_ACC_PUBLIC)
1989    PHP_ME(sqlite3result, fetchArray,       arginfo_sqlite3result_fetcharray, ZEND_ACC_PUBLIC)
1990    PHP_ME(sqlite3result, reset,            arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1991    PHP_ME(sqlite3result, finalize,         arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1992    PHP_ME(sqlite3result, __construct,      arginfo_sqlite3_void, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
1993    PHP_FE_END
1994};
1995/* }}} */
1996
1997/* {{{ Authorization Callback
1998*/
1999static int php_sqlite3_authorizer(void *autharg, int access_type, const char *arg3, const char *arg4, const char *arg5, const char *arg6)
2000{
2001    switch (access_type) {
2002        case SQLITE_ATTACH:
2003        {
2004            if (memcmp(arg3, ":memory:", sizeof(":memory:")) && *arg3) {
2005                TSRMLS_FETCH();
2006
2007#if PHP_API_VERSION < 20100412
2008                if (PG(safe_mode) && (!php_checkuid(arg3, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
2009                    return SQLITE_DENY;
2010                }
2011#endif
2012
2013                if (php_check_open_basedir(arg3 TSRMLS_CC)) {
2014                    return SQLITE_DENY;
2015                }
2016            }
2017            return SQLITE_OK;
2018        }
2019
2020        default:
2021            /* access allowed */
2022            return SQLITE_OK;
2023    }
2024}
2025/* }}} */
2026
2027/* {{{ php_sqlite3_free_list_dtor
2028*/
2029static void php_sqlite3_free_list_dtor(void **item)
2030{
2031    php_sqlite3_free_list *free_item = (php_sqlite3_free_list *)*item;
2032
2033    if (free_item->stmt_obj && free_item->stmt_obj->initialised) {
2034        sqlite3_finalize(free_item->stmt_obj->stmt);
2035        free_item->stmt_obj->initialised = 0;
2036    }
2037    efree(*item);
2038}
2039/* }}} */
2040
2041static int php_sqlite3_compare_stmt_zval_free( php_sqlite3_free_list **free_list, zval *statement ) /* {{{ */
2042{
2043    return ((*free_list)->stmt_obj->initialised && statement == (*free_list)->stmt_obj_zval);
2044}
2045/* }}} */
2046
2047static int php_sqlite3_compare_stmt_free( php_sqlite3_free_list **free_list, sqlite3_stmt *statement ) /* {{{ */
2048{
2049    return ((*free_list)->stmt_obj->initialised && statement == (*free_list)->stmt_obj->stmt);
2050}
2051/* }}} */
2052
2053static void php_sqlite3_object_free_storage(void *object TSRMLS_DC) /* {{{ */
2054{
2055    php_sqlite3_db_object *intern = (php_sqlite3_db_object *)object;
2056    php_sqlite3_func *func;
2057    php_sqlite3_collation *collation;
2058
2059    if (!intern) {
2060        return;
2061    }
2062
2063    while (intern->funcs) {
2064        func = intern->funcs;
2065        intern->funcs = func->next;
2066        if (intern->initialised && intern->db) {
2067            sqlite3_create_function(intern->db, func->func_name, func->argc, SQLITE_UTF8, func, NULL, NULL, NULL);
2068        }
2069
2070        efree((char*)func->func_name);
2071
2072        if (func->func) {
2073            zval_ptr_dtor(&func->func);
2074        }
2075        if (func->step) {
2076            zval_ptr_dtor(&func->step);
2077        }
2078        if (func->fini) {
2079            zval_ptr_dtor(&func->fini);
2080        }
2081        efree(func);
2082    }
2083
2084    while (intern->collations){
2085        collation = intern->collations;
2086        intern->collations = collation->next;
2087        if (intern->initialised && intern->db){
2088            sqlite3_create_collation(intern->db, collation->collation_name, SQLITE_UTF8, NULL, NULL);
2089        }
2090        efree((char*)collation->collation_name);
2091        if (collation->cmp_func){
2092            zval_ptr_dtor(&collation->cmp_func);
2093        }
2094        efree(collation);
2095    }
2096
2097    if (intern->initialised && intern->db) {
2098        sqlite3_close(intern->db);
2099        intern->initialised = 0;
2100    }
2101
2102    zend_object_std_dtor(&intern->zo TSRMLS_CC);
2103    efree(intern);
2104}
2105/* }}} */
2106
2107static void php_sqlite3_stmt_object_free_storage(void *object TSRMLS_DC) /* {{{ */
2108{
2109    php_sqlite3_stmt *intern = (php_sqlite3_stmt *)object;
2110
2111    if (!intern) {
2112        return;
2113    }
2114
2115    if (intern->bound_params) {
2116        zend_hash_destroy(intern->bound_params);
2117        FREE_HASHTABLE(intern->bound_params);
2118        intern->bound_params = NULL;
2119    }
2120
2121    if (intern->initialised) {
2122        zend_llist_del_element(&(intern->db_obj->free_list), intern->stmt,
2123            (int (*)(void *, void *)) php_sqlite3_compare_stmt_free);
2124    }
2125
2126    if (intern->db_obj_zval) {
2127        zval_ptr_dtor(&intern->db_obj_zval);
2128    }
2129
2130    zend_object_std_dtor(&intern->zo TSRMLS_CC);
2131    efree(intern);
2132}
2133/* }}} */
2134
2135static void php_sqlite3_result_object_free_storage(void *object TSRMLS_DC) /* {{{ */
2136{
2137    php_sqlite3_result *intern = (php_sqlite3_result *)object;
2138
2139    if (!intern) {
2140        return;
2141    }
2142
2143    if (intern->stmt_obj_zval) {
2144        if (intern->stmt_obj->initialised) {
2145            sqlite3_reset(intern->stmt_obj->stmt);
2146        }
2147
2148        if (intern->is_prepared_statement == 0) {
2149            zval_dtor(intern->stmt_obj_zval);
2150            FREE_ZVAL(intern->stmt_obj_zval);
2151        } else {
2152            zval_ptr_dtor(&intern->stmt_obj_zval);
2153        }
2154    }
2155
2156    zend_object_std_dtor(&intern->zo TSRMLS_CC);
2157    efree(intern);
2158}
2159/* }}} */
2160
2161static zend_object_value php_sqlite3_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
2162{
2163    zend_object_value retval;
2164    php_sqlite3_db_object *intern;
2165
2166    /* Allocate memory for it */
2167    intern = emalloc(sizeof(php_sqlite3_db_object));
2168    memset(intern, 0, sizeof(php_sqlite3_db_object));
2169    intern->exception = 0;
2170
2171    /* Need to keep track of things to free */
2172    zend_llist_init(&(intern->free_list),   sizeof(php_sqlite3_free_list *), (llist_dtor_func_t)php_sqlite3_free_list_dtor, 0);
2173
2174    zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
2175    object_properties_init(&intern->zo, class_type);
2176
2177    retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_sqlite3_object_free_storage, NULL TSRMLS_CC);
2178    retval.handlers = (zend_object_handlers *) &sqlite3_object_handlers;
2179
2180    return retval;
2181}
2182/* }}} */
2183
2184static zend_object_value php_sqlite3_stmt_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
2185{
2186    zend_object_value retval;
2187    php_sqlite3_stmt *intern;
2188
2189    /* Allocate memory for it */
2190    intern = emalloc(sizeof(php_sqlite3_stmt));
2191    memset(intern, 0, sizeof(php_sqlite3_stmt));
2192
2193    intern->db_obj_zval = NULL;
2194
2195    zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
2196    object_properties_init(&intern->zo, class_type);
2197
2198    retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_sqlite3_stmt_object_free_storage, NULL TSRMLS_CC);
2199    retval.handlers = (zend_object_handlers *) &sqlite3_stmt_object_handlers;
2200
2201    return retval;
2202}
2203/* }}} */
2204
2205static zend_object_value php_sqlite3_result_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
2206{
2207    zend_object_value retval;
2208    php_sqlite3_result *intern;
2209
2210    /* Allocate memory for it */
2211    intern = emalloc(sizeof(php_sqlite3_result));
2212    memset(intern, 0, sizeof(php_sqlite3_result));
2213
2214    intern->complete = 0;
2215    intern->is_prepared_statement = 0;
2216    intern->stmt_obj_zval = NULL;
2217
2218    zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
2219    object_properties_init(&intern->zo, class_type);
2220
2221    retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_sqlite3_result_object_free_storage, NULL TSRMLS_CC);
2222    retval.handlers = (zend_object_handlers *) &sqlite3_result_object_handlers;
2223
2224    return retval;
2225}
2226/* }}} */
2227
2228static void sqlite3_param_dtor(void *data) /* {{{ */
2229{
2230    struct php_sqlite3_bound_param *param = (struct php_sqlite3_bound_param*)data;
2231
2232    if (param->name) {
2233        efree(param->name);
2234    }
2235
2236    if (param->parameter) {
2237        zval_ptr_dtor(&(param->parameter));
2238        param->parameter = NULL;
2239    }
2240}
2241/* }}} */
2242
2243/* {{{ PHP_MINIT_FUNCTION
2244*/
2245PHP_MINIT_FUNCTION(sqlite3)
2246{
2247    zend_class_entry ce;
2248
2249#if defined(ZTS)
2250    /* Refuse to load if this wasn't a threasafe library loaded */
2251    if (!sqlite3_threadsafe()) {
2252        php_error_docref(NULL TSRMLS_CC, E_WARNING, "A thread safe version of SQLite is required when using a thread safe version of PHP.");
2253        return FAILURE;
2254    }
2255#endif
2256
2257    memcpy(&sqlite3_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
2258    memcpy(&sqlite3_stmt_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
2259    memcpy(&sqlite3_result_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
2260
2261    /* Register SQLite 3 Class */
2262    INIT_CLASS_ENTRY(ce, "SQLite3", php_sqlite3_class_methods);
2263    ce.create_object = php_sqlite3_object_new;
2264    sqlite3_object_handlers.clone_obj = NULL;
2265    php_sqlite3_sc_entry = zend_register_internal_class(&ce TSRMLS_CC);
2266
2267    /* Register SQLite 3 Prepared Statement Class */
2268    INIT_CLASS_ENTRY(ce, "SQLite3Stmt", php_sqlite3_stmt_class_methods);
2269    ce.create_object = php_sqlite3_stmt_object_new;
2270    sqlite3_stmt_object_handlers.clone_obj = NULL;
2271    php_sqlite3_stmt_entry = zend_register_internal_class(&ce TSRMLS_CC);
2272
2273    /* Register SQLite 3 Result Class */
2274    INIT_CLASS_ENTRY(ce, "SQLite3Result", php_sqlite3_result_class_methods);
2275    ce.create_object = php_sqlite3_result_object_new;
2276    sqlite3_result_object_handlers.clone_obj = NULL;
2277    php_sqlite3_result_entry = zend_register_internal_class(&ce TSRMLS_CC);
2278
2279    REGISTER_INI_ENTRIES();
2280
2281    REGISTER_LONG_CONSTANT("SQLITE3_ASSOC", PHP_SQLITE3_ASSOC, CONST_CS | CONST_PERSISTENT);
2282    REGISTER_LONG_CONSTANT("SQLITE3_NUM", PHP_SQLITE3_NUM, CONST_CS | CONST_PERSISTENT);
2283    REGISTER_LONG_CONSTANT("SQLITE3_BOTH", PHP_SQLITE3_BOTH, CONST_CS | CONST_PERSISTENT);
2284
2285    REGISTER_LONG_CONSTANT("SQLITE3_INTEGER", SQLITE_INTEGER, CONST_CS | CONST_PERSISTENT);
2286    REGISTER_LONG_CONSTANT("SQLITE3_FLOAT", SQLITE_FLOAT, CONST_CS | CONST_PERSISTENT);
2287    REGISTER_LONG_CONSTANT("SQLITE3_TEXT", SQLITE3_TEXT, CONST_CS | CONST_PERSISTENT);
2288    REGISTER_LONG_CONSTANT("SQLITE3_BLOB", SQLITE_BLOB, CONST_CS | CONST_PERSISTENT);
2289    REGISTER_LONG_CONSTANT("SQLITE3_NULL", SQLITE_NULL, CONST_CS | CONST_PERSISTENT);
2290
2291    REGISTER_LONG_CONSTANT("SQLITE3_OPEN_READONLY", SQLITE_OPEN_READONLY, CONST_CS | CONST_PERSISTENT);
2292    REGISTER_LONG_CONSTANT("SQLITE3_OPEN_READWRITE", SQLITE_OPEN_READWRITE, CONST_CS | CONST_PERSISTENT);
2293    REGISTER_LONG_CONSTANT("SQLITE3_OPEN_CREATE", SQLITE_OPEN_CREATE, CONST_CS | CONST_PERSISTENT);
2294
2295    return SUCCESS;
2296}
2297/* }}} */
2298
2299/* {{{ PHP_MSHUTDOWN_FUNCTION
2300*/
2301PHP_MSHUTDOWN_FUNCTION(sqlite3)
2302{
2303    UNREGISTER_INI_ENTRIES();
2304
2305    return SUCCESS;
2306}
2307/* }}} */
2308
2309/* {{{ PHP_MINFO_FUNCTION
2310*/
2311PHP_MINFO_FUNCTION(sqlite3)
2312{
2313    php_info_print_table_start();
2314    php_info_print_table_header(2, "SQLite3 support", "enabled");
2315    php_info_print_table_row(2, "SQLite3 module version", PHP_SQLITE3_VERSION);
2316    php_info_print_table_row(2, "SQLite Library", sqlite3_libversion());
2317    php_info_print_table_end();
2318
2319    DISPLAY_INI_ENTRIES();
2320}
2321/* }}} */
2322
2323/* {{{ PHP_GINIT_FUNCTION
2324*/
2325static PHP_GINIT_FUNCTION(sqlite3)
2326{
2327    memset(sqlite3_globals, 0, sizeof(*sqlite3_globals));
2328}
2329/* }}} */
2330
2331/* {{{ sqlite3_module_entry
2332*/
2333zend_module_entry sqlite3_module_entry = {
2334    STANDARD_MODULE_HEADER,
2335    "sqlite3",
2336    NULL,
2337    PHP_MINIT(sqlite3),
2338    PHP_MSHUTDOWN(sqlite3),
2339    NULL,
2340    NULL,
2341    PHP_MINFO(sqlite3),
2342    PHP_SQLITE3_VERSION,
2343    PHP_MODULE_GLOBALS(sqlite3),
2344    PHP_GINIT(sqlite3),
2345    NULL,
2346    NULL,
2347    STANDARD_MODULE_PROPERTIES_EX
2348};
2349/* }}} */
2350
2351#ifdef COMPILE_DL_SQLITE3
2352ZEND_GET_MODULE(sqlite3)
2353#endif
2354
2355/*
2356 * Local variables:
2357 * tab-width: 4
2358 * c-basic-offset: 4
2359 * End:
2360 * vim600: sw=4 ts=4 fdm=marker
2361 * vim<600: sw=4 ts=4
2362 */
2363