From f63b0f4540b61d8cdac7a3c52ca6e190f7c1b8cf Mon Sep 17 00:00:00 2001 From: John Ericson Date: Wed, 20 Apr 2022 17:37:59 +0000 Subject: [PATCH] Actually, solve this in a lighter-weight way The templating is very superficial --- src/libstore/local-store.cc | 1 - src/libstore/sqlite-impl.hh | 42 ------------------------------------- src/libstore/sqlite.cc | 29 ++++++++++++++++++++++++- src/libstore/sqlite.hh | 14 +++++++++++-- 4 files changed, 40 insertions(+), 46 deletions(-) delete mode 100644 src/libstore/sqlite-impl.hh diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index 42cc30cbf..ece5bb5ef 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -10,7 +10,6 @@ #include "topo-sort.hh" #include "finally.hh" #include "compression.hh" -#include "sqlite-impl.hh" #include #include diff --git a/src/libstore/sqlite-impl.hh b/src/libstore/sqlite-impl.hh deleted file mode 100644 index c0a99403b..000000000 --- a/src/libstore/sqlite-impl.hh +++ /dev/null @@ -1,42 +0,0 @@ -#include "sqlite.hh" -#include "globals.hh" -#include "util.hh" - -#include - -#include - -namespace nix { - -template -SQLiteError::SQLiteError(const char *path, int errNo, int extendedErrNo, const Args & ... args) - : Error(""), path(path), errNo(errNo), extendedErrNo(extendedErrNo) -{ - auto hf = hintfmt(args...); - err.msg = hintfmt("%s: %s (in '%s')", - normaltxt(hf.str()), - sqlite3_errstr(extendedErrNo), - path ? path : "(in-memory)"); -} - -template -[[noreturn]] void SQLiteError::throw_(sqlite3 * db, const std::string & fs, const Args & ... args) -{ - int err = sqlite3_errcode(db); - int exterr = sqlite3_extended_errcode(db); - - auto path = sqlite3_db_filename(db, nullptr); - - if (err == SQLITE_BUSY || err == SQLITE_PROTOCOL) { - auto exp = SQLiteBusy(path, err, exterr, fs, args...); - exp.err.msg = hintfmt( - err == SQLITE_PROTOCOL - ? "SQLite database '%s' is busy (SQLITE_PROTOCOL)" - : "SQLite database '%s' is busy", - path ? path : "(in-memory)"); - throw exp; - } else - throw SQLiteError(path, err, exterr, fs, args...); -} - -} diff --git a/src/libstore/sqlite.cc b/src/libstore/sqlite.cc index 80290fa87..2090beabd 100644 --- a/src/libstore/sqlite.cc +++ b/src/libstore/sqlite.cc @@ -1,5 +1,4 @@ #include "sqlite.hh" -#include "sqlite-impl.hh" #include "globals.hh" #include "util.hh" @@ -9,6 +8,34 @@ namespace nix { +SQLiteError::SQLiteError(const char *path, int errNo, int extendedErrNo, hintformat && hf) + : Error(""), path(path), errNo(errNo), extendedErrNo(extendedErrNo) +{ + err.msg = hintfmt("%s: %s (in '%s')", + normaltxt(hf.str()), + sqlite3_errstr(extendedErrNo), + path ? path : "(in-memory)"); +} + +[[noreturn]] void SQLiteError::throw_(sqlite3 * db, hintformat && hf) +{ + int err = sqlite3_errcode(db); + int exterr = sqlite3_extended_errcode(db); + + auto path = sqlite3_db_filename(db, nullptr); + + if (err == SQLITE_BUSY || err == SQLITE_PROTOCOL) { + auto exp = SQLiteBusy(path, err, exterr, std::move(hf)); + exp.err.msg = hintfmt( + err == SQLITE_PROTOCOL + ? "SQLite database '%s' is busy (SQLITE_PROTOCOL)" + : "SQLite database '%s' is busy", + path ? path : "(in-memory)"); + throw exp; + } else + throw SQLiteError(path, err, exterr, std::move(hf)); +} + SQLite::SQLite(const Path & path, bool create) { // useSQLiteWAL also indicates what virtual file system we need. Using diff --git a/src/libstore/sqlite.hh b/src/libstore/sqlite.hh index 72ec302e1..3a4ad8633 100644 --- a/src/libstore/sqlite.hh +++ b/src/libstore/sqlite.hh @@ -102,11 +102,21 @@ struct SQLiteError : Error int errNo, extendedErrNo; template - [[noreturn]] static void throw_(sqlite3 * db, const std::string & fs, const Args & ... args); + [[noreturn]] static void throw_(sqlite3 * db, const std::string & fs, const Args & ... args) { + throw_(db, hintfmt(fs, args...)); + } protected: + + SQLiteError(const char *path, int errNo, int extendedErrNo, hintformat && hf); + template - SQLiteError(const char *path, int errNo, int extendedErrNo, const Args & ... args); + SQLiteError(const char *path, int errNo, int extendedErrNo, const std::string & fs, const Args & ... args) + : SQLiteError(path, errNo, extendedErrNo, hintfmt(fs, args...)) + { } + + [[noreturn]] static void throw_(sqlite3 * db, hintformat && hf); + }; MakeError(SQLiteBusy, SQLiteError);