Bitcoin Core 22.99.0
P2P Digital Currency
sqlite.cpp
Go to the documentation of this file.
1// Copyright (c) 2020 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <wallet/sqlite.h>
6
7#include <chainparams.h>
8#include <crypto/common.h>
9#include <logging.h>
10#include <sync.h>
11#include <util/strencodings.h>
12#include <util/system.h>
13#include <util/translation.h>
14#include <wallet/db.h>
15
16#include <sqlite3.h>
17#include <stdint.h>
18
19#include <optional>
20#include <utility>
21#include <vector>
22
23static constexpr int32_t WALLET_SCHEMA_VERSION = 0;
24
26static int g_sqlite_count GUARDED_BY(g_sqlite_mutex) = 0;
27
28static void ErrorLogCallback(void* arg, int code, const char* msg)
29{
30 // From sqlite3_config() documentation for the SQLITE_CONFIG_LOG option:
31 // "The void pointer that is the second argument to SQLITE_CONFIG_LOG is passed through as
32 // the first parameter to the application-defined logger function whenever that function is
33 // invoked."
34 // Assert that this is the case:
35 assert(arg == nullptr);
36 LogPrintf("SQLite Error. Code: %d. Message: %s\n", code, msg);
37}
38
39static std::optional<int> ReadPragmaInteger(sqlite3* db, const std::string& key, const std::string& description, bilingual_str& error)
40{
41 std::string stmt_text = strprintf("PRAGMA %s", key);
42 sqlite3_stmt* pragma_read_stmt{nullptr};
43 int ret = sqlite3_prepare_v2(db, stmt_text.c_str(), -1, &pragma_read_stmt, nullptr);
44 if (ret != SQLITE_OK) {
45 sqlite3_finalize(pragma_read_stmt);
46 error = Untranslated(strprintf("SQLiteDatabase: Failed to prepare the statement to fetch %s: %s", description, sqlite3_errstr(ret)));
47 return std::nullopt;
48 }
49 ret = sqlite3_step(pragma_read_stmt);
50 if (ret != SQLITE_ROW) {
51 sqlite3_finalize(pragma_read_stmt);
52 error = Untranslated(strprintf("SQLiteDatabase: Failed to fetch %s: %s", description, sqlite3_errstr(ret)));
53 return std::nullopt;
54 }
55 int result = sqlite3_column_int(pragma_read_stmt, 0);
56 sqlite3_finalize(pragma_read_stmt);
57 return result;
58}
59
60static void SetPragma(sqlite3* db, const std::string& key, const std::string& value, const std::string& err_msg)
61{
62 std::string stmt_text = strprintf("PRAGMA %s = %s", key, value);
63 int ret = sqlite3_exec(db, stmt_text.c_str(), nullptr, nullptr, nullptr);
64 if (ret != SQLITE_OK) {
65 throw std::runtime_error(strprintf("SQLiteDatabase: %s: %s\n", err_msg, sqlite3_errstr(ret)));
66 }
67}
68
69SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, bool mock)
70 : WalletDatabase(), m_mock(mock), m_dir_path(fs::PathToString(dir_path)), m_file_path(fs::PathToString(file_path))
71{
72 {
74 LogPrintf("Using SQLite Version %s\n", SQLiteDatabaseVersion());
75 LogPrintf("Using wallet %s\n", m_dir_path);
76
77 if (++g_sqlite_count == 1) {
78 // Setup logging
79 int ret = sqlite3_config(SQLITE_CONFIG_LOG, ErrorLogCallback, nullptr);
80 if (ret != SQLITE_OK) {
81 throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup error log: %s\n", sqlite3_errstr(ret)));
82 }
83 // Force serialized threading mode
84 ret = sqlite3_config(SQLITE_CONFIG_SERIALIZED);
85 if (ret != SQLITE_OK) {
86 throw std::runtime_error(strprintf("SQLiteDatabase: Failed to configure serialized threading mode: %s\n", sqlite3_errstr(ret)));
87 }
88 }
89 int ret = sqlite3_initialize(); // This is a no-op if sqlite3 is already initialized
90 if (ret != SQLITE_OK) {
91 throw std::runtime_error(strprintf("SQLiteDatabase: Failed to initialize SQLite: %s\n", sqlite3_errstr(ret)));
92 }
93 }
94
95 try {
96 Open();
97 } catch (const std::runtime_error&) {
98 // If open fails, cleanup this object and rethrow the exception
99 Cleanup();
100 throw;
101 }
102}
103
105{
106 const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{
107 {&m_read_stmt, "SELECT value FROM main WHERE key = ?"},
108 {&m_insert_stmt, "INSERT INTO main VALUES(?, ?)"},
109 {&m_overwrite_stmt, "INSERT or REPLACE into main values(?, ?)"},
110 {&m_delete_stmt, "DELETE FROM main WHERE key = ?"},
111 {&m_cursor_stmt, "SELECT key, value FROM main"},
112 };
113
114 for (const auto& [stmt_prepared, stmt_text] : statements) {
115 if (*stmt_prepared == nullptr) {
116 int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, stmt_prepared, nullptr);
117 if (res != SQLITE_OK) {
118 throw std::runtime_error(strprintf(
119 "SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
120 }
121 }
122 }
123}
124
126{
127 Cleanup();
128}
129
131{
132 Close();
133
135 if (--g_sqlite_count == 0) {
136 int ret = sqlite3_shutdown();
137 if (ret != SQLITE_OK) {
138 LogPrintf("SQLiteDatabase: Failed to shutdown SQLite: %s\n", sqlite3_errstr(ret));
139 }
140 }
141}
142
144{
145 assert(m_db);
146
147 // Check the application ID matches our network magic
148 auto read_result = ReadPragmaInteger(m_db, "application_id", "the application id", error);
149 if (!read_result.has_value()) return false;
150 uint32_t app_id = static_cast<uint32_t>(read_result.value());
151 uint32_t net_magic = ReadBE32(Params().MessageStart());
152 if (app_id != net_magic) {
153 error = strprintf(_("SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id);
154 return false;
155 }
156
157 // Check our schema version
158 read_result = ReadPragmaInteger(m_db, "user_version", "sqlite wallet schema version", error);
159 if (!read_result.has_value()) return false;
160 int32_t user_ver = read_result.value();
161 if (user_ver != WALLET_SCHEMA_VERSION) {
162 error = strprintf(_("SQLiteDatabase: Unknown sqlite wallet schema version %d. Only version %d is supported"), user_ver, WALLET_SCHEMA_VERSION);
163 return false;
164 }
165
166 sqlite3_stmt* stmt{nullptr};
167 int ret = sqlite3_prepare_v2(m_db, "PRAGMA integrity_check", -1, &stmt, nullptr);
168 if (ret != SQLITE_OK) {
169 sqlite3_finalize(stmt);
170 error = strprintf(_("SQLiteDatabase: Failed to prepare statement to verify database: %s"), sqlite3_errstr(ret));
171 return false;
172 }
173 while (true) {
174 ret = sqlite3_step(stmt);
175 if (ret == SQLITE_DONE) {
176 break;
177 }
178 if (ret != SQLITE_ROW) {
179 error = strprintf(_("SQLiteDatabase: Failed to execute statement to verify database: %s"), sqlite3_errstr(ret));
180 break;
181 }
182 const char* msg = (const char*)sqlite3_column_text(stmt, 0);
183 if (!msg) {
184 error = strprintf(_("SQLiteDatabase: Failed to read database verification error: %s"), sqlite3_errstr(ret));
185 break;
186 }
187 std::string str_msg(msg);
188 if (str_msg == "ok") {
189 continue;
190 }
191 if (error.empty()) {
192 error = _("Failed to verify database") + Untranslated("\n");
193 }
194 error += Untranslated(strprintf("%s\n", str_msg));
195 }
196 sqlite3_finalize(stmt);
197 return error.empty();
198}
199
201{
202 int flags = SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
203 if (m_mock) {
204 flags |= SQLITE_OPEN_MEMORY; // In memory database for mock db
205 }
206
207 if (m_db == nullptr) {
208 if (!m_mock) {
210 }
211 int ret = sqlite3_open_v2(m_file_path.c_str(), &m_db, flags, nullptr);
212 if (ret != SQLITE_OK) {
213 throw std::runtime_error(strprintf("SQLiteDatabase: Failed to open database: %s\n", sqlite3_errstr(ret)));
214 }
215 ret = sqlite3_extended_result_codes(m_db, 1);
216 if (ret != SQLITE_OK) {
217 throw std::runtime_error(strprintf("SQLiteDatabase: Failed to enable extended result codes: %s\n", sqlite3_errstr(ret)));
218 }
219 }
220
221 if (sqlite3_db_readonly(m_db, "main") != 0) {
222 throw std::runtime_error("SQLiteDatabase: Database opened in readonly mode but read-write permissions are needed");
223 }
224
225 // Acquire an exclusive lock on the database
226 // First change the locking mode to exclusive
227 SetPragma(m_db, "locking_mode", "exclusive", "Unable to change database locking mode to exclusive");
228 // Now begin a transaction to acquire the exclusive lock. This lock won't be released until we close because of the exclusive locking mode.
229 int ret = sqlite3_exec(m_db, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, nullptr);
230 if (ret != SQLITE_OK) {
231 throw std::runtime_error("SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of " PACKAGE_NAME "?\n");
232 }
233 ret = sqlite3_exec(m_db, "COMMIT", nullptr, nullptr, nullptr);
234 if (ret != SQLITE_OK) {
235 throw std::runtime_error(strprintf("SQLiteDatabase: Unable to end exclusive lock transaction: %s\n", sqlite3_errstr(ret)));
236 }
237
238 // Enable fullfsync for the platforms that use it
239 SetPragma(m_db, "fullfsync", "true", "Failed to enable fullfsync");
240
241 if (gArgs.GetBoolArg("-unsafesqlitesync", false)) {
242 // Use normal synchronous mode for the journal
243 LogPrintf("WARNING SQLite is configured to not wait for data to be flushed to disk. Data loss and corruption may occur.\n");
244 SetPragma(m_db, "synchronous", "OFF", "Failed to set synchronous mode to OFF");
245 }
246
247 // Make the table for our key-value pairs
248 // First check that the main table exists
249 sqlite3_stmt* check_main_stmt{nullptr};
250 ret = sqlite3_prepare_v2(m_db, "SELECT name FROM sqlite_master WHERE type='table' AND name='main'", -1, &check_main_stmt, nullptr);
251 if (ret != SQLITE_OK) {
252 throw std::runtime_error(strprintf("SQLiteDatabase: Failed to prepare statement to check table existence: %s\n", sqlite3_errstr(ret)));
253 }
254 ret = sqlite3_step(check_main_stmt);
255 if (sqlite3_finalize(check_main_stmt) != SQLITE_OK) {
256 throw std::runtime_error(strprintf("SQLiteDatabase: Failed to finalize statement checking table existence: %s\n", sqlite3_errstr(ret)));
257 }
258 bool table_exists;
259 if (ret == SQLITE_DONE) {
260 table_exists = false;
261 } else if (ret == SQLITE_ROW) {
262 table_exists = true;
263 } else {
264 throw std::runtime_error(strprintf("SQLiteDatabase: Failed to execute statement to check table existence: %s\n", sqlite3_errstr(ret)));
265 }
266
267 // Do the db setup things because the table doesn't exist only when we are creating a new wallet
268 if (!table_exists) {
269 ret = sqlite3_exec(m_db, "CREATE TABLE main(key BLOB PRIMARY KEY NOT NULL, value BLOB NOT NULL)", nullptr, nullptr, nullptr);
270 if (ret != SQLITE_OK) {
271 throw std::runtime_error(strprintf("SQLiteDatabase: Failed to create new database: %s\n", sqlite3_errstr(ret)));
272 }
273
274 // Set the application id
275 uint32_t app_id = ReadBE32(Params().MessageStart());
276 SetPragma(m_db, "application_id", strprintf("%d", static_cast<int32_t>(app_id)),
277 "Failed to set the application id");
278
279 // Set the user version
280 SetPragma(m_db, "user_version", strprintf("%d", WALLET_SCHEMA_VERSION),
281 "Failed to set the wallet schema version");
282 }
283}
284
285bool SQLiteDatabase::Rewrite(const char* skip)
286{
287 // Rewrite the database using the VACUUM command: https://sqlite.org/lang_vacuum.html
288 int ret = sqlite3_exec(m_db, "VACUUM", nullptr, nullptr, nullptr);
289 return ret == SQLITE_OK;
290}
291
292bool SQLiteDatabase::Backup(const std::string& dest) const
293{
294 sqlite3* db_copy;
295 int res = sqlite3_open(dest.c_str(), &db_copy);
296 if (res != SQLITE_OK) {
297 sqlite3_close(db_copy);
298 return false;
299 }
300 sqlite3_backup* backup = sqlite3_backup_init(db_copy, "main", m_db, "main");
301 if (!backup) {
302 LogPrintf("%s: Unable to begin backup: %s\n", __func__, sqlite3_errmsg(m_db));
303 sqlite3_close(db_copy);
304 return false;
305 }
306 // Specifying -1 will copy all of the pages
307 res = sqlite3_backup_step(backup, -1);
308 if (res != SQLITE_DONE) {
309 LogPrintf("%s: Unable to backup: %s\n", __func__, sqlite3_errstr(res));
310 sqlite3_backup_finish(backup);
311 sqlite3_close(db_copy);
312 return false;
313 }
314 res = sqlite3_backup_finish(backup);
315 sqlite3_close(db_copy);
316 return res == SQLITE_OK;
317}
318
320{
321 int res = sqlite3_close(m_db);
322 if (res != SQLITE_OK) {
323 throw std::runtime_error(strprintf("SQLiteDatabase: Failed to close database: %s\n", sqlite3_errstr(res)));
324 }
325 m_db = nullptr;
326}
327
328std::unique_ptr<DatabaseBatch> SQLiteDatabase::MakeBatch(bool flush_on_close)
329{
330 // We ignore flush_on_close because we don't do manual flushing for SQLite
331 return std::make_unique<SQLiteBatch>(*this);
332}
333
335 : m_database(database)
336{
337 // Make sure we have a db handle
339
341}
342
344{
345 // If m_db is in a transaction (i.e. not in autocommit mode), then abort the transaction in progress
346 if (m_database.m_db && sqlite3_get_autocommit(m_database.m_db) == 0) {
347 if (TxnAbort()) {
348 LogPrintf("SQLiteBatch: Batch closed unexpectedly without the transaction being explicitly committed or aborted\n");
349 } else {
350 LogPrintf("SQLiteBatch: Batch closed and failed to abort transaction\n");
351 }
352 }
353
354 // Free all of the prepared statements
355 const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{
356 {&m_read_stmt, "read"},
357 {&m_insert_stmt, "insert"},
358 {&m_overwrite_stmt, "overwrite"},
359 {&m_delete_stmt, "delete"},
360 {&m_cursor_stmt, "cursor"},
361 };
362
363 for (const auto& [stmt_prepared, stmt_description] : statements) {
364 int res = sqlite3_finalize(*stmt_prepared);
365 if (res != SQLITE_OK) {
366 LogPrintf("SQLiteBatch: Batch closed but could not finalize %s statement: %s\n",
367 stmt_description, sqlite3_errstr(res));
368 }
369 *stmt_prepared = nullptr;
370 }
371}
372
374{
375 if (!m_database.m_db) return false;
377
378 // Bind: leftmost parameter in statement is index 1
379 int res = sqlite3_bind_blob(m_read_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
380 if (res != SQLITE_OK) {
381 LogPrintf("%s: Unable to bind statement: %s\n", __func__, sqlite3_errstr(res));
382 sqlite3_clear_bindings(m_read_stmt);
383 sqlite3_reset(m_read_stmt);
384 return false;
385 }
386 res = sqlite3_step(m_read_stmt);
387 if (res != SQLITE_ROW) {
388 if (res != SQLITE_DONE) {
389 // SQLITE_DONE means "not found", don't log an error in that case.
390 LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
391 }
392 sqlite3_clear_bindings(m_read_stmt);
393 sqlite3_reset(m_read_stmt);
394 return false;
395 }
396 // Leftmost column in result is index 0
397 const char* data = reinterpret_cast<const char*>(sqlite3_column_blob(m_read_stmt, 0));
398 int data_size = sqlite3_column_bytes(m_read_stmt, 0);
399 value.write(data, data_size);
400
401 sqlite3_clear_bindings(m_read_stmt);
402 sqlite3_reset(m_read_stmt);
403 return true;
404}
405
406bool SQLiteBatch::WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite)
407{
408 if (!m_database.m_db) return false;
410
411 sqlite3_stmt* stmt;
412 if (overwrite) {
413 stmt = m_overwrite_stmt;
414 } else {
415 stmt = m_insert_stmt;
416 }
417
418 // Bind: leftmost parameter in statement is index 1
419 // Insert index 1 is key, 2 is value
420 int res = sqlite3_bind_blob(stmt, 1, key.data(), key.size(), SQLITE_STATIC);
421 if (res != SQLITE_OK) {
422 LogPrintf("%s: Unable to bind key to statement: %s\n", __func__, sqlite3_errstr(res));
423 sqlite3_clear_bindings(stmt);
424 sqlite3_reset(stmt);
425 return false;
426 }
427 res = sqlite3_bind_blob(stmt, 2, value.data(), value.size(), SQLITE_STATIC);
428 if (res != SQLITE_OK) {
429 LogPrintf("%s: Unable to bind value to statement: %s\n", __func__, sqlite3_errstr(res));
430 sqlite3_clear_bindings(stmt);
431 sqlite3_reset(stmt);
432 return false;
433 }
434
435 // Execute
436 res = sqlite3_step(stmt);
437 sqlite3_clear_bindings(stmt);
438 sqlite3_reset(stmt);
439 if (res != SQLITE_DONE) {
440 LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
441 }
442 return res == SQLITE_DONE;
443}
444
446{
447 if (!m_database.m_db) return false;
449
450 // Bind: leftmost parameter in statement is index 1
451 int res = sqlite3_bind_blob(m_delete_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
452 if (res != SQLITE_OK) {
453 LogPrintf("%s: Unable to bind statement: %s\n", __func__, sqlite3_errstr(res));
454 sqlite3_clear_bindings(m_delete_stmt);
455 sqlite3_reset(m_delete_stmt);
456 return false;
457 }
458
459 // Execute
460 res = sqlite3_step(m_delete_stmt);
461 sqlite3_clear_bindings(m_delete_stmt);
462 sqlite3_reset(m_delete_stmt);
463 if (res != SQLITE_DONE) {
464 LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
465 }
466 return res == SQLITE_DONE;
467}
468
470{
471 if (!m_database.m_db) return false;
473
474 // Bind: leftmost parameter in statement is index 1
475 bool ret = false;
476 int res = sqlite3_bind_blob(m_read_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
477 if (res == SQLITE_OK) {
478 res = sqlite3_step(m_read_stmt);
479 if (res == SQLITE_ROW) {
480 ret = true;
481 }
482 }
483
484 sqlite3_clear_bindings(m_read_stmt);
485 sqlite3_reset(m_read_stmt);
486 return ret;
487}
488
490{
492 if (!m_database.m_db) return false;
493 m_cursor_init = true;
494 return true;
495}
496
497bool SQLiteBatch::ReadAtCursor(CDataStream& key, CDataStream& value, bool& complete)
498{
499 complete = false;
500
501 if (!m_cursor_init) return false;
502
503 int res = sqlite3_step(m_cursor_stmt);
504 if (res == SQLITE_DONE) {
505 complete = true;
506 return true;
507 }
508 if (res != SQLITE_ROW) {
509 LogPrintf("SQLiteBatch::ReadAtCursor: Unable to execute cursor step: %s\n", sqlite3_errstr(res));
510 return false;
511 }
512
513 // Leftmost column in result is index 0
514 const char* key_data = reinterpret_cast<const char*>(sqlite3_column_blob(m_cursor_stmt, 0));
515 int key_data_size = sqlite3_column_bytes(m_cursor_stmt, 0);
516 key.write(key_data, key_data_size);
517 const char* value_data = reinterpret_cast<const char*>(sqlite3_column_blob(m_cursor_stmt, 1));
518 int value_data_size = sqlite3_column_bytes(m_cursor_stmt, 1);
519 value.write(value_data, value_data_size);
520 return true;
521}
522
524{
525 sqlite3_reset(m_cursor_stmt);
526 m_cursor_init = false;
527}
528
530{
531 if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) == 0) return false;
532 int res = sqlite3_exec(m_database.m_db, "BEGIN TRANSACTION", nullptr, nullptr, nullptr);
533 if (res != SQLITE_OK) {
534 LogPrintf("SQLiteBatch: Failed to begin the transaction\n");
535 }
536 return res == SQLITE_OK;
537}
538
540{
541 if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) != 0) return false;
542 int res = sqlite3_exec(m_database.m_db, "COMMIT TRANSACTION", nullptr, nullptr, nullptr);
543 if (res != SQLITE_OK) {
544 LogPrintf("SQLiteBatch: Failed to commit the transaction\n");
545 }
546 return res == SQLITE_OK;
547}
548
550{
551 if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) != 0) return false;
552 int res = sqlite3_exec(m_database.m_db, "ROLLBACK TRANSACTION", nullptr, nullptr, nullptr);
553 if (res != SQLITE_OK) {
554 LogPrintf("SQLiteBatch: Failed to abort the transaction\n");
555 }
556 return res == SQLITE_OK;
557}
558
559std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
560{
561 try {
562 fs::path data_file = SQLiteDataFile(path);
563 auto db = std::make_unique<SQLiteDatabase>(data_file.parent_path(), data_file);
564 if (options.verify && !db->Verify(error)) {
566 return nullptr;
567 }
569 return db;
570 } catch (const std::runtime_error& e) {
572 error = Untranslated(e.what());
573 return nullptr;
574 }
575}
576
578{
579 return std::string(sqlite3_libversion());
580}
#define PACKAGE_NAME
int flags
Definition: bitcoin-tx.cpp:525
const CChainParams & Params()
Return the currently selected parameters.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:602
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:205
void write(const char *pch, size_t nSize)
Definition: streams.h:402
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: sqlite.cpp:406
sqlite3_stmt * m_read_stmt
Definition: sqlite.h:23
bool TxnAbort() override
Definition: sqlite.cpp:549
bool m_cursor_init
Definition: sqlite.h:21
sqlite3_stmt * m_cursor_stmt
Definition: sqlite.h:27
void SetupSQLStatements()
Definition: sqlite.cpp:104
bool HasKey(CDataStream &&key) override
Definition: sqlite.cpp:469
sqlite3_stmt * m_insert_stmt
Definition: sqlite.h:24
sqlite3_stmt * m_overwrite_stmt
Definition: sqlite.h:25
bool StartCursor() override
Definition: sqlite.cpp:489
bool EraseKey(CDataStream &&key) override
Definition: sqlite.cpp:445
bool ReadAtCursor(CDataStream &key, CDataStream &value, bool &complete) override
Definition: sqlite.cpp:497
bool TxnBegin() override
Definition: sqlite.cpp:529
void Close() override
Definition: sqlite.cpp:343
sqlite3_stmt * m_delete_stmt
Definition: sqlite.h:26
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition: sqlite.cpp:373
bool TxnCommit() override
Definition: sqlite.cpp:539
SQLiteDatabase & m_database
Definition: sqlite.h:19
void CloseCursor() override
Definition: sqlite.cpp:523
SQLiteBatch(SQLiteDatabase &database)
Definition: sqlite.cpp:334
An instance of this class represents one SQLite3 database.
Definition: sqlite.h:56
void Close() override
Close the database.
Definition: sqlite.cpp:319
void Open() override
Open the database if it is not already opened.
Definition: sqlite.cpp:200
sqlite3 * m_db
Definition: sqlite.h:113
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a SQLiteBatch connected to this database.
Definition: sqlite.cpp:328
const std::string m_dir_path
Definition: sqlite.h:60
void Cleanup() noexcept
Definition: sqlite.cpp:130
bool Verify(bilingual_str &error)
Definition: sqlite.cpp:143
SQLiteDatabase()=delete
const bool m_mock
Definition: sqlite.h:58
const std::string m_file_path
Definition: sqlite.h:62
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
Definition: sqlite.cpp:292
bool Rewrite(const char *skip=nullptr) override
Rewrite the entire database on disk.
Definition: sqlite.cpp:285
An instance of this class represents one database.
Definition: db.h:104
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:34
static uint32_t ReadBE32(const unsigned char *ptr)
Definition: common.h:63
fs::path SQLiteDataFile(const fs::path &path)
Definition: db.cpp:72
DatabaseStatus
Definition: db.h:212
#define LogPrintf(...)
Definition: logging.h:187
Filesystem operations and types.
Definition: fs.h:19
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:120
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:133
static constexpr int32_t WALLET_SCHEMA_VERSION
Definition: sqlite.cpp:23
static void SetPragma(sqlite3 *db, const std::string &key, const std::string &value, const std::string &err_msg)
Definition: sqlite.cpp:60
static void ErrorLogCallback(void *arg, int code, const char *msg)
Definition: sqlite.cpp:28
std::string SQLiteDatabaseVersion()
Definition: sqlite.cpp:577
static Mutex g_sqlite_mutex
Definition: sqlite.cpp:25
static std::optional< int > ReadPragmaInteger(sqlite3 *db, const std::string &key, const std::string &description, bilingual_str &error)
Definition: sqlite.cpp:39
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:559
static int g_sqlite_count GUARDED_BY(g_sqlite_mutex)=0
bool verify
Definition: db.h:209
Bilingual messages:
Definition: translation.h:16
#define LOCK(cs)
Definition: sync.h:226
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:63
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:46
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by Boost's create_directories if the requested directory exists.
Definition: system.cpp:1081
ArgsManager gArgs
Definition: system.cpp:85
assert(!tx.IsCoinBase())