aboutsummaryrefslogtreecommitdiff
path: root/core/sql.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/sql.c')
-rw-r--r--core/sql.c18
1 files changed, 13 insertions, 5 deletions
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) {