2016-03-30 11:27:25 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
|
2020-05-11 21:52:15 +00:00
|
|
|
#include "error.hh"
|
2016-03-30 11:27:25 +00:00
|
|
|
|
2019-09-24 15:28:18 +00:00
|
|
|
struct sqlite3;
|
|
|
|
struct sqlite3_stmt;
|
2016-03-30 11:27:25 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
/* RAII wrapper to close a SQLite database automatically. */
|
|
|
|
struct SQLite
|
|
|
|
{
|
2016-08-09 12:27:30 +00:00
|
|
|
sqlite3 * db = 0;
|
|
|
|
SQLite() { }
|
2019-06-07 20:25:48 +00:00
|
|
|
SQLite(const Path & path, bool create = true);
|
2016-08-09 12:27:30 +00: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 11:27:25 +00:00
|
|
|
~SQLite();
|
|
|
|
operator sqlite3 * () { return db; }
|
2016-08-09 12:27:30 +00:00
|
|
|
|
2019-06-07 20:25:48 +00:00
|
|
|
/* Disable synchronous mode, set truncate journal mode. */
|
|
|
|
void isCache();
|
|
|
|
|
2016-08-09 12:27:30 +00:00
|
|
|
void exec(const std::string & stmt);
|
2020-04-17 21:04:21 +00:00
|
|
|
|
|
|
|
uint64_t getLastInsertedRowId();
|
2016-03-30 11:27:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* RAII wrapper to create and destroy SQLite prepared statements. */
|
|
|
|
struct SQLiteStmt
|
|
|
|
{
|
2016-03-30 13:50:45 +00:00
|
|
|
sqlite3 * db = 0;
|
|
|
|
sqlite3_stmt * stmt = 0;
|
2017-02-28 12:59:11 +00:00
|
|
|
std::string sql;
|
2016-03-30 13:50:45 +00:00
|
|
|
SQLiteStmt() { }
|
2017-02-28 12:59:11 +00:00
|
|
|
SQLiteStmt(sqlite3 * db, const std::string & sql) { create(db, sql); }
|
2016-03-30 11:27:25 +00:00
|
|
|
void create(sqlite3 * db, const std::string & s);
|
|
|
|
~SQLiteStmt();
|
|
|
|
operator sqlite3_stmt * () { return stmt; }
|
|
|
|
|
2016-03-30 13:50:45 +00:00
|
|
|
/* Helper for binding / executing statements. */
|
|
|
|
class Use
|
|
|
|
{
|
|
|
|
friend struct SQLiteStmt;
|
|
|
|
private:
|
|
|
|
SQLiteStmt & stmt;
|
|
|
|
unsigned int curArg = 1;
|
|
|
|
Use(SQLiteStmt & stmt);
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2016-04-05 13:29:56 +00:00
|
|
|
~Use();
|
|
|
|
|
2016-03-30 13:50:45 +00:00
|
|
|
/* Bind the next parameter. */
|
2020-04-16 23:00:56 +00:00
|
|
|
Use & operator () (std::string_view value, bool notNull = true);
|
2019-06-07 20:25:48 +00:00
|
|
|
Use & operator () (const unsigned char * data, size_t len, bool notNull = true);
|
2016-03-30 13:50:45 +00:00
|
|
|
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);
|
2016-04-20 12:12:38 +00:00
|
|
|
bool isNull(int col);
|
2016-03-30 13:50:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Use use()
|
|
|
|
{
|
|
|
|
return Use(*this);
|
|
|
|
}
|
2016-03-30 11:27:25 +00: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);
|
|
|
|
|
2018-03-14 18:01:22 +00:00
|
|
|
[[noreturn]] void throwSQLiteError(sqlite3 * db, const FormatOrString & fs);
|
2016-03-30 11:27:25 +00:00
|
|
|
|
2017-02-28 12:59:11 +00:00
|
|
|
void handleSQLiteBusy(const SQLiteBusy & e);
|
|
|
|
|
2016-03-30 11:27:25 +00:00
|
|
|
/* Convenience function for retrying a SQLite transaction when the
|
|
|
|
database is busy. */
|
2019-12-17 16:17:53 +00:00
|
|
|
template<typename T, typename F>
|
|
|
|
T retrySQLite(F && fun)
|
2016-03-30 11:27:25 +00:00
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
return fun();
|
|
|
|
} catch (SQLiteBusy & e) {
|
2017-02-28 12:59:11 +00:00
|
|
|
handleSQLiteBusy(e);
|
2016-03-30 11:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|