From: Marcus Boerger Date: Thu, 28 Aug 2003 23:19:51 +0000 (+0000) Subject: Add sqlite_fetch_object() X-Git-Tag: RELEASE_0_7~391 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=90561350f92cce92bf2deec323ed3812d1086733;p=php Add sqlite_fetch_object() --- diff --git a/ext/sqlite/php_sqlite.h b/ext/sqlite/php_sqlite.h index 69bbeb0efc..80ceba1435 100644 --- a/ext/sqlite/php_sqlite.h +++ b/ext/sqlite/php_sqlite.h @@ -51,6 +51,7 @@ PHP_FUNCTION(sqlite_array_query); PHP_FUNCTION(sqlite_single_query); PHP_FUNCTION(sqlite_fetch_array); +PHP_FUNCTION(sqlite_fetch_object); PHP_FUNCTION(sqlite_fetch_single); PHP_FUNCTION(sqlite_fetch_all); PHP_FUNCTION(sqlite_current); diff --git a/ext/sqlite/sqlite.c b/ext/sqlite/sqlite.c index da134d7140..2b0deacb8b 100644 --- a/ext/sqlite/sqlite.c +++ b/ext/sqlite/sqlite.c @@ -161,6 +161,7 @@ function_entry sqlite_functions[] = { PHP_FE(sqlite_array_query, NULL) PHP_FE(sqlite_single_query, NULL) PHP_FE(sqlite_fetch_array, NULL) + PHP_FE(sqlite_fetch_object, NULL) PHP_FE(sqlite_fetch_single, NULL) PHP_FALIAS(sqlite_fetch_string, sqlite_fetch_single, NULL) PHP_FE(sqlite_fetch_all, NULL) @@ -214,6 +215,7 @@ function_entry sqlite_funcs_db[] = { function_entry sqlite_funcs_query[] = { PHP_ME_MAPPING(fetch_array, sqlite_fetch_array, NULL) + PHP_ME_MAPPING(fetch_object, sqlite_fetch_object, NULL) PHP_ME_MAPPING(fetch_single, sqlite_fetch_single, NULL) PHP_ME_MAPPING(fetch_all, sqlite_fetch_all, NULL) PHP_ME_MAPPING(column, sqlite_column, NULL) @@ -236,6 +238,7 @@ function_entry sqlite_funcs_query[] = { function_entry sqlite_funcs_ub_query[] = { PHP_ME_MAPPING(fetch_array, sqlite_fetch_array, NULL) + PHP_ME_MAPPING(fetch_object, sqlite_fetch_object, NULL) PHP_ME_MAPPING(fetch_single, sqlite_fetch_single, NULL) PHP_ME_MAPPING(fetch_all, sqlite_fetch_all, NULL) PHP_ME_MAPPING(column, sqlite_column, NULL) @@ -1159,7 +1162,7 @@ PHP_FUNCTION(sqlite_factory) 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); + php_std_error_handling(); RETURN_NULL(); } if (errmsg) { @@ -1167,18 +1170,18 @@ PHP_FUNCTION(sqlite_factory) } if (PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { - php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); + php_std_error_handling(); RETURN_NULL(); } if (php_check_open_basedir(filename TSRMLS_CC)) { - php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); + php_std_error_handling(); RETURN_NULL(); } php_sqlite_open(filename, mode, NULL, return_value, errmsg, return_value TSRMLS_CC); - php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC); + php_std_error_handling(); } /* }}} */ @@ -1660,6 +1663,84 @@ PHP_FUNCTION(sqlite_fetch_array) } /* }}} */ +/* {{{ proto object sqlite_fetch_object(resource result [, string class_name [, bool decode_binary]]) + Fetches the next row from a result set as an object. */ +PHP_FUNCTION(sqlite_fetch_object) +{ + zval *zres; + zend_bool decode_binary = 1; + struct php_sqlite_result *res; + zval *object = getThis(); + char *class_name; + int class_name_len; + zend_class_entry *ce; + zval dataset; + zend_fcall_info fci; + zend_fcall_info_cache fcc; + zval *retval_ptr; + + php_set_error_handling(object ? EH_THROW : EH_NORMAL, sqlite_ce_exception TSRMLS_CC); + if (object) { + if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sb", &class_name, &class_name_len, &decode_binary)) { + php_std_error_handling(); + return; + } + RES_FROM_OBJECT(res, object); + if (!ZEND_NUM_ARGS()) { + ce = zend_standard_class_def; + } else { + ce = zend_fetch_class(class_name, class_name_len, ZEND_FETCH_CLASS_AUTO TSRMLS_CC); + } + } else { + if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|sb", &zres, &class_name, &class_name_len, &decode_binary)) { + php_std_error_handling(); + return; + } + ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result); + if (ZEND_NUM_ARGS() < 2) { + ce = zend_standard_class_def; + } else { + ce = zend_fetch_class(class_name, class_name_len, ZEND_FETCH_CLASS_AUTO TSRMLS_CC); + } + } + + if (!ce) { + zend_throw_exception_ex(sqlite_ce_exception, 0 TSRMLS_CC, "Could not find class '%s'", class_name); + php_std_error_handling(); + return; + } + + php_sqlite_fetch_array(res, PHPSQLITE_ASSOC, decode_binary, 1, &dataset TSRMLS_CC); + + object_and_properties_init(return_value, ce, Z_ARRVAL(dataset)); + + php_std_error_handling(); /* before calling the ctor */ + + if (ce->constructor) { + fci.size = sizeof(fci); + fci.function_table = &ce->function_table; + fci.function_name = NULL; + fci.symbol_table = NULL; + fci.object_pp = &return_value; + fci.retval_ptr_ptr = &retval_ptr; + fci.param_count = 0; + fci.params = NULL; + fci.no_separation = 1; + + fcc.initialized = 1; + fcc.function_handler = ce->constructor; + fcc.calling_scope = EG(scope); + fcc.object_pp = &return_value; + + if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) { + zend_throw_exception_ex(sqlite_ce_exception, 0 TSRMLS_CC, "Could not execute %s::%s()", class_name, ce->constructor->common.function_name); + } else { + zval_ptr_dtor(&retval_ptr); + } + } +} +/* }}} */ + /* {{{ proto array sqlite_array_query(resource db, string query [ , int result_type [, bool decode_binary]]) Executes a query against a given database and returns an array of arrays. */ PHP_FUNCTION(sqlite_array_query) diff --git a/ext/sqlite/tests/sqlite_024.phpt b/ext/sqlite/tests/sqlite_024.phpt new file mode 100755 index 0000000000..531b50fead --- /dev/null +++ b/ext/sqlite/tests/sqlite_024.phpt @@ -0,0 +1,74 @@ +--TEST-- +sqlite: sqlite_fetch_object +--INI-- +sqlite.assoc_case=0 +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +====stdclass==== +class24::__construct +object(class24)#%d (1) { + ["a"]=> + string(3) "one" +} +class24::__construct +object(class24)#%d (1) { + ["a"]=> + string(3) "two" +} +class24::__construct +object(class24)#%d (1) { + ["a"]=> + string(5) "three" +} +====class24!==== +object(stdClass)#%d (1) { + ["a"]=> + string(3) "one" +} +object(stdClass)#%d (1) { + ["a"]=> + string(3) "two" +} +object(stdClass)#%d (1) { + ["a"]=> + string(5) "three" +} +====DONE!==== diff --git a/ext/sqlite/tests/sqlite_oo_024.phpt b/ext/sqlite/tests/sqlite_oo_024.phpt new file mode 100755 index 0000000000..8234c2d49a --- /dev/null +++ b/ext/sqlite/tests/sqlite_oo_024.phpt @@ -0,0 +1,74 @@ +--TEST-- +sqlite-oo: sqlite::fetch_object +--INI-- +sqlite.assoc_case=0 +--SKIPIF-- + +--FILE-- +query("CREATE TABLE strings(a)"); + +foreach ($data as $str) { + $db->query("INSERT INTO strings VALUES('$str')"); +} + +echo "====stdclass====\n"; +$res = $db->query("SELECT a FROM strings", SQLITE_ASSOC); +while ($res->has_more()) { + var_dump($res->fetch_object('class24')); +} + +echo "====class24!====\n"; +$res = $db->query("SELECT a FROM strings", SQLITE_ASSOC); +while ($res->has_more()) { + var_dump($res->fetch_object()); +} + +echo "====DONE!====\n"; +?> +--EXPECTF-- +====stdclass==== +class24::__construct +object(class24)#%d (1) { + ["a"]=> + string(3) "one" +} +class24::__construct +object(class24)#%d (1) { + ["a"]=> + string(3) "two" +} +class24::__construct +object(class24)#%d (1) { + ["a"]=> + string(5) "three" +} +====class24!==== +object(stdClass)#%d (1) { + ["a"]=> + string(3) "one" +} +object(stdClass)#%d (1) { + ["a"]=> + string(3) "two" +} +object(stdClass)#%d (1) { + ["a"]=> + string(5) "three" +} +====DONE!====