2016-03-30 14:27:25 +03:00
|
|
|
#pragma once
|
2023-04-01 06:18:41 +03:00
|
|
|
///@file
|
2016-03-30 14:27:25 +03:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
|
2020-05-12 00:52:15 +03:00
|
|
|
#include "error.hh"
|
2016-03-30 14:27:25 +03:00
|
|
|
|
2019-09-24 18:28:18 +03:00
|
|
|
struct sqlite3;
|
|
|
|
struct sqlite3_stmt;
|
2016-03-30 14:27:25 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2023-06-20 12:34:09 +03:00
|
|
|
enum class SQLiteOpenMode {
|
|
|
|
/**
|
|
|
|
* Open the database in read-write mode.
|
|
|
|
* If the database does not exist, it will be created.
|
|
|
|
*/
|
|
|
|
Normal,
|
|
|
|
/**
|
|
|
|
* Open the database in read-write mode.
|
|
|
|
* Fails with an error if the database does not exist.
|
|
|
|
*/
|
|
|
|
NoCreate,
|
|
|
|
/**
|
|
|
|
* Open the database in immutable mode.
|
|
|
|
* In addition to the database being read-only,
|
|
|
|
* no wal or journal files will be created by sqlite.
|
|
|
|
* Use this mode if the database is on a read-only filesystem.
|
|
|
|
* Fails with an error if the database does not exist.
|
|
|
|
*/
|
|
|
|
Immutable,
|
|
|
|
};
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* RAII wrapper to close a SQLite database automatically.
|
|
|
|
*/
|
2016-03-30 14:27:25 +03:00
|
|
|
struct SQLite
|
|
|
|
{
|
2016-08-09 15:27:30 +03:00
|
|
|
sqlite3 * db = 0;
|
|
|
|
SQLite() { }
|
2023-06-20 12:34:09 +03:00
|
|
|
SQLite(const Path & path, SQLiteOpenMode mode = SQLiteOpenMode::Normal);
|
2016-08-09 15:27:30 +03:00
|
|
|
SQLite(const SQLite & from) = delete;
|
|
|
|
SQLite& operator = (const SQLite & from) = delete;
|
|
|
|
SQLite& operator = (SQLite && from) { db = from.db; from.db = 0; return *this; }
|
2016-03-30 14:27:25 +03:00
|
|
|
~SQLite();
|
|
|
|
operator sqlite3 * () { return db; }
|
2016-08-09 15:27:30 +03:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Disable synchronous mode, set truncate journal mode.
|
|
|
|
*/
|
2019-06-07 23:25:48 +03:00
|
|
|
void isCache();
|
|
|
|
|
2016-08-09 15:27:30 +03:00
|
|
|
void exec(const std::string & stmt);
|
2020-04-18 00:04:21 +03:00
|
|
|
|
|
|
|
uint64_t getLastInsertedRowId();
|
2016-03-30 14:27:25 +03:00
|
|
|
};
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* RAII wrapper to create and destroy SQLite prepared statements.
|
|
|
|
*/
|
2016-03-30 14:27:25 +03:00
|
|
|
struct SQLiteStmt
|
|
|
|
{
|
2016-03-30 16:50:45 +03:00
|
|
|
sqlite3 * db = 0;
|
|
|
|
sqlite3_stmt * stmt = 0;
|
2017-02-28 14:59:11 +02:00
|
|
|
std::string sql;
|
2016-03-30 16:50:45 +03:00
|
|
|
SQLiteStmt() { }
|
2017-02-28 14:59:11 +02:00
|
|
|
SQLiteStmt(sqlite3 * db, const std::string & sql) { create(db, sql); }
|
2016-03-30 14:27:25 +03:00
|
|
|
void create(sqlite3 * db, const std::string & s);
|
|
|
|
~SQLiteStmt();
|
|
|
|
operator sqlite3_stmt * () { return stmt; }
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Helper for binding / executing statements.
|
|
|
|
*/
|
2016-03-30 16:50:45 +03:00
|
|
|
class Use
|
|
|
|
{
|
|
|
|
friend struct SQLiteStmt;
|
|
|
|
private:
|
|
|
|
SQLiteStmt & stmt;
|
|
|
|
unsigned int curArg = 1;
|
|
|
|
Use(SQLiteStmt & stmt);
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2016-04-05 16:29:56 +03:00
|
|
|
~Use();
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Bind the next parameter.
|
|
|
|
*/
|
2020-04-17 02:00:56 +03:00
|
|
|
Use & operator () (std::string_view value, bool notNull = true);
|
2019-06-07 23:25:48 +03:00
|
|
|
Use & operator () (const unsigned char * data, size_t len, bool notNull = true);
|
2016-03-30 16:50:45 +03:00
|
|
|
Use & operator () (int64_t value, bool notNull = true);
|
|
|
|
Use & bind(); // null
|
|
|
|
|
|
|
|
int step();
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Execute a statement that does not return rows.
|
|
|
|
*/
|
2016-03-30 16:50:45 +03:00
|
|
|
void exec();
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* For statements that return 0 or more rows. Returns true iff
|
|
|
|
* a row is available.
|
|
|
|
*/
|
2016-03-30 16:50:45 +03:00
|
|
|
bool next();
|
|
|
|
|
|
|
|
std::string getStr(int col);
|
|
|
|
int64_t getInt(int col);
|
2016-04-20 15:12:38 +03:00
|
|
|
bool isNull(int col);
|
2016-03-30 16:50:45 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Use use()
|
|
|
|
{
|
|
|
|
return Use(*this);
|
|
|
|
}
|
2016-03-30 14:27:25 +03:00
|
|
|
};
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* RAII helper that ensures transactions are aborted unless explicitly
|
|
|
|
* committed.
|
|
|
|
*/
|
2016-03-30 14:27:25 +03:00
|
|
|
struct SQLiteTxn
|
|
|
|
{
|
|
|
|
bool active = false;
|
|
|
|
sqlite3 * db;
|
|
|
|
|
|
|
|
SQLiteTxn(sqlite3 * db);
|
|
|
|
|
|
|
|
void commit();
|
|
|
|
|
|
|
|
~SQLiteTxn();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-06-18 20:54:16 +03:00
|
|
|
struct SQLiteError : Error
|
|
|
|
{
|
Improve sqlite error messages
They did not include the detailed error message, losing essential
information for troubleshooting.
Example message:
warning: creating statement 'insert or rplace into NARs(cache, hashPart, namePart, url, compression, fileHash, fileSize, narHash, narSize, refs, deriver, sigs, ca, timestamp, present) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)': at offset 10: SQL logic error, near "rplace": syntax error (in '/tmp/nix-shell.grQ6f7/nix-test/tests/binary-cache/test-home/.cache/nix/binary-cache-v6.sqlite')
It's not the best example; more important information will be in
the message for e.g. a constraint violation.
I don't see why this specific error is printed as a warning, but
that's for another commit.
2022-12-17 15:50:43 +02:00
|
|
|
std::string path;
|
|
|
|
std::string errMsg;
|
|
|
|
int errNo, extendedErrNo, offset;
|
2020-06-18 20:54:16 +03:00
|
|
|
|
|
|
|
template<typename... Args>
|
2022-04-20 20:37:59 +03:00
|
|
|
[[noreturn]] static void throw_(sqlite3 * db, const std::string & fs, const Args & ... args) {
|
|
|
|
throw_(db, hintfmt(fs, args...));
|
|
|
|
}
|
2016-03-30 14:27:25 +03:00
|
|
|
|
2024-02-04 05:16:30 +02:00
|
|
|
SQLiteError(const char *path, const char *errMsg, int errNo, int extendedErrNo, int offset, hintfmt && hf);
|
2022-04-20 20:37:59 +03:00
|
|
|
|
2022-04-21 10:40:55 +03:00
|
|
|
protected:
|
|
|
|
|
2020-06-18 20:54:16 +03:00
|
|
|
template<typename... Args>
|
Improve sqlite error messages
They did not include the detailed error message, losing essential
information for troubleshooting.
Example message:
warning: creating statement 'insert or rplace into NARs(cache, hashPart, namePart, url, compression, fileHash, fileSize, narHash, narSize, refs, deriver, sigs, ca, timestamp, present) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)': at offset 10: SQL logic error, near "rplace": syntax error (in '/tmp/nix-shell.grQ6f7/nix-test/tests/binary-cache/test-home/.cache/nix/binary-cache-v6.sqlite')
It's not the best example; more important information will be in
the message for e.g. a constraint violation.
I don't see why this specific error is printed as a warning, but
that's for another commit.
2022-12-17 15:50:43 +02:00
|
|
|
SQLiteError(const char *path, const char *errMsg, int errNo, int extendedErrNo, int offset, const std::string & fs, const Args & ... args)
|
2024-02-04 05:16:30 +02:00
|
|
|
: SQLiteError(path, errMsg, errNo, extendedErrNo, offset, hintfmt(fs, args...))
|
2022-04-20 20:37:59 +03:00
|
|
|
{ }
|
|
|
|
|
2024-02-04 05:16:30 +02:00
|
|
|
[[noreturn]] static void throw_(sqlite3 * db, hintfmt && hf);
|
2022-04-20 20:37:59 +03:00
|
|
|
|
2020-06-18 20:54:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
MakeError(SQLiteBusy, SQLiteError);
|
2016-03-30 14:27:25 +03:00
|
|
|
|
2023-04-11 18:03:37 +03:00
|
|
|
void handleSQLiteBusy(const SQLiteBusy & e, time_t & nextWarning);
|
2017-02-28 14:59:11 +02:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Convenience function for retrying a SQLite transaction when the
|
|
|
|
* database is busy.
|
|
|
|
*/
|
2019-12-17 18:17:53 +02:00
|
|
|
template<typename T, typename F>
|
|
|
|
T retrySQLite(F && fun)
|
2016-03-30 14:27:25 +03:00
|
|
|
{
|
2023-04-11 12:22:07 +03:00
|
|
|
time_t nextWarning = time(0) + 1;
|
|
|
|
|
2016-03-30 14:27:25 +03:00
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
return fun();
|
|
|
|
} catch (SQLiteBusy & e) {
|
2023-04-11 18:03:37 +03:00
|
|
|
handleSQLiteBusy(e, nextWarning);
|
2016-03-30 14:27:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|