Nix 2.26.3
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 <functional>
5#include <string>
6
7#include "error.hh"
8
9struct sqlite3;
10struct sqlite3_stmt;
11
12namespace nix {
13
34
38struct SQLite
39{
40 sqlite3 * db = 0;
41 SQLite() { }
42 SQLite(const Path & path, SQLiteOpenMode mode = SQLiteOpenMode::Normal);
43 SQLite(const SQLite & from) = delete;
44 SQLite& operator = (const SQLite & from) = delete;
45 // NOTE: This is noexcept since we are only copying and assigning raw pointers.
46 SQLite& operator = (SQLite && from) noexcept { db = from.db; from.db = 0; return *this; }
47 ~SQLite();
48 operator sqlite3 * () { return db; }
49
53 void isCache();
54
55 void exec(const std::string & stmt);
56
57 uint64_t getLastInsertedRowId();
58};
59
63struct SQLiteStmt
64{
65 sqlite3 * db = 0;
66 sqlite3_stmt * stmt = 0;
67 std::string sql;
68 SQLiteStmt() { }
69 SQLiteStmt(sqlite3 * db, const std::string & sql) { create(db, sql); }
70 void create(sqlite3 * db, const std::string & s);
71 ~SQLiteStmt();
72 operator sqlite3_stmt * () { return stmt; }
73
77 class Use
78 {
79 friend struct SQLiteStmt;
80 private:
81 SQLiteStmt & stmt;
82 unsigned int curArg = 1;
83 Use(SQLiteStmt & stmt);
84
85 public:
86
87 ~Use();
88
92 Use & operator () (std::string_view value, bool notNull = true);
93 Use & operator () (const unsigned char * data, size_t len, bool notNull = true);
94 Use & operator () (int64_t value, bool notNull = true);
95 Use & bind(); // null
96
97 int step();
98
102 void exec();
103
108 bool next();
109
110 std::string getStr(int col);
111 int64_t getInt(int col);
112 bool isNull(int col);
113 };
114
115 Use use()
116 {
117 return Use(*this);
118 }
119};
120
125struct SQLiteTxn
126{
127 bool active = false;
128 sqlite3 * db;
129
130 SQLiteTxn(sqlite3 * db);
131
132 void commit();
133
134 ~SQLiteTxn();
135};
136
137
138struct SQLiteError : Error
139{
140 std::string path;
141 std::string errMsg;
142 int errNo, extendedErrNo, offset;
143
144 template<typename... Args>
145 [[noreturn]] static void throw_(sqlite3 * db, const std::string & fs, const Args & ... args) {
146 throw_(db, HintFmt(fs, args...));
147 }
148
149 SQLiteError(const char *path, const char *errMsg, int errNo, int extendedErrNo, int offset, HintFmt && hf);
150
151protected:
152
153 template<typename... Args>
154 SQLiteError(const char *path, const char *errMsg, int errNo, int extendedErrNo, int offset, const std::string & fs, const Args & ... args)
155 : SQLiteError(path, errMsg, errNo, extendedErrNo, offset, HintFmt(fs, args...))
156 { }
157
158 [[noreturn]] static void throw_(sqlite3 * db, HintFmt && hf);
159
160};
161
162MakeError(SQLiteBusy, SQLiteError);
163
164void handleSQLiteBusy(const SQLiteBusy & e, time_t & nextWarning);
165
170template<typename T, typename F>
172{
173 time_t nextWarning = time(0) + 1;
174
175 while (true) {
176 try {
177 return fun();
178 } catch (SQLiteBusy & e) {
179 handleSQLiteBusy(e, nextWarning);
180 }
181 }
182}
183
184}
Definition args.hh:28
Definition fmt.hh:136
Definition sqlite.hh:78
bool next()
Definition sqlite.cc:196
void exec()
Definition sqlite.cc:188
Use & operator()(std::string_view value, bool notNull=true)
Definition sqlite.cc:146
This file defines two main structs/classes used in nix error handling.
return s
Definition lexer.l:459
std::function< void(Sink &)> fun
Definition lexer.l:3485
std::variant< std::string, std::string_view > data
Definition lexer.l:177
std::string std::string_view from
Definition lexer.l:2591
const T & value
Definition lexer.l:492
std::vector< Expr * > args
Definition lexer.l:6126
SQLiteOpenMode
Definition sqlite.hh:14
@ Immutable
Definition sqlite.hh:32
@ NoCreate
Definition sqlite.hh:24
@ Normal
Definition sqlite.hh:19
T retrySQLite(F &&fun)
Definition sqlite.hh:171
Definition sqlite.hh:139
void isCache()
Definition sqlite.cc:93
std::string Path
Definition types.hh:22