2020-05-27 22:56:34 +02:00
|
|
|
#include "libstore/sqlite.hh"
|
2020-05-19 16:54:39 +02:00
|
|
|
|
2017-02-28 13:59:11 +01:00
|
|
|
#include <atomic>
|
2020-05-19 16:54:39 +02:00
|
|
|
|
2020-05-19 02:02:44 +02:00
|
|
|
#include <glog/logging.h>
|
2016-03-30 13:27:25 +02:00
|
|
|
#include <sqlite3.h>
|
2020-05-19 16:54:39 +02:00
|
|
|
|
2020-05-27 22:56:34 +02:00
|
|
|
#include "libutil/util.hh"
|
2017-02-28 13:59:11 +01:00
|
|
|
|
2016-03-30 13:27:25 +02:00
|
|
|
namespace nix {
|
|
|
|
|
2018-03-14 19:01:22 +01:00
|
|
|
[[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);
|
2017-02-28 13:20:12 +01:00
|
|
|
|
|
|
|
auto path = sqlite3_db_filename(db, nullptr);
|
2020-05-20 23:27:37 +02:00
|
|
|
if (path == nullptr) {
|
2020-05-19 18:38:04 +02:00
|
|
|
path = "(in-memory)";
|
|
|
|
}
|
2017-02-28 13:20:12 +01:00
|
|
|
|
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
|
2017-07-30 13:27:57 +02:00
|
|
|
? fmt("SQLite database '%s' is busy (SQLITE_PROTOCOL)", path)
|
|
|
|
: fmt("SQLite database '%s' is busy", path));
|
2020-05-19 18:38:04 +02:00
|
|
|
}
|
2020-05-20 23:27:37 +02:00
|
|
|
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,
|
2020-05-20 05:33:07 +02:00
|
|
|
nullptr) != SQLITE_OK) {
|
2017-07-30 13:27:57 +02:00
|
|
|
throw Error(format("cannot open SQLite database '%s'") % path);
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
2016-08-09 14:27:30 +02:00
|
|
|
}
|
|
|
|
|
2016-03-30 13:27:25 +02:00
|
|
|
SQLite::~SQLite() {
|
|
|
|
try {
|
2020-05-20 23:27:37 +02:00
|
|
|
if ((db != nullptr) && sqlite3_close(db) != SQLITE_OK) {
|
2016-03-30 13:27:25 +02:00
|
|
|
throwSQLiteError(db, "closing database");
|
2020-05-19 18:38:04 +02:00
|
|
|
}
|
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>([&]() {
|
2020-05-20 05:33:07 +02:00
|
|
|
if (sqlite3_exec(db, stmt.c_str(), nullptr, nullptr, nullptr) !=
|
|
|
|
SQLITE_OK) {
|
2017-07-30 13:27:57 +02:00
|
|
|
throwSQLiteError(db, format("executing SQLite statement '%s'") % stmt);
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
2017-02-28 13:59:11 +01:00
|
|
|
});
|
2016-08-09 14:27:30 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 23:29:21 +02:00
|
|
|
void SQLiteStmt::create(sqlite3* db, const std::string& sql) {
|
2016-03-30 13:27:25 +02:00
|
|
|
checkInterrupt();
|
|
|
|
assert(!stmt);
|
2020-05-20 05:33:07 +02:00
|
|
|
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
|
2017-07-30 13:27:57 +02:00
|
|
|
throwSQLiteError(db, fmt("creating statement '%s'", sql));
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
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 {
|
2020-05-20 23:27:37 +02:00
|
|
|
if ((stmt != nullptr) && sqlite3_finalize(stmt) != SQLITE_OK) {
|
2017-07-30 13:27:57 +02:00
|
|
|
throwSQLiteError(db, fmt("finalizing statement '%s'", sql));
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
2016-03-30 13:27:25 +02:00
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 15:50:45 +02:00
|
|
|
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
|
|
|
|
2016-03-30 15:50:45 +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");
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
2020-05-19 18:38:04 +02:00
|
|
|
} else {
|
2016-03-30 15:50:45 +02:00
|
|
|
bind();
|
2020-05-19 18:38:04 +02:00
|
|
|
}
|
2016-03-30 15:50:45 +02:00
|
|
|
return *this;
|
2016-03-30 13:27:25 +02:00
|
|
|
}
|
|
|
|
|
2016-03-30 15:50:45 +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");
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
2020-05-19 18:38:04 +02:00
|
|
|
} else {
|
2016-03-30 15:50:45 +02:00
|
|
|
bind();
|
2020-05-19 18:38:04 +02:00
|
|
|
}
|
2016-03-30 15:50:45 +02:00
|
|
|
return *this;
|
2016-03-30 13:27:25 +02:00
|
|
|
}
|
|
|
|
|
2016-03-30 15:50:45 +02:00
|
|
|
SQLiteStmt::Use& SQLiteStmt::Use::bind() {
|
2020-05-19 18:38:04 +02:00
|
|
|
if (sqlite3_bind_null(stmt, curArg++) != SQLITE_OK) {
|
2016-03-30 15:50:45 +02:00
|
|
|
throwSQLiteError(stmt.db, "binding argument");
|
2020-05-19 18:38:04 +02:00
|
|
|
}
|
2016-03-30 15:50:45 +02:00
|
|
|
return *this;
|
2016-03-30 13:27:25 +02:00
|
|
|
}
|
|
|
|
|
2016-03-30 15:50:45 +02:00
|
|
|
int SQLiteStmt::Use::step() { return sqlite3_step(stmt); }
|
2016-03-30 13:27:25 +02:00
|
|
|
|
2016-03-30 15:50:45 +02:00
|
|
|
void SQLiteStmt::Use::exec() {
|
|
|
|
int r = step();
|
|
|
|
assert(r != SQLITE_ROW);
|
|
|
|
if (r != SQLITE_DONE) {
|
2017-07-30 13:27:57 +02:00
|
|
|
throwSQLiteError(stmt.db, fmt("executing SQLite statement '%s'", stmt.sql));
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
2016-03-30 15:50:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SQLiteStmt::Use::next() {
|
|
|
|
int r = step();
|
|
|
|
if (r != SQLITE_DONE && r != SQLITE_ROW) {
|
2017-07-30 13:27:57 +02:00
|
|
|
throwSQLiteError(stmt.db, fmt("executing SQLite query '%s'", stmt.sql));
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
2016-03-30 15:50:45 +02:00
|
|
|
return r == SQLITE_ROW;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string SQLiteStmt::Use::getStr(int col) {
|
2020-08-02 02:17:44 +02:00
|
|
|
auto s = reinterpret_cast<const char*>(sqlite3_column_text(stmt, col));
|
2016-03-30 15:50:45 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-04-20 14:12:38 +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;
|
2020-05-20 05:33:07 +02:00
|
|
|
if (sqlite3_exec(db, "begin;", nullptr, nullptr, nullptr) != SQLITE_OK) {
|
2016-03-30 13:27:25 +02:00
|
|
|
throwSQLiteError(db, "starting transaction");
|
2020-05-19 18:38:04 +02:00
|
|
|
}
|
2016-03-30 13:27:25 +02:00
|
|
|
active = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SQLiteTxn::commit() {
|
2020-05-20 05:33:07 +02:00
|
|
|
if (sqlite3_exec(db, "commit;", nullptr, nullptr, nullptr) != SQLITE_OK) {
|
2016-03-30 13:27:25 +02:00
|
|
|
throwSQLiteError(db, "committing transaction");
|
2020-05-19 18:38:04 +02:00
|
|
|
}
|
2016-03-30 13:27:25 +02:00
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SQLiteTxn::~SQLiteTxn() {
|
|
|
|
try {
|
2020-05-20 05:33:07 +02:00
|
|
|
if (active &&
|
|
|
|
sqlite3_exec(db, "rollback;", nullptr, nullptr, nullptr) != SQLITE_OK) {
|
2016-03-30 13:27:25 +02:00
|
|
|
throwSQLiteError(db, "aborting transaction");
|
2020-05-19 18:38:04 +02:00
|
|
|
}
|
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};
|
|
|
|
|
2020-05-20 05:33:07 +02:00
|
|
|
time_t now = time(nullptr);
|
2017-02-28 13:59:11 +01:00
|
|
|
|
|
|
|
if (now > lastWarned + 10) {
|
|
|
|
lastWarned = now;
|
2020-05-19 02:02:44 +02:00
|
|
|
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();
|
2020-08-02 00:32:00 +02:00
|
|
|
struct timespec t;
|
2017-02-28 13:59:11 +01:00
|
|
|
t.tv_sec = 0;
|
|
|
|
t.tv_nsec = (random() % 100) * 1000 * 1000; /* <= 0.1s */
|
2020-05-20 05:33:07 +02:00
|
|
|
nanosleep(&t, nullptr);
|
2017-02-28 13:59:11 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 13:27:25 +02:00
|
|
|
} // namespace nix
|