Bitcoin Core  24.1.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, const DatabaseOptions& options, 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};
117 };
118 
119 std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
120 
121 std::string SQLiteDatabaseVersion();
122 } // namespace wallet
123 
124 #endif // BITCOIN_WALLET_SQLITE_H
assert(!tx.IsCoinBase())
Bilingual messages:
Definition: translation.h:18
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:545
void IncrementUpdateCounter() override
Definition: sqlite.h:107
bool TxnCommit() override
Definition: sqlite.cpp:525
const std::string m_file_path
Definition: sqlite.h:64
void SetupSQLStatements()
Definition: sqlite.cpp:121
~SQLiteBatch() override
Definition: sqlite.h:40
SQLiteDatabase & m_database
Definition: sqlite.h:21
void CloseCursor() override
Definition: sqlite.cpp:509
std::string SQLiteDatabaseVersion()
Definition: sqlite.cpp:563
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:185
sqlite3_stmt * m_overwrite_stmt
Definition: sqlite.h:27
bool m_cursor_init
Definition: sqlite.h:23
std::string Format() override
Definition: sqlite.h:110
sqlite3_stmt * m_insert_stmt
Definition: sqlite.h:26
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:150
RAII class that provides access to a WalletDatabase.
Definition: db.h:26
Filesystem operations and types.
Definition: fs.h:20
bool EraseKey(CDataStream &&key) override
Definition: sqlite.cpp:444
bool Verify(bilingual_str &error)
Definition: sqlite.cpp:160
const bool m_mock
Definition: sqlite.h:60
bool Rewrite(const char *skip=nullptr) override
Rewrite the entire database on disk.
Definition: sqlite.cpp:302
bool TxnAbort() override
Definition: sqlite.cpp:535
void Flush() override
Definition: sqlite.h:43
void Open() override
Open the database if it is not already opened.
Definition: sqlite.cpp:217
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: sqlite.cpp:417
sqlite3_stmt * m_cursor_stmt
Definition: sqlite.h:29
void Close() override
Definition: sqlite.cpp:360
Definition: node.h:39
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a SQLiteBatch connected to this database.
Definition: sqlite.cpp:345
SQLiteBatch(SQLiteDatabase &database)
Definition: sqlite.cpp:351
DatabaseStatus
Definition: db.h:219
const std::string m_dir_path
Definition: sqlite.h:62
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: sqlite.h:109
bool HasKey(CDataStream &&key) override
Definition: sqlite.cpp:462
bool TxnBegin() override
Definition: sqlite.cpp:515
void AddRef() override
Indicate the a new database user has began using the database.
Definition: sqlite.h:85
void Cleanup() noexcept
Definition: sqlite.cpp:147
An instance of this class represents one SQLite3 database.
Definition: sqlite.h:57
RAII class that provides access to a WalletDatabase.
Definition: sqlite.h:18
bool StartCursor() override
Definition: sqlite.cpp:475
bool ReadAtCursor(CDataStream &key, CDataStream &value, bool &complete) override
Definition: sqlite.cpp:483
sqlite3_stmt * m_delete_stmt
Definition: sqlite.h:28
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed...
Definition: sqlite.h:86
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition: sqlite.cpp:390
void Flush() override
No-ops.
Definition: sqlite.h:103
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
Definition: sqlite.cpp:309
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
void ReloadDbEnv() override
Definition: sqlite.h:105
An instance of this class represents one database.
Definition: db.h:105
sqlite3_stmt * m_read_stmt
Definition: sqlite.h:25
void Close() override
Close the database.
Definition: sqlite.cpp:336
bool PeriodicFlush() override
Definition: sqlite.h:104