1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2014 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    //retval ought to contain a ZVAL_LONG by now
910    // (the result of a comparison, i.e. most likely -1, 0, or 1)
911    //I suppose we could accept any scalar return type, though.
912    if (Z_TYPE_P(retval) != IS_LONG){
913        php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the compare callback (invalid return type).  Collation behaviour is undefined.");
914    }else{
915        ret = Z_LVAL_P(retval);
916    }
917
918    zval_ptr_dtor(&retval);
919
920    return ret;
921}
922/* }}} */
923
924/* {{{ proto bool SQLite3::createFunction(string name, mixed callback [, int argcount])
925   Allows registration of a PHP function as a SQLite UDF that can be called within SQL statements. */
926PHP_METHOD(sqlite3, createFunction)
927{
928    php_sqlite3_db_object *db_obj;
929    zval *object = getThis();
930    php_sqlite3_func *func;
931    char *sql_func, *callback_name;
932    int sql_func_len;
933    zval *callback_func;
934    long sql_func_num_args = -1;
935    db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
936
937    SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
938
939    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &sql_func, &sql_func_len, &callback_func, &sql_func_num_args) == FAILURE) {
940        return;
941    }
942
943    if (!sql_func_len) {
944        RETURN_FALSE;
945    }
946
947    if (!zend_is_callable(callback_func, 0, &callback_name TSRMLS_CC)) {
948        php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
949        efree(callback_name);
950        RETURN_FALSE;
951    }
952    efree(callback_name);
953
954    func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
955
956    if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, SQLITE_UTF8, func, php_sqlite3_callback_func, NULL, NULL) == SQLITE_OK) {
957        func->func_name = estrdup(sql_func);
958
959        MAKE_STD_ZVAL(func->func);
960        MAKE_COPY_ZVAL(&callback_func, func->func);
961
962        func->argc = sql_func_num_args;
963        func->next = db_obj->funcs;
964        db_obj->funcs = func;
965
966        RETURN_TRUE;
967    }
968    efree(func);
969
970    RETURN_FALSE;
971}
972/* }}} */
973
974/* {{{ proto bool SQLite3::createAggregate(string name, mixed step, mixed final [, int argcount])
975   Allows registration of a PHP function for use as an aggregate. */
976PHP_METHOD(sqlite3, createAggregate)
977{
978    php_sqlite3_db_object *db_obj;
979    zval *object = getThis();
980    php_sqlite3_func *func;
981    char *sql_func, *callback_name;
982    int sql_func_len;
983    zval *step_callback, *fini_callback;
984    long sql_func_num_args = -1;
985    db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
986
987    SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
988
989    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) {
990        return;
991    }
992
993    if (!sql_func_len) {
994        RETURN_FALSE;
995    }
996
997    if (!zend_is_callable(step_callback, 0, &callback_name TSRMLS_CC)) {
998        php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
999        efree(callback_name);
1000        RETURN_FALSE;
1001    }
1002    efree(callback_name);
1003
1004    if (!zend_is_callable(fini_callback, 0, &callback_name TSRMLS_CC)) {
1005        php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
1006        efree(callback_name);
1007        RETURN_FALSE;
1008    }
1009    efree(callback_name);
1010
1011    func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
1012
1013    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) {
1014        func->func_name = estrdup(sql_func);
1015
1016        MAKE_STD_ZVAL(func->step);
1017        MAKE_COPY_ZVAL(&step_callback, func->step);
1018
1019        MAKE_STD_ZVAL(func->fini);
1020        MAKE_COPY_ZVAL(&fini_callback, func->fini);
1021
1022        func->argc = sql_func_num_args;
1023        func->next = db_obj->funcs;
1024        db_obj->funcs = func;
1025
1026        RETURN_TRUE;
1027    }
1028    efree(func);
1029
1030    RETURN_FALSE;
1031}
1032/* }}} */
1033
1034/* {{{ proto bool SQLite3::createCollation(string name, mixed callback)
1035   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()). */
1036PHP_METHOD(sqlite3, createCollation)
1037{
1038    php_sqlite3_db_object *db_obj;
1039    zval *object = getThis();
1040    php_sqlite3_collation *collation;
1041    char *collation_name, *callback_name;
1042    int collation_name_len;
1043    zval *callback_func;
1044    db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
1045
1046    SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
1047
1048    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &collation_name, &collation_name_len, &callback_func) == FAILURE) {
1049        RETURN_FALSE;
1050    }
1051
1052    if (!collation_name_len) {
1053        RETURN_FALSE;
1054    }
1055
1056    if (!zend_is_callable(callback_func, 0, &callback_name TSRMLS_CC)) {
1057        php_sqlite3_error(db_obj, "Not a valid callback function %s", callback_name);
1058        efree(callback_name);
1059        RETURN_FALSE;
1060    }
1061    efree(callback_name);
1062
1063    collation = (php_sqlite3_collation *)ecalloc(1, sizeof(*collation));
1064    if (sqlite3_create_collation(db_obj->db, collation_name, SQLITE_UTF8, collation, php_sqlite3_callback_compare) == SQLITE_OK) {
1065        collation->collation_name = estrdup(collation_name);
1066
1067        MAKE_STD_ZVAL(collation->cmp_func);
1068        MAKE_COPY_ZVAL(&callback_func, collation->cmp_func);
1069
1070        collation->next = db_obj->collations;
1071        db_obj->collations = collation;
1072
1073        RETURN_TRUE;
1074    }
1075    efree(collation);
1076
1077    RETURN_FALSE;
1078}
1079/* }}} */
1080
1081typedef struct {
1082    sqlite3_blob *blob;
1083    size_t       position;
1084    size_t       size;
1085} php_stream_sqlite3_data;
1086
1087static size_t php_sqlite3_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
1088{
1089/*  php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract; */
1090
1091    return 0;
1092}
1093
1094static size_t php_sqlite3_stream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
1095{
1096    php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
1097
1098    if (sqlite3_stream->position + count >= sqlite3_stream->size) {
1099        count = sqlite3_stream->size - sqlite3_stream->position;
1100        stream->eof = 1;
1101    }
1102    if (count) {
1103        if (sqlite3_blob_read(sqlite3_stream->blob, buf, count, sqlite3_stream->position) != SQLITE_OK) {
1104            return 0;
1105        }
1106        sqlite3_stream->position += count;
1107    }
1108    return count;
1109}
1110
1111static int php_sqlite3_stream_close(php_stream *stream, int close_handle TSRMLS_DC)
1112{
1113    php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
1114
1115    if (sqlite3_blob_close(sqlite3_stream->blob) != SQLITE_OK) {
1116        /* Error occurred, but it still closed */
1117    }
1118
1119    efree(sqlite3_stream);
1120
1121    return 0;
1122}
1123
1124static int php_sqlite3_stream_flush(php_stream *stream TSRMLS_DC)
1125{
1126    /* do nothing */
1127    return 0;
1128}
1129
1130/* {{{ */
1131static int php_sqlite3_stream_seek(php_stream *stream, off_t offset, int whence, off_t *newoffs TSRMLS_DC)
1132{
1133    php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
1134
1135    switch(whence) {
1136        case SEEK_CUR:
1137            if (offset < 0) {
1138                if (sqlite3_stream->position < (size_t)(-offset)) {
1139                    sqlite3_stream->position = 0;
1140                    *newoffs = -1;
1141                    return -1;
1142                } else {
1143                    sqlite3_stream->position = sqlite3_stream->position + offset;
1144                    *newoffs = sqlite3_stream->position;
1145                    stream->eof = 0;
1146                    return 0;
1147                }
1148            } else {
1149                if (sqlite3_stream->position + (size_t)(offset) > sqlite3_stream->size) {
1150                    sqlite3_stream->position = sqlite3_stream->size;
1151                    *newoffs = -1;
1152                    return -1;
1153                } else {
1154                    sqlite3_stream->position = sqlite3_stream->position + offset;
1155                    *newoffs = sqlite3_stream->position;
1156                    stream->eof = 0;
1157                    return 0;
1158                }
1159            }
1160        case SEEK_SET:
1161            if (sqlite3_stream->size < (size_t)(offset)) {
1162                sqlite3_stream->position = sqlite3_stream->size;
1163                *newoffs = -1;
1164                return -1;
1165            } else {
1166                sqlite3_stream->position = offset;
1167                *newoffs = sqlite3_stream->position;
1168                stream->eof = 0;
1169                return 0;
1170            }
1171        case SEEK_END:
1172            if (offset > 0) {
1173                sqlite3_stream->position = sqlite3_stream->size;
1174                *newoffs = -1;
1175                return -1;
1176            } else if (sqlite3_stream->size < (size_t)(-offset)) {
1177                sqlite3_stream->position = 0;
1178                *newoffs = -1;
1179                return -1;
1180            } else {
1181                sqlite3_stream->position = sqlite3_stream->size + offset;
1182                *newoffs = sqlite3_stream->position;
1183                stream->eof = 0;
1184                return 0;
1185            }
1186        default:
1187            *newoffs = sqlite3_stream->position;
1188            return -1;
1189    }
1190}
1191/* }}} */
1192
1193
1194static int php_sqlite3_stream_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
1195{
1196    return FAILURE;
1197}
1198
1199static int php_sqlite3_stream_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
1200{
1201    php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
1202    ssb->sb.st_size = sqlite3_stream->size;
1203    return 0;
1204}
1205
1206static php_stream_ops php_stream_sqlite3_ops = {
1207    php_sqlite3_stream_write,
1208    php_sqlite3_stream_read,
1209    php_sqlite3_stream_close,
1210    php_sqlite3_stream_flush,
1211    "SQLite3",
1212    php_sqlite3_stream_seek,
1213    php_sqlite3_stream_cast,
1214    php_sqlite3_stream_stat
1215};
1216
1217/* {{{ proto resource SQLite3::openBlob(string table, string column, int rowid [, string dbname])
1218   Open a blob as a stream which we can read / write to. */
1219PHP_METHOD(sqlite3, openBlob)
1220{
1221    php_sqlite3_db_object *db_obj;
1222    zval *object = getThis();
1223    char *table, *column, *dbname = "main";
1224    int table_len, column_len, dbname_len;
1225    long rowid, flags = 0;
1226    sqlite3_blob *blob = NULL;
1227    php_stream_sqlite3_data *sqlite3_stream;
1228    php_stream *stream;
1229
1230    db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
1231
1232    SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
1233
1234    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssl|s", &table, &table_len, &column, &column_len, &rowid, &dbname, &dbname_len) == FAILURE) {
1235        return;
1236    }
1237
1238    if (sqlite3_blob_open(db_obj->db, dbname, table, column, rowid, flags, &blob) != SQLITE_OK) {
1239        php_sqlite3_error(db_obj, "Unable to open blob: %s", sqlite3_errmsg(db_obj->db));
1240        RETURN_FALSE;
1241    }
1242
1243    sqlite3_stream = emalloc(sizeof(php_stream_sqlite3_data));
1244    sqlite3_stream->blob = blob;
1245    sqlite3_stream->position = 0;
1246    sqlite3_stream->size = sqlite3_blob_bytes(blob);
1247
1248    stream = php_stream_alloc(&php_stream_sqlite3_ops, sqlite3_stream, 0, "rb");
1249
1250    if (stream) {
1251        php_stream_to_zval(stream, return_value);
1252    } else {
1253        RETURN_FALSE;
1254    }
1255}
1256/* }}} */
1257
1258/* {{{ proto bool SQLite3::enableExceptions([bool enableExceptions = false])
1259   Enables an exception error mode. */
1260PHP_METHOD(sqlite3, enableExceptions)
1261{
1262    php_sqlite3_db_object *db_obj;
1263    zval *object = getThis();
1264    zend_bool enableExceptions = 0;
1265
1266    db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
1267
1268    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enableExceptions) == FAILURE) {
1269        return;
1270    }
1271
1272    RETVAL_BOOL(db_obj->exception);
1273
1274    db_obj->exception = enableExceptions;
1275}
1276/* }}} */
1277
1278/* {{{ proto int SQLite3Stmt::paramCount()
1279   Returns the number of parameters within the prepared statement. */
1280PHP_METHOD(sqlite3stmt, paramCount)
1281{
1282    php_sqlite3_stmt *stmt_obj;
1283    zval *object = getThis();
1284    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1285
1286    if (zend_parse_parameters_none() == FAILURE) {
1287        return;
1288    }
1289
1290    SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
1291
1292    RETURN_LONG(sqlite3_bind_parameter_count(stmt_obj->stmt));
1293}
1294/* }}} */
1295
1296/* {{{ proto bool SQLite3Stmt::close()
1297   Closes the prepared statement. */
1298PHP_METHOD(sqlite3stmt, close)
1299{
1300    php_sqlite3_stmt *stmt_obj;
1301    zval *object = getThis();
1302    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1303
1304    if (zend_parse_parameters_none() == FAILURE) {
1305        return;
1306    }
1307
1308    if(stmt_obj->db_obj) {
1309            zend_llist_del_element(&(stmt_obj->db_obj->free_list), object, (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
1310    }
1311
1312    RETURN_TRUE;
1313}
1314/* }}} */
1315
1316/* {{{ proto bool SQLite3Stmt::reset()
1317   Reset the prepared statement to the state before it was executed, bindings still remain. */
1318PHP_METHOD(sqlite3stmt, reset)
1319{
1320    php_sqlite3_stmt *stmt_obj;
1321    zval *object = getThis();
1322    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1323
1324    if (zend_parse_parameters_none() == FAILURE) {
1325        return;
1326    }
1327
1328    SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
1329
1330    if (sqlite3_reset(stmt_obj->stmt) != SQLITE_OK) {
1331        php_sqlite3_error(stmt_obj->db_obj, "Unable to reset statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
1332        RETURN_FALSE;
1333    }
1334    RETURN_TRUE;
1335}
1336/* }}} */
1337
1338/* {{{ proto bool SQLite3Stmt::clear()
1339   Clear all current bound parameters. */
1340PHP_METHOD(sqlite3stmt, clear)
1341{
1342    php_sqlite3_stmt *stmt_obj;
1343    zval *object = getThis();
1344    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1345
1346    if (zend_parse_parameters_none() == FAILURE) {
1347        return;
1348    }
1349
1350    SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
1351
1352    if (sqlite3_clear_bindings(stmt_obj->stmt) != SQLITE_OK) {
1353        php_sqlite3_error(stmt_obj->db_obj, "Unable to clear statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
1354        RETURN_FALSE;
1355    }
1356
1357    RETURN_TRUE;
1358}
1359/* }}} */
1360
1361/* {{{ proto bool SQLite3Stmt::readOnly()
1362   Returns true if a statement is definitely read only */
1363PHP_METHOD(sqlite3stmt, readOnly)
1364{
1365    php_sqlite3_stmt *stmt_obj;
1366    zval *object = getThis();
1367    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1368
1369    if (zend_parse_parameters_none() == FAILURE) {
1370        return;
1371    }
1372
1373    SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
1374
1375#if SQLITE_VERSION_NUMBER >= 3007004
1376    if (sqlite3_stmt_readonly(stmt_obj->stmt)) {
1377        RETURN_TRUE;
1378    }
1379#endif
1380    RETURN_FALSE;
1381}
1382/* }}} */
1383
1384static int register_bound_parameter_to_sqlite(struct php_sqlite3_bound_param *param, php_sqlite3_stmt *stmt TSRMLS_DC) /* {{{ */
1385{
1386    HashTable *hash;
1387    hash = stmt->bound_params;
1388
1389    if (!hash) {
1390        ALLOC_HASHTABLE(hash);
1391        zend_hash_init(hash, 13, NULL, sqlite3_param_dtor, 0);
1392        stmt->bound_params = hash;
1393    }
1394
1395    /* We need a : prefix to resolve a name to a parameter number */
1396    if (param->name) {
1397        if (param->name[0] != ':') {
1398            /* pre-increment for character + 1 for null */
1399            char *temp = emalloc(++param->name_len + 1);
1400            temp[0] = ':';
1401            memmove(temp+1, param->name, param->name_len);
1402            param->name = temp;
1403        } else {
1404            param->name = estrndup(param->name, param->name_len);
1405        }
1406        /* do lookup*/
1407        param->param_number = sqlite3_bind_parameter_index(stmt->stmt, param->name);
1408    }
1409
1410    if (param->param_number < 1) {
1411        efree(param->name);
1412        return 0;
1413    }
1414
1415    if (param->param_number >= 1) {
1416        zend_hash_index_del(hash, param->param_number);
1417    }
1418
1419    if (param->name) {
1420        zend_hash_update(hash, param->name, param->name_len, param, sizeof(*param), NULL);
1421    } else {
1422        zend_hash_index_update(hash, param->param_number, param, sizeof(*param), NULL);
1423    }
1424
1425    return 1;
1426}
1427/* }}} */
1428
1429/* {{{ proto bool SQLite3Stmt::bindParam(int parameter_number, mixed parameter [, int type])
1430   Bind Parameter to a stmt variable. */
1431PHP_METHOD(sqlite3stmt, bindParam)
1432{
1433    php_sqlite3_stmt *stmt_obj;
1434    zval *object = getThis();
1435    struct php_sqlite3_bound_param param = {0};
1436    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1437
1438    param.param_number = -1;
1439    param.type = SQLITE3_TEXT;
1440
1441    if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "lz|l", &param.param_number, &param.parameter, &param.type) == FAILURE) {
1442        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &param.name, &param.name_len, &param.parameter, &param.type) == FAILURE) {
1443            return;
1444        }
1445    }
1446
1447    SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
1448
1449    Z_ADDREF_P(param.parameter);
1450
1451    if (!register_bound_parameter_to_sqlite(&param, stmt_obj TSRMLS_CC)) {
1452        if (param.parameter) {
1453            zval_ptr_dtor(&(param.parameter));
1454            param.parameter = NULL;
1455        }
1456        RETURN_FALSE;
1457    }
1458    RETURN_TRUE;
1459}
1460/* }}} */
1461
1462/* {{{ proto bool SQLite3Stmt::bindValue(int parameter_number, mixed parameter [, int type])
1463   Bind Value of a parameter to a stmt variable. */
1464PHP_METHOD(sqlite3stmt, bindValue)
1465{
1466    php_sqlite3_stmt *stmt_obj;
1467    zval *object = getThis();
1468    struct php_sqlite3_bound_param param = {0};
1469    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1470
1471    param.param_number = -1;
1472    param.type = SQLITE3_TEXT;
1473
1474    if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "lz/|l", &param.param_number, &param.parameter, &param.type) == FAILURE) {
1475        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz/|l", &param.name, &param.name_len, &param.parameter, &param.type) == FAILURE) {
1476            return;
1477        }
1478    }
1479
1480    SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
1481
1482    Z_ADDREF_P(param.parameter);
1483
1484    if (!register_bound_parameter_to_sqlite(&param, stmt_obj TSRMLS_CC)) {
1485        if (param.parameter) {
1486            zval_ptr_dtor(&(param.parameter));
1487            param.parameter = NULL;
1488        }
1489        RETURN_FALSE;
1490    }
1491    RETURN_TRUE;
1492}
1493/* }}} */
1494
1495/* {{{ proto SQLite3Result SQLite3Stmt::execute()
1496   Executes a prepared statement and returns a result set object. */
1497PHP_METHOD(sqlite3stmt, execute)
1498{
1499    php_sqlite3_stmt *stmt_obj;
1500    php_sqlite3_result *result;
1501    zval *object = getThis();
1502    int return_code = 0;
1503    struct php_sqlite3_bound_param *param;
1504
1505    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1506
1507    if (zend_parse_parameters_none() == FAILURE) {
1508        return;
1509    }
1510
1511    SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3)
1512
1513    if (stmt_obj->bound_params) {
1514        zend_hash_internal_pointer_reset(stmt_obj->bound_params);
1515        while (zend_hash_get_current_data(stmt_obj->bound_params, (void **)&param) == SUCCESS) {
1516            /* If the ZVAL is null then it should be bound as that */
1517            if (Z_TYPE_P(param->parameter) == IS_NULL) {
1518                sqlite3_bind_null(stmt_obj->stmt, param->param_number);
1519                zend_hash_move_forward(stmt_obj->bound_params);
1520                continue;
1521            }
1522
1523            switch (param->type) {
1524                case SQLITE_INTEGER:
1525                    convert_to_long(param->parameter);
1526#if LONG_MAX > 2147483647
1527                    sqlite3_bind_int64(stmt_obj->stmt, param->param_number, Z_LVAL_P(param->parameter));
1528#else
1529                    sqlite3_bind_int(stmt_obj->stmt, param->param_number, Z_LVAL_P(param->parameter));
1530#endif
1531                    break;
1532
1533                case SQLITE_FLOAT:
1534                    /* convert_to_double(param->parameter);*/
1535                    sqlite3_bind_double(stmt_obj->stmt, param->param_number, Z_DVAL_P(param->parameter));
1536                    break;
1537
1538                case SQLITE_BLOB:
1539                {
1540                    php_stream *stream = NULL;
1541                    int blength;
1542                    char *buffer = NULL;
1543                    if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
1544                        php_stream_from_zval_no_verify(stream, &param->parameter);
1545                        if (stream == NULL) {
1546                            php_sqlite3_error(stmt_obj->db_obj, "Unable to read stream for parameter %ld", param->param_number);
1547                            RETURN_FALSE;
1548                        }
1549                        blength = php_stream_copy_to_mem(stream, (void *)&buffer, PHP_STREAM_COPY_ALL, 0);
1550                    } else {
1551                        convert_to_string(param->parameter);
1552                        blength =  Z_STRLEN_P(param->parameter);
1553                        buffer = Z_STRVAL_P(param->parameter);
1554                    }
1555
1556                    sqlite3_bind_blob(stmt_obj->stmt, param->param_number, buffer, blength, SQLITE_TRANSIENT);
1557
1558                    if (stream) {
1559                        pefree(buffer, 0);
1560                    }
1561                    break;
1562                }
1563
1564                case SQLITE3_TEXT:
1565                    convert_to_string(param->parameter);
1566                    sqlite3_bind_text(stmt_obj->stmt, param->param_number, Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter), SQLITE_STATIC);
1567                    break;
1568
1569                case SQLITE_NULL:
1570                    sqlite3_bind_null(stmt_obj->stmt, param->param_number);
1571                    break;
1572
1573                default:
1574                    php_sqlite3_error(stmt_obj->db_obj, "Unknown parameter type: %ld for parameter %ld", param->type, param->param_number);
1575                    RETURN_FALSE;
1576            }
1577            zend_hash_move_forward(stmt_obj->bound_params);
1578        }
1579    }
1580
1581    return_code = sqlite3_step(stmt_obj->stmt);
1582
1583    switch (return_code) {
1584        case SQLITE_ROW: /* Valid Row */
1585        case SQLITE_DONE: /* Valid but no results */
1586        {
1587            sqlite3_reset(stmt_obj->stmt);
1588            object_init_ex(return_value, php_sqlite3_result_entry);
1589            result = (php_sqlite3_result *)zend_object_store_get_object(return_value TSRMLS_CC);
1590
1591            Z_ADDREF_P(object);
1592
1593            result->is_prepared_statement = 1;
1594            result->db_obj = stmt_obj->db_obj;
1595            result->stmt_obj = stmt_obj;
1596            result->stmt_obj_zval = getThis();
1597
1598            break;
1599        }
1600        case SQLITE_ERROR:
1601            sqlite3_reset(stmt_obj->stmt);
1602
1603        default:
1604            php_sqlite3_error(stmt_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
1605            zval_dtor(return_value);
1606            RETURN_FALSE;
1607    }
1608
1609    return;
1610}
1611/* }}} */
1612
1613/* {{{ proto int SQLite3Stmt::__construct(SQLite3 dbobject, String Statement)
1614   __constructor for SQLite3Stmt. */
1615PHP_METHOD(sqlite3stmt, __construct)
1616{
1617    php_sqlite3_stmt *stmt_obj;
1618    php_sqlite3_db_object *db_obj;
1619    zval *object = getThis();
1620    zval *db_zval;
1621    char *sql;
1622    int sql_len, errcode;
1623    zend_error_handling error_handling;
1624    php_sqlite3_free_list *free_item;
1625
1626    stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1627    zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
1628
1629    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &db_zval, php_sqlite3_sc_entry, &sql, &sql_len) == FAILURE) {
1630        zend_restore_error_handling(&error_handling TSRMLS_CC);
1631        return;
1632    }
1633
1634    db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(db_zval TSRMLS_CC);
1635
1636    SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
1637
1638    zend_restore_error_handling(&error_handling TSRMLS_CC);
1639
1640    if (!sql_len) {
1641        RETURN_FALSE;
1642    }
1643
1644    stmt_obj->db_obj = db_obj;
1645    stmt_obj->db_obj_zval = db_zval;
1646
1647    Z_ADDREF_P(db_zval);
1648
1649    errcode = sqlite3_prepare_v2(db_obj->db, sql, sql_len, &(stmt_obj->stmt), NULL);
1650    if (errcode != SQLITE_OK) {
1651        php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
1652        zval_dtor(return_value);
1653        RETURN_FALSE;
1654    }
1655    stmt_obj->initialised = 1;
1656
1657    free_item = emalloc(sizeof(php_sqlite3_free_list));
1658    free_item->stmt_obj = stmt_obj;
1659    free_item->stmt_obj_zval = getThis();
1660
1661    zend_llist_add_element(&(db_obj->free_list), &free_item);
1662}
1663/* }}} */
1664
1665/* {{{ proto int SQLite3Result::numColumns()
1666   Number of columns in the result set. */
1667PHP_METHOD(sqlite3result, numColumns)
1668{
1669    php_sqlite3_result *result_obj;
1670    zval *object = getThis();
1671    result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
1672
1673    SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
1674
1675    if (zend_parse_parameters_none() == FAILURE) {
1676        return;
1677    }
1678
1679    RETURN_LONG(sqlite3_column_count(result_obj->stmt_obj->stmt));
1680}
1681/* }}} */
1682
1683/* {{{ proto string SQLite3Result::columnName(int column)
1684   Returns the name of the nth column. */
1685PHP_METHOD(sqlite3result, columnName)
1686{
1687    php_sqlite3_result *result_obj;
1688    zval *object = getThis();
1689    long column = 0;
1690    char *column_name;
1691    result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
1692
1693    SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
1694
1695    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &column) == FAILURE) {
1696        return;
1697    }
1698    column_name = (char*) sqlite3_column_name(result_obj->stmt_obj->stmt, column);
1699
1700    if (column_name == NULL) {
1701        RETURN_FALSE;
1702    }
1703
1704    RETVAL_STRING(column_name, 1);
1705}
1706/* }}} */
1707
1708/* {{{ proto int SQLite3Result::columnType(int column)
1709   Returns the type of the nth column. */
1710PHP_METHOD(sqlite3result, columnType)
1711{
1712    php_sqlite3_result *result_obj;
1713    zval *object = getThis();
1714    long column = 0;
1715    result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
1716
1717    SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
1718
1719    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &column) == FAILURE) {
1720        return;
1721    }
1722
1723    if (result_obj->complete) {
1724        RETURN_FALSE;
1725    }
1726
1727    RETURN_LONG(sqlite3_column_type(result_obj->stmt_obj->stmt, column));
1728}
1729/* }}} */
1730
1731/* {{{ proto array SQLite3Result::fetchArray([int mode])
1732   Fetch a result row as both an associative or numerically indexed array or both. */
1733PHP_METHOD(sqlite3result, fetchArray)
1734{
1735    php_sqlite3_result *result_obj;
1736    zval *object = getThis();
1737    int i, ret;
1738    long mode = PHP_SQLITE3_BOTH;
1739    result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
1740
1741    SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
1742
1743    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &mode) == FAILURE) {
1744        return;
1745    }
1746
1747    ret = sqlite3_step(result_obj->stmt_obj->stmt);
1748    switch (ret) {
1749        case SQLITE_ROW:
1750            /* If there was no return value then just skip fetching */
1751            if (!return_value_used) {
1752                return;
1753            }
1754
1755            array_init(return_value);
1756
1757            for (i = 0; i < sqlite3_data_count(result_obj->stmt_obj->stmt); i++) {
1758                zval *data;
1759
1760                data = sqlite_value_to_zval(result_obj->stmt_obj->stmt, i);
1761
1762                if (mode & PHP_SQLITE3_NUM) {
1763                    add_index_zval(return_value, i, data);
1764                }
1765
1766                if (mode & PHP_SQLITE3_ASSOC) {
1767                    if (mode & PHP_SQLITE3_NUM) {
1768                        Z_ADDREF_P(data);
1769                    }
1770                    add_assoc_zval(return_value, (char*)sqlite3_column_name(result_obj->stmt_obj->stmt, i), data);
1771                }
1772            }
1773            break;
1774
1775        case SQLITE_DONE:
1776            result_obj->complete = 1;
1777            RETURN_FALSE;
1778            break;
1779
1780        default:
1781            php_sqlite3_error(result_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(result_obj->stmt_obj->stmt)));
1782    }
1783}
1784/* }}} */
1785
1786/* {{{ proto bool SQLite3Result::reset()
1787   Resets the result set back to the first row. */
1788PHP_METHOD(sqlite3result, reset)
1789{
1790    php_sqlite3_result *result_obj;
1791    zval *object = getThis();
1792    result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
1793
1794    SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
1795
1796    if (zend_parse_parameters_none() == FAILURE) {
1797        return;
1798    }
1799
1800    if (sqlite3_reset(result_obj->stmt_obj->stmt) != SQLITE_OK) {
1801        RETURN_FALSE;
1802    }
1803
1804    result_obj->complete = 0;
1805
1806    RETURN_TRUE;
1807}
1808/* }}} */
1809
1810/* {{{ proto bool SQLite3Result::finalize()
1811   Closes the result set. */
1812PHP_METHOD(sqlite3result, finalize)
1813{
1814    php_sqlite3_result *result_obj;
1815    zval *object = getThis();
1816    result_obj = (php_sqlite3_result *)zend_object_store_get_object(object TSRMLS_CC);
1817
1818    SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
1819
1820    if (zend_parse_parameters_none() == FAILURE) {
1821        return;
1822    }
1823
1824    /* We need to finalize an internal statement */
1825    if (result_obj->is_prepared_statement == 0) {
1826        zend_llist_del_element(&(result_obj->db_obj->free_list), result_obj->stmt_obj_zval,
1827            (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
1828    } else {
1829        sqlite3_reset(result_obj->stmt_obj->stmt);
1830    }
1831
1832    RETURN_TRUE;
1833}
1834/* }}} */
1835
1836/* {{{ proto int SQLite3Result::__construct()
1837   __constructor for SQLite3Result. */
1838PHP_METHOD(sqlite3result, __construct)
1839{
1840    zend_throw_exception(zend_exception_get_default(TSRMLS_C), "SQLite3Result cannot be directly instantiated", 0 TSRMLS_CC);
1841}
1842/* }}} */
1843
1844/* {{{ arginfo */
1845ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_open, 0)
1846    ZEND_ARG_INFO(0, filename)
1847    ZEND_ARG_INFO(0, flags)
1848    ZEND_ARG_INFO(0, encryption_key)
1849ZEND_END_ARG_INFO()
1850
1851ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_busytimeout, 0)
1852    ZEND_ARG_INFO(0, ms)
1853ZEND_END_ARG_INFO()
1854
1855#ifndef SQLITE_OMIT_LOAD_EXTENSION
1856ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_loadextension, 0)
1857    ZEND_ARG_INFO(0, shared_library)
1858ZEND_END_ARG_INFO()
1859#endif
1860
1861ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_escapestring, 0, 0, 1)
1862    ZEND_ARG_INFO(0, value)
1863ZEND_END_ARG_INFO()
1864
1865ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_query, 0, 0, 1)
1866    ZEND_ARG_INFO(0, query)
1867ZEND_END_ARG_INFO()
1868
1869ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_querysingle, 0, 0, 1)
1870    ZEND_ARG_INFO(0, query)
1871    ZEND_ARG_INFO(0, entire_row)
1872ZEND_END_ARG_INFO()
1873
1874ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createfunction, 0, 0, 2)
1875    ZEND_ARG_INFO(0, name)
1876    ZEND_ARG_INFO(0, callback)
1877    ZEND_ARG_INFO(0, argument_count)
1878ZEND_END_ARG_INFO()
1879
1880ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createaggregate, 0, 0, 3)
1881    ZEND_ARG_INFO(0, name)
1882    ZEND_ARG_INFO(0, step_callback)
1883    ZEND_ARG_INFO(0, final_callback)
1884    ZEND_ARG_INFO(0, argument_count)
1885ZEND_END_ARG_INFO()
1886
1887ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3_createcollation, 0, 0, 2)
1888    ZEND_ARG_INFO(0, name)
1889    ZEND_ARG_INFO(0, callback)
1890ZEND_END_ARG_INFO()
1891
1892ZEND_BEGIN_ARG_INFO_EX(argingo_sqlite3_openblob, 0, 0, 3)
1893    ZEND_ARG_INFO(0, table)
1894    ZEND_ARG_INFO(0, column)
1895    ZEND_ARG_INFO(0, rowid)
1896    ZEND_ARG_INFO(0, dbname)
1897ZEND_END_ARG_INFO()
1898
1899ZEND_BEGIN_ARG_INFO_EX(argingo_sqlite3_enableexceptions, 0, 0, 1)
1900    ZEND_ARG_INFO(0, enableExceptions)
1901ZEND_END_ARG_INFO()
1902
1903ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3stmt_bindparam, 0, 0, 2)
1904    ZEND_ARG_INFO(0, param_number)
1905    ZEND_ARG_INFO(1, param)
1906    ZEND_ARG_INFO(0, type)
1907ZEND_END_ARG_INFO()
1908
1909ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3stmt_bindvalue, 0, 0, 2)
1910    ZEND_ARG_INFO(0, param_number)
1911    ZEND_ARG_INFO(0, param)
1912    ZEND_ARG_INFO(0, type)
1913ZEND_END_ARG_INFO()
1914
1915ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3stmt_construct, 0, 0, 1)
1916    ZEND_ARG_INFO(0, sqlite3)
1917ZEND_END_ARG_INFO()
1918
1919ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_columnname, 0, 0, 1)
1920    ZEND_ARG_INFO(0, column_number)
1921ZEND_END_ARG_INFO()
1922
1923ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_columntype, 0, 0, 1)
1924    ZEND_ARG_INFO(0, column_number)
1925ZEND_END_ARG_INFO()
1926
1927ZEND_BEGIN_ARG_INFO_EX(arginfo_sqlite3result_fetcharray, 0, 0, 1)
1928    ZEND_ARG_INFO(0, mode)
1929ZEND_END_ARG_INFO()
1930
1931ZEND_BEGIN_ARG_INFO(arginfo_sqlite3_void, 0)
1932ZEND_END_ARG_INFO()
1933/* }}} */
1934
1935/* {{{ php_sqlite3_class_methods */
1936static zend_function_entry php_sqlite3_class_methods[] = {
1937    PHP_ME(sqlite3,     open,               arginfo_sqlite3_open, ZEND_ACC_PUBLIC)
1938    PHP_ME(sqlite3,     close,              arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1939    PHP_ME(sqlite3,     exec,               arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
1940    PHP_ME(sqlite3,     version,            arginfo_sqlite3_void, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1941    PHP_ME(sqlite3,     lastInsertRowID,    arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1942    PHP_ME(sqlite3,     lastErrorCode,      arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1943    PHP_ME(sqlite3,     lastErrorMsg,       arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1944    PHP_ME(sqlite3,     busyTimeout,        arginfo_sqlite3_busytimeout, ZEND_ACC_PUBLIC)
1945#ifndef SQLITE_OMIT_LOAD_EXTENSION
1946    PHP_ME(sqlite3,     loadExtension,      arginfo_sqlite3_loadextension, ZEND_ACC_PUBLIC)
1947#endif
1948    PHP_ME(sqlite3,     changes,            arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1949    PHP_ME(sqlite3,     escapeString,       arginfo_sqlite3_escapestring, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1950    PHP_ME(sqlite3,     prepare,            arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
1951    PHP_ME(sqlite3,     query,              arginfo_sqlite3_query, ZEND_ACC_PUBLIC)
1952    PHP_ME(sqlite3,     querySingle,        arginfo_sqlite3_querysingle, ZEND_ACC_PUBLIC)
1953    PHP_ME(sqlite3,     createFunction,     arginfo_sqlite3_createfunction, ZEND_ACC_PUBLIC)
1954    PHP_ME(sqlite3,     createAggregate,    arginfo_sqlite3_createaggregate, ZEND_ACC_PUBLIC)
1955    PHP_ME(sqlite3,     createCollation,    arginfo_sqlite3_createcollation, ZEND_ACC_PUBLIC)
1956    PHP_ME(sqlite3,     openBlob,           argingo_sqlite3_openblob, ZEND_ACC_PUBLIC)
1957    PHP_ME(sqlite3,     enableExceptions,   argingo_sqlite3_enableexceptions, ZEND_ACC_PUBLIC)
1958    /* Aliases */
1959    PHP_MALIAS(sqlite3, __construct, open, arginfo_sqlite3_open, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1960    PHP_FE_END
1961};
1962/* }}} */
1963
1964/* {{{ php_sqlite3_stmt_class_methods */
1965static zend_function_entry php_sqlite3_stmt_class_methods[] = {
1966    PHP_ME(sqlite3stmt, paramCount, arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1967    PHP_ME(sqlite3stmt, close,      arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1968    PHP_ME(sqlite3stmt, reset,      arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1969    PHP_ME(sqlite3stmt, clear,      arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1970    PHP_ME(sqlite3stmt, execute,    arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1971    PHP_ME(sqlite3stmt, bindParam,  arginfo_sqlite3stmt_bindparam, ZEND_ACC_PUBLIC)
1972    PHP_ME(sqlite3stmt, bindValue,  arginfo_sqlite3stmt_bindvalue, ZEND_ACC_PUBLIC)
1973    PHP_ME(sqlite3stmt, readOnly,   arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1974    PHP_ME(sqlite3stmt, __construct, arginfo_sqlite3stmt_construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
1975    PHP_FE_END
1976};
1977/* }}} */
1978
1979/* {{{ php_sqlite3_result_class_methods */
1980static zend_function_entry php_sqlite3_result_class_methods[] = {
1981    PHP_ME(sqlite3result, numColumns,       arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1982    PHP_ME(sqlite3result, columnName,       arginfo_sqlite3result_columnname, ZEND_ACC_PUBLIC)
1983    PHP_ME(sqlite3result, columnType,       arginfo_sqlite3result_columntype, ZEND_ACC_PUBLIC)
1984    PHP_ME(sqlite3result, fetchArray,       arginfo_sqlite3result_fetcharray, ZEND_ACC_PUBLIC)
1985    PHP_ME(sqlite3result, reset,            arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1986    PHP_ME(sqlite3result, finalize,         arginfo_sqlite3_void, ZEND_ACC_PUBLIC)
1987    PHP_ME(sqlite3result, __construct,      arginfo_sqlite3_void, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
1988    PHP_FE_END
1989};
1990/* }}} */
1991
1992/* {{{ Authorization Callback
1993*/
1994static int php_sqlite3_authorizer(void *autharg, int access_type, const char *arg3, const char *arg4, const char *arg5, const char *arg6)
1995{
1996    switch (access_type) {
1997        case SQLITE_ATTACH:
1998        {
1999            if (memcmp(arg3, ":memory:", sizeof(":memory:")) && *arg3) {
2000                TSRMLS_FETCH();
2001
2002#if PHP_API_VERSION < 20100412
2003                if (PG(safe_mode) && (!php_checkuid(arg3, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
2004                    return SQLITE_DENY;
2005                }
2006#endif
2007
2008                if (php_check_open_basedir(arg3 TSRMLS_CC)) {
2009                    return SQLITE_DENY;
2010                }
2011            }
2012            return SQLITE_OK;
2013        }
2014
2015        default:
2016            /* access allowed */
2017            return SQLITE_OK;
2018    }
2019}
2020/* }}} */
2021
2022/* {{{ php_sqlite3_free_list_dtor
2023*/
2024static void php_sqlite3_free_list_dtor(void **item)
2025{
2026    php_sqlite3_free_list *free_item = (php_sqlite3_free_list *)*item;
2027
2028    if (free_item->stmt_obj && free_item->stmt_obj->initialised) {
2029        sqlite3_finalize(free_item->stmt_obj->stmt);
2030        free_item->stmt_obj->initialised = 0;
2031    }
2032    efree(*item);
2033}
2034/* }}} */
2035
2036static int php_sqlite3_compare_stmt_zval_free( php_sqlite3_free_list **free_list, zval *statement ) /* {{{ */
2037{
2038    return ((*free_list)->stmt_obj->initialised && statement == (*free_list)->stmt_obj_zval);
2039}
2040/* }}} */
2041
2042static int php_sqlite3_compare_stmt_free( php_sqlite3_free_list **free_list, sqlite3_stmt *statement ) /* {{{ */
2043{
2044    return ((*free_list)->stmt_obj->initialised && statement == (*free_list)->stmt_obj->stmt);
2045}
2046/* }}} */
2047
2048static void php_sqlite3_object_free_storage(void *object TSRMLS_DC) /* {{{ */
2049{
2050    php_sqlite3_db_object *intern = (php_sqlite3_db_object *)object;
2051    php_sqlite3_func *func;
2052    php_sqlite3_collation *collation;
2053
2054    if (!intern) {
2055        return;
2056    }
2057
2058    while (intern->funcs) {
2059        func = intern->funcs;
2060        intern->funcs = func->next;
2061        if (intern->initialised && intern->db) {
2062            sqlite3_create_function(intern->db, func->func_name, func->argc, SQLITE_UTF8, func, NULL, NULL, NULL);
2063        }
2064
2065        efree((char*)func->func_name);
2066
2067        if (func->func) {
2068            zval_ptr_dtor(&func->func);
2069        }
2070        if (func->step) {
2071            zval_ptr_dtor(&func->step);
2072        }
2073        if (func->fini) {
2074            zval_ptr_dtor(&func->fini);
2075        }
2076        efree(func);
2077    }
2078
2079    while (intern->collations){
2080        collation = intern->collations;
2081        intern->collations = collation->next;
2082        if (intern->initialised && intern->db){
2083            sqlite3_create_collation(intern->db, collation->collation_name, SQLITE_UTF8, NULL, NULL);
2084        }
2085        efree((char*)collation->collation_name);
2086        if (collation->cmp_func){
2087            zval_ptr_dtor(&collation->cmp_func);
2088        }
2089        efree(collation);
2090    }
2091
2092    if (intern->initialised && intern->db) {
2093        sqlite3_close(intern->db);
2094        intern->initialised = 0;
2095    }
2096
2097    zend_object_std_dtor(&intern->zo TSRMLS_CC);
2098    efree(intern);
2099}
2100/* }}} */
2101
2102static void php_sqlite3_stmt_object_free_storage(void *object TSRMLS_DC) /* {{{ */
2103{
2104    php_sqlite3_stmt *intern = (php_sqlite3_stmt *)object;
2105
2106    if (!intern) {
2107        return;
2108    }
2109
2110    if (intern->bound_params) {
2111        zend_hash_destroy(intern->bound_params);
2112        FREE_HASHTABLE(intern->bound_params);
2113        intern->bound_params = NULL;
2114    }
2115
2116    if (intern->initialised) {
2117        zend_llist_del_element(&(intern->db_obj->free_list), intern->stmt,
2118            (int (*)(void *, void *)) php_sqlite3_compare_stmt_free);
2119    }
2120
2121    if (intern->db_obj_zval) {
2122        zval_ptr_dtor(&intern->db_obj_zval);
2123    }
2124
2125    zend_object_std_dtor(&intern->zo TSRMLS_CC);
2126    efree(intern);
2127}
2128/* }}} */
2129
2130static void php_sqlite3_result_object_free_storage(void *object TSRMLS_DC) /* {{{ */
2131{
2132    php_sqlite3_result *intern = (php_sqlite3_result *)object;
2133
2134    if (!intern) {
2135        return;
2136    }
2137
2138    if (intern->stmt_obj_zval) {
2139        if (intern->stmt_obj->initialised) {
2140            sqlite3_reset(intern->stmt_obj->stmt);
2141        }
2142
2143        if (intern->is_prepared_statement == 0) {
2144            zval_dtor(intern->stmt_obj_zval);
2145            FREE_ZVAL(intern->stmt_obj_zval);
2146        } else {
2147            zval_ptr_dtor(&intern->stmt_obj_zval);
2148        }
2149    }
2150
2151    zend_object_std_dtor(&intern->zo TSRMLS_CC);
2152    efree(intern);
2153}
2154/* }}} */
2155
2156static zend_object_value php_sqlite3_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
2157{
2158    zend_object_value retval;
2159    php_sqlite3_db_object *intern;
2160
2161    /* Allocate memory for it */
2162    intern = emalloc(sizeof(php_sqlite3_db_object));
2163    memset(intern, 0, sizeof(php_sqlite3_db_object));
2164    intern->exception = 0;
2165
2166    /* Need to keep track of things to free */
2167    zend_llist_init(&(intern->free_list),   sizeof(php_sqlite3_free_list *), (llist_dtor_func_t)php_sqlite3_free_list_dtor, 0);
2168
2169    zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
2170    object_properties_init(&intern->zo, class_type);
2171
2172    retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_sqlite3_object_free_storage, NULL TSRMLS_CC);
2173    retval.handlers = (zend_object_handlers *) &sqlite3_object_handlers;
2174
2175    return retval;
2176}
2177/* }}} */
2178
2179static zend_object_value php_sqlite3_stmt_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
2180{
2181    zend_object_value retval;
2182    php_sqlite3_stmt *intern;
2183
2184    /* Allocate memory for it */
2185    intern = emalloc(sizeof(php_sqlite3_stmt));
2186    memset(intern, 0, sizeof(php_sqlite3_stmt));
2187
2188    intern->db_obj_zval = NULL;
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_stmt_object_free_storage, NULL TSRMLS_CC);
2194    retval.handlers = (zend_object_handlers *) &sqlite3_stmt_object_handlers;
2195
2196    return retval;
2197}
2198/* }}} */
2199
2200static zend_object_value php_sqlite3_result_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
2201{
2202    zend_object_value retval;
2203    php_sqlite3_result *intern;
2204
2205    /* Allocate memory for it */
2206    intern = emalloc(sizeof(php_sqlite3_result));
2207    memset(intern, 0, sizeof(php_sqlite3_result));
2208
2209    intern->complete = 0;
2210    intern->is_prepared_statement = 0;
2211    intern->stmt_obj_zval = NULL;
2212
2213    zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
2214    object_properties_init(&intern->zo, class_type);
2215
2216    retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) php_sqlite3_result_object_free_storage, NULL TSRMLS_CC);
2217    retval.handlers = (zend_object_handlers *) &sqlite3_result_object_handlers;
2218
2219    return retval;
2220}
2221/* }}} */
2222
2223static void sqlite3_param_dtor(void *data) /* {{{ */
2224{
2225    struct php_sqlite3_bound_param *param = (struct php_sqlite3_bound_param*)data;
2226
2227    if (param->name) {
2228        efree(param->name);
2229    }
2230
2231    if (param->parameter) {
2232        zval_ptr_dtor(&(param->parameter));
2233        param->parameter = NULL;
2234    }
2235}
2236/* }}} */
2237
2238/* {{{ PHP_MINIT_FUNCTION
2239*/
2240PHP_MINIT_FUNCTION(sqlite3)
2241{
2242    zend_class_entry ce;
2243
2244#if defined(ZTS)
2245    /* Refuse to load if this wasn't a threasafe library loaded */
2246    if (!sqlite3_threadsafe()) {
2247        php_error_docref(NULL TSRMLS_CC, E_WARNING, "A thread safe version of SQLite is required when using a thread safe version of PHP.");
2248        return FAILURE;
2249    }
2250#endif
2251
2252    memcpy(&sqlite3_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
2253    memcpy(&sqlite3_stmt_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
2254    memcpy(&sqlite3_result_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
2255
2256    /* Register SQLite 3 Class */
2257    INIT_CLASS_ENTRY(ce, "SQLite3", php_sqlite3_class_methods);
2258    ce.create_object = php_sqlite3_object_new;
2259    sqlite3_object_handlers.clone_obj = NULL;
2260    php_sqlite3_sc_entry = zend_register_internal_class(&ce TSRMLS_CC);
2261
2262    /* Register SQLite 3 Prepared Statement Class */
2263    INIT_CLASS_ENTRY(ce, "SQLite3Stmt", php_sqlite3_stmt_class_methods);
2264    ce.create_object = php_sqlite3_stmt_object_new;
2265    sqlite3_stmt_object_handlers.clone_obj = NULL;
2266    php_sqlite3_stmt_entry = zend_register_internal_class(&ce TSRMLS_CC);
2267
2268    /* Register SQLite 3 Result Class */
2269    INIT_CLASS_ENTRY(ce, "SQLite3Result", php_sqlite3_result_class_methods);
2270    ce.create_object = php_sqlite3_result_object_new;
2271    sqlite3_result_object_handlers.clone_obj = NULL;
2272    php_sqlite3_result_entry = zend_register_internal_class(&ce TSRMLS_CC);
2273
2274    REGISTER_INI_ENTRIES();
2275
2276    REGISTER_LONG_CONSTANT("SQLITE3_ASSOC", PHP_SQLITE3_ASSOC, CONST_CS | CONST_PERSISTENT);
2277    REGISTER_LONG_CONSTANT("SQLITE3_NUM", PHP_SQLITE3_NUM, CONST_CS | CONST_PERSISTENT);
2278    REGISTER_LONG_CONSTANT("SQLITE3_BOTH", PHP_SQLITE3_BOTH, CONST_CS | CONST_PERSISTENT);
2279
2280    REGISTER_LONG_CONSTANT("SQLITE3_INTEGER", SQLITE_INTEGER, CONST_CS | CONST_PERSISTENT);
2281    REGISTER_LONG_CONSTANT("SQLITE3_FLOAT", SQLITE_FLOAT, CONST_CS | CONST_PERSISTENT);
2282    REGISTER_LONG_CONSTANT("SQLITE3_TEXT", SQLITE3_TEXT, CONST_CS | CONST_PERSISTENT);
2283    REGISTER_LONG_CONSTANT("SQLITE3_BLOB", SQLITE_BLOB, CONST_CS | CONST_PERSISTENT);
2284    REGISTER_LONG_CONSTANT("SQLITE3_NULL", SQLITE_NULL, CONST_CS | CONST_PERSISTENT);
2285
2286    REGISTER_LONG_CONSTANT("SQLITE3_OPEN_READONLY", SQLITE_OPEN_READONLY, CONST_CS | CONST_PERSISTENT);
2287    REGISTER_LONG_CONSTANT("SQLITE3_OPEN_READWRITE", SQLITE_OPEN_READWRITE, CONST_CS | CONST_PERSISTENT);
2288    REGISTER_LONG_CONSTANT("SQLITE3_OPEN_CREATE", SQLITE_OPEN_CREATE, CONST_CS | CONST_PERSISTENT);
2289
2290    return SUCCESS;
2291}
2292/* }}} */
2293
2294/* {{{ PHP_MSHUTDOWN_FUNCTION
2295*/
2296PHP_MSHUTDOWN_FUNCTION(sqlite3)
2297{
2298    UNREGISTER_INI_ENTRIES();
2299
2300    return SUCCESS;
2301}
2302/* }}} */
2303
2304/* {{{ PHP_MINFO_FUNCTION
2305*/
2306PHP_MINFO_FUNCTION(sqlite3)
2307{
2308    php_info_print_table_start();
2309    php_info_print_table_header(2, "SQLite3 support", "enabled");
2310    php_info_print_table_row(2, "SQLite3 module version", PHP_SQLITE3_VERSION);
2311    php_info_print_table_row(2, "SQLite Library", sqlite3_libversion());
2312    php_info_print_table_end();
2313
2314    DISPLAY_INI_ENTRIES();
2315}
2316/* }}} */
2317
2318/* {{{ PHP_GINIT_FUNCTION
2319*/
2320static PHP_GINIT_FUNCTION(sqlite3)
2321{
2322    memset(sqlite3_globals, 0, sizeof(*sqlite3_globals));
2323}
2324/* }}} */
2325
2326/* {{{ sqlite3_module_entry
2327*/
2328zend_module_entry sqlite3_module_entry = {
2329    STANDARD_MODULE_HEADER,
2330    "sqlite3",
2331    NULL,
2332    PHP_MINIT(sqlite3),
2333    PHP_MSHUTDOWN(sqlite3),
2334    NULL,
2335    NULL,
2336    PHP_MINFO(sqlite3),
2337    PHP_SQLITE3_VERSION,
2338    PHP_MODULE_GLOBALS(sqlite3),
2339    PHP_GINIT(sqlite3),
2340    NULL,
2341    NULL,
2342    STANDARD_MODULE_PROPERTIES_EX
2343};
2344/* }}} */
2345
2346#ifdef COMPILE_DL_SQLITE3
2347ZEND_GET_MODULE(sqlite3)
2348#endif
2349
2350/*
2351 * Local variables:
2352 * tab-width: 4
2353 * c-basic-offset: 4
2354 * End:
2355 * vim600: sw=4 ts=4 fdm=marker
2356 * vim<600: sw=4 ts=4
2357 */
2358