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