Bitcoin Core  24.99.0
P2P Digital Currency
sqlite.h
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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 {
19 public:
20  sqlite3_stmt* m_cursor_stmt{nullptr};
21 
22  explicit SQLiteCursor() {}
23  ~SQLiteCursor() override;
24 
25  Status Next(DataStream& key, DataStream& value) override;
26 };
27 
29 class SQLiteBatch : public DatabaseBatch
30 {
31 private:
33 
34  sqlite3_stmt* m_read_stmt{nullptr};
35  sqlite3_stmt* m_insert_stmt{nullptr};
36  sqlite3_stmt* m_overwrite_stmt{nullptr};
37  sqlite3_stmt* m_delete_stmt{nullptr};
38 
39  void SetupSQLStatements();
40 
41  bool ReadKey(DataStream&& key, DataStream& value) override;
42  bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override;
43  bool EraseKey(DataStream&& key) override;
44  bool HasKey(DataStream&& key) override;
45 
46 public:
47  explicit SQLiteBatch(SQLiteDatabase& database);
48  ~SQLiteBatch() override { Close(); }
49 
50  /* No-op. See comment on SQLiteDatabase::Flush */
51  void Flush() override {}
52 
53  void Close() override;
54 
55  std::unique_ptr<DatabaseCursor> GetNewCursor() override;
56  bool TxnBegin() override;
57  bool TxnCommit() override;
58  bool TxnAbort() override;
59 };
60 
64 {
65 private:
66  const bool m_mock{false};
67 
68  const std::string m_dir_path;
69 
70  const std::string m_file_path;
71 
72  void Cleanup() noexcept;
73 
74 public:
75  SQLiteDatabase() = delete;
76 
78  SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, const DatabaseOptions& options, bool mock = false);
79 
80  ~SQLiteDatabase();
81 
82  bool Verify(bilingual_str& error);
83 
85  void Open() override;
86 
88  void Close() override;
89 
90  /* These functions are unused */
91  void AddRef() override { assert(false); }
92  void RemoveRef() override { assert(false); }
93 
95  bool Rewrite(const char* skip = nullptr) override;
96 
99  bool Backup(const std::string& dest) const override;
100 
109  void Flush() override {}
110  bool PeriodicFlush() override { return false; }
111  void ReloadDbEnv() override {}
112 
113  void IncrementUpdateCounter() override { ++nUpdateCounter; }
114 
115  std::string Filename() override { return m_file_path; }
116  std::string Format() override { return "sqlite"; }
117 
119  std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
120 
121  sqlite3* m_db{nullptr};
123 };
124 
125 std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
126 
127 std::string SQLiteDatabaseVersion();
128 } // namespace wallet
129 
130 #endif // BITCOIN_WALLET_SQLITE_H
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:186
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
RAII class that provides access to a WalletDatabase.
Definition: db.h:46
RAII class that provides access to a WalletDatabase.
Definition: sqlite.h:30
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: sqlite.cpp:388
bool TxnCommit() override
Definition: sqlite.cpp:529
SQLiteBatch(SQLiteDatabase &database)
Definition: sqlite.cpp:350
bool HasKey(DataStream &&key) override
Definition: sqlite.cpp:460
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: sqlite.cpp:504
bool EraseKey(DataStream &&key) override
Definition: sqlite.cpp:442
sqlite3_stmt * m_delete_stmt
Definition: sqlite.h:37
bool TxnBegin() override
Definition: sqlite.cpp:519
~SQLiteBatch() override
Definition: sqlite.h:48
sqlite3_stmt * m_read_stmt
Definition: sqlite.h:34
void Close() override
Definition: sqlite.cpp:359
sqlite3_stmt * m_overwrite_stmt
Definition: sqlite.h:36
void Flush() override
Definition: sqlite.h:51
void SetupSQLStatements()
Definition: sqlite.cpp:121
sqlite3_stmt * m_insert_stmt
Definition: sqlite.h:35
SQLiteDatabase & m_database
Definition: sqlite.h:32
bool TxnAbort() override
Definition: sqlite.cpp:539
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: sqlite.cpp:415
~SQLiteCursor() override
Definition: sqlite.cpp:494
Status Next(DataStream &key, DataStream &value) override
Definition: sqlite.cpp:473
sqlite3_stmt * m_cursor_stmt
Definition: sqlite.h:20
An instance of this class represents one SQLite3 database.
Definition: sqlite.h:64
void Flush() override
No-ops.
Definition: sqlite.h:109
std::string Format() override
Definition: sqlite.h:116
bool PeriodicFlush() override
Definition: sqlite.h:110
void IncrementUpdateCounter() override
Definition: sqlite.h:113
void Open() override
Open the database if it is not already opened.
Definition: sqlite.cpp:216
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: sqlite.h:115
void Cleanup() noexcept
Definition: sqlite.cpp:146
bool Rewrite(const char *skip=nullptr) override
Rewrite the entire database on disk.
Definition: sqlite.cpp:301
const bool m_mock
Definition: sqlite.h:66
void Close() override
Close the database.
Definition: sqlite.cpp:335
void ReloadDbEnv() override
Definition: sqlite.h:111
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
Definition: sqlite.cpp:308
const std::string m_dir_path
Definition: sqlite.h:68
void AddRef() override
Indicate the a new database user has began using the database.
Definition: sqlite.h:91
const std::string m_file_path
Definition: sqlite.h:70
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a SQLiteBatch connected to this database.
Definition: sqlite.cpp:344
bool Verify(bilingual_str &error)
Definition: sqlite.cpp:159
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: sqlite.h:92
An instance of this class represents one database.
Definition: db.h:123
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:167
Filesystem operations and types.
Definition: fs.h:20
Definition: node.h:39
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:549
std::string SQLiteDatabaseVersion()
Definition: sqlite.cpp:567
DatabaseStatus
Definition: db.h:239
Bilingual messages:
Definition: translation.h:18
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
assert(!tx.IsCoinBase())