diff options
Diffstat (limited to 'ext')
| -rw-r--r-- | ext/sqlite/php_sqlite.h | 1 | ||||
| -rw-r--r-- | ext/sqlite/sqlite.c | 51 | 
2 files changed, 52 insertions, 0 deletions
diff --git a/ext/sqlite/php_sqlite.h b/ext/sqlite/php_sqlite.h index c3538dd0f0..e6aae260c3 100644 --- a/ext/sqlite/php_sqlite.h +++ b/ext/sqlite/php_sqlite.h @@ -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); diff --git a/ext/sqlite/sqlite.c b/ext/sqlite/sqlite.c index f3d8d7fc3e..26b1dd8158 100644 --- a/ext/sqlite/sqlite.c +++ b/ext/sqlite/sqlite.c @@ -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)  | 
