tvl-depot/third_party/nix/src/libstore/sqlite.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

196 lines
4.8 KiB
C++
Raw Normal View History

#include "libstore/sqlite.hh"
2017-02-28 13:59:11 +01:00
#include <atomic>
#include <glog/logging.h>
2016-03-30 13:27:25 +02:00
#include <sqlite3.h>
#include "libutil/util.hh"
2017-02-28 13:59:11 +01:00
2016-03-30 13:27:25 +02:00
namespace nix {
[[noreturn]] void throwSQLiteError(sqlite3* db, const FormatOrString& fs) {
2016-03-30 13:27:25 +02:00
int err = sqlite3_errcode(db);
2018-04-16 11:37:58 +02:00
int exterr = sqlite3_extended_errcode(db);
auto path = sqlite3_db_filename(db, nullptr);
if (path == nullptr) {
path = "(in-memory)";
}
2016-03-30 13:27:25 +02:00
if (err == SQLITE_BUSY || err == SQLITE_PROTOCOL) {
2017-02-28 13:59:11 +01:00
throw SQLiteBusy(
err == SQLITE_PROTOCOL
? fmt("SQLite database '%s' is busy (SQLITE_PROTOCOL)", path)
: fmt("SQLite database '%s' is busy", path));
}
throw SQLiteError("%s: %s (in '%s')", fs.s, sqlite3_errstr(exterr), path);
2016-03-30 13:27:25 +02:00
}
2016-08-09 14:27:30 +02:00
SQLite::SQLite(const Path& path) {
if (sqlite3_open_v2(path.c_str(), &db,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
nullptr) != SQLITE_OK) {
throw Error(format("cannot open SQLite database '%s'") % path);
}
2016-08-09 14:27:30 +02:00
}
2016-03-30 13:27:25 +02:00
SQLite::~SQLite() {
try {
if ((db != nullptr) && sqlite3_close(db) != SQLITE_OK) {
2016-03-30 13:27:25 +02:00
throwSQLiteError(db, "closing database");
}
2016-03-30 13:27:25 +02:00
} catch (...) {
ignoreException();
}
}
2016-08-09 14:27:30 +02:00
void SQLite::exec(const std::string& stmt) {
2017-02-28 13:59:11 +01:00
retrySQLite<void>([&]() {
if (sqlite3_exec(db, stmt.c_str(), nullptr, nullptr, nullptr) !=
SQLITE_OK) {
throwSQLiteError(db, format("executing SQLite statement '%s'") % stmt);
}
2017-02-28 13:59:11 +01:00
});
2016-08-09 14:27:30 +02:00
}
void SQLiteStmt::create(sqlite3* db, const std::string& sql) {
2016-03-30 13:27:25 +02:00
checkInterrupt();
assert(!stmt);
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
throwSQLiteError(db, fmt("creating statement '%s'", sql));
}
2016-03-30 13:27:25 +02:00
this->db = db;
2017-02-28 13:59:11 +01:00
this->sql = sql;
2016-03-30 13:27:25 +02:00
}
SQLiteStmt::~SQLiteStmt() {
try {
if ((stmt != nullptr) && sqlite3_finalize(stmt) != SQLITE_OK) {
throwSQLiteError(db, fmt("finalizing statement '%s'", sql));
}
2016-03-30 13:27:25 +02:00
} catch (...) {
ignoreException();
}
}
SQLiteStmt::Use::Use(SQLiteStmt& stmt) : stmt(stmt) {
assert(stmt.stmt);
/* Note: sqlite3_reset() returns the error code for the most
recent call to sqlite3_step(). So ignore it. */
sqlite3_reset(stmt);
2016-04-05 15:29:56 +02:00
}
SQLiteStmt::Use::~Use() { sqlite3_reset(stmt); }
2016-03-30 13:27:25 +02:00
SQLiteStmt::Use& SQLiteStmt::Use::operator()(const std::string& value,
bool notNull) {
if (notNull) {
if (sqlite3_bind_text(stmt, curArg++, value.c_str(), -1,
SQLITE_TRANSIENT) != SQLITE_OK) {
throwSQLiteError(stmt.db, "binding argument");
}
} else {
bind();
}
return *this;
2016-03-30 13:27:25 +02:00
}
SQLiteStmt::Use& SQLiteStmt::Use::operator()(int64_t value, bool notNull) {
if (notNull) {
if (sqlite3_bind_int64(stmt, curArg++, value) != SQLITE_OK) {
throwSQLiteError(stmt.db, "binding argument");
}
} else {
bind();
}
return *this;
2016-03-30 13:27:25 +02:00
}
SQLiteStmt::Use& SQLiteStmt::Use::bind() {
if (sqlite3_bind_null(stmt, curArg++) != SQLITE_OK) {
throwSQLiteError(stmt.db, "binding argument");
}
return *this;
2016-03-30 13:27:25 +02:00
}
int SQLiteStmt::Use::step() { return sqlite3_step(stmt); }
2016-03-30 13:27:25 +02:00
void SQLiteStmt::Use::exec() {
int r = step();
assert(r != SQLITE_ROW);
if (r != SQLITE_DONE) {
throwSQLiteError(stmt.db, fmt("executing SQLite statement '%s'", stmt.sql));
}
}
bool SQLiteStmt::Use::next() {
int r = step();
if (r != SQLITE_DONE && r != SQLITE_ROW) {
throwSQLiteError(stmt.db, fmt("executing SQLite query '%s'", stmt.sql));
}
return r == SQLITE_ROW;
}
std::string SQLiteStmt::Use::getStr(int col) {
auto s = reinterpret_cast<const char*>(sqlite3_column_text(stmt, col));
assert(s);
return s;
}
int64_t SQLiteStmt::Use::getInt(int col) {
// FIXME: detect nulls?
return sqlite3_column_int64(stmt, col);
2016-03-30 13:27:25 +02:00
}
bool SQLiteStmt::Use::isNull(int col) {
return sqlite3_column_type(stmt, col) == SQLITE_NULL;
}
2016-03-30 13:27:25 +02:00
SQLiteTxn::SQLiteTxn(sqlite3* db) {
this->db = db;
if (sqlite3_exec(db, "begin;", nullptr, nullptr, nullptr) != SQLITE_OK) {
2016-03-30 13:27:25 +02:00
throwSQLiteError(db, "starting transaction");
}
2016-03-30 13:27:25 +02:00
active = true;
}
void SQLiteTxn::commit() {
if (sqlite3_exec(db, "commit;", nullptr, nullptr, nullptr) != SQLITE_OK) {
2016-03-30 13:27:25 +02:00
throwSQLiteError(db, "committing transaction");
}
2016-03-30 13:27:25 +02:00
active = false;
}
SQLiteTxn::~SQLiteTxn() {
try {
if (active &&
sqlite3_exec(db, "rollback;", nullptr, nullptr, nullptr) != SQLITE_OK) {
2016-03-30 13:27:25 +02:00
throwSQLiteError(db, "aborting transaction");
}
2016-03-30 13:27:25 +02:00
} catch (...) {
ignoreException();
}
}
2017-02-28 13:59:11 +01:00
void handleSQLiteBusy(const SQLiteBusy& e) {
static std::atomic<time_t> lastWarned{0};
time_t now = time(nullptr);
2017-02-28 13:59:11 +01:00
if (now > lastWarned + 10) {
lastWarned = now;
LOG(ERROR) << e.what();
2017-02-28 13:59:11 +01:00
}
/* Sleep for a while since retrying the transaction right away
is likely to fail again. */
checkInterrupt();
struct timespec t;
2017-02-28 13:59:11 +01:00
t.tv_sec = 0;
t.tv_nsec = (random() % 100) * 1000 * 1000; /* <= 0.1s */
nanosleep(&t, nullptr);
2017-02-28 13:59:11 +01:00
}
2016-03-30 13:27:25 +02:00
} // namespace nix