From 8897c1af452b96a06cd0a49afcaa5f9704a1f255 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Mon, 20 Jan 2025 23:06:25 +0100 Subject: [PATCH] libstore: hide raw sqlite C api this is, once again, what wrappers are for in the first place. Change-Id: Ied8cb7384f561a345e457ccc977ba98e50dba207 --- lix/libstore/local-store.cc | 3 +-- lix/libstore/sqlite.cc | 13 +++---------- lix/libstore/sqlite.hh | 30 ++++++++++++++++-------------- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/lix/libstore/local-store.cc b/lix/libstore/local-store.cc index ca0bca98d..e52f562ab 100644 --- a/lix/libstore/local-store.cc +++ b/lix/libstore/local-store.cc @@ -524,8 +524,7 @@ void LocalStore::openDB(DBState & state, bool create) { SQLiteStmt stmt = db.create("pragma main.journal_mode;"); auto use = stmt.use(); - if (use.step() != SQLITE_ROW) - SQLiteError::throw_(db, "querying journal mode"); + assert(use.next()); prevMode = use.getStr(0); } if (prevMode != mode) diff --git a/lix/libstore/sqlite.cc b/lix/libstore/sqlite.cc index 8b4a77ba4..217f470a2 100644 --- a/lix/libstore/sqlite.cc +++ b/lix/libstore/sqlite.cc @@ -206,22 +206,15 @@ SQLiteStmt::Use & SQLiteStmt::Use::bind() return *this; } -int SQLiteStmt::Use::step() -{ - return sqlite3_step(stmt.stmt.get()); -} - void SQLiteStmt::Use::exec() { - int r = step(); - assert(r != SQLITE_ROW); - if (r != SQLITE_DONE) - SQLiteError::throw_(stmt.db, fmt("executing SQLite statement '%s'", sqlite3_expanded_sql(stmt.stmt.get()))); + bool r = next(); + assert(!r); } bool SQLiteStmt::Use::next() { - int r = step(); + int r = sqlite3_step(stmt.stmt.get()); if (r != SQLITE_DONE && r != SQLITE_ROW) SQLiteError::throw_(stmt.db, fmt("executing SQLite query '%s'", sqlite3_expanded_sql(stmt.stmt.get()))); return r == SQLITE_ROW; diff --git a/lix/libstore/sqlite.hh b/lix/libstore/sqlite.hh index e499c3c50..f3412f366 100644 --- a/lix/libstore/sqlite.hh +++ b/lix/libstore/sqlite.hh @@ -74,6 +74,8 @@ public: */ class SQLiteStmt { + friend SQLite; + struct Finalize { SQLiteStmt * parent; void operator()(sqlite3_stmt * stmt); @@ -83,9 +85,10 @@ class SQLiteStmt std::unique_ptr stmt; std::string sql; + SQLiteStmt(sqlite3 * db, const std::string & sql); + public: SQLiteStmt() = default; - SQLiteStmt(sqlite3 * db, const std::string & sql); /** * Helper for binding / executing statements. @@ -110,8 +113,6 @@ public: Use & operator () (int64_t value, bool notNull = true); Use & bind(); // null - int step(); - /** * Execute a statement that does not return rows. */ @@ -141,34 +142,30 @@ public: */ class SQLiteTxn { + friend SQLite; + struct Rollback { void operator()(sqlite3 * db); }; std::unique_ptr db; -public: explicit SQLiteTxn(sqlite3 * db); +public: void commit(); }; struct SQLiteError : Error { + friend SQLite; + friend SQLiteStmt; + friend SQLiteTxn; + std::string path; std::string errMsg; int errNo, extendedErrNo, offset; - template - [[noreturn]] static void throw_(SQLite & db, const std::string & fs, const Args & ... args) { - throw_(db.db.get(), HintFmt(fs, args...)); - } - - template - [[noreturn]] static void throw_(sqlite3 * db, const std::string & fs, const Args & ... args) { - throw_(db, HintFmt(fs, args...)); - } - SQLiteError(const char *path, const char *errMsg, int errNo, int extendedErrNo, int offset, HintFmt && hf); protected: @@ -178,6 +175,11 @@ protected: : SQLiteError(path, errMsg, errNo, extendedErrNo, offset, HintFmt(fs, args...)) { } + template + [[noreturn]] static void throw_(sqlite3 * db, const std::string & fs, const Args & ... args) { + throw_(db, HintFmt(fs, args...)); + } + [[noreturn]] static void throw_(sqlite3 * db, HintFmt && hf); };