Nix
2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Toggle main menu visibility
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
10
struct
sqlite3;
11
struct
sqlite3_stmt;
12
13
namespace
nix {
14
15
enum class
SQLiteOpenMode
{
20
Normal
,
25
NoCreate
,
33
Immutable
,
34
};
35
36
struct
SQLiteSettings
37
{
38
SQLiteOpenMode
mode =
SQLiteOpenMode::Normal
;
39
bool
useWAL;
40
};
41
45
struct
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
85
struct
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
157
struct
SQLiteTxn
158
{
159
bool
active =
false
;
160
sqlite3 * db;
161
162
SQLiteTxn(sqlite3 * db);
163
164
void
commit();
165
166
~SQLiteTxn();
167
};
168
169
class
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
177
public
:
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
186
protected
:
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
204
MakeError(SQLiteBusy,
SQLiteError
);
205
206
void
handleSQLiteBusy(
const
SQLiteBusy & e, time_t & nextWarning);
207
212
template
<
typename
T,
typename
F>
213
T
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
nix::Args
Definition
args.hh:29
nix::HintFmt
Definition
fmt.hh:169
nix::HintFmt::HintFmt
HintFmt(const std::string &literal)
Definition
fmt.hh:178
nix::SQLiteError
Definition
sqlite.hh:170
nix::SQLiteStmt::Use
Definition
sqlite.hh:110
nix::SQLiteStmt::Use::next
bool next()
Definition
sqlite.cc:235
nix::SQLiteStmt::Use::exec
void exec()
Definition
sqlite.cc:227
nix::SQLiteStmt::Use::apply
Use & apply(std::string_view value, bool notNull=true)
Definition
sqlite.cc:185
nix::fun
Definition
fun.hh:19
error.hh
This file defines two main structs/classes used in nix error handling.
nix::retrySQLite
T retrySQLite(const F &fun)
Definition
sqlite.hh:213
nix::SQLiteOpenMode
SQLiteOpenMode
Definition
sqlite.hh:15
nix::SQLiteOpenMode::Immutable
@ Immutable
Definition
sqlite.hh:33
nix::SQLiteOpenMode::NoCreate
@ NoCreate
Definition
sqlite.hh:25
nix::SQLiteOpenMode::Normal
@ Normal
Definition
sqlite.hh:20
nix::SQLiteSettings
Definition
sqlite.hh:37
nix::SQLite::isCache
void isCache()
Definition
sqlite.cc:132
libstore
include
nix
store
sqlite.hh
Generated by
1.18.0