Skip to content

Commit 7c25edb

Browse files
committed
Cache column names before stepping, Add support for STRING_NAME
1 parent 5a59340 commit 7c25edb

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

src/gdsqlite.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ bool SQLite::query_with_bindings(const String &p_query, Array param_bindings) {
269269
break;
270270

271271
case Variant::STRING:
272+
case Variant::STRING_NAME:
272273
{
273274
const CharString dummy_binding = (binding_value.operator String()).utf8();
274275
const char *binding = dummy_binding.get_data();
@@ -302,46 +303,54 @@ bool SQLite::query_with_bindings(const String &p_query, Array param_bindings) {
302303
sqlite3_free(expanded_sql);
303304
}
304305

306+
/* Column names don't change for every row -> Cache them! */
307+
int argc = sqlite3_column_count(stmt);
308+
Vector<StringName> column_names;
309+
column_names.resize(argc);
310+
for (int i = 0; i < argc; i++) {
311+
const char *azColName = sqlite3_column_name(stmt, i);
312+
column_names.write[i] = StringName(String::utf8(azColName));
313+
}
314+
305315
// Execute the statement and iterate over all the resulting rows.
306316
while (sqlite3_step(stmt) == SQLITE_ROW) {
307317
Dictionary column_dict;
308-
int argc = sqlite3_column_count(stmt);
309318

310319
/* Loop over all columns and add them to the Dictionary */
311320
for (int i = 0; i < argc; i++) {
312321
Variant column_value;
313322
/* Check the column type and do correct casting */
314323
switch (sqlite3_column_type(stmt, i)) {
315324
case SQLITE_INTEGER:
316-
column_value = Variant((int64_t)sqlite3_column_int64(stmt, i));
325+
column_value = (int64_t)sqlite3_column_int64(stmt, i);
317326
break;
318327

319328
case SQLITE_FLOAT:
320-
column_value = Variant(sqlite3_column_double(stmt, i));
329+
column_value = sqlite3_column_double(stmt, i);
321330
break;
322331

323332
case SQLITE_TEXT:
324-
column_value = Variant(String::utf8((char *)sqlite3_column_text(stmt, i)));
333+
column_value = String::utf8((const char *)sqlite3_column_text(stmt, i));
325334
break;
326335

327336
case SQLITE_BLOB: {
328337
int bytes = sqlite3_column_bytes(stmt, i);
329338
PackedByteArray arr = PackedByteArray();
330339
arr.resize(bytes);
331-
memcpy((void *)arr.ptrw(), (char *)sqlite3_column_blob(stmt, i), bytes);
340+
memcpy(arr.ptrw(), sqlite3_column_blob(stmt, i), bytes);
332341
column_value = arr;
333342
break;
334343
}
335344

336345
case SQLITE_NULL:
346+
column_value = Variant(); // explicit null
337347
break;
338348

339349
default:
340350
break;
341351
}
342352

343-
const char *azColName = sqlite3_column_name(stmt, i);
344-
column_dict[String::utf8(azColName)] = column_value;
353+
column_dict[column_names[i]] = column_value;
345354
}
346355
/* Add result to query_result Array */
347356
query_result.append(column_dict);
@@ -757,6 +766,7 @@ static void function_callback(sqlite3_context *context, int argc, sqlite3_value
757766
break;
758767

759768
case Variant::STRING:
769+
case Variant::STRING_NAME:
760770
{
761771
const CharString dummy_binding = (output.operator String()).utf8();
762772
const char *binding = dummy_binding.get_data();

0 commit comments

Comments
 (0)