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