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