From: Marcus Boerger Date: Sat, 3 May 2003 12:51:13 +0000 (+0000) Subject: - Indendation fix X-Git-Tag: RELEASE_0_9b~122 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=815ad75c490b4fd24112428fd504839a8cd51071;p=php - Indendation fix - Proto fix - Add efficient single column access function: sqlite_column() --- diff --git a/ext/sqlite/CREDITS b/ext/sqlite/CREDITS index bd253dd63b..87f769b6f9 100644 --- a/ext/sqlite/CREDITS +++ b/ext/sqlite/CREDITS @@ -1,2 +1,2 @@ sqlite -Wez Furlong, Tal Peer +Wez Furlong, Tal Peer, Marcus Börger diff --git a/ext/sqlite/php_sqlite.h b/ext/sqlite/php_sqlite.h index 519dade0c0..c3538dd0f0 100644 --- a/ext/sqlite/php_sqlite.h +++ b/ext/sqlite/php_sqlite.h @@ -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); diff --git a/ext/sqlite/sqlite.c b/ext/sqlite/sqlite.c index 5ec6e7e122..f3d8d7fc3e 100644 --- a/ext/sqlite/sqlite.c +++ b/ext/sqlite/sqlite.c @@ -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 index 0000000000..6ade531c62 --- /dev/null +++ b/ext/sqlite/tests/sqlite_013.phpt @@ -0,0 +1,55 @@ +--TEST-- +sqlite: fetch column +--INI-- +sqlite.assoc_case=0 +--SKIPIF-- + +--FILE-- + '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!