]> granicus.if.org Git - php/commitdiff
- Indendation fix
authorMarcus Boerger <helly@php.net>
Sat, 3 May 2003 12:51:13 +0000 (12:51 +0000)
committerMarcus Boerger <helly@php.net>
Sat, 3 May 2003 12:51:13 +0000 (12:51 +0000)
- Proto fix
- Add efficient single column access function: sqlite_column()

ext/sqlite/CREDITS
ext/sqlite/php_sqlite.h
ext/sqlite/sqlite.c
ext/sqlite/tests/sqlite_013.phpt [new file with mode: 0755]

index bd253dd63bcbc1f4e510717375fff8775012bb04..87f769b6f93b3c0f58abb3cb95ed88f1dc90c063 100644 (file)
@@ -1,2 +1,2 @@
 sqlite
-Wez Furlong, Tal Peer
+Wez Furlong, Tal Peer, Marcus Börger
index 519dade0c05b57086ffc9211119d7a895dfdd3c4..c3538dd0f04a904794bce7c6dbad12ba39257941 100644 (file)
@@ -49,6 +49,7 @@ PHP_FUNCTION(sqlite_query);
 PHP_FUNCTION(sqlite_unbuffered_query);
 PHP_FUNCTION(sqlite_fetch_array);
 PHP_FUNCTION(sqlite_current);
+PHP_FUNCTION(sqlite_column);
 
 PHP_FUNCTION(sqlite_num_rows);
 PHP_FUNCTION(sqlite_num_fields);
index 5ec6e7e122e6bc21205e34ab7dc8a9cb04640af2..f3d8d7fc3e0c0086397532cfa5e8828efb9dfc48 100644 (file)
@@ -116,6 +116,7 @@ function_entry sqlite_functions[] = {
        PHP_FE(sqlite_query, NULL)
        PHP_FE(sqlite_fetch_array, NULL)
        PHP_FE(sqlite_current, NULL)
+       PHP_FE(sqlite_column, NULL)
        PHP_FE(sqlite_libversion, NULL)
        PHP_FE(sqlite_libencoding, NULL)
        PHP_FE(sqlite_changes, NULL)
@@ -1075,13 +1076,73 @@ static void php_sqlite_fetch_array(struct php_sqlite_result *res, int mode, zend
        }
 
        if (move_next) {
-       if (!res->buffered) {
-               /* non buffered: fetch next row */
-               php_sqlite_fetch(res TSRMLS_CC);
+               if (!res->buffered) {
+                       /* non buffered: fetch next row */
+                       php_sqlite_fetch(res TSRMLS_CC);
+               }
+               /* advance the row pointer */
+               res->curr_row++;
        }
-       /* advance the row pointer */
-       res->curr_row++;
 }
+/* }}} */
+
+/* {{{ php_sqlite_fetch_column */
+static void php_sqlite_fetch_column(struct php_sqlite_result *res, zval *which, zend_bool decode_binary, zval *return_value TSRMLS_DC)
+{
+       int j;
+       const char **rowdata, **colnames;
+       char *decoded = NULL;
+       int decoded_len;
+
+       /* check range of the row */
+       if (res->curr_row >= res->nrows) {
+               /* no more */
+               RETURN_FALSE;
+       }
+       colnames = (const char**)res->col_names;
+       if (res->buffered) {
+               rowdata = (const char**)&res->table[res->curr_row * res->ncolumns];
+       } else {
+               rowdata = (const char**)res->table;
+       }
+
+       if (Z_TYPE_P(which) == IS_LONG) {
+               j = Z_LVAL_P(which);
+       } else {
+               convert_to_string_ex(&which);
+               for (j = 0; j < res->ncolumns; j++) {
+                       if (!strcasecmp((char*)colnames[j], Z_STRVAL_P(which))) {
+                               break;
+                       }
+               }
+       }
+       if (j < 0 || j >= res->ncolumns) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such column %d", j);
+               RETURN_FALSE;
+       }
+
+       if (decode_binary && rowdata[j] != NULL && rowdata[j][0] == '\x01') {
+               int l = strlen(rowdata[j]);
+               decoded = do_alloca(l);
+               decoded_len = sqlite_decode_binary(rowdata[j]+1, decoded);
+       } else {
+               decoded = (char*)rowdata[j];
+               if (decoded) {
+                       decoded_len = strlen(decoded);
+               } else {
+                       decoded_len = 0;
+               }
+       }
+       
+       if (decoded == NULL) {
+               RETURN_NULL();
+       } else {
+               RETURN_STRINGL(decoded, decoded_len, 1);
+       }
+
+       if (decode_binary && rowdata[j] != NULL && rowdata[j][0] == '\x01') {
+               free_alloca(decoded);
+       }
 }
 /* }}} */
 
@@ -1107,7 +1168,7 @@ PHP_FUNCTION(sqlite_fetch_array)
 /* }}} */
 
 /* {{{ proto array sqlite_fetch_array(resource result [, int result_type, bool decode_binary])
-   Fetches the next row from a result set as an array */
+   Fetches the current row from a result set as an array */
 PHP_FUNCTION(sqlite_current)
 {
        zval *zres;
@@ -1127,6 +1188,24 @@ PHP_FUNCTION(sqlite_current)
 }
 /* }}} */
 
+/* {{{ proto array sqlite_column(resource result, mixed index_or_name [, bool decode_binary])
+   Fetches a column from the current row from a result set */
+PHP_FUNCTION(sqlite_column)
+{
+       zval *zres;
+       zval *which;
+       zend_bool decode_binary = 1;
+       struct php_sqlite_result *res;
+
+       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);
+
+       php_sqlite_fetch_column(res, which, decode_binary, return_value TSRMLS_CC);
+}
+/* }}} */
+
 /* {{{ proto string sqlite_libversion()
    Returns the version of the linked SQLite library */
 PHP_FUNCTION(sqlite_libversion)
diff --git a/ext/sqlite/tests/sqlite_013.phpt b/ext/sqlite/tests/sqlite_013.phpt
new file mode 100755 (executable)
index 0000000..6ade531
--- /dev/null
@@ -0,0 +1,55 @@
+--TEST--
+sqlite: fetch column
+--INI--
+sqlite.assoc_case=0
+--SKIPIF--
+<?php # vim:ft=php
+if (!extension_loaded("sqlite")) print "skip"; ?>
+--FILE--
+<?php 
+include "blankdb.inc";
+
+$data = array(
+       array (0 => 'one', 1 => 'two'),
+       array (0 => 'three', 1 => 'four')
+       );
+
+sqlite_query("CREATE TABLE strings(a VARCHAR, b VARCHAR)", $db);
+
+foreach ($data as $str) {
+       sqlite_query("INSERT INTO strings VALUES('${str[0]}','${str[1]}')", $db);
+}
+
+$r = sqlite_unbuffered_query("SELECT a, b from strings", $db);
+while (sqlite_has_more($r)) {
+       var_dump(sqlite_current($r, SQLITE_NUM));
+       var_dump(sqlite_column($r, 0));
+       var_dump(sqlite_column($r, 1));
+       var_dump(sqlite_column($r, 'a'));
+       var_dump(sqlite_column($r, 'b'));
+       sqlite_next($r);
+}
+echo "DONE!\n";
+?>
+--EXPECT--
+array(2) {
+  [0]=>
+  string(3) "one"
+  [1]=>
+  string(3) "two"
+}
+string(3) "one"
+string(3) "two"
+string(3) "one"
+string(3) "two"
+array(2) {
+  [0]=>
+  string(5) "three"
+  [1]=>
+  string(4) "four"
+}
+string(5) "three"
+string(4) "four"
+string(5) "three"
+string(4) "four"
+DONE!