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 <optional>
20 #include <utility>
21 #include <vector>
22 
23 static constexpr int32_t WALLET_SCHEMA_VERSION = 0;
24 
26 static int g_sqlite_count GUARDED_BY(g_sqlite_mutex) = 0;
27 
28 static 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 
39 static 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 
60 static 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 
69 SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, bool mock)
70  : WalletDatabase(), m_mock(mock), m_dir_path(dir_path.string()), m_file_path(file_path.string())
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 
130 void SQLiteDatabase::Cleanup() noexcept
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  }
216 
217  if (sqlite3_db_readonly(m_db, "main") != 0) {
218  throw std::runtime_error("SQLiteDatabase: Database opened in readonly mode but read-write permissions are needed");
219  }
220 
221  // Acquire an exclusive lock on the database
222  // First change the locking mode to exclusive
223  SetPragma(m_db, "locking_mode", "exclusive", "Unable to change database locking mode to exclusive");
224  // Now begin a transaction to acquire the exclusive lock. This lock won't be released until we close because of the exclusive locking mode.
225  int ret = sqlite3_exec(m_db, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, nullptr);
226  if (ret != SQLITE_OK) {
227  throw std::runtime_error("SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another bitcoind?\n");
228  }
229  ret = sqlite3_exec(m_db, "COMMIT", nullptr, nullptr, nullptr);
230  if (ret != SQLITE_OK) {
231  throw std::runtime_error(strprintf("SQLiteDatabase: Unable to end exclusive lock transaction: %s\n", sqlite3_errstr(ret)));
232  }
233 
234  // Enable fullfsync for the platforms that use it
235  SetPragma(m_db, "fullfsync", "true", "Failed to enable fullfsync");
236 
237  if (gArgs.GetBoolArg("-unsafesqlitesync", false)) {
238  // Use normal synchronous mode for the journal
239  LogPrintf("WARNING SQLite is configured to not wait for data to be flushed to disk. Data loss and corruption may occur.\n");
240  SetPragma(m_db, "synchronous", "OFF", "Failed to set synchronous mode to OFF");
241  }
242 
243  // Make the table for our key-value pairs
244  // First check that the main table exists
245  sqlite3_stmt* check_main_stmt{nullptr};
246  ret = sqlite3_prepare_v2(m_db, "SELECT name FROM sqlite_master WHERE type='table' AND name='main'", -1, &check_main_stmt, nullptr);
247  if (ret != SQLITE_OK) {
248  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to prepare statement to check table existence: %s\n", sqlite3_errstr(ret)));
249  }
250  ret = sqlite3_step(check_main_stmt);
251  if (sqlite3_finalize(check_main_stmt) != SQLITE_OK) {
252  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to finalize statement checking table existence: %s\n", sqlite3_errstr(ret)));
253  }
254  bool table_exists;
255  if (ret == SQLITE_DONE) {
256  table_exists = false;
257  } else if (ret == SQLITE_ROW) {
258  table_exists = true;
259  } else {
260  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to execute statement to check table existence: %s\n", sqlite3_errstr(ret)));
261  }
262 
263  // Do the db setup things because the table doesn't exist only when we are creating a new wallet
264  if (!table_exists) {
265  ret = sqlite3_exec(m_db, "CREATE TABLE main(key BLOB PRIMARY KEY NOT NULL, value BLOB NOT NULL)", nullptr, nullptr, nullptr);
266  if (ret != SQLITE_OK) {
267  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to create new database: %s\n", sqlite3_errstr(ret)));
268  }
269 
270  // Set the application id
271  uint32_t app_id = ReadBE32(Params().MessageStart());
272  SetPragma(m_db, "application_id", strprintf("%d", static_cast<int32_t>(app_id)),
273  "Failed to set the application id");
274 
275  // Set the user version
276  SetPragma(m_db, "user_version", strprintf("%d", WALLET_SCHEMA_VERSION),
277  "Failed to set the wallet schema version");
278  }
279 }
280 
281 bool SQLiteDatabase::Rewrite(const char* skip)
282 {
283  // Rewrite the database using the VACUUM command: https://sqlite.org/lang_vacuum.html
284  int ret = sqlite3_exec(m_db, "VACUUM", nullptr, nullptr, nullptr);
285  return ret == SQLITE_OK;
286 }
287 
288 bool SQLiteDatabase::Backup(const std::string& dest) const
289 {
290  sqlite3* db_copy;
291  int res = sqlite3_open(dest.c_str(), &db_copy);
292  if (res != SQLITE_OK) {
293  sqlite3_close(db_copy);
294  return false;
295  }
296  sqlite3_backup* backup = sqlite3_backup_init(db_copy, "main", m_db, "main");
297  if (!backup) {
298  LogPrintf("%s: Unable to begin backup: %s\n", __func__, sqlite3_errmsg(m_db));
299  sqlite3_close(db_copy);
300  return false;
301  }
302  // Specifying -1 will copy all of the pages
303  res = sqlite3_backup_step(backup, -1);
304  if (res != SQLITE_DONE) {
305  LogPrintf("%s: Unable to backup: %s\n", __func__, sqlite3_errstr(res));
306  sqlite3_backup_finish(backup);
307  sqlite3_close(db_copy);
308  return false;
309  }
310  res = sqlite3_backup_finish(backup);
311  sqlite3_close(db_copy);
312  return res == SQLITE_OK;
313 }
314 
316 {
317  int res = sqlite3_close(m_db);
318  if (res != SQLITE_OK) {
319  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to close database: %s\n", sqlite3_errstr(res)));
320  }
321  m_db = nullptr;
322 }
323 
324 std::unique_ptr<DatabaseBatch> SQLiteDatabase::MakeBatch(bool flush_on_close)
325 {
326  // We ignore flush_on_close because we don't do manual flushing for SQLite
327  return std::make_unique<SQLiteBatch>(*this);
328 }
329 
331  : m_database(database)
332 {
333  // Make sure we have a db handle
335 
337 }
338 
340 {
341  // If m_db is in a transaction (i.e. not in autocommit mode), then abort the transaction in progress
342  if (m_database.m_db && sqlite3_get_autocommit(m_database.m_db) == 0) {
343  if (TxnAbort()) {
344  LogPrintf("SQLiteBatch: Batch closed unexpectedly without the transaction being explicitly committed or aborted\n");
345  } else {
346  LogPrintf("SQLiteBatch: Batch closed and failed to abort transaction\n");
347  }
348  }
349 
350  // Free all of the prepared statements
351  const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{
352  {&m_read_stmt, "read"},
353  {&m_insert_stmt, "insert"},
354  {&m_overwrite_stmt, "overwrite"},
355  {&m_delete_stmt, "delete"},
356  {&m_cursor_stmt, "cursor"},
357  };
358 
359  for (const auto& [stmt_prepared, stmt_description] : statements) {
360  int res = sqlite3_finalize(*stmt_prepared);
361  if (res != SQLITE_OK) {
362  LogPrintf("SQLiteBatch: Batch closed but could not finalize %s statement: %s\n",
363  stmt_description, sqlite3_errstr(res));
364  }
365  *stmt_prepared = nullptr;
366  }
367 }
368 
370 {
371  if (!m_database.m_db) return false;
373 
374  // Bind: leftmost parameter in statement is index 1
375  int res = sqlite3_bind_blob(m_read_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
376  if (res != SQLITE_OK) {
377  LogPrintf("%s: Unable to bind statement: %s\n", __func__, sqlite3_errstr(res));
378  sqlite3_clear_bindings(m_read_stmt);
379  sqlite3_reset(m_read_stmt);
380  return false;
381  }
382  res = sqlite3_step(m_read_stmt);
383  if (res != SQLITE_ROW) {
384  if (res != SQLITE_DONE) {
385  // SQLITE_DONE means "not found", don't log an error in that case.
386  LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
387  }
388  sqlite3_clear_bindings(m_read_stmt);
389  sqlite3_reset(m_read_stmt);
390  return false;
391  }
392  // Leftmost column in result is index 0
393  const char* data = reinterpret_cast<const char*>(sqlite3_column_blob(m_read_stmt, 0));
394  int data_size = sqlite3_column_bytes(m_read_stmt, 0);
395  value.write(data, data_size);
396 
397  sqlite3_clear_bindings(m_read_stmt);
398  sqlite3_reset(m_read_stmt);
399  return true;
400 }
401 
402 bool SQLiteBatch::WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite)
403 {
404  if (!m_database.m_db) return false;
406 
407  sqlite3_stmt* stmt;
408  if (overwrite) {
409  stmt = m_overwrite_stmt;
410  } else {
411  stmt = m_insert_stmt;
412  }
413 
414  // Bind: leftmost parameter in statement is index 1
415  // Insert index 1 is key, 2 is value
416  int res = sqlite3_bind_blob(stmt, 1, key.data(), key.size(), SQLITE_STATIC);
417  if (res != SQLITE_OK) {
418  LogPrintf("%s: Unable to bind key to statement: %s\n", __func__, sqlite3_errstr(res));
419  sqlite3_clear_bindings(stmt);
420  sqlite3_reset(stmt);
421  return false;
422  }
423  res = sqlite3_bind_blob(stmt, 2, value.data(), value.size(), SQLITE_STATIC);
424  if (res != SQLITE_OK) {
425  LogPrintf("%s: Unable to bind value to statement: %s\n", __func__, sqlite3_errstr(res));
426  sqlite3_clear_bindings(stmt);
427  sqlite3_reset(stmt);
428  return false;
429  }
430 
431  // Execute
432  res = sqlite3_step(stmt);
433  sqlite3_clear_bindings(stmt);
434  sqlite3_reset(stmt);
435  if (res != SQLITE_DONE) {
436  LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
437  }
438  return res == SQLITE_DONE;
439 }
440 
442 {
443  if (!m_database.m_db) return false;
445 
446  // Bind: leftmost parameter in statement is index 1
447  int res = sqlite3_bind_blob(m_delete_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
448  if (res != SQLITE_OK) {
449  LogPrintf("%s: Unable to bind statement: %s\n", __func__, sqlite3_errstr(res));
450  sqlite3_clear_bindings(m_delete_stmt);
451  sqlite3_reset(m_delete_stmt);
452  return false;
453  }
454 
455  // Execute
456  res = sqlite3_step(m_delete_stmt);
457  sqlite3_clear_bindings(m_delete_stmt);
458  sqlite3_reset(m_delete_stmt);
459  if (res != SQLITE_DONE) {
460  LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
461  }
462  return res == SQLITE_DONE;
463 }
464 
466 {
467  if (!m_database.m_db) return false;
469 
470  // Bind: leftmost parameter in statement is index 1
471  bool ret = false;
472  int res = sqlite3_bind_blob(m_read_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
473  if (res == SQLITE_OK) {
474  res = sqlite3_step(m_read_stmt);
475  if (res == SQLITE_ROW) {
476  ret = true;
477  }
478  }
479 
480  sqlite3_clear_bindings(m_read_stmt);
481  sqlite3_reset(m_read_stmt);
482  return ret;
483 }
484 
486 {
488  if (!m_database.m_db) return false;
489  m_cursor_init = true;
490  return true;
491 }
492 
493 bool SQLiteBatch::ReadAtCursor(CDataStream& key, CDataStream& value, bool& complete)
494 {
495  complete = false;
496 
497  if (!m_cursor_init) return false;
498 
499  int res = sqlite3_step(m_cursor_stmt);
500  if (res == SQLITE_DONE) {
501  complete = true;
502  return true;
503  }
504  if (res != SQLITE_ROW) {
505  LogPrintf("SQLiteBatch::ReadAtCursor: Unable to execute cursor step: %s\n", sqlite3_errstr(res));
506  return false;
507  }
508 
509  // Leftmost column in result is index 0
510  const char* key_data = reinterpret_cast<const char*>(sqlite3_column_blob(m_cursor_stmt, 0));
511  int key_data_size = sqlite3_column_bytes(m_cursor_stmt, 0);
512  key.write(key_data, key_data_size);
513  const char* value_data = reinterpret_cast<const char*>(sqlite3_column_blob(m_cursor_stmt, 1));
514  int value_data_size = sqlite3_column_bytes(m_cursor_stmt, 1);
515  value.write(value_data, value_data_size);
516  return true;
517 }
518 
520 {
521  sqlite3_reset(m_cursor_stmt);
522  m_cursor_init = false;
523 }
524 
526 {
527  if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) == 0) return false;
528  int res = sqlite3_exec(m_database.m_db, "BEGIN TRANSACTION", nullptr, nullptr, nullptr);
529  if (res != SQLITE_OK) {
530  LogPrintf("SQLiteBatch: Failed to begin the transaction\n");
531  }
532  return res == SQLITE_OK;
533 }
534 
536 {
537  if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) != 0) return false;
538  int res = sqlite3_exec(m_database.m_db, "COMMIT TRANSACTION", nullptr, nullptr, nullptr);
539  if (res != SQLITE_OK) {
540  LogPrintf("SQLiteBatch: Failed to commit the transaction\n");
541  }
542  return res == SQLITE_OK;
543 }
544 
546 {
547  if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) != 0) return false;
548  int res = sqlite3_exec(m_database.m_db, "ROLLBACK TRANSACTION", nullptr, nullptr, nullptr);
549  if (res != SQLITE_OK) {
550  LogPrintf("SQLiteBatch: Failed to abort the transaction\n");
551  }
552  return res == SQLITE_OK;
553 }
554 
555 std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
556 {
557  try {
558  fs::path data_file = SQLiteDataFile(path);
559  auto db = std::make_unique<SQLiteDatabase>(data_file.parent_path(), data_file);
560  if (options.verify && !db->Verify(error)) {
562  return nullptr;
563  }
564  status = DatabaseStatus::SUCCESS;
565  return db;
566  } catch (const std::runtime_error& e) {
568  error = Untranslated(e.what());
569  return nullptr;
570  }
571 }
572 
574 {
575  return std::string(sqlite3_libversion());
576 }
DatabaseOptions
Definition: db.h:203
SQLiteDatabase::Rewrite
bool Rewrite(const char *skip=nullptr) override
Rewrite the entire database on disk.
Definition: sqlite.cpp:281
SQLiteBatch::ReadKey
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition: sqlite.cpp:369
ArgsManager::GetBoolArg
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:595
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:57
assert
assert(!tx.IsCoinBase())
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:324
sync.h
SQLiteDatabaseVersion
std::string SQLiteDatabaseVersion()
Definition: sqlite.cpp:573
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:200
bilingual_str
Bilingual messages:
Definition: translation.h:16
SQLiteBatch::SetupSQLStatements
void SetupSQLStatements()
Definition: sqlite.cpp:104
SQLiteBatch::m_overwrite_stmt
sqlite3_stmt * m_overwrite_stmt
Definition: sqlite.h:25
ReadPragmaInteger
static std::optional< int > ReadPragmaInteger(sqlite3 *db, const std::string &key, const std::string &description, bilingual_str &error)
Definition: sqlite.cpp:39
SQLiteBatch::TxnCommit
bool TxnCommit() override
Definition: sqlite.cpp:535
g_sqlite_mutex
static Mutex g_sqlite_mutex
Definition: sqlite.cpp:25
SQLiteDatabase::m_db
sqlite3 * m_db
Definition: sqlite.h:113
SQLiteBatch::Close
void Close() override
Definition: sqlite.cpp:339
AnnotatedMixin< std::mutex >
ReadBE32
static uint32_t ReadBE32(const unsigned char *ptr)
Definition: common.h:63
WalletDatabase
An instance of this class represents one database.
Definition: db.h:103
SQLiteBatch::StartCursor
bool StartCursor() override
Definition: sqlite.cpp:485
SQLiteDatabase::Backup
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
Definition: sqlite.cpp:288
common.h
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:330
SQLiteBatch::WriteKey
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: sqlite.cpp:402
strencodings.h
SQLiteBatch::ReadAtCursor
bool ReadAtCursor(CDataStream &key, CDataStream &value, bool &complete) override
Definition: sqlite.cpp:493
Untranslated
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:40
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:555
LogPrintf
#define LogPrintf(...)
Definition: logging.h:184
SQLiteBatch::m_delete_stmt
sqlite3_stmt * m_delete_stmt
Definition: sqlite.h:26
DatabaseStatus::FAILED_VERIFY
@ FAILED_VERIFY
SQLiteDatabase::~SQLiteDatabase
~SQLiteDatabase()
Definition: sqlite.cpp:125
SQLiteBatch::EraseKey
bool EraseKey(CDataStream &&key) override
Definition: sqlite.cpp:441
SQLiteBatch::TxnBegin
bool TxnBegin() override
Definition: sqlite.cpp:525
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:315
DatabaseStatus::FAILED_LOAD
@ FAILED_LOAD
translation.h
SQLiteBatch::TxnAbort
bool TxnAbort() override
Definition: sqlite.cpp:545
SQLiteBatch::CloseCursor
void CloseCursor() override
Definition: sqlite.cpp:519
LOCK
#define LOCK(cs)
Definition: sync.h:232
SQLiteDataFile
fs::path SQLiteDataFile(const fs::path &path)
Definition: db.cpp:75
gArgs
ArgsManager gArgs
Definition: system.cpp:79
SQLiteBatch::HasKey
bool HasKey(CDataStream &&key) override
Definition: sqlite.cpp:465
logging.h
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:538
WALLET_SCHEMA_VERSION
static constexpr int32_t WALLET_SCHEMA_VERSION
Definition: sqlite.cpp:23
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:1070
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
DatabaseStatus::SUCCESS
@ SUCCESS
SQLiteDatabase::Verify
bool Verify(bilingual_str &error)
Definition: sqlite.cpp:143
SQLiteBatch::m_database
SQLiteDatabase & m_database
Definition: sqlite.h:19
ErrorLogCallback
static void ErrorLogCallback(void *arg, int code, const char *msg)
Definition: sqlite.cpp:28
SQLiteDatabase::Cleanup
void Cleanup() noexcept
Definition: sqlite.cpp:130
GUARDED_BY
static int g_sqlite_count GUARDED_BY(g_sqlite_mutex)=0
SetPragma
static void SetPragma(sqlite3 *db, const std::string &key, const std::string &value, const std::string &err_msg)
Definition: sqlite.cpp:60