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)
}
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);
+ }
}
/* }}} */
/* }}} */
/* {{{ 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;
}
/* }}} */
+/* {{{ 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)
--- /dev/null
+--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!