Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
sqlite.hh
Go to the documentation of this file.
1#pragma once
3
4#include <filesystem>
5#include <functional>
6#include <string>
7
8#include "nix/util/error.hh"
9
10struct sqlite3;
11struct sqlite3_stmt;
12
13namespace nix {
14
35
37{
39 bool useWAL;
40};
41
45struct SQLite
46{
47 sqlite3 * db = 0;
48
49 SQLite() {}
50
51 using Settings = SQLiteSettings;
52
53 SQLite(const std::filesystem::path & path, Settings && settings);
54 SQLite(const SQLite & from) = delete;
55 SQLite & operator=(const SQLite & from) = delete;
56
57 // NOTE: This is noexcept since we are only copying and assigning raw pointers.
58 SQLite & operator=(SQLite && from) noexcept
59 {
60 db = from.db;
61 from.db = 0;
62 return *this;
63 }
64
65 ~SQLite();
66
67 operator sqlite3 *()
68 {
69 return db;
70 }
71
75 void isCache();
76
77 void exec(const std::string & stmt);
78
79 uint64_t getLastInsertedRowId();
80};
81
85struct SQLiteStmt
86{
87 sqlite3 * db = 0;
88 sqlite3_stmt * stmt = 0;
89 std::string sql;
90
91 SQLiteStmt() {}
92
93 SQLiteStmt(sqlite3 * db, const std::string & sql)
94 {
95 create(db, sql);
96 }
97
98 void create(sqlite3 * db, const std::string & s);
99 ~SQLiteStmt();
100
101 operator sqlite3_stmt *()
102 {
103 return stmt;
104 }
105
109 class Use
110 {
111 friend struct SQLiteStmt;
112 private:
113 SQLiteStmt & stmt;
114 unsigned int curArg = 1;
115 Use(SQLiteStmt & stmt);
116
117 public:
118
119 ~Use();
120
124 Use & apply(std::string_view value, bool notNull = true);
125 Use & apply(const unsigned char * data, size_t len, bool notNull = true);
126 Use & apply(int64_t value, bool notNull = true);
127 Use & bind(); // null
128
129 int step();
130
134 void exec();
135
140 bool next();
141
142 std::string getStr(int col);
143 int64_t getInt(int col);
144 bool isNull(int col);
145 };
146
147 Use use()
148 {
149 return Use(*this);
150 }
151};
152
157struct SQLiteTxn
158{
159 bool active = false;
160 sqlite3 * db;
161
162 SQLiteTxn(sqlite3 * db);
163
164 void commit();
165
166 ~SQLiteTxn();
167};
168
169class SQLiteError : public CloneableError<SQLiteError, Error>
170{
171 std::string path;
172 std::string errMsg;
173 int errNo, extendedErrNo, offset;
174
175 void anchor() override;
176
177public:
178 template<typename... Args>
179 [[noreturn]] static void throw_(sqlite3 * db, const std::string & fs, Args &&... args)
180 {
181 throw_(db, HintFmt(fs, std::forward<Args>(args)...));
182 }
183
184 SQLiteError(const char * path, const char * errMsg, int errNo, int extendedErrNo, int offset, HintFmt && hf);
185
186protected:
187
188 template<typename... Args>
189 SQLiteError(
190 const char * path,
191 const char * errMsg,
192 int errNo,
193 int extendedErrNo,
194 int offset,
195 const std::string & fs,
196 Args &&... args)
197 : SQLiteError(path, errMsg, errNo, extendedErrNo, offset, HintFmt(fs, std::forward<Args>(args)...))
198 {
199 }
200
201 [[noreturn]] static void throw_(sqlite3 * db, HintFmt && hf);
202};
203
204MakeError(SQLiteBusy, SQLiteError);
205
206void handleSQLiteBusy(const SQLiteBusy & e, time_t & nextWarning);
207
212template<typename T, typename F>
213T retrySQLite(const F & fun)
214{
215 time_t nextWarning = time(nullptr) + 1;
216
217 while (true) {
218 try {
219 return fun();
220 } catch (SQLiteBusy & e) {
221 handleSQLiteBusy(e, nextWarning);
222 }
223 }
224}
225
226} // namespace nix
Definition args.hh:29
Definition fmt.hh:169
HintFmt(const std::string &literal)
Definition fmt.hh:178
Definition sqlite.hh:170
Definition sqlite.hh:110
bool next()
Definition sqlite.cc:235
void exec()
Definition sqlite.cc:227
Use & apply(std::string_view value, bool notNull=true)
Definition sqlite.cc:185
Definition fun.hh:19
This file defines two main structs/classes used in nix error handling.
T retrySQLite(const F &fun)
Definition sqlite.hh:213
SQLiteOpenMode
Definition sqlite.hh:15
@ Immutable
Definition sqlite.hh:33
@ NoCreate
Definition sqlite.hh:25
@ Normal
Definition sqlite.hh:20
Definition sqlite.hh:37
void isCache()
Definition sqlite.cc:132