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