]> granicus.if.org Git - php/commitdiff
Add sqlite_fetch_object()
authorMarcus Boerger <helly@php.net>
Thu, 28 Aug 2003 23:19:51 +0000 (23:19 +0000)
committerMarcus Boerger <helly@php.net>
Thu, 28 Aug 2003 23:19:51 +0000 (23:19 +0000)
ext/sqlite/php_sqlite.h
ext/sqlite/sqlite.c
ext/sqlite/tests/sqlite_024.phpt [new file with mode: 0755]
ext/sqlite/tests/sqlite_oo_024.phpt [new file with mode: 0755]

index 69bbeb0efc22c62e8555e6f0939cbb9ebd4b5a89..80ceba14354cb32dfe403a6d4a947507f765b56c 100644 (file)
@@ -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);
index da134d7140425740482047decaac1cb304ce2d75..2b0deacb8b5d9488b64dc109cf6cc4103d4d2700 100644 (file)
@@ -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 (executable)
index 0000000..531b50f
--- /dev/null
@@ -0,0 +1,74 @@
+--TEST--
+sqlite: sqlite_fetch_object
+--INI--
+sqlite.assoc_case=0
+--SKIPIF--
+<?php # vim:ft=php
+if (!extension_loaded("sqlite")) print "skip"; ?>
+--FILE--
+<?php 
+include "blankdb.inc";
+
+class class24 {
+       function __construct() {
+               echo __METHOD__ . "\n";
+       }
+}
+
+$data = array(
+       "one",
+       "two",
+       "three"
+       );
+
+sqlite_query($db, "CREATE TABLE strings(a)");
+
+foreach ($data as $str) {
+       sqlite_query($db, "INSERT INTO strings VALUES('$str')");
+}
+
+echo "====stdclass====\n";
+$res = sqlite_query($db, "SELECT a FROM strings", SQLITE_ASSOC);
+while (sqlite_has_more($res)) {
+       var_dump(sqlite_fetch_object($res, 'class24'));
+}
+
+echo "====class24!====\n";
+$res = sqlite_query($db, "SELECT a FROM strings", SQLITE_ASSOC);
+while (sqlite_has_more($res)) {
+       var_dump(sqlite_fetch_object($res));
+}
+
+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!====
diff --git a/ext/sqlite/tests/sqlite_oo_024.phpt b/ext/sqlite/tests/sqlite_oo_024.phpt
new file mode 100755 (executable)
index 0000000..8234c2d
--- /dev/null
@@ -0,0 +1,74 @@
+--TEST--
+sqlite-oo: sqlite::fetch_object
+--INI--
+sqlite.assoc_case=0
+--SKIPIF--
+<?php # vim:ft=php
+if (!extension_loaded("sqlite")) print "skip"; ?>
+--FILE--
+<?php 
+include "blankdb_oo.inc";
+
+class class24 {
+       function __construct() {
+               echo __METHOD__ . "\n";
+       }
+}
+
+$data = array(
+       "one",
+       "two",
+       "three"
+       );
+
+$db->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!====