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/memory.h>
12 #include <util/strencodings.h>
13 #include <util/system.h>
14 #include <util/translation.h>
15 #include <wallet/db.h>
16 
17 #include <sqlite3.h>
18 #include <stdint.h>
19 
20 static constexpr int32_t WALLET_SCHEMA_VERSION = 0;
21 
23 static int g_sqlite_count GUARDED_BY(g_sqlite_mutex) = 0;
24 
25 static void ErrorLogCallback(void* arg, int code, const char* msg)
26 {
27  // From sqlite3_config() documentation for the SQLITE_CONFIG_LOG option:
28  // "The void pointer that is the second argument to SQLITE_CONFIG_LOG is passed through as
29  // the first parameter to the application-defined logger function whenever that function is
30  // invoked."
31  // Assert that this is the case:
32  assert(arg == nullptr);
33  LogPrintf("SQLite Error. Code: %d. Message: %s\n", code, msg);
34 }
35 
36 SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, bool mock)
37  : WalletDatabase(), m_mock(mock), m_dir_path(dir_path.string()), m_file_path(file_path.string())
38 {
39  {
40  LOCK(g_sqlite_mutex);
41  LogPrintf("Using SQLite Version %s\n", SQLiteDatabaseVersion());
42  LogPrintf("Using wallet %s\n", m_dir_path);
43 
44  if (++g_sqlite_count == 1) {
45  // Setup logging
46  int ret = sqlite3_config(SQLITE_CONFIG_LOG, ErrorLogCallback, nullptr);
47  if (ret != SQLITE_OK) {
48  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup error log: %s\n", sqlite3_errstr(ret)));
49  }
50  // Force serialized threading mode
51  ret = sqlite3_config(SQLITE_CONFIG_SERIALIZED);
52  if (ret != SQLITE_OK) {
53  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to configure serialized threading mode: %s\n", sqlite3_errstr(ret)));
54  }
55  }
56  int ret = sqlite3_initialize(); // This is a no-op if sqlite3 is already initialized
57  if (ret != SQLITE_OK) {
58  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to initialize SQLite: %s\n", sqlite3_errstr(ret)));
59  }
60  }
61 
62  try {
63  Open();
64  } catch (const std::runtime_error&) {
65  // If open fails, cleanup this object and rethrow the exception
66  Cleanup();
67  throw;
68  }
69 }
70 
72 {
73  int res;
74  if (!m_read_stmt) {
75  if ((res = sqlite3_prepare_v2(m_database.m_db, "SELECT value FROM main WHERE key = ?", -1, &m_read_stmt, nullptr)) != SQLITE_OK) {
76  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
77  }
78  }
79  if (!m_insert_stmt) {
80  if ((res = sqlite3_prepare_v2(m_database.m_db, "INSERT INTO main VALUES(?, ?)", -1, &m_insert_stmt, nullptr)) != SQLITE_OK) {
81  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
82  }
83  }
84  if (!m_overwrite_stmt) {
85  if ((res = sqlite3_prepare_v2(m_database.m_db, "INSERT or REPLACE into main values(?, ?)", -1, &m_overwrite_stmt, nullptr)) != SQLITE_OK) {
86  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
87  }
88  }
89  if (!m_delete_stmt) {
90  if ((res = sqlite3_prepare_v2(m_database.m_db, "DELETE FROM main WHERE key = ?", -1, &m_delete_stmt, nullptr)) != SQLITE_OK) {
91  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
92  }
93  }
94  if (!m_cursor_stmt) {
95  if ((res = sqlite3_prepare_v2(m_database.m_db, "SELECT key, value FROM main", -1, &m_cursor_stmt, nullptr)) != SQLITE_OK) {
96  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements : %s\n", sqlite3_errstr(res)));
97  }
98  }
99 }
100 
102 {
103  Cleanup();
104 }
105 
106 void SQLiteDatabase::Cleanup() noexcept
107 {
108  Close();
109 
110  LOCK(g_sqlite_mutex);
111  if (--g_sqlite_count == 0) {
112  int ret = sqlite3_shutdown();
113  if (ret != SQLITE_OK) {
114  LogPrintf("SQLiteDatabase: Failed to shutdown SQLite: %s\n", sqlite3_errstr(ret));
115  }
116  }
117 }
118 
120 {
121  assert(m_db);
122 
123  // Check the application ID matches our network magic
124  sqlite3_stmt* app_id_stmt{nullptr};
125  int ret = sqlite3_prepare_v2(m_db, "PRAGMA application_id", -1, &app_id_stmt, nullptr);
126  if (ret != SQLITE_OK) {
127  sqlite3_finalize(app_id_stmt);
128  error = strprintf(_("SQLiteDatabase: Failed to prepare the statement to fetch the application id: %s"), sqlite3_errstr(ret));
129  return false;
130  }
131  ret = sqlite3_step(app_id_stmt);
132  if (ret != SQLITE_ROW) {
133  sqlite3_finalize(app_id_stmt);
134  error = strprintf(_("SQLiteDatabase: Failed to fetch the application id: %s"), sqlite3_errstr(ret));
135  return false;
136  }
137  uint32_t app_id = static_cast<uint32_t>(sqlite3_column_int(app_id_stmt, 0));
138  sqlite3_finalize(app_id_stmt);
139  uint32_t net_magic = ReadBE32(Params().MessageStart());
140  if (app_id != net_magic) {
141  error = strprintf(_("SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id);
142  return false;
143  }
144 
145  // Check our schema version
146  sqlite3_stmt* user_ver_stmt{nullptr};
147  ret = sqlite3_prepare_v2(m_db, "PRAGMA user_version", -1, &user_ver_stmt, nullptr);
148  if (ret != SQLITE_OK) {
149  sqlite3_finalize(user_ver_stmt);
150  error = strprintf(_("SQLiteDatabase: Failed to prepare the statement to fetch sqlite wallet schema version: %s"), sqlite3_errstr(ret));
151  return false;
152  }
153  ret = sqlite3_step(user_ver_stmt);
154  if (ret != SQLITE_ROW) {
155  sqlite3_finalize(user_ver_stmt);
156  error = strprintf(_("SQLiteDatabase: Failed to fetch sqlite wallet schema version: %s"), sqlite3_errstr(ret));
157  return false;
158  }
159  int32_t user_ver = sqlite3_column_int(user_ver_stmt, 0);
160  sqlite3_finalize(user_ver_stmt);
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  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  int ret = sqlite3_exec(m_db, "PRAGMA locking_mode = exclusive", nullptr, nullptr, nullptr);
224  if (ret != SQLITE_OK) {
225  throw std::runtime_error(strprintf("SQLiteDatabase: Unable to change database locking mode to exclusive: %s\n", sqlite3_errstr(ret)));
226  }
227  // Now begin a transaction to acquire the exclusive lock. This lock won't be released until we close because of the exclusive locking mode.
228  ret = sqlite3_exec(m_db, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, nullptr);
229  if (ret != SQLITE_OK) {
230  throw std::runtime_error("SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another bitcoind?\n");
231  }
232  ret = sqlite3_exec(m_db, "COMMIT", nullptr, nullptr, nullptr);
233  if (ret != SQLITE_OK) {
234  throw std::runtime_error(strprintf("SQLiteDatabase: Unable to end exclusive lock transaction: %s\n", sqlite3_errstr(ret)));
235  }
236 
237  // Enable fullfsync for the platforms that use it
238  ret = sqlite3_exec(m_db, "PRAGMA fullfsync = true", nullptr, nullptr, nullptr);
239  if (ret != SQLITE_OK) {
240  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to enable fullfsync: %s\n", sqlite3_errstr(ret)));
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  std::string set_app_id = strprintf("PRAGMA application_id = %d", static_cast<int32_t>(app_id));
273  ret = sqlite3_exec(m_db, set_app_id.c_str(), nullptr, nullptr, nullptr);
274  if (ret != SQLITE_OK) {
275  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to set the application id: %s\n", sqlite3_errstr(ret)));
276  }
277 
278  // Set the user version
279  std::string set_user_ver = strprintf("PRAGMA user_version = %d", WALLET_SCHEMA_VERSION);
280  ret = sqlite3_exec(m_db, set_user_ver.c_str(), nullptr, nullptr, nullptr);
281  if (ret != SQLITE_OK) {
282  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to set the wallet schema version: %s\n", sqlite3_errstr(ret)));
283  }
284  }
285 }
286 
287 bool SQLiteDatabase::Rewrite(const char* skip)
288 {
289  // Rewrite the database using the VACUUM command: https://sqlite.org/lang_vacuum.html
290  int ret = sqlite3_exec(m_db, "VACUUM", nullptr, nullptr, nullptr);
291  return ret == SQLITE_OK;
292 }
293 
294 bool SQLiteDatabase::Backup(const std::string& dest) const
295 {
296  sqlite3* db_copy;
297  int res = sqlite3_open(dest.c_str(), &db_copy);
298  if (res != SQLITE_OK) {
299  sqlite3_close(db_copy);
300  return false;
301  }
302  sqlite3_backup* backup = sqlite3_backup_init(db_copy, "main", m_db, "main");
303  if (!backup) {
304  LogPrintf("%s: Unable to begin backup: %s\n", __func__, sqlite3_errmsg(m_db));
305  sqlite3_close(db_copy);
306  return false;
307  }
308  // Specifying -1 will copy all of the pages
309  res = sqlite3_backup_step(backup, -1);
310  if (res != SQLITE_DONE) {
311  LogPrintf("%s: Unable to backup: %s\n", __func__, sqlite3_errstr(res));
312  sqlite3_backup_finish(backup);
313  sqlite3_close(db_copy);
314  return false;
315  }
316  res = sqlite3_backup_finish(backup);
317  sqlite3_close(db_copy);
318  return res == SQLITE_OK;
319 }
320 
322 {
323  int res = sqlite3_close(m_db);
324  if (res != SQLITE_OK) {
325  throw std::runtime_error(strprintf("SQLiteDatabase: Failed to close database: %s\n", sqlite3_errstr(res)));
326  }
327  m_db = nullptr;
328 }
329 
330 std::unique_ptr<DatabaseBatch> SQLiteDatabase::MakeBatch(bool flush_on_close)
331 {
332  // We ignore flush_on_close because we don't do manual flushing for SQLite
333  return MakeUnique<SQLiteBatch>(*this);
334 }
335 
337  : m_database(database)
338 {
339  // Make sure we have a db handle
341 
343 }
344 
346 {
347  // If m_db is in a transaction (i.e. not in autocommit mode), then abort the transaction in progress
348  if (m_database.m_db && sqlite3_get_autocommit(m_database.m_db) == 0) {
349  if (TxnAbort()) {
350  LogPrintf("SQLiteBatch: Batch closed unexpectedly without the transaction being explicitly committed or aborted\n");
351  } else {
352  LogPrintf("SQLiteBatch: Batch closed and failed to abort transaction\n");
353  }
354  }
355 
356  // Free all of the prepared statements
357  int ret = sqlite3_finalize(m_read_stmt);
358  if (ret != SQLITE_OK) {
359  LogPrintf("SQLiteBatch: Batch closed but could not finalize read statement: %s\n", sqlite3_errstr(ret));
360  }
361  ret = sqlite3_finalize(m_insert_stmt);
362  if (ret != SQLITE_OK) {
363  LogPrintf("SQLiteBatch: Batch closed but could not finalize insert statement: %s\n", sqlite3_errstr(ret));
364  }
365  ret = sqlite3_finalize(m_overwrite_stmt);
366  if (ret != SQLITE_OK) {
367  LogPrintf("SQLiteBatch: Batch closed but could not finalize overwrite statement: %s\n", sqlite3_errstr(ret));
368  }
369  ret = sqlite3_finalize(m_delete_stmt);
370  if (ret != SQLITE_OK) {
371  LogPrintf("SQLiteBatch: Batch closed but could not finalize delete statement: %s\n", sqlite3_errstr(ret));
372  }
373  ret = sqlite3_finalize(m_cursor_stmt);
374  if (ret != SQLITE_OK) {
375  LogPrintf("SQLiteBatch: Batch closed but could not finalize cursor statement: %s\n", sqlite3_errstr(ret));
376  }
377  m_read_stmt = nullptr;
378  m_insert_stmt = nullptr;
379  m_overwrite_stmt = nullptr;
380  m_delete_stmt = nullptr;
381  m_cursor_stmt = nullptr;
382 }
383 
385 {
386  if (!m_database.m_db) return false;
388 
389  // Bind: leftmost parameter in statement is index 1
390  int res = sqlite3_bind_blob(m_read_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
391  if (res != SQLITE_OK) {
392  LogPrintf("%s: Unable to bind statement: %s\n", __func__, sqlite3_errstr(res));
393  sqlite3_clear_bindings(m_read_stmt);
394  sqlite3_reset(m_read_stmt);
395  return false;
396  }
397  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 char* data = reinterpret_cast<const char*>(sqlite3_column_blob(m_read_stmt, 0));
409  int 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(CDataStream&& key, CDataStream&& 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  int res = sqlite3_bind_blob(stmt, 1, key.data(), key.size(), SQLITE_STATIC);
432  if (res != SQLITE_OK) {
433  LogPrintf("%s: Unable to bind key to statement: %s\n", __func__, sqlite3_errstr(res));
434  sqlite3_clear_bindings(stmt);
435  sqlite3_reset(stmt);
436  return false;
437  }
438  res = sqlite3_bind_blob(stmt, 2, value.data(), value.size(), SQLITE_STATIC);
439  if (res != SQLITE_OK) {
440  LogPrintf("%s: Unable to bind value to statement: %s\n", __func__, sqlite3_errstr(res));
441  sqlite3_clear_bindings(stmt);
442  sqlite3_reset(stmt);
443  return false;
444  }
445 
446  // Execute
447  res = sqlite3_step(stmt);
448  sqlite3_clear_bindings(stmt);
449  sqlite3_reset(stmt);
450  if (res != SQLITE_DONE) {
451  LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
452  }
453  return res == SQLITE_DONE;
454 }
455 
457 {
458  if (!m_database.m_db) return false;
460 
461  // Bind: leftmost parameter in statement is index 1
462  int res = sqlite3_bind_blob(m_delete_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
463  if (res != SQLITE_OK) {
464  LogPrintf("%s: Unable to bind statement: %s\n", __func__, sqlite3_errstr(res));
465  sqlite3_clear_bindings(m_delete_stmt);
466  sqlite3_reset(m_delete_stmt);
467  return false;
468  }
469 
470  // Execute
471  res = sqlite3_step(m_delete_stmt);
472  sqlite3_clear_bindings(m_delete_stmt);
473  sqlite3_reset(m_delete_stmt);
474  if (res != SQLITE_DONE) {
475  LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
476  }
477  return res == SQLITE_DONE;
478 }
479 
481 {
482  if (!m_database.m_db) return false;
484 
485  // Bind: leftmost parameter in statement is index 1
486  bool ret = false;
487  int res = sqlite3_bind_blob(m_read_stmt, 1, key.data(), key.size(), SQLITE_STATIC);
488  if (res == SQLITE_OK) {
489  res = sqlite3_step(m_read_stmt);
490  if (res == SQLITE_ROW) {
491  ret = true;
492  }
493  }
494 
495  sqlite3_clear_bindings(m_read_stmt);
496  sqlite3_reset(m_read_stmt);
497  return ret;
498 }
499 
501 {
503  if (!m_database.m_db) return false;
504  m_cursor_init = true;
505  return true;
506 }
507 
508 bool SQLiteBatch::ReadAtCursor(CDataStream& key, CDataStream& value, bool& complete)
509 {
510  complete = false;
511 
512  if (!m_cursor_init) return false;
513 
514  int res = sqlite3_step(m_cursor_stmt);
515  if (res == SQLITE_DONE) {
516  complete = true;
517  return true;
518  }
519  if (res != SQLITE_ROW) {
520  LogPrintf("SQLiteBatch::ReadAtCursor: Unable to execute cursor step: %s\n", sqlite3_errstr(res));
521  return false;
522  }
523 
524  // Leftmost column in result is index 0
525  const char* key_data = reinterpret_cast<const char*>(sqlite3_column_blob(m_cursor_stmt, 0));
526  int key_data_size = sqlite3_column_bytes(m_cursor_stmt, 0);
527  key.write(key_data, key_data_size);
528  const char* value_data = reinterpret_cast<const char*>(sqlite3_column_blob(m_cursor_stmt, 1));
529  int value_data_size = sqlite3_column_bytes(m_cursor_stmt, 1);
530  value.write(value_data, value_data_size);
531  return true;
532 }
533 
535 {
536  sqlite3_reset(m_cursor_stmt);
537  m_cursor_init = false;
538 }
539 
541 {
542  if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) == 0) return false;
543  int res = sqlite3_exec(m_database.m_db, "BEGIN TRANSACTION", nullptr, nullptr, nullptr);
544  if (res != SQLITE_OK) {
545  LogPrintf("SQLiteBatch: Failed to begin the transaction\n");
546  }
547  return res == SQLITE_OK;
548 }
549 
551 {
552  if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) != 0) return false;
553  int res = sqlite3_exec(m_database.m_db, "COMMIT TRANSACTION", nullptr, nullptr, nullptr);
554  if (res != SQLITE_OK) {
555  LogPrintf("SQLiteBatch: Failed to commit the transaction\n");
556  }
557  return res == SQLITE_OK;
558 }
559 
561 {
562  if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) != 0) return false;
563  int res = sqlite3_exec(m_database.m_db, "ROLLBACK TRANSACTION", nullptr, nullptr, nullptr);
564  if (res != SQLITE_OK) {
565  LogPrintf("SQLiteBatch: Failed to abort the transaction\n");
566  }
567  return res == SQLITE_OK;
568 }
569 
570 std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
571 {
572  try {
573  fs::path data_file = SQLiteDataFile(path);
574  auto db = MakeUnique<SQLiteDatabase>(data_file.parent_path(), data_file);
575  if (options.verify && !db->Verify(error)) {
577  return nullptr;
578  }
579  status = DatabaseStatus::SUCCESS;
580  return db;
581  } catch (const std::runtime_error& e) {
583  error = Untranslated(e.what());
584  return nullptr;
585  }
586 }
587 
589 {
590  return std::string(sqlite3_libversion());
591 }
sqlite3 * m_db
Definition: sqlite.h:113
bool TxnBegin() override
Definition: sqlite.cpp:540
void CloseCursor() override
Definition: sqlite.cpp:534
bool TxnCommit() override
Definition: sqlite.cpp:550
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
Definition: sqlite.cpp:294
assert(!tx.IsCoinBase())
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: sqlite.cpp:417
Bilingual messages:
Definition: translation.h:16
bool empty() const
Definition: translation.h:27
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
static Mutex g_sqlite_mutex
Definition: sqlite.cpp:22
static int g_sqlite_count GUARDED_BY(g_sqlite_mutex)=0
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:40
bool Rewrite(const char *skip=nullptr) override
Rewrite the entire database on disk.
Definition: sqlite.cpp:287
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by Boost&#39;s create_directories if the requested directory exists...
Definition: system.cpp:1024
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:166
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:202
void write(const char *pch, size_t nSize)
Definition: streams.h:435
void SetupSQLStatements()
Definition: sqlite.cpp:71
void Open() override
Open the database if it is not already opened.
Definition: sqlite.cpp:200
bool EraseKey(CDataStream &&key) override
Definition: sqlite.cpp:456
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:570
sqlite3_stmt * m_read_stmt
Definition: sqlite.h:23
bool verify
Definition: db.h:210
sqlite3_stmt * m_overwrite_stmt
Definition: sqlite.h:25
const bool m_mock
Definition: sqlite.h:58
SQLiteDatabase()=delete
#define LOCK(cs)
Definition: sync.h:232
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:57
const std::string m_dir_path
Definition: sqlite.h:60
bool HasKey(CDataStream &&key) override
Definition: sqlite.cpp:480
bool ReadAtCursor(CDataStream &key, CDataStream &value, bool &complete) override
Definition: sqlite.cpp:508
void Close() override
Definition: sqlite.cpp:345
static uint32_t ReadBE32(const unsigned char *ptr)
Definition: common.h:63
static constexpr int32_t WALLET_SCHEMA_VERSION
Definition: sqlite.cpp:20
An instance of this class represents one SQLite3 database.
Definition: sqlite.h:55
bool StartCursor() override
Definition: sqlite.cpp:500
SQLiteDatabase & m_database
Definition: sqlite.h:19
static void ErrorLogCallback(void *arg, int code, const char *msg)
Definition: sqlite.cpp:25
std::string SQLiteDatabaseVersion()
Definition: sqlite.cpp:588
const std::string m_file_path
Definition: sqlite.h:62
int flags
Definition: bitcoin-tx.cpp:512
bool Verify(bilingual_str &error)
Definition: sqlite.cpp:119
void Cleanup() noexcept
Definition: sqlite.cpp:106
sqlite3_stmt * m_cursor_stmt
Definition: sqlite.h:27
const CChainParams & Params()
Return the currently selected parameters.
fs::path SQLiteDataFile(const fs::path &path)
Definition: db.cpp:70
SQLiteBatch(SQLiteDatabase &database)
Definition: sqlite.cpp:336
sqlite3_stmt * m_delete_stmt
Definition: sqlite.h:26
bool TxnAbort() override
Definition: sqlite.cpp:560
void Close() override
Close the database.
Definition: sqlite.cpp:321
bool m_cursor_init
Definition: sqlite.h:21
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition: sqlite.cpp:384
DatabaseStatus
Definition: db.h:213
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a SQLiteBatch connected to this database.
Definition: sqlite.cpp:330
bool error(const char *fmt, const Args &... args)
Definition: system.h:52
sqlite3_stmt * m_insert_stmt
Definition: sqlite.h:24
An instance of this class represents one database.
Definition: db.h:104