Skip to content

Commit 6d15475

Browse files
committed
sqlite3: Split off column name cache generation
Also no need to reset the cache unconditionally in fetchAll().
1 parent e7678cd commit 6d15475

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

ext/sqlite3/sqlite3.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,16 @@ PHP_METHOD(SQLite3Result, columnType)
19871987
}
19881988
/* }}} */
19891989

1990+
static void sqlite3result_fill_column_names_cache(php_sqlite3_result *result_obj, int nb_cols)
1991+
{
1992+
result_obj->column_names = safe_emalloc(nb_cols, sizeof(zend_string*), 0);
1993+
1994+
for (int i = 0; i < nb_cols; i++) {
1995+
const char *column = sqlite3_column_name(result_obj->stmt_obj->stmt, i);
1996+
result_obj->column_names[i] = zend_string_init(column, strlen(column), 0);
1997+
}
1998+
}
1999+
19902000
/* {{{ Fetch a result row as both an associative or numerically indexed array or both. */
19912001
PHP_METHOD(SQLite3Result, fetchArray)
19922002
{
@@ -2019,12 +2029,7 @@ PHP_METHOD(SQLite3Result, fetchArray)
20192029

20202030
/* Cache column names to speed up repeated fetchArray calls. */
20212031
if (mode & PHP_SQLITE3_ASSOC && !result_obj->column_names) {
2022-
result_obj->column_names = emalloc(n_cols * sizeof(zend_string*));
2023-
2024-
for (int i = 0; i < n_cols; i++) {
2025-
const char *column = sqlite3_column_name(result_obj->stmt_obj->stmt, i);
2026-
result_obj->column_names[i] = zend_string_init(column, strlen(column), 0);
2027-
}
2032+
sqlite3result_fill_column_names_cache(result_obj, n_cols);
20282033
}
20292034

20302035
array_init(return_value);
@@ -2056,7 +2061,7 @@ static void sqlite3result_clear_column_names_cache(php_sqlite3_result *result) {
20562061

20572062
PHP_METHOD(SQLite3Result, fetchAll)
20582063
{
2059-
int i, nb_cols;
2064+
int nb_cols;
20602065
bool done = false;
20612066
php_sqlite3_result *result_obj;
20622067
zval *object = ZEND_THIS;
@@ -2071,14 +2076,8 @@ PHP_METHOD(SQLite3Result, fetchAll)
20712076
SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
20722077

20732078
nb_cols = sqlite3_column_count(result_obj->stmt_obj->stmt);
2074-
if (mode & PHP_SQLITE3_ASSOC) {
2075-
sqlite3result_clear_column_names_cache(result_obj);
2076-
result_obj->column_names = emalloc(nb_cols * sizeof(zend_string*));
2077-
2078-
for (i = 0; i < nb_cols; i++) {
2079-
const char *column = sqlite3_column_name(result_obj->stmt_obj->stmt, i);
2080-
result_obj->column_names[i] = zend_string_init(column, strlen(column), 0);
2081-
}
2079+
if (mode & PHP_SQLITE3_ASSOC && !result_obj->column_names) {
2080+
sqlite3result_fill_column_names_cache(result_obj, nb_cols);
20822081
}
20832082
result_obj->column_count = nb_cols;
20842083
array_init(return_value);

0 commit comments

Comments
 (0)