diff options
| author | Marcus Boerger <helly@php.net> | 2003-06-04 20:54:48 +0000 | 
|---|---|---|
| committer | Marcus Boerger <helly@php.net> | 2003-06-04 20:54:48 +0000 | 
| commit | b70cf4074252a2e524ead2927c8e29ecb2eaa25e (patch) | |
| tree | d764afe2e6f8b0b2cfef5e833f8b26edb984e76e /ext/sqlite/sqlite.c | |
| parent | a8c7c3bc5e93a0de0639bd25965a15375db0d04b (diff) | |
| download | php-git-b70cf4074252a2e524ead2927c8e29ecb2eaa25e.tar.gz | |
Goodbye  namespaces
Diffstat (limited to 'ext/sqlite/sqlite.c')
| -rw-r--r-- | ext/sqlite/sqlite.c | 631 | 
1 files changed, 530 insertions, 101 deletions
| diff --git a/ext/sqlite/sqlite.c b/ext/sqlite/sqlite.c index 5aa2a55b21..150dac5b6d 100644 --- a/ext/sqlite/sqlite.c +++ b/ext/sqlite/sqlite.c @@ -38,6 +38,13 @@  #include <sqlite.h> +#ifdef HAVE_SPL +#include "ext/spl/php_spl.h" +#include "ext/spl/spl_functions.h" +#endif + +#include "Zend/zend_default_classes.h" +  #ifndef safe_emalloc  #define safe_emalloc(a,b,c) emalloc((a)*(b)+(c))  #endif @@ -70,13 +77,49 @@ static inline void php_sqlite_strtolower(char *s)  /* {{{ PHP_INI   */  PHP_INI_BEGIN() -STD_PHP_INI_ENTRY_EX("sqlite.assoc_case", "0", PHP_INI_ALL, OnUpdateInt, assoc_case, zend_sqlite_globals, sqlite_globals, display_link_numbers) +STD_PHP_INI_ENTRY_EX("sqlite.assoc_case", "0", PHP_INI_ALL, OnUpdateLong, assoc_case, zend_sqlite_globals, sqlite_globals, display_link_numbers)  PHP_INI_END()  /* }}} */  #define DB_FROM_ZVAL(db, zv)	ZEND_FETCH_RESOURCE2(db, struct php_sqlite_db *, zv, -1, "sqlite database", le_sqlite_db, le_sqlite_pdb) +#define DB_FROM_OBJECT(db, object) \ +	{ \ +		sqlite_object *obj = (sqlite_object*) zend_object_store_get_object(object TSRMLS_CC); \ +		db = obj->u.db; \ +		if (!db) { \ +			php_error_docref(NULL TSRMLS_CC, E_WARNING, "The database wasn't opened"); \ +			RETURN_NULL(); \ +		} \ +	} + +#define RES_FROM_OBJECT(res, object) \ +	{ \ +		sqlite_object *obj = (sqlite_object*) zend_object_store_get_object(object TSRMLS_CC); \ +		res = obj->u.res; \ +		if (!res) { \ +			php_error_docref(NULL TSRMLS_CC, E_WARNING, "No result set available"); \ +			RETURN_NULL(); \ +		} \ +	} + +#define SQLITE_THROW(message) \ +	PG(suppress_errors) = 0; \ +	EG(exception) = sqlite_instanciate(sqlite_ce_exception, NULL TSRMLS_CC); \ +	{ \ +		zval *tmp; \ +		MAKE_STD_ZVAL(tmp); \ +		ZVAL_STRING(tmp, message, 1); \ +		zend_hash_update(Z_OBJPROP_P(EG(exception)), "message", sizeof("message"), (void **) message, sizeof(zval *), NULL); \ +		MAKE_STD_ZVAL(tmp); \ +		ZVAL_STRING(tmp, zend_get_executed_filename(TSRMLS_C), 1); \ +		zend_hash_update(Z_OBJPROP_P(EG(exception)), "file", sizeof("file"), (void **) &tmp, sizeof(zval *), NULL); \ +		MAKE_STD_ZVAL(tmp); \ +		ZVAL_LONG(tmp, zend_get_executed_lineno(TSRMLS_C)); \ +		zend_hash_update(Z_OBJPROP_P(EG(exception)), "line", sizeof("line"), (void **) &tmp, sizeof(zval *), NULL); \ +	} +  struct php_sqlite_result {  	struct php_sqlite_db *db;  	sqlite_vm *vm; @@ -136,9 +179,46 @@ function_entry sqlite_functions[] = {  	PHP_FE(sqlite_unbuffered_query, NULL)  	PHP_FE(sqlite_create_aggregate, NULL)  	PHP_FE(sqlite_create_function, NULL) +	PHP_FE(sqlite_factory, NULL) +	{NULL, NULL, NULL} +}; + +#define PHP_ME_MAPPING(name, func_name, arg_types) \ +	ZEND_NAMED_FE(name, ZEND_FN(func_name), arg_types) + +function_entry sqlite_funcs_db[] = { +	PHP_ME_MAPPING(sqlite_db, sqlite_open, NULL) +	PHP_ME_MAPPING(close, sqlite_close, NULL) +	PHP_ME_MAPPING(query, sqlite_query, NULL) +	PHP_ME_MAPPING(unbuffered_query, sqlite_unbuffered_query, NULL) +	PHP_ME_MAPPING(last_insert_rowid, sqlite_last_insert_rowid, NULL) +	PHP_ME_MAPPING(create_aggregate, sqlite_create_aggregate, NULL) +	PHP_ME_MAPPING(create_function, sqlite_create_function, NULL) +	PHP_ME_MAPPING(busy_timeout, sqlite_busy_timeout, NULL) +	PHP_ME_MAPPING(last_error, sqlite_last_error, NULL) +/*	PHP_ME_MAPPING(error_string, sqlite_error_string, NULL) static */ +/*	PHP_ME_MAPPING(escape_string, sqlite_escape_string, NULL) static */  	{NULL, NULL, NULL}  }; +function_entry sqlite_funcs_query[] = { +	PHP_ME_MAPPING(fetch_array, sqlite_fetch_array, NULL) +	PHP_ME_MAPPING(current, sqlite_current, NULL) +	PHP_ME_MAPPING(column, sqlite_column, NULL) +	PHP_ME_MAPPING(changes, sqlite_changes, NULL) +	PHP_ME_MAPPING(num_rows, sqlite_num_rows, NULL) +	PHP_ME_MAPPING(num_fields, sqlite_num_fields, NULL) +	PHP_ME_MAPPING(field_name, sqlite_field_name, NULL) +	PHP_ME_MAPPING(seek, sqlite_seek, NULL) +	PHP_ME_MAPPING(rewind, sqlite_rewind, NULL) +	PHP_ME_MAPPING(next, sqlite_next, NULL) +	PHP_ME_MAPPING(has_more, sqlite_has_more, NULL) +	{NULL, NULL, NULL} +}; + +function_entry sqlite_funcs_exception[] = { +	{NULL, NULL, NULL} +};  zend_module_entry sqlite_module_entry = {  #if ZEND_MODULE_API_NO >= 20010901 @@ -148,7 +228,7 @@ zend_module_entry sqlite_module_entry = {  	sqlite_functions,  	PHP_MINIT(sqlite),  	NULL, -	NULL, +	PHP_RINIT(sqlite),  	PHP_RSHUTDOWN(sqlite),  	PHP_MINFO(sqlite),  #if ZEND_MODULE_API_NO >= 20010901 @@ -576,6 +656,130 @@ static int php_sqlite_authorizer(void *autharg, int access_type, const char *arg  }  /* }}} */ +/* {{{ OO init/structure stuff */ +#define REGISTER_SQLITE_CLASS(name, parent) \ +	{ \ +		zend_class_entry ce; \ +		INIT_CLASS_ENTRY(ce, "sqlite_" # name, sqlite_funcs_ ## name); \ +		ce.create_object = sqlite_object_new_ ## name; \ +		sqlite_ce_ ## name = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \ +		memcpy(&sqlite_object_handlers_ ## name, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \ +	} + +zend_class_entry *sqlite_ce_db, *sqlite_ce_query, *sqlite_ce_exception; + +static zend_object_handlers sqlite_object_handlers_db; +static zend_object_handlers sqlite_object_handlers_query; +static zend_object_handlers sqlite_object_handlers_exception; + +typedef enum { +	is_db, +	is_result +} sqlite_obj_type; + +typedef struct _sqlite_object { +	zend_object       std; +	sqlite_obj_type   type; +	union { +		struct php_sqlite_db     *db; +		struct php_sqlite_result *res; +		void *ptr; +	} u; +} sqlite_object; + +static void sqlite_object_clone(void *object, void **object_clone TSRMLS_DC) +{ +	/* TODO */ +} + +static int sqlite_free_persistent(list_entry *le, void *ptr TSRMLS_DC) +{ +	return le->ptr == ptr; +} + +static void sqlite_object_dtor(void *object, zend_object_handle handle TSRMLS_DC) +{ +	sqlite_object *intern = (sqlite_object *)object; + +	zend_hash_destroy(intern->std.properties); +	FREE_HASHTABLE(intern->std.properties); + +	if (intern->u.ptr) { +		if (intern->type == is_db) { +			if (intern->u.db->rsrc_id) { +				zend_list_delete(intern->u.db->rsrc_id); +				zend_hash_apply_with_argument(&EG(persistent_list), (apply_func_arg_t) sqlite_free_persistent, &intern->u.ptr TSRMLS_CC); +			} +		} else { +			real_result_dtor(intern->u.res TSRMLS_CC); +		} +	} + +	efree(object); +} + +static void sqlite_object_new(zend_class_entry *class_type, zend_object_handlers *handlers, zend_object_value *retval TSRMLS_DC) +{ +	sqlite_object *intern; +	zval *tmp; + +	intern = emalloc(sizeof(sqlite_object)); +	memset(intern, 0, sizeof(sqlite_object)); +	intern->std.ce = class_type; + +	ALLOC_HASHTABLE(intern->std.properties); +	zend_hash_init(intern->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0); +	zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *)); + +	retval->handle = zend_objects_store_put(intern, sqlite_object_dtor, sqlite_object_clone TSRMLS_CC); +	retval->handlers = handlers; +} + +static zend_object_value sqlite_object_new_db(zend_class_entry *class_type TSRMLS_DC) +{ +	zend_object_value retval; +	 +	sqlite_object_new(class_type, &sqlite_object_handlers_db, &retval TSRMLS_CC); +	return retval; +} + +static zend_object_value sqlite_object_new_query(zend_class_entry *class_type TSRMLS_DC) +{ +	zend_object_value retval; +	 +	sqlite_object_new(class_type, &sqlite_object_handlers_query, &retval TSRMLS_CC); +	return retval; +} + +static zend_object_value sqlite_object_new_exception(zend_class_entry *class_type TSRMLS_DC) +{ +	zend_object_value retval; +	 +	sqlite_object_new(class_type, &sqlite_object_handlers_exception, &retval TSRMLS_CC); +	return retval; +} + +#define SQLITE_REGISTER_OBJECT(_type, _object, _ptr) \ +	{ \ +		sqlite_object *obj; \ +		obj = (sqlite_object*)zend_object_store_get_object(_object TSRMLS_CC); \ +		obj->type = is_ ## _type; \ +		obj->u._type = _ptr; \ +	} + +static zval * sqlite_instanciate(zend_class_entry *pce, zval *object TSRMLS_DC) +{ +	if (!object) { +		ALLOC_ZVAL(object); +	} +	Z_TYPE_P(object) = IS_OBJECT; +	object_init_ex(object, pce); +	object->refcount = 1; +	object->is_ref = 1; +	return object; +} +/* }}} */ +  static int init_sqlite_globals(zend_sqlite_globals *g)  {  	g->assoc_case = 0; @@ -584,8 +788,12 @@ static int init_sqlite_globals(zend_sqlite_globals *g)  PHP_MINIT_FUNCTION(sqlite)  { +	REGISTER_SQLITE_CLASS(db,        NULL); +	REGISTER_SQLITE_CLASS(query,     NULL); +	REGISTER_SQLITE_CLASS(exception, zend_exception_get_default()); +  	ZEND_INIT_MODULE_GLOBALS(sqlite, init_sqlite_globals, NULL); -	 +  	REGISTER_INI_ENTRIES();  	le_sqlite_db = zend_register_list_destructors_ex(php_sqlite_db_dtor, NULL, "sqlite database", module_number); @@ -626,6 +834,17 @@ PHP_MINIT_FUNCTION(sqlite)  	return SUCCESS;  } +PHP_RINIT_FUNCTION(sqlite) +{ +#ifdef HAVE_SPL +	if (!sqlite_ce_query->num_interfaces) { +		spl_register_implement(sqlite_ce_query, spl_ce_forward TSRMLS_CC); +	} +#endif + +	return SUCCESS; +} +  PHP_MINFO_FUNCTION(sqlite)  {  	php_info_print_table_start(); @@ -638,7 +857,7 @@ PHP_MINFO_FUNCTION(sqlite)  	DISPLAY_INI_ENTRIES();  } -static struct php_sqlite_db *php_sqlite_open(char *filename, int mode, char *persistent_id, zval *return_value, zval *errmsg TSRMLS_DC) +static struct php_sqlite_db *php_sqlite_open(char *filename, int mode, char *persistent_id, zval *return_value, zval *errmsg, zval *object TSRMLS_DC)  {  	char *errtext = NULL;  	sqlite *sdb = NULL; @@ -656,7 +875,12 @@ static struct php_sqlite_db *php_sqlite_open(char *filename, int mode, char *per  		sqlite_freemem(errtext); -		RETVAL_FALSE; +		/* if object is not an object then we're called from the factory() function */ +		if (object && Z_TYPE_P(object) != IS_OBJECT) { +			RETVAL_NULL(); +		} else { +			RETVAL_FALSE; +		}  		return NULL;  	} @@ -677,7 +901,15 @@ static struct php_sqlite_db *php_sqlite_open(char *filename, int mode, char *per  	/* authorizer hook so we can enforce safe mode */  	sqlite_set_authorizer(sdb, php_sqlite_authorizer, NULL); -	db->rsrc_id = ZEND_REGISTER_RESOURCE(return_value, db, persistent_id ? le_sqlite_pdb : le_sqlite_db); +	db->rsrc_id = ZEND_REGISTER_RESOURCE(object ? NULL : return_value, db, persistent_id ? le_sqlite_pdb : le_sqlite_db); +	if (object) { +		/* if object is not an object then we're called from the factory() function */ +		if (Z_TYPE_P(object) != IS_OBJECT) { +			sqlite_instanciate(sqlite_ce_db, object TSRMLS_CC); +		} +		/* and now register the object */ +		SQLITE_REGISTER_OBJECT(db, object, db) +	}  	if (persistent_id) {  		list_entry le; @@ -748,7 +980,7 @@ PHP_FUNCTION(sqlite_popen)  	}  	/* now we need to open the database */ -	php_sqlite_open(fullpath, mode, hashkey, return_value, errmsg TSRMLS_CC); +	php_sqlite_open(fullpath, mode, hashkey, return_value, errmsg, NULL TSRMLS_CC);  	efree(fullpath);  	efree(hashkey); @@ -763,21 +995,68 @@ PHP_FUNCTION(sqlite_open)  	char *filename;  	long filename_len;  	zval *errmsg = NULL; +	zval *object = getThis(); +	php_set_error_handling(object ? EH_THROW : EH_NORMAL, zend_exception_get_default() TSRMLS_CC);  	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz/",  				&filename, &filename_len, &mode, &errmsg)) { +		php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);  		return;  	}  	if (PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { -		RETURN_FALSE; +		php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); +		if (object) { +			RETURN_NULL(); +		} else { +			RETURN_FALSE; +		}  	}  	if (php_check_open_basedir(filename TSRMLS_CC)) { -		RETURN_FALSE; +		php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); +		if (object) { +			RETURN_NULL(); +		} else { +			RETURN_FALSE; +		}  	} -	php_sqlite_open(filename, mode, NULL, return_value, errmsg TSRMLS_CC); +	php_sqlite_open(filename, mode, NULL, return_value, errmsg, object TSRMLS_CC); + +	php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); +} +/* }}} */ + +/* {{{ proto object factory(string filename [, int mode, string &errmessage]) +   Opens a SQLite database and create an object for it. Will create the database if it does not exist */ +PHP_FUNCTION(sqlite_factory) +{ +	int mode = 0666; +	char *filename; +	long filename_len; +	zval *errmsg = NULL; + +	php_set_error_handling(EH_THROW, sqlite_ce_exception TSRMLS_CC); +	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz/", +				&filename, &filename_len, &mode, &errmsg)) { +		php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); +		RETURN_NULL(); +	} + +	if (PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { +		php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); +		RETURN_NULL(); +	} + +	if (php_check_open_basedir(filename TSRMLS_CC)) { +		php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); +		RETURN_NULL(); +	} + +	php_sqlite_open(filename, mode, NULL, return_value, errmsg, return_value TSRMLS_CC); + +	php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);  }  /* }}} */ @@ -788,13 +1067,20 @@ PHP_FUNCTION(sqlite_busy_timeout)  	zval *zdb;  	struct php_sqlite_db *db;  	long ms; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zdb, &ms)) { -		return; +	if (object) { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ms)) { +			return; +		} +		DB_FROM_OBJECT(db, object); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zdb, &ms)) { +			return; +		} +		DB_FROM_ZVAL(db, &zdb);  	} -	DB_FROM_ZVAL(db, &zdb); -  	sqlite_busy_timeout(db->db, ms);  }  /* }}} */ @@ -805,18 +1091,17 @@ PHP_FUNCTION(sqlite_close)  {  	zval *zdb;  	struct php_sqlite_db *db; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) { -		return; -	} -	DB_FROM_ZVAL(db, &zdb); - -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) { -		return; +	if (object) { +		php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Ignored, you must destruct the object instead"); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) { +			return; +		} +		DB_FROM_ZVAL(db, &zdb);  	} -	DB_FROM_ZVAL(db, &zdb); -  	zend_list_delete(Z_RESVAL_P(zdb));  }  /* }}} */ @@ -911,7 +1196,7 @@ next_row:  /* }}} */  /* {{{ sqlite_query */ -void sqlite_query(struct php_sqlite_db *db, char *sql, long sql_len, int mode, int buffered, zval *return_value TSRMLS_DC) +void sqlite_query(zval *object, struct php_sqlite_db *db, char *sql, long sql_len, int mode, int buffered, zval *return_value TSRMLS_DC)  {  	struct php_sqlite_result res, *rres;  	int ret; @@ -946,7 +1231,15 @@ void sqlite_query(struct php_sqlite_db *db, char *sql, long sql_len, int mode, i  	rres->curr_row = 0; -	ZEND_REGISTER_RESOURCE(return_value, rres, le_sqlite_result); +	if (object) { +		sqlite_object *obj; +		sqlite_instanciate(sqlite_ce_query, return_value TSRMLS_CC); +		obj = (sqlite_object *) zend_object_store_get_object(return_value TSRMLS_CC); +		obj->type = is_result; +		obj->u.res = rres; +	} else { +		ZEND_REGISTER_RESOURCE(object ? NULL : return_value, rres, le_sqlite_result); +	}  }  /* }}} */ @@ -960,15 +1253,22 @@ PHP_FUNCTION(sqlite_unbuffered_query)  	long sql_len;  	int mode = PHPSQLITE_BOTH;  	char *errtext = NULL; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, +	if (object) { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &sql, &sql_len, &mode)) { +			return; +		} +		DB_FROM_OBJECT(db, object); +	} else { +		if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,  				ZEND_NUM_ARGS() TSRMLS_CC, "sr|l", &sql, &sql_len, &zdb, &mode) &&   			FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &zdb, &sql, &sql_len, &mode)) { -		return; +			return; +		} +		DB_FROM_ZVAL(db, &zdb);  	} -	DB_FROM_ZVAL(db, &zdb); -  	/* avoid doing work if we can */  	if (!return_value_used) {  		db->last_err_code = sqlite_exec(db->db, sql, NULL, NULL, &errtext); @@ -979,8 +1279,8 @@ PHP_FUNCTION(sqlite_unbuffered_query)  		}  		return;  	} -	 -	sqlite_query(db, sql, sql_len, mode, 0, return_value TSRMLS_CC); + +	sqlite_query(object, db, sql, sql_len, mode, 0, return_value TSRMLS_CC);  }  /* }}} */ @@ -994,26 +1294,34 @@ PHP_FUNCTION(sqlite_query)  	long sql_len;  	int mode = PHPSQLITE_BOTH;  	char *errtext = NULL; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, -			ZEND_NUM_ARGS() TSRMLS_CC, "sr|l", &sql, &sql_len, &zdb, &mode) &&  -		FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &zdb, &sql, &sql_len, &mode)) { -		return; +	if (object) { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &sql, &sql_len, &mode)) { +			return; +		} +		DB_FROM_OBJECT(db, object); +	} else { +		if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, +				ZEND_NUM_ARGS() TSRMLS_CC, "sr|l", &sql, &sql_len, &zdb, &mode) &&  +			FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &zdb, &sql, &sql_len, &mode)) { +			return; +		} +		DB_FROM_ZVAL(db, &zdb);  	} -	DB_FROM_ZVAL(db, &zdb);  	/* avoid doing work if we can */  	if (!return_value_used) {  		db->last_err_code = sqlite_exec(db->db, sql, NULL, NULL, &errtext);  		if (db->last_err_code != SQLITE_OK) { -		php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext); -		sqlite_freemem(errtext); -	} +			php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errtext); +			sqlite_freemem(errtext); +		}  		return;  	} -	 -	sqlite_query(db, sql, sql_len, mode, 1, return_value TSRMLS_CC); + +	sqlite_query(object, db, sql, sql_len, mode, 1, return_value TSRMLS_CC);  }  /* }}} */ @@ -1022,7 +1330,7 @@ static void php_sqlite_fetch_array(struct php_sqlite_result *res, int mode, zend  {  	int j;  	const char **rowdata, **colnames; -	 +  	/* check range of the row */  	if (res->curr_row >= res->nrows) {  		/* no more */ @@ -1155,13 +1463,24 @@ PHP_FUNCTION(sqlite_fetch_array)  	int mode = PHPSQLITE_BOTH;  	zend_bool decode_binary = 1;  	struct php_sqlite_result *res; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|lb", &zres, &mode, &decode_binary)) { -		return; -	} -	ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result); -	if (ZEND_NUM_ARGS() < 2) { -		mode = res->mode; +	if (object) { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lb", &mode, &decode_binary)) { +			return; +		} +		RES_FROM_OBJECT(res, object); +		if (!ZEND_NUM_ARGS()) { +			mode = res->mode; +		} +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|lb", &zres, &mode, &decode_binary)) { +			return; +		} +		ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result); +		if (ZEND_NUM_ARGS() < 2) { +			mode = res->mode; +		}  	}  	php_sqlite_fetch_array(res, mode, decode_binary, 1, return_value TSRMLS_CC); @@ -1226,13 +1545,24 @@ PHP_FUNCTION(sqlite_current)  	int mode = PHPSQLITE_BOTH;  	zend_bool decode_binary = 1;  	struct php_sqlite_result *res; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|lb", &zres, &mode, &decode_binary)) { -		return; -	} -	ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result); -	if (ZEND_NUM_ARGS() < 2) { -		mode = res->mode; +	if (object) { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lb", &mode, &decode_binary)) { +			return; +		} +		RES_FROM_OBJECT(res, object); +		if (!ZEND_NUM_ARGS()) { +			mode = res->mode; +		} +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|lb", &zres, &mode, &decode_binary)) { +			return; +		} +		ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result); +		if (ZEND_NUM_ARGS() < 2) { +			mode = res->mode; +		}  	}  	php_sqlite_fetch_array(res, mode, decode_binary, 0, return_value TSRMLS_CC); @@ -1247,11 +1577,19 @@ PHP_FUNCTION(sqlite_column)  	zval *which;  	zend_bool decode_binary = 1;  	struct php_sqlite_result *res; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|b", &zres, &which, &decode_binary)) { -		return; +	if (object) { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &which, &decode_binary)) { +			return; +		} +		RES_FROM_OBJECT(res, object); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|b", &zres, &which, &decode_binary)) { +			return; +		} +		ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);  	} -	ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);  	php_sqlite_fetch_column(res, which, decode_binary, return_value TSRMLS_CC);  } @@ -1285,13 +1623,20 @@ PHP_FUNCTION(sqlite_changes)  {  	zval *zdb;  	struct php_sqlite_db *db; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) { -		return; +	if (object) { +		if (ZEND_NUM_ARGS() != 0) { +			WRONG_PARAM_COUNT +		} +		DB_FROM_OBJECT(db, object); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) { +			return; +		} +		DB_FROM_ZVAL(db, &zdb);  	} -	DB_FROM_ZVAL(db, &zdb); -  	RETURN_LONG(sqlite_changes(db->db));  }  /* }}} */ @@ -1302,13 +1647,20 @@ PHP_FUNCTION(sqlite_last_insert_rowid)  {  	zval *zdb;  	struct php_sqlite_db *db; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) { -		return; +	if (object) { +		if (ZEND_NUM_ARGS() != 0) { +			WRONG_PARAM_COUNT +		} +		DB_FROM_OBJECT(db, object); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) { +			return; +		} +		DB_FROM_ZVAL(db, &zdb);  	} -	DB_FROM_ZVAL(db, &zdb); -  	RETURN_LONG(sqlite_last_insert_rowid(db->db));  }  /* }}} */ @@ -1319,13 +1671,20 @@ PHP_FUNCTION(sqlite_num_rows)  {  	zval *zres;  	struct php_sqlite_result *res; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) { -		return; +	if (object) { +		if (ZEND_NUM_ARGS() != 0) { +			WRONG_PARAM_COUNT +		} +		RES_FROM_OBJECT(res, object); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) { +			return; +		} +		ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);  	} -	ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result); -  	if (res->buffered) {  		RETURN_LONG(res->nrows);  	} else { @@ -1341,11 +1700,19 @@ PHP_FUNCTION(sqlite_has_more)  {  	zval *zres;  	struct php_sqlite_result *res; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) { -		return; +	if (object) { +		if (ZEND_NUM_ARGS() != 0) { +			WRONG_PARAM_COUNT +		} +		RES_FROM_OBJECT(res, object); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) { +			return; +		} +		ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);  	} -	ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);  	RETURN_BOOL(res->nrows && res->curr_row < res->nrows); /* curr_row may be -1 */  } @@ -1357,13 +1724,20 @@ PHP_FUNCTION(sqlite_num_fields)  {  	zval *zres;  	struct php_sqlite_result *res; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) { -		return; +	if (object) { +		if (ZEND_NUM_ARGS() != 0) { +			WRONG_PARAM_COUNT +		} +		RES_FROM_OBJECT(res, object); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) { +			return; +		} +		ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);  	} -	ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result); -  	RETURN_LONG(res->ncolumns);  }  /* }}} */ @@ -1375,13 +1749,20 @@ PHP_FUNCTION(sqlite_field_name)  	zval *zres;  	struct php_sqlite_result *res;  	int field; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zres, &field)) { -		return; +	if (object) { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &field)) { +			return; +		} +		RES_FROM_OBJECT(res, object); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zres, &field)) { +			return; +		} +		ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);  	} -	ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result); -  	if (field < 0 || field >= res->ncolumns) {  		php_error_docref(NULL TSRMLS_CC, E_WARNING, "field %d out of range", field);  		RETURN_FALSE; @@ -1398,13 +1779,20 @@ PHP_FUNCTION(sqlite_seek)  	zval *zres;  	struct php_sqlite_result *res;  	int row; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zres, &row)) { -		return; +	if (object) { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &row)) { +			return; +		} +		RES_FROM_OBJECT(res, object); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zres, &row)) { +			return; +		} +		ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);  	} -	ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result); -  	if (!res->buffered) {  		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot seek an unbuffered result set");  		RETURN_FALSE; @@ -1426,11 +1814,19 @@ PHP_FUNCTION(sqlite_rewind)  {  	zval *zres;  	struct php_sqlite_result *res; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) { -		return; +	if (object) { +		if (ZEND_NUM_ARGS() != 0) { +			WRONG_PARAM_COUNT +		} +		RES_FROM_OBJECT(res, object); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) { +			return; +		} +		ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);  	} -	ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);  	if (!res->buffered) {  		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot seek an unbuffered result set"); @@ -1453,11 +1849,19 @@ PHP_FUNCTION(sqlite_next)  {  	zval *zres;  	struct php_sqlite_result *res; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) { -		return; +	if (object) { +		if (ZEND_NUM_ARGS() != 0) { +			WRONG_PARAM_COUNT +		} +		RES_FROM_OBJECT(res, object); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zres)) { +			return; +		} +		ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);  	} -	ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);  	if (!res->buffered && res->vm) {  		php_sqlite_fetch(res TSRMLS_CC); @@ -1511,13 +1915,20 @@ PHP_FUNCTION(sqlite_last_error)  {  	zval *zdb;  	struct php_sqlite_db *db; +	zval *object = getThis(); -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) { -		return; +	if (object) { +		if (ZEND_NUM_ARGS() != 0) { +			WRONG_PARAM_COUNT +		} +		DB_FROM_OBJECT(db, object); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zdb)) { +			return; +		} +		DB_FROM_ZVAL(db, &zdb);  	} -	DB_FROM_ZVAL(db, &zdb); -  	RETURN_LONG(db->last_err_code);  }  /* }}} */ @@ -1616,11 +2027,19 @@ PHP_FUNCTION(sqlite_create_aggregate)  	struct php_sqlite_agg_functions *funcs;  	char *callable = NULL;  	long num_args = -1; -	 -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rszz|l", &zdb, &funcname, &funcname_len, &zstep, &zfinal, &num_args)) { -		return; +	zval *object = getThis(); + +	if (object) { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szz|l", &zdb, &funcname, &funcname_len, &zstep, &zfinal, &num_args)) { +			return; +		} +		DB_FROM_OBJECT(db, object); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rszz|l", &zdb, &funcname, &funcname_len, &zstep, &zfinal, &num_args)) { +			return; +		} +		DB_FROM_ZVAL(db, &zdb);  	} -	DB_FROM_ZVAL(db, &zdb);  	if (!zend_is_callable(zstep, 0, &callable)) {  		php_error_docref(NULL TSRMLS_CC, E_WARNING, "step function `%s' is not callable", callable); @@ -1636,6 +2055,7 @@ PHP_FUNCTION(sqlite_create_aggregate)  	}  	efree(callable); +	  	if (prep_callback_struct(db, 1, funcname, zstep, zfinal, &funcs) == DO_REG) {  		sqlite_create_aggregate(db->db, funcname, num_args,  				php_sqlite_agg_step_function_callback, @@ -1658,10 +2078,19 @@ PHP_FUNCTION(sqlite_create_function)  	char *callable = NULL;  	long num_args = -1; -	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsz|l", &zdb, &funcname, &funcname_len, &zcall, &num_args)) { -		return; +	zval *object = getThis(); + +	if (object) { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &zdb, &funcname, &funcname_len, &zcall, &num_args)) { +			return; +		} +		DB_FROM_OBJECT(db, object); +	} else { +		if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsz|l", &zdb, &funcname, &funcname_len, &zcall, &num_args)) { +			return; +		} +		DB_FROM_ZVAL(db, &zdb);  	} -	DB_FROM_ZVAL(db, &zdb);  	if (!zend_is_callable(zcall, 0, &callable)) {  		php_error_docref(NULL TSRMLS_CC, E_WARNING, "function `%s' is not callable", callable); | 
