From 6c648338e85f673df46a98f13fda2c85862c2c34 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Tue, 26 Nov 2024 12:04:57 +0200 Subject: [PATCH 1/4] libsql-sqlite3: Fix formatting in libsql_wal_get_frame() --- libsql-sqlite3/src/main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libsql-sqlite3/src/main.c b/libsql-sqlite3/src/main.c index 42c60a2a59..81327b821e 100644 --- a/libsql-sqlite3/src/main.c +++ b/libsql-sqlite3/src/main.c @@ -2495,12 +2495,12 @@ int libsql_wal_get_frame( if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; #endif - sqlite3_mutex_enter(db->mutex); - pPager = sqlite3BtreePager(db->aDb[0].pBt); - rc = sqlite3PagerWalReadFrameRaw(pPager, iFrame, pBuf, nBuf); - sqlite3_mutex_leave(db->mutex); + sqlite3_mutex_enter(db->mutex); + pPager = sqlite3BtreePager(db->aDb[0].pBt); + rc = sqlite3PagerWalReadFrameRaw(pPager, iFrame, pBuf, nBuf); + sqlite3_mutex_leave(db->mutex); - return rc; + return rc; #endif } From 36b37acdf2fc4aa78ae57a41e231f08fa38c2a52 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Tue, 26 Nov 2024 12:06:04 +0200 Subject: [PATCH 2/4] libsql-sqlite3: Fix error reporting in WAL API We need to call sqlite3Error() to report anything but "not an error" to callers. --- libsql-sqlite3/src/main.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libsql-sqlite3/src/main.c b/libsql-sqlite3/src/main.c index 81327b821e..62a4a63276 100644 --- a/libsql-sqlite3/src/main.c +++ b/libsql-sqlite3/src/main.c @@ -2470,6 +2470,7 @@ int libsql_wal_frame_count( sqlite3_mutex_enter(db->mutex); pPager = sqlite3BtreePager(db->aDb[0].pBt); rc = sqlite3PagerWalFrameCount(pPager, pnFrame); + sqlite3Error(db, rc); sqlite3_mutex_leave(db->mutex); return rc; #endif @@ -2498,6 +2499,7 @@ int libsql_wal_get_frame( sqlite3_mutex_enter(db->mutex); pPager = sqlite3BtreePager(db->aDb[0].pBt); rc = sqlite3PagerWalReadFrameRaw(pPager, iFrame, pBuf, nBuf); + sqlite3Error(db, rc); sqlite3_mutex_leave(db->mutex); return rc; @@ -2527,6 +2529,7 @@ int libsql_wal_insert_begin(sqlite3 *db) { goto out_unlock; } out_unlock: + sqlite3Error(db, rc); sqlite3_mutex_leave(db->mutex); return rc; } @@ -2542,6 +2545,7 @@ int libsql_wal_insert_end(sqlite3 *db) { goto out_unlock; } out_unlock: + sqlite3Error(db, rc); sqlite3_mutex_leave(db->mutex); return rc; } @@ -2573,6 +2577,7 @@ int libsql_wal_insert_frame( goto out_unlock; } out_unlock: + sqlite3Error(db, rc); sqlite3_mutex_leave(db->mutex); return rc; From c9217dfbfe13820b506a53460a6d7b62a29e7457 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Tue, 26 Nov 2024 12:10:36 +0200 Subject: [PATCH 3/4] libsql-sqlite3: Unify database opening in test_walapi.c --- libsql-sqlite3/src/test_walapi.c | 49 ++++++++++++++++---------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/libsql-sqlite3/src/test_walapi.c b/libsql-sqlite3/src/test_walapi.c index f7bdd54628..fb6ac58519 100644 --- a/libsql-sqlite3/src/test_walapi.c +++ b/libsql-sqlite3/src/test_walapi.c @@ -103,42 +103,41 @@ static void gen_data(sqlite3 *db){ sqlite3_exec(db, "INSERT INTO users (id, name) VALUES (3, 'Jim Beam')", 0, 0, 0); } +int open_db(const char *path, sqlite3 **db) { + int rc; + + rc = sqlite3_open(path, db); + if (rc != SQLITE_OK) { + fprintf(stderr, "Can't open database %s: %s\n", path, sqlite3_errmsg(*db)); + return rc; + } + rc = sqlite3_exec(*db, "PRAGMA journal_mode=WAL", NULL, NULL, NULL); + if (rc != SQLITE_OK) { + fprintf(stderr, "Can't set journal mode for %s: %s\n", path, sqlite3_errmsg(*db)); + return rc; + } + rc = sqlite3_wal_autocheckpoint(*db, 0); + if (rc != SQLITE_OK) { + fprintf(stderr, "Can't disable checkpointing for %s: %s\n", path, sqlite3_errmsg(*db)); + return rc; + } + return rc; +} + int main(int argc, char *argv[]) { sqlite3 *db_primary, *db_backup; int rc; - rc = sqlite3_open("primary.db", &db_primary); + rc = open_db("primary.db", &db_primary); if (rc != SQLITE_OK) { - fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db_primary)); return 1; } - rc = sqlite3_wal_autocheckpoint(db_primary, 0); - if (rc != SQLITE_OK) { - fprintf(stderr, "Can't disable checkpointing: %s\n", sqlite3_errmsg(db_primary)); - return 1; - } - sqlite3_exec(db_primary, "PRAGMA journal_mode=WAL", NULL, NULL, NULL); - gen_data(db_primary); - rc = sqlite3_open("backup.db", &db_backup); - if (rc != SQLITE_OK) { - fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db_backup)); - return 1; - } - rc = sqlite3_wal_autocheckpoint(db_backup, 0); - if (rc != SQLITE_OK) { - fprintf(stderr, "Can't disable checkpointing: %s\n", sqlite3_errmsg(db_backup)); - return 1; - } - rc = sqlite3_exec(db_backup, "PRAGMA journal_mode=WAL", NULL, NULL, NULL); - if (rc != SQLITE_OK) { - fprintf(stderr, "Can't set journal mode: %s\n", sqlite3_errmsg(db_backup)); - return 1; - } - + rc = open_db("backup.db", &db_backup); sync_db(db_primary, db_backup); + if (cmp_data(db_primary, db_backup)) { return 1; } From e2e3a6e85bab280094385b1eeda51e64e70c778c Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Tue, 26 Nov 2024 12:22:40 +0200 Subject: [PATCH 4/4] libsql-ffi: Update bundled SQLite code --- .../bundled/SQLite3MultipleCiphers/src/sqlite3.c | 15 ++++++++++----- libsql-ffi/bundled/src/sqlite3.c | 15 ++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c b/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c index caeb03816e..dcf254a18a 100644 --- a/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c +++ b/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c @@ -183219,6 +183219,7 @@ int libsql_wal_frame_count( sqlite3_mutex_enter(db->mutex); pPager = sqlite3BtreePager(db->aDb[0].pBt); rc = sqlite3PagerWalFrameCount(pPager, pnFrame); + sqlite3Error(db, rc); sqlite3_mutex_leave(db->mutex); return rc; #endif @@ -183244,12 +183245,13 @@ int libsql_wal_get_frame( if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; #endif - sqlite3_mutex_enter(db->mutex); - pPager = sqlite3BtreePager(db->aDb[0].pBt); - rc = sqlite3PagerWalReadFrameRaw(pPager, iFrame, pBuf, nBuf); - sqlite3_mutex_leave(db->mutex); + sqlite3_mutex_enter(db->mutex); + pPager = sqlite3BtreePager(db->aDb[0].pBt); + rc = sqlite3PagerWalReadFrameRaw(pPager, iFrame, pBuf, nBuf); + sqlite3Error(db, rc); + sqlite3_mutex_leave(db->mutex); - return rc; + return rc; #endif } @@ -183276,6 +183278,7 @@ int libsql_wal_insert_begin(sqlite3 *db) { goto out_unlock; } out_unlock: + sqlite3Error(db, rc); sqlite3_mutex_leave(db->mutex); return rc; } @@ -183291,6 +183294,7 @@ int libsql_wal_insert_end(sqlite3 *db) { goto out_unlock; } out_unlock: + sqlite3Error(db, rc); sqlite3_mutex_leave(db->mutex); return rc; } @@ -183322,6 +183326,7 @@ int libsql_wal_insert_frame( goto out_unlock; } out_unlock: + sqlite3Error(db, rc); sqlite3_mutex_leave(db->mutex); return rc; diff --git a/libsql-ffi/bundled/src/sqlite3.c b/libsql-ffi/bundled/src/sqlite3.c index caeb03816e..dcf254a18a 100644 --- a/libsql-ffi/bundled/src/sqlite3.c +++ b/libsql-ffi/bundled/src/sqlite3.c @@ -183219,6 +183219,7 @@ int libsql_wal_frame_count( sqlite3_mutex_enter(db->mutex); pPager = sqlite3BtreePager(db->aDb[0].pBt); rc = sqlite3PagerWalFrameCount(pPager, pnFrame); + sqlite3Error(db, rc); sqlite3_mutex_leave(db->mutex); return rc; #endif @@ -183244,12 +183245,13 @@ int libsql_wal_get_frame( if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; #endif - sqlite3_mutex_enter(db->mutex); - pPager = sqlite3BtreePager(db->aDb[0].pBt); - rc = sqlite3PagerWalReadFrameRaw(pPager, iFrame, pBuf, nBuf); - sqlite3_mutex_leave(db->mutex); + sqlite3_mutex_enter(db->mutex); + pPager = sqlite3BtreePager(db->aDb[0].pBt); + rc = sqlite3PagerWalReadFrameRaw(pPager, iFrame, pBuf, nBuf); + sqlite3Error(db, rc); + sqlite3_mutex_leave(db->mutex); - return rc; + return rc; #endif } @@ -183276,6 +183278,7 @@ int libsql_wal_insert_begin(sqlite3 *db) { goto out_unlock; } out_unlock: + sqlite3Error(db, rc); sqlite3_mutex_leave(db->mutex); return rc; } @@ -183291,6 +183294,7 @@ int libsql_wal_insert_end(sqlite3 *db) { goto out_unlock; } out_unlock: + sqlite3Error(db, rc); sqlite3_mutex_leave(db->mutex); return rc; } @@ -183322,6 +183326,7 @@ int libsql_wal_insert_frame( goto out_unlock; } out_unlock: + sqlite3Error(db, rc); sqlite3_mutex_leave(db->mutex); return rc;