]> granicus.if.org Git - php/commitdiff
Added sqlite_fetch_string(), for speedy fetching of data from database
authorIlia Alshanetsky <iliaa@php.net>
Tue, 13 May 2003 23:51:25 +0000 (23:51 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Tue, 13 May 2003 23:51:25 +0000 (23:51 +0000)
cursor containing only a single column.

ext/sqlite/php_sqlite.h
ext/sqlite/sqlite.c

index c3538dd0f04a904794bce7c6dbad12ba39257941..e6aae260c3a92470dda298b21417d7509e6970b9 100644 (file)
@@ -48,6 +48,7 @@ PHP_FUNCTION(sqlite_close);
 PHP_FUNCTION(sqlite_query);
 PHP_FUNCTION(sqlite_unbuffered_query);
 PHP_FUNCTION(sqlite_fetch_array);
+PHP_FUNCTION(sqlite_fetch_string);
 PHP_FUNCTION(sqlite_current);
 PHP_FUNCTION(sqlite_column);
 
index f3d8d7fc3e0c0086397532cfa5e8828efb9dfc48..26b1dd8158b7ef0462d5af5026b449b049c04ad9 100644 (file)
@@ -115,6 +115,7 @@ function_entry sqlite_functions[] = {
        PHP_FE(sqlite_close, NULL)
        PHP_FE(sqlite_query, NULL)
        PHP_FE(sqlite_fetch_array, NULL)
+       PHP_FE(sqlite_fetch_string, NULL)
        PHP_FE(sqlite_current, NULL)
        PHP_FE(sqlite_column, NULL)
        PHP_FE(sqlite_libversion, NULL)
@@ -1167,6 +1168,56 @@ PHP_FUNCTION(sqlite_fetch_array)
 }
 /* }}} */
 
+/* {{{ proto string sqlite_fetch_array(resource result [, bool decode_binary])
+   Fetches first column of a result set as a string */
+PHP_FUNCTION(sqlite_fetch_string)
+{
+       zval *zres;
+       zend_bool decode_binary = 1;
+       struct php_sqlite_result *res;
+       char *decoded = NULL;
+       int decoded_len;
+       const char **rowdata;
+
+       if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|b", &zres, &decode_binary)) {
+               return;
+       }
+       ZEND_FETCH_RESOURCE(res, struct php_sqlite_result *, &zres, -1, "sqlite result", le_sqlite_result);
+
+       /* check if there are any more rows on the cursor */
+       if (res->curr_row >= res->nrows) {
+               RETURN_FALSE;
+       }
+
+       if (res->buffered) {
+               rowdata = (const char**)&res->table[res->curr_row * res->ncolumns];
+       } else {
+               rowdata = (const char**)res->table;
+       }
+
+       if (decode_binary && rowdata[0] != NULL && rowdata[0][0] == '\x01') {
+               decoded = do_alloca(strlen(rowdata[0]));
+               decoded_len = sqlite_decode_binary(rowdata[0]+1, decoded);
+       } else {
+               decoded = (char*)rowdata[0];
+               decoded_len = decoded ? strlen(decoded) : 0;
+       }
+
+       if (!res->buffered) {
+               /* non buffered: fetch next row */
+               php_sqlite_fetch(res TSRMLS_CC);
+       }
+       /* advance the row pointer */
+       res->curr_row++;
+
+       if (decoded == NULL) {
+               RETURN_NULL();
+       } else {
+               RETURN_STRINGL(decoded, decoded_len, 1);
+       }
+}
+/* }}} */
+
 /* {{{ proto array sqlite_fetch_array(resource result [, int result_type, bool decode_binary])
    Fetches the current row from a result set as an array */
 PHP_FUNCTION(sqlite_current)