Bitcoin Core  22.99.0
P2P Digital Currency
sqlite.h
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 #ifndef BITCOIN_WALLET_SQLITE_H
6 #define BITCOIN_WALLET_SQLITE_H
7 
8 #include <wallet/db.h>
9 
10 #include <sqlite3.h>
11 
12 struct bilingual_str;
13 
14 namespace wallet {
15 class SQLiteDatabase;
16 
18 class SQLiteBatch : public DatabaseBatch
19 {
20 private:
22 
23  bool m_cursor_init = false;
24 
25  sqlite3_stmt* m_read_stmt{nullptr};
26  sqlite3_stmt* m_insert_stmt{nullptr};
27  sqlite3_stmt* m_overwrite_stmt{nullptr};
28  sqlite3_stmt* m_delete_stmt{nullptr};
29  sqlite3_stmt* m_cursor_stmt{nullptr};
30 
31  void SetupSQLStatements();
32 
33  bool ReadKey(CDataStream&& key, CDataStream& value) override;
34  bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite = true) override;
35  bool EraseKey(CDataStream&& key) override;
36  bool HasKey(CDataStream&& key) override;
37 
38 public:
39  explicit SQLiteBatch(SQLiteDatabase& database);
40  ~SQLiteBatch() override { Close(); }
41 
42  /* No-op. See comment on SQLiteDatabase::Flush */
43  void Flush() override {}
44 
45  void Close() override;
46 
47  bool StartCursor() override;
48  bool ReadAtCursor(CDataStream& key, CDataStream& value, bool& complete) override;
49  void CloseCursor() override;
50  bool TxnBegin() override;
51  bool TxnCommit() override;
52  bool TxnAbort() override;
53 };
54 
58 {
59 private:
60  const bool m_mock{false};
61 
62  const std::string m_dir_path;
63 
64  const std::string m_file_path;
65 
66  void Cleanup() noexcept;
67 
68 public:
69  SQLiteDatabase() = delete;
70 
72  SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, bool mock = false);
73 
74  ~SQLiteDatabase();
75 
76  bool Verify(bilingual_str& error);
77 
79  void Open() override;
80 
82  void Close() override;
83 
84  /* These functions are unused */
85  void AddRef() override { assert(false); }
86  void RemoveRef() override { assert(false); }
87 
89  bool Rewrite(const char* skip = nullptr) override;
90 
93  bool Backup(const std::string& dest) const override;
94 
103  void Flush() override {}
104  bool PeriodicFlush() override { return false; }
105  void ReloadDbEnv() override {}
106 
107  void IncrementUpdateCounter() override { ++nUpdateCounter; }
108 
109  std::string Filename() override { return m_file_path; }
110  std::string Format() override { return "sqlite"; }
111 
113  std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
114 
115  sqlite3* m_db{nullptr};
116 };
117 
118 std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
119 
120 std::string SQLiteDatabaseVersion();
121 } // namespace wallet
122 
123 #endif // BITCOIN_WALLET_SQLITE_H
wallet::SQLiteBatch::Close
void Close() override
Definition: sqlite.cpp:344
wallet::WalletDatabase
An instance of this class represents one database.
Definition: db.h:104
wallet::SQLiteDatabase::Format
std::string Format() override
Definition: sqlite.h:110
wallet::SQLiteBatch::TxnAbort
bool TxnAbort() override
Definition: sqlite.cpp:550
wallet::SQLiteBatch::m_cursor_init
bool m_cursor_init
Definition: sqlite.h:23
assert
assert(!tx.IsCoinBase())
wallet::SQLiteBatch::m_database
SQLiteDatabase & m_database
Definition: sqlite.h:21
wallet::SQLiteBatch::HasKey
bool HasKey(CDataStream &&key) override
Definition: sqlite.cpp:470
wallet::SQLiteDatabase
An instance of this class represents one SQLite3 database.
Definition: sqlite.h:57
wallet::SQLiteDatabase::IncrementUpdateCounter
void IncrementUpdateCounter() override
Definition: sqlite.h:107
wallet::DatabaseStatus
DatabaseStatus
Definition: db.h:213
wallet::SQLiteBatch::SetupSQLStatements
void SetupSQLStatements()
Definition: sqlite.cpp:105
wallet::DatabaseBatch
RAII class that provides access to a WalletDatabase.
Definition: db.h:25
bilingual_str
Bilingual messages:
Definition: translation.h:16
wallet::SQLiteBatch::StartCursor
bool StartCursor() override
Definition: sqlite.cpp:490
wallet::SQLiteDatabase::ReloadDbEnv
void ReloadDbEnv() override
Definition: sqlite.h:105
wallet::SQLiteBatch
RAII class that provides access to a WalletDatabase.
Definition: sqlite.h:18
wallet
Definition: node.h:38
wallet::SQLiteBatch::m_cursor_stmt
sqlite3_stmt * m_cursor_stmt
Definition: sqlite.h:29
wallet::SQLiteBatch::TxnBegin
bool TxnBegin() override
Definition: sqlite.cpp:530
wallet::SQLiteBatch::m_read_stmt
sqlite3_stmt * m_read_stmt
Definition: sqlite.h:25
wallet::SQLiteDatabase::Open
void Open() override
Open the database if it is not already opened.
Definition: sqlite.cpp:201
db.h
wallet::SQLiteDatabase::PeriodicFlush
bool PeriodicFlush() override
Definition: sqlite.h:104
wallet::SQLiteDatabase::RemoveRef
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: sqlite.h:86
wallet::SQLiteBatch::EraseKey
bool EraseKey(CDataStream &&key) override
Definition: sqlite.cpp:446
wallet::SQLiteDatabaseVersion
std::string SQLiteDatabaseVersion()
Definition: sqlite.cpp:578
wallet::SQLiteDatabase::MakeBatch
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a SQLiteBatch connected to this database.
Definition: sqlite.cpp:329
fs::path
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:29
wallet::SQLiteBatch::m_delete_stmt
sqlite3_stmt * m_delete_stmt
Definition: sqlite.h:28
wallet::SQLiteDatabase::m_mock
const bool m_mock
Definition: sqlite.h:60
wallet::SQLiteDatabase::m_db
sqlite3 * m_db
Definition: sqlite.h:115
wallet::SQLiteDatabase::m_dir_path
const std::string m_dir_path
Definition: sqlite.h:62
wallet::SQLiteDatabase::Filename
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: sqlite.h:109
wallet::WalletDatabase::nUpdateCounter
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:149
wallet::SQLiteDatabase::Flush
void Flush() override
No-ops.
Definition: sqlite.h:103
wallet::SQLiteDatabase::Rewrite
bool Rewrite(const char *skip=nullptr) override
Rewrite the entire database on disk.
Definition: sqlite.cpp:286
wallet::SQLiteBatch::m_overwrite_stmt
sqlite3_stmt * m_overwrite_stmt
Definition: sqlite.h:27
wallet::SQLiteBatch::ReadKey
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition: sqlite.cpp:374
wallet::SQLiteBatch::m_insert_stmt
sqlite3_stmt * m_insert_stmt
Definition: sqlite.h:26
wallet::SQLiteBatch::~SQLiteBatch
~SQLiteBatch() override
Definition: sqlite.h:40
wallet::SQLiteBatch::WriteKey
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: sqlite.cpp:407
wallet::SQLiteDatabase::AddRef
void AddRef() override
Indicate the a new database user has began using the database.
Definition: sqlite.h:85
wallet::SQLiteBatch::CloseCursor
void CloseCursor() override
Definition: sqlite.cpp:524
fs
Filesystem operations and types.
Definition: fs.h:19
wallet::SQLiteDatabase::Backup
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
Definition: sqlite.cpp:293
wallet::MakeSQLiteDatabase
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:560
wallet::SQLiteBatch::Flush
void Flush() override
Definition: sqlite.h:43
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:184
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
wallet::SQLiteBatch::ReadAtCursor
bool ReadAtCursor(CDataStream &key, CDataStream &value, bool &complete) override
Definition: sqlite.cpp:498
wallet::SQLiteDatabase::Close
void Close() override
Close the database.
Definition: sqlite.cpp:320
wallet::SQLiteDatabase::m_file_path
const std::string m_file_path
Definition: sqlite.h:64
wallet::SQLiteDatabase::Verify
bool Verify(bilingual_str &error)
Definition: sqlite.cpp:144
wallet::SQLiteBatch::TxnCommit
bool TxnCommit() override
Definition: sqlite.cpp:540
wallet::SQLiteBatch::SQLiteBatch
SQLiteBatch(SQLiteDatabase &database)
Definition: sqlite.cpp:335
wallet::SQLiteDatabase::Cleanup
void Cleanup() noexcept
Definition: sqlite.cpp:131