libstore: hide raw sqlite C api

this is, once again, what wrappers are for in the first place.

Change-Id: Ied8cb7384f561a345e457ccc977ba98e50dba207
This commit is contained in:
eldritch horrors
2025-01-20 23:07:44 +01:00
parent 509cc7d348
commit 8897c1af45
3 changed files with 20 additions and 26 deletions
+1 -2
View File
@@ -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)
+3 -10
View File
@@ -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;
+16 -14
View File
@@ -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<sqlite3_stmt, Finalize> 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<sqlite3, Rollback> 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<typename... Args>
[[noreturn]] static void throw_(SQLite & db, const std::string & fs, const Args & ... args) {
throw_(db.db.get(), HintFmt(fs, args...));
}
template<typename... Args>
[[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<typename... Args>
[[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);
};