2016-03-30 11:27:25 +00:00
|
|
|
#include "sqlite.hh"
|
2021-11-03 12:10:16 +00:00
|
|
|
#include "globals.hh"
|
2016-03-30 11:27:25 +00:00
|
|
|
#include "util.hh"
|
|
|
|
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
2017-02-28 12:59:11 +00:00
|
|
|
#include <atomic>
|
|
|
|
|
2016-03-30 11:27:25 +00:00
|
|
|
namespace nix {
|
|
|
|
|
2018-03-14 18:01:22 +00:00
|
|
|
[[noreturn]] void throwSQLiteError(sqlite3 * db, const FormatOrString & fs)
|
2016-03-30 11:27:25 +00:00
|
|
|
{
|
|
|
|
int err = sqlite3_errcode(db);
|
2018-04-16 09:37:58 +00:00
|
|
|
int exterr = sqlite3_extended_errcode(db);
|
2017-02-28 12:20:12 +00:00
|
|
|
|
|
|
|
auto path = sqlite3_db_filename(db, nullptr);
|
|
|
|
if (!path) path = "(in-memory)";
|
|
|
|
|
2016-03-30 11:27:25 +00:00
|
|
|
if (err == SQLITE_BUSY || err == SQLITE_PROTOCOL) {
|
2017-02-28 12:59:11 +00:00
|
|
|
throw SQLiteBusy(
|
|
|
|
err == SQLITE_PROTOCOL
|
2017-07-30 11:27:57 +00:00
|
|
|
? fmt("SQLite database '%s' is busy (SQLITE_PROTOCOL)", path)
|
|
|
|
: fmt("SQLite database '%s' is busy", path));
|
2016-03-30 11:27:25 +00:00
|
|
|
}
|
|
|
|
else
|
2018-04-16 09:37:58 +00:00
|
|
|
throw SQLiteError("%s: %s (in '%s')", fs.s, sqlite3_errstr(exterr), path);
|
2016-03-30 11:27:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 20:25:48 +00:00
|
|
|
SQLite::SQLite(const Path & path, bool create)
|
2016-08-09 12:27:30 +00:00
|
|
|
{
|
2021-11-03 12:10:16 +00:00
|
|
|
// useSQLiteWAL also indicates what virtual file system we need. Using
|
|
|
|
// `unix-dotfile` is needed on NFS file systems and on Windows' Subsystem
|
|
|
|
// for Linux (WSL) where useSQLiteWAL should be false by default.
|
|
|
|
const char *vfs = settings.useSQLiteWAL ? 0 : "unix-dotfile";
|
2016-08-09 12:27:30 +00:00
|
|
|
if (sqlite3_open_v2(path.c_str(), &db,
|
2021-11-03 12:10:16 +00:00
|
|
|
SQLITE_OPEN_READWRITE | (create ? SQLITE_OPEN_CREATE : 0), vfs) != SQLITE_OK)
|
2020-04-21 23:07:07 +00:00
|
|
|
throw Error("cannot open SQLite database '%s'", path);
|
2019-06-07 20:25:48 +00:00
|
|
|
|
|
|
|
if (sqlite3_busy_timeout(db, 60 * 60 * 1000) != SQLITE_OK)
|
|
|
|
throwSQLiteError(db, "setting timeout");
|
|
|
|
|
|
|
|
exec("pragma foreign_keys = 1");
|
2016-08-09 12:27:30 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 11:27:25 +00:00
|
|
|
SQLite::~SQLite()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (db && sqlite3_close(db) != SQLITE_OK)
|
|
|
|
throwSQLiteError(db, "closing database");
|
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 20:25:48 +00:00
|
|
|
void SQLite::isCache()
|
|
|
|
{
|
|
|
|
exec("pragma synchronous = off");
|
|
|
|
exec("pragma main.journal_mode = truncate");
|
|
|
|
}
|
|
|
|
|
2016-08-09 12:27:30 +00:00
|
|
|
void SQLite::exec(const std::string & stmt)
|
|
|
|
{
|
2017-02-28 12:59:11 +00:00
|
|
|
retrySQLite<void>([&]() {
|
|
|
|
if (sqlite3_exec(db, stmt.c_str(), 0, 0, 0) != SQLITE_OK)
|
2017-07-30 11:27:57 +00:00
|
|
|
throwSQLiteError(db, format("executing SQLite statement '%s'") % stmt);
|
2017-02-28 12:59:11 +00:00
|
|
|
});
|
2016-08-09 12:27:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 21:04:21 +00:00
|
|
|
uint64_t SQLite::getLastInsertedRowId()
|
|
|
|
{
|
|
|
|
return sqlite3_last_insert_rowid(db);
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:00:00 +00:00
|
|
|
void SQLiteStmt::create(sqlite3 * db, const std::string & sql)
|
2016-03-30 11:27:25 +00:00
|
|
|
{
|
|
|
|
checkInterrupt();
|
|
|
|
assert(!stmt);
|
2017-02-28 12:59:11 +00:00
|
|
|
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, 0) != SQLITE_OK)
|
2017-07-30 11:27:57 +00:00
|
|
|
throwSQLiteError(db, fmt("creating statement '%s'", sql));
|
2016-03-30 11:27:25 +00:00
|
|
|
this->db = db;
|
2017-02-28 12:59:11 +00:00
|
|
|
this->sql = sql;
|
2016-03-30 11:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SQLiteStmt::~SQLiteStmt()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (stmt && sqlite3_finalize(stmt) != SQLITE_OK)
|
2017-07-30 11:27:57 +00:00
|
|
|
throwSQLiteError(db, fmt("finalizing statement '%s'", sql));
|
2016-03-30 11:27:25 +00:00
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 13:50:45 +00:00
|
|
|
SQLiteStmt::Use::Use(SQLiteStmt & stmt)
|
|
|
|
: stmt(stmt)
|
2016-03-30 11:27:25 +00:00
|
|
|
{
|
2016-03-30 13:50:45 +00:00
|
|
|
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 13:29:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SQLiteStmt::Use::~Use()
|
|
|
|
{
|
|
|
|
sqlite3_reset(stmt);
|
2016-03-30 11:27:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 23:00:56 +00:00
|
|
|
SQLiteStmt::Use & SQLiteStmt::Use::operator () (std::string_view value, bool notNull)
|
2016-03-30 11:27:25 +00:00
|
|
|
{
|
2016-03-30 13:50:45 +00:00
|
|
|
if (notNull) {
|
2020-04-16 23:00:56 +00:00
|
|
|
if (sqlite3_bind_text(stmt, curArg++, value.data(), -1, SQLITE_TRANSIENT) != SQLITE_OK)
|
2016-03-30 13:50:45 +00:00
|
|
|
throwSQLiteError(stmt.db, "binding argument");
|
|
|
|
} else
|
|
|
|
bind();
|
|
|
|
return *this;
|
2016-03-30 11:27:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 20:25:48 +00:00
|
|
|
SQLiteStmt::Use & SQLiteStmt::Use::operator () (const unsigned char * data, size_t len, bool notNull)
|
|
|
|
{
|
|
|
|
if (notNull) {
|
|
|
|
if (sqlite3_bind_blob(stmt, curArg++, data, len, SQLITE_TRANSIENT) != SQLITE_OK)
|
|
|
|
throwSQLiteError(stmt.db, "binding argument");
|
|
|
|
} else
|
|
|
|
bind();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-03-30 13:50:45 +00:00
|
|
|
SQLiteStmt::Use & SQLiteStmt::Use::operator () (int64_t value, bool notNull)
|
2016-03-30 11:27:25 +00:00
|
|
|
{
|
2016-03-30 13:50:45 +00:00
|
|
|
if (notNull) {
|
|
|
|
if (sqlite3_bind_int64(stmt, curArg++, value) != SQLITE_OK)
|
|
|
|
throwSQLiteError(stmt.db, "binding argument");
|
|
|
|
} else
|
|
|
|
bind();
|
|
|
|
return *this;
|
2016-03-30 11:27:25 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 13:50:45 +00:00
|
|
|
SQLiteStmt::Use & SQLiteStmt::Use::bind()
|
2016-03-30 11:27:25 +00:00
|
|
|
{
|
|
|
|
if (sqlite3_bind_null(stmt, curArg++) != SQLITE_OK)
|
2016-03-30 13:50:45 +00:00
|
|
|
throwSQLiteError(stmt.db, "binding argument");
|
|
|
|
return *this;
|
2016-03-30 11:27:25 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 13:50:45 +00:00
|
|
|
int SQLiteStmt::Use::step()
|
2016-03-30 11:27:25 +00:00
|
|
|
{
|
2016-03-30 13:50:45 +00:00
|
|
|
return sqlite3_step(stmt);
|
2016-03-30 11:27:25 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 13:50:45 +00:00
|
|
|
void SQLiteStmt::Use::exec()
|
2016-03-30 11:27:25 +00:00
|
|
|
{
|
2016-03-30 13:50:45 +00:00
|
|
|
int r = step();
|
|
|
|
assert(r != SQLITE_ROW);
|
|
|
|
if (r != SQLITE_DONE)
|
2020-11-13 16:00:32 +00:00
|
|
|
throwSQLiteError(stmt.db, fmt("executing SQLite statement '%s'", sqlite3_expanded_sql(stmt.stmt)));
|
2016-03-30 13:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SQLiteStmt::Use::next()
|
|
|
|
{
|
|
|
|
int r = step();
|
|
|
|
if (r != SQLITE_DONE && r != SQLITE_ROW)
|
2020-11-13 16:00:32 +00:00
|
|
|
throwSQLiteError(stmt.db, fmt("executing SQLite query '%s'", sqlite3_expanded_sql(stmt.stmt)));
|
2016-03-30 13:50:45 +00:00
|
|
|
return r == SQLITE_ROW;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string SQLiteStmt::Use::getStr(int col)
|
|
|
|
{
|
|
|
|
auto s = (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 11:27:25 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 12:12:38 +00:00
|
|
|
bool SQLiteStmt::Use::isNull(int col)
|
|
|
|
{
|
|
|
|
return sqlite3_column_type(stmt, col) == SQLITE_NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-30 11:27:25 +00:00
|
|
|
SQLiteTxn::SQLiteTxn(sqlite3 * db)
|
|
|
|
{
|
|
|
|
this->db = db;
|
|
|
|
if (sqlite3_exec(db, "begin;", 0, 0, 0) != SQLITE_OK)
|
|
|
|
throwSQLiteError(db, "starting transaction");
|
|
|
|
active = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SQLiteTxn::commit()
|
|
|
|
{
|
|
|
|
if (sqlite3_exec(db, "commit;", 0, 0, 0) != SQLITE_OK)
|
|
|
|
throwSQLiteError(db, "committing transaction");
|
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SQLiteTxn::~SQLiteTxn()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (active && sqlite3_exec(db, "rollback;", 0, 0, 0) != SQLITE_OK)
|
|
|
|
throwSQLiteError(db, "aborting transaction");
|
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 12:59:11 +00:00
|
|
|
void handleSQLiteBusy(const SQLiteBusy & e)
|
|
|
|
{
|
|
|
|
static std::atomic<time_t> lastWarned{0};
|
|
|
|
|
|
|
|
time_t now = time(0);
|
|
|
|
|
|
|
|
if (now > lastWarned + 10) {
|
|
|
|
lastWarned = now;
|
2020-06-15 12:06:58 +00:00
|
|
|
logWarning({
|
2021-01-20 23:27:36 +00:00
|
|
|
.msg = hintfmt(e.what())
|
2020-06-15 12:06:58 +00:00
|
|
|
});
|
2017-02-28 12:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sleep for a while since retrying the transaction right away
|
|
|
|
is likely to fail again. */
|
|
|
|
checkInterrupt();
|
|
|
|
struct timespec t;
|
|
|
|
t.tv_sec = 0;
|
|
|
|
t.tv_nsec = (random() % 100) * 1000 * 1000; /* <= 0.1s */
|
|
|
|
nanosleep(&t, 0);
|
|
|
|
}
|
|
|
|
|
2016-03-30 11:27:25 +00:00
|
|
|
}
|