Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions ext/sqlite3/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,16 @@ PHP_METHOD(SQLite3Result, columnType)
}
/* }}} */

static void sqlite3result_fill_column_names_cache(php_sqlite3_result *result_obj, int nb_cols)
{
result_obj->column_names = safe_emalloc(nb_cols, sizeof(zend_string*), 0);

for (int i = 0; i < nb_cols; i++) {
const char *column = sqlite3_column_name(result_obj->stmt_obj->stmt, i);
result_obj->column_names[i] = zend_string_init(column, strlen(column), 0);
}
}

/* {{{ Fetch a result row as both an associative or numerically indexed array or both. */
PHP_METHOD(SQLite3Result, fetchArray)
{
Expand Down Expand Up @@ -2019,12 +2029,7 @@ PHP_METHOD(SQLite3Result, fetchArray)

/* Cache column names to speed up repeated fetchArray calls. */
if (mode & PHP_SQLITE3_ASSOC && !result_obj->column_names) {
result_obj->column_names = emalloc(n_cols * sizeof(zend_string*));

for (int i = 0; i < n_cols; i++) {
const char *column = sqlite3_column_name(result_obj->stmt_obj->stmt, i);
result_obj->column_names[i] = zend_string_init(column, strlen(column), 0);
}
sqlite3result_fill_column_names_cache(result_obj, n_cols);
}

array_init(return_value);
Expand Down Expand Up @@ -2056,7 +2061,7 @@ static void sqlite3result_clear_column_names_cache(php_sqlite3_result *result) {

PHP_METHOD(SQLite3Result, fetchAll)
{
int i, nb_cols;
int nb_cols;
bool done = false;
php_sqlite3_result *result_obj;
zval *object = ZEND_THIS;
Expand All @@ -2071,14 +2076,8 @@ PHP_METHOD(SQLite3Result, fetchAll)
SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)

nb_cols = sqlite3_column_count(result_obj->stmt_obj->stmt);
if (mode & PHP_SQLITE3_ASSOC) {
sqlite3result_clear_column_names_cache(result_obj);
result_obj->column_names = emalloc(nb_cols * sizeof(zend_string*));

for (i = 0; i < nb_cols; i++) {
const char *column = sqlite3_column_name(result_obj->stmt_obj->stmt, i);
result_obj->column_names[i] = zend_string_init(column, strlen(column), 0);
}
if (mode & PHP_SQLITE3_ASSOC && !result_obj->column_names) {
sqlite3result_fill_column_names_cache(result_obj, nb_cols);
}
result_obj->column_count = nb_cols;
array_init(return_value);
Expand Down Expand Up @@ -2479,9 +2478,7 @@ static zend_always_inline void php_sqlite3_fetch_one(int n_cols, php_sqlite3_res

if (mode & PHP_SQLITE3_ASSOC) {
if (mode & PHP_SQLITE3_NUM) {
if (Z_REFCOUNTED(data)) {
Z_ADDREF(data);
}
Z_TRY_ADDREF(data);
}
/* Note: we can't use the "add_new" variant here instead of "update" because
* when the same column name is encountered, the last result should be taken. */
Expand Down