projects/guix·blob kernel-updates · refs

169 lines · 3.9 KiB

1#include "sqlite.hh"
2#include "util.hh"
3
4#include <format>
5#include <cassert>
6
7#include <sqlite3.h>
8
9namespace nix {
10
11[[noreturn]] void throwSQLiteError(sqlite3 * db, std::string_view f)
12{
13 int err = sqlite3_errcode(db);
14 if (err == SQLITE_BUSY || err == SQLITE_PROTOCOL) {
15 if (err == SQLITE_PROTOCOL)
16 printMsg(lvlError, "warning: SQLite database is busy (SQLITE_PROTOCOL)");
17 else {
18 static bool warned = false;
19 if (!warned) {
20 printMsg(lvlError, "warning: SQLite database is busy");
21 warned = true;
22 }
23 }
24 /* Sleep for a while since retrying the transaction right away
25 is likely to fail again. */
26#if HAVE_NANOSLEEP
27 struct timespec t;
28 t.tv_sec = 0;
29 t.tv_nsec = (random() % 100) * 1000 * 1000; /* <= 0.1s */
30 nanosleep(&t, 0);
31#else
32 sleep(1);
33#endif
34 throw SQLiteBusy(std::format("{}: {}", f, sqlite3_errmsg(db)));
35 }
36 else
37 throw SQLiteError(std::format("{}: {}", f, sqlite3_errmsg(db)));
38}
39
40SQLite::~SQLite()
41{
42 try {
43 if (db && sqlite3_close(db) != SQLITE_OK)
44 throwSQLiteError(db, "closing database");
45 } catch (...) {
46 ignoreException();
47 }
48}
49
50void SQLiteStmt::create(sqlite3 * db, const string & s)
51{
52 checkInterrupt();
53 assert(!stmt);
54 if (sqlite3_prepare_v2(db, s.c_str(), -1, &stmt, 0) != SQLITE_OK)
55 throwSQLiteError(db, "creating statement");
56 this->db = db;
57}
58
59SQLiteStmt::~SQLiteStmt()
60{
61 try {
62 if (stmt && sqlite3_finalize(stmt) != SQLITE_OK)
63 throwSQLiteError(db, "finalizing statement");
64 } catch (...) {
65 ignoreException();
66 }
67}
68
69SQLiteStmt::Use::Use(SQLiteStmt & stmt)
70 : stmt(stmt)
71{
72 assert(stmt.stmt);
73 /* Note: sqlite3_reset() returns the error code for the most
74 recent call to sqlite3_step(). So ignore it. */
75 sqlite3_reset(stmt);
76}
77
78SQLiteStmt::Use::~Use()
79{
80 sqlite3_reset(stmt);
81}
82
83SQLiteStmt::Use & SQLiteStmt::Use::operator () (const std::string & value, bool notNull)
84{
85 if (notNull) {
86 if (sqlite3_bind_text(stmt, curArg++, value.c_str(), -1, SQLITE_TRANSIENT) != SQLITE_OK)
87 throwSQLiteError(stmt.db, "binding argument");
88 } else
89 bind();
90 return *this;
91}
92
93SQLiteStmt::Use & SQLiteStmt::Use::operator () (int64_t value, bool notNull)
94{
95 if (notNull) {
96 if (sqlite3_bind_int64(stmt, curArg++, value) != SQLITE_OK)
97 throwSQLiteError(stmt.db, "binding argument");
98 } else
99 bind();
100 return *this;
101}
102
103SQLiteStmt::Use & SQLiteStmt::Use::bind()
104{
105 if (sqlite3_bind_null(stmt, curArg++) != SQLITE_OK)
106 throwSQLiteError(stmt.db, "binding argument");
107 return *this;
108}
109
110int SQLiteStmt::Use::step()
111{
112 return sqlite3_step(stmt);
113}
114
115void SQLiteStmt::Use::exec()
116{
117 int r = step();
118 assert(r != SQLITE_ROW);
119 if (r != SQLITE_DONE)
120 throwSQLiteError(stmt.db, "executing SQLite statement");
121}
122
123bool SQLiteStmt::Use::next()
124{
125 int r = step();
126 if (r != SQLITE_DONE && r != SQLITE_ROW)
127 throwSQLiteError(stmt.db, "executing SQLite query");
128 return r == SQLITE_ROW;
129}
130
131std::string SQLiteStmt::Use::getStr(int col)
132{
133 auto s = (const char *) sqlite3_column_text(stmt, col);
134 assert(s);
135 return s;
136}
137
138int64_t SQLiteStmt::Use::getInt(int col)
139{
140 // FIXME: detect nulls?
141 return sqlite3_column_int64(stmt, col);
142}
143
144SQLiteTxn::SQLiteTxn(sqlite3 * db)
145{
146 this->db = db;
147 if (sqlite3_exec(db, "begin;", 0, 0, 0) != SQLITE_OK)
148 throwSQLiteError(db, "starting transaction");
149 active = true;
150}
151
152void SQLiteTxn::commit()
153{
154 if (sqlite3_exec(db, "commit;", 0, 0, 0) != SQLITE_OK)
155 throwSQLiteError(db, "committing transaction");
156 active = false;
157}
158
159SQLiteTxn::~SQLiteTxn()
160{
161 try {
162 if (active && sqlite3_exec(db, "rollback;", 0, 0, 0) != SQLITE_OK)
163 throwSQLiteError(db, "aborting transaction");
164 } catch (...) {
165 ignoreException();
166 }
167}
168
169}