Skip to content

Commit 3510530

Browse files
committed
ext/sqlite3: Sqlite3Result::fetchAll()
support associative and indexes arrays for results.
1 parent be70f42 commit 3510530

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

ext/sqlite3/sqlite3.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,6 +2010,74 @@ static void sqlite3result_clear_column_names_cache(php_sqlite3_result *result) {
20102010
result->column_count = -1;
20112011
}
20122012

2013+
PHP_METHOD(SQLite3Result, fetchAll)
2014+
{
2015+
int i;
2016+
bool done = false;
2017+
php_sqlite3_result *result_obj;
2018+
zval *object = ZEND_THIS;
2019+
zend_long mode = PHP_SQLITE3_BOTH;
2020+
result_obj = Z_SQLITE3_RESULT_P(object);
2021+
2022+
ZEND_PARSE_PARAMETERS_START(0, 1)
2023+
Z_PARAM_OPTIONAL
2024+
Z_PARAM_LONG(mode)
2025+
ZEND_PARSE_PARAMETERS_END();
2026+
2027+
SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
2028+
2029+
result_obj->column_count = sqlite3_column_count(result_obj->stmt_obj->stmt);
2030+
if (mode & PHP_SQLITE3_ASSOC) {
2031+
result_obj->column_names = emalloc(result_obj->column_count * sizeof(zend_string*));
2032+
2033+
for (i = 0; i < result_obj->column_count; i++) {
2034+
const char *column = sqlite3_column_name(result_obj->stmt_obj->stmt, i);
2035+
result_obj->column_names[i] = zend_string_init(column, strlen(column), 0);
2036+
}
2037+
}
2038+
array_init(return_value);
2039+
2040+
while (!done) {
2041+
int step = sqlite3_step(result_obj->stmt_obj->stmt);
2042+
2043+
switch (step) {
2044+
case SQLITE_ROW: {
2045+
zval result;
2046+
array_init_size(&result, result_obj->column_count);
2047+
2048+
for (i = 0; i < result_obj->column_count; i ++) {
2049+
zval data;
2050+
sqlite_value_to_zval(result_obj->stmt_obj->stmt, i, &data);
2051+
2052+
if (mode & PHP_SQLITE3_NUM) {
2053+
add_index_zval(&result, i, &data);
2054+
}
2055+
2056+
if (mode & PHP_SQLITE3_ASSOC) {
2057+
if (mode & PHP_SQLITE3_NUM) {
2058+
if (Z_REFCOUNTED(data)) {
2059+
Z_ADDREF(data);
2060+
}
2061+
}
2062+
zend_symtable_update(Z_ARR_P(&result), result_obj->column_names[i], &data);
2063+
}
2064+
}
2065+
2066+
add_next_index_zval(return_value, &result);
2067+
}
2068+
case SQLITE_DONE:
2069+
done = true;
2070+
break;
2071+
default:
2072+
if (!EG(exception)) {
2073+
php_sqlite3_error(result_obj->db_obj, sqlite3_errcode(sqlite3_db_handle(result_obj->stmt_obj->stmt)), "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(result_obj->stmt_obj->stmt)));
2074+
}
2075+
zval_ptr_dtor(return_value);
2076+
RETURN_FALSE;
2077+
}
2078+
}
2079+
}
2080+
20132081
/* {{{ Resets the result set back to the first row. */
20142082
PHP_METHOD(SQLite3Result, reset)
20152083
{

ext/sqlite3/sqlite3.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ public function columnType(int $column): int|false {}
293293
/** @tentative-return-type */
294294
public function fetchArray(int $mode = SQLITE3_BOTH): array|false {}
295295

296+
public function fetchAll(int $mode = SQLITE3_BOTH): array|false {}
297+
296298
/** @tentative-return-type */
297299
public function reset(): bool {}
298300

ext/sqlite3/sqlite3_arginfo.h

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)