From 848c5cca715c514ce6e57e0fa7ef8e71b09c0b08 Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Mon, 25 May 2026 23:00:48 +0200 Subject: Updates sql_exec function to allow reading data --- core/salis.c | 4 ++-- core/sql.c | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) (limited to 'core') diff --git a/core/salis.c b/core/salis.c index d6efcf2..a5b517a 100644 --- a/core/salis.c +++ b/core/salis.c @@ -691,7 +691,7 @@ void salis_push_data_header(void) { log_info("Creating core table in SQLite database"); sql_exec( - 0, NULL, NULL, + 0, NULL, NULL, NULL, NULL, "create table core (" #define EVENT_ARRAY(core, index, ev) \ #ev "_size_" #core " int not null, " \ @@ -778,7 +778,7 @@ void salis_push_data_line(void) { log_info("Pushing row to core table in SQLite database"); sql_exec( - CORES * EVENT_ARRAYS_COUNT, (const void **)blobs, (int *)blob_sizes, + CORES * EVENT_ARRAYS_COUNT, (const void **)blobs, (int *)blob_sizes, NULL, NULL, "insert into core (" #define EVENT_ARRAY(core, index, ev) \ #ev "_size_" #core ", " \ diff --git a/core/sql.c b/core/sql.c index 4cfa1b0..b1c0c2a 100644 --- a/core/sql.c +++ b/core/sql.c @@ -2,7 +2,7 @@ sqlite3 *g_sim_db; -void sql_exec(int blob_cnt, const void **blobs, const int *blob_sizes, const char *sql_format, ...) { +void sql_exec(int blob_cnt, const void **blobs, const int *blob_sizes, void (*callback)(void *data), void *data, const char *sql_format, ...) { assert(sql_format); va_list args; @@ -29,18 +29,26 @@ void sql_exec(int blob_cnt, const void **blobs, const int *blob_sizes, const cha assert(sql_res == SQLITE_OK); } - // Only handle SQLITE_BUSY error, in which case we retry the query. - // Setting 'journal_mode=wal;' should help prevent busy database errors. while (true) { sql_res = sqlite3_step(sql_stmt); - if (sql_res == SQLITE_DONE || sql_res == SQLITE_ROW) { + if (sql_res == SQLITE_ROW) { + if (callback) { + callback(data); + } + + continue; + } + + if (sql_res == SQLITE_DONE) { break; } log_warn("SQLite database returned error %d with message:", sql_res); log_warn(sqlite3_errmsg(g_sim_db)); + // Only handle SQLITE_BUSY error, in which case we retry the query. + // Setting 'journal_mode=wal;' should help prevent busy database errors. if (sql_res == SQLITE_BUSY) { log_info("Will retry query..."); continue; @@ -62,7 +70,7 @@ void sql_open(void) { // Enable Write-Ahead Logging (WAL) // This seems to help prevent DB locks when displaying live data. // See: https://sqlite.org/wal.html - sql_exec(0, NULL, NULL, "pragma journal_mode=wal;"); + sql_exec(0, NULL, NULL, NULL, NULL, "pragma journal_mode=wal;"); } void sql_close(void) { -- cgit v1.3