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