2016-03-30 13:27:25 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <string>
|
2023-08-26 09:00:37 +02:00
|
|
|
#include <cstdint>
|
daemon: Bump to C++20 and use ‘std::format’ instead of ‘boost::format’.
* nix/boost: This directory and all files inside it are removed.
* nix/libstore/build.cc (Goal::trace): Use ‘std::string’ instead of ‘const format &’.
(DerivationGoal::startBuilder, ...): Use ‘std::format’ or ‘std::vformat’ instead of ‘boost::format’.
* nix/libstore/builtins.cc (builtinDownload): Same.
* nix/libstore/derivations.cc (DerivationOutput::parseHashInfo, ...): Same.
* nix/libstore/gc.cc (LocalStore::openGCLock, ...): Same.
* nix/libstore/globals.cc (Settings::_get): Same.
* nix/libstore/local-store.cc: (checkStoreNotSymlink, ...): Same.
* nix/libstore/misc.cc (dfsVisit, showBytes): Same
* nix/libstore/optimise-store.cc (makeWritable, ...): Same.
* nix/libstore/pathlocks.cc (openLockFile, ...): Same.
* nix/libstore/references.cc (search, scanForReferences): Same.
* nix/libstore/sqlite.hh (throwSQLiteError): Use ‘std::string’ instead of ‘const format &’.
* nix/libstore/sqlite.cc (throwSQLiteError): Use ‘std::string’ instead of ‘const format &’.
* nix/libstore/store-api.cc (assertStorePath, ...): Use ‘std::format’ instead of ‘boost::format’.
* nix/libutil/affinity.cc (setAffinityTo): Same.
* nix/libutil/archive.cc (dumpContents, ...): Same.
* nix/libutil/hash.cc (parseHash, parseHash32, parseHash16or32, hashFile): Same.
* nix/libutil/hash.hh (parseHash, parseHash32, parseHash16or32, isHash): Same.
* nix/libutil/serialise.cc : Add ‘<cassert>’ header file.
* nix/libutil/spawn.cc (addPhaseAfter, ...): Use ‘std::format’ instead of ‘boost::format’.
* nix/libutil/types.hh (FormatOrString): Removed.
(BaseError, BaseError::addPrefix, SysError, MakeError):
Use ‘std::string or std::string_view’ instead of ‘FormatOrString’.
* nix/libutil/util.hh (Nest::open, printMsg_, warnOnce, expect): Same.
* nix/libutil/util.cc (BaseError::BaseError, ...): Same.
(writeToStderr, _interrupted): Use std::uncaught_exceptions() instead of std::uncaught_exception()
* nix/nix-daemon/nix-daemon.cc (performOp, ...): Same.
* nix/nix-daemon/guix-daemon.cc (string_to_bool, ...): Same.
* nix/local.mk: Remove ‘libformat.a’ from ‘noinst_LIBRARIES’,
remove ‘libformat_a_SOURCES’ and ‘libformat_headers’,
remove ‘libformat_a_CPPFLAGS’ from ‘libutil_a_CPPFLAGS’ and ‘guix_daemon_LDADD’,
update ‘AM_CXXFLAGS’ to ‘-std=c++20’.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2025-07-21 11:02:40 +08:00
|
|
|
#include <string_view>
|
2016-03-30 13:27:25 +02:00
|
|
|
|
|
|
|
|
#include "types.hh"
|
|
|
|
|
|
|
|
|
|
class sqlite3;
|
|
|
|
|
class sqlite3_stmt;
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
/* RAII wrapper to close a SQLite database automatically. */
|
|
|
|
|
struct SQLite
|
|
|
|
|
{
|
|
|
|
|
sqlite3 * db;
|
|
|
|
|
SQLite() { db = 0; }
|
|
|
|
|
~SQLite();
|
|
|
|
|
operator sqlite3 * () { return db; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* RAII wrapper to create and destroy SQLite prepared statements. */
|
|
|
|
|
struct SQLiteStmt
|
|
|
|
|
{
|
2016-03-30 15:50:45 +02:00
|
|
|
sqlite3 * db = 0;
|
|
|
|
|
sqlite3_stmt * stmt = 0;
|
|
|
|
|
SQLiteStmt() { }
|
2016-03-30 13:27:25 +02:00
|
|
|
void create(sqlite3 * db, const std::string & s);
|
|
|
|
|
~SQLiteStmt();
|
|
|
|
|
operator sqlite3_stmt * () { return stmt; }
|
|
|
|
|
|
2016-03-30 15:50:45 +02:00
|
|
|
/* Helper for binding / executing statements. */
|
|
|
|
|
class Use
|
|
|
|
|
{
|
|
|
|
|
friend struct SQLiteStmt;
|
|
|
|
|
private:
|
|
|
|
|
SQLiteStmt & stmt;
|
|
|
|
|
unsigned int curArg = 1;
|
|
|
|
|
Use(SQLiteStmt & stmt);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
~Use();
|
|
|
|
|
|
|
|
|
|
/* Bind the next parameter. */
|
|
|
|
|
Use & operator () (const std::string & value, bool notNull = true);
|
|
|
|
|
Use & operator () (int64_t value, bool notNull = true);
|
|
|
|
|
Use & bind(); // null
|
|
|
|
|
|
|
|
|
|
int step();
|
|
|
|
|
|
|
|
|
|
/* Execute a statement that does not return rows. */
|
|
|
|
|
void exec();
|
|
|
|
|
|
|
|
|
|
/* For statements that return 0 or more rows. Returns true iff
|
|
|
|
|
a row is available. */
|
|
|
|
|
bool next();
|
|
|
|
|
|
|
|
|
|
std::string getStr(int col);
|
|
|
|
|
int64_t getInt(int col);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Use use()
|
|
|
|
|
{
|
|
|
|
|
return Use(*this);
|
|
|
|
|
}
|
2016-03-30 13:27:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* RAII helper that ensures transactions are aborted unless explicitly
|
|
|
|
|
committed. */
|
|
|
|
|
struct SQLiteTxn
|
|
|
|
|
{
|
|
|
|
|
bool active = false;
|
|
|
|
|
sqlite3 * db;
|
|
|
|
|
|
|
|
|
|
SQLiteTxn(sqlite3 * db);
|
|
|
|
|
|
|
|
|
|
void commit();
|
|
|
|
|
|
|
|
|
|
~SQLiteTxn();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MakeError(SQLiteError, Error);
|
|
|
|
|
MakeError(SQLiteBusy, SQLiteError);
|
|
|
|
|
|
daemon: Bump to C++20 and use ‘std::format’ instead of ‘boost::format’.
* nix/boost: This directory and all files inside it are removed.
* nix/libstore/build.cc (Goal::trace): Use ‘std::string’ instead of ‘const format &’.
(DerivationGoal::startBuilder, ...): Use ‘std::format’ or ‘std::vformat’ instead of ‘boost::format’.
* nix/libstore/builtins.cc (builtinDownload): Same.
* nix/libstore/derivations.cc (DerivationOutput::parseHashInfo, ...): Same.
* nix/libstore/gc.cc (LocalStore::openGCLock, ...): Same.
* nix/libstore/globals.cc (Settings::_get): Same.
* nix/libstore/local-store.cc: (checkStoreNotSymlink, ...): Same.
* nix/libstore/misc.cc (dfsVisit, showBytes): Same
* nix/libstore/optimise-store.cc (makeWritable, ...): Same.
* nix/libstore/pathlocks.cc (openLockFile, ...): Same.
* nix/libstore/references.cc (search, scanForReferences): Same.
* nix/libstore/sqlite.hh (throwSQLiteError): Use ‘std::string’ instead of ‘const format &’.
* nix/libstore/sqlite.cc (throwSQLiteError): Use ‘std::string’ instead of ‘const format &’.
* nix/libstore/store-api.cc (assertStorePath, ...): Use ‘std::format’ instead of ‘boost::format’.
* nix/libutil/affinity.cc (setAffinityTo): Same.
* nix/libutil/archive.cc (dumpContents, ...): Same.
* nix/libutil/hash.cc (parseHash, parseHash32, parseHash16or32, hashFile): Same.
* nix/libutil/hash.hh (parseHash, parseHash32, parseHash16or32, isHash): Same.
* nix/libutil/serialise.cc : Add ‘<cassert>’ header file.
* nix/libutil/spawn.cc (addPhaseAfter, ...): Use ‘std::format’ instead of ‘boost::format’.
* nix/libutil/types.hh (FormatOrString): Removed.
(BaseError, BaseError::addPrefix, SysError, MakeError):
Use ‘std::string or std::string_view’ instead of ‘FormatOrString’.
* nix/libutil/util.hh (Nest::open, printMsg_, warnOnce, expect): Same.
* nix/libutil/util.cc (BaseError::BaseError, ...): Same.
(writeToStderr, _interrupted): Use std::uncaught_exceptions() instead of std::uncaught_exception()
* nix/nix-daemon/nix-daemon.cc (performOp, ...): Same.
* nix/nix-daemon/guix-daemon.cc (string_to_bool, ...): Same.
* nix/local.mk: Remove ‘libformat.a’ from ‘noinst_LIBRARIES’,
remove ‘libformat_a_SOURCES’ and ‘libformat_headers’,
remove ‘libformat_a_CPPFLAGS’ from ‘libutil_a_CPPFLAGS’ and ‘guix_daemon_LDADD’,
update ‘AM_CXXFLAGS’ to ‘-std=c++20’.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
2025-07-21 11:02:40 +08:00
|
|
|
[[noreturn]] void throwSQLiteError(sqlite3 * db, std::string_view f);
|
2016-03-30 13:27:25 +02:00
|
|
|
|
|
|
|
|
/* Convenience function for retrying a SQLite transaction when the
|
|
|
|
|
database is busy. */
|
|
|
|
|
template<typename T>
|
|
|
|
|
T retrySQLite(std::function<T()> fun)
|
|
|
|
|
{
|
|
|
|
|
while (true) {
|
|
|
|
|
try {
|
|
|
|
|
return fun();
|
|
|
|
|
} catch (SQLiteBusy & e) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|