Bitcoin ABC  0.24.10
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 class SQLiteDatabase;
14 
16 class SQLiteBatch : public DatabaseBatch {
17 private:
19 
20  bool m_cursor_init = false;
21 
22  sqlite3_stmt *m_read_stmt{nullptr};
23  sqlite3_stmt *m_insert_stmt{nullptr};
24  sqlite3_stmt *m_overwrite_stmt{nullptr};
25  sqlite3_stmt *m_delete_stmt{nullptr};
26  sqlite3_stmt *m_cursor_stmt{nullptr};
27 
28  void SetupSQLStatements();
29 
30  bool ReadKey(CDataStream &&key, CDataStream &value) override;
31  bool WriteKey(CDataStream &&key, CDataStream &&value,
32  bool overwrite = true) override;
33  bool EraseKey(CDataStream &&key) override;
34  bool HasKey(CDataStream &&key) override;
35 
36 public:
37  explicit SQLiteBatch(SQLiteDatabase &database);
38  ~SQLiteBatch() override { Close(); }
39 
41  void Flush() override {}
42 
43  void Close() override;
44 
45  bool StartCursor() override;
46  bool ReadAtCursor(CDataStream &key, CDataStream &value,
47  bool &complete) override;
48  void CloseCursor() override;
49  bool TxnBegin() override;
50  bool TxnCommit() override;
51  bool TxnAbort() override;
52 };
53 
56 private:
57  const bool m_mock{false};
58 
59  const std::string m_dir_path;
60 
61  const std::string m_file_path;
62 
63  void Cleanup() noexcept;
64 
65 public:
66  SQLiteDatabase() = delete;
67 
69  SQLiteDatabase(const fs::path &dir_path, const fs::path &file_path,
70  bool mock = false);
71 
72  ~SQLiteDatabase();
73 
74  bool Verify(bilingual_str &error);
75 
77  void Open() override;
78 
80  void Close() override;
81 
82  /* These functions are unused */
83  void AddRef() override { assert(false); }
84  void RemoveRef() override { assert(false); }
85 
87  bool Rewrite(const char *skip = nullptr) override;
88 
90  bool Backup(const std::string &dest) const override;
91 
102  void Flush() override {}
103  bool PeriodicFlush() override { return false; }
104  void ReloadDbEnv() override {}
105 
106  void IncrementUpdateCounter() override { ++nUpdateCounter; }
107 
108  std::string Filename() override { return m_file_path; }
109 
111  std::unique_ptr<DatabaseBatch>
112  MakeBatch(bool flush_on_close = true) override;
113 
114  sqlite3 *m_db{nullptr};
115 };
116 
117 bool ExistsSQLiteDatabase(const fs::path &path);
118 std::unique_ptr<SQLiteDatabase>
119 MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options,
121 
122 std::string SQLiteDatabaseVersion();
123 
124 #endif // BITCOIN_WALLET_SQLITE_H
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
SQLiteBatch::~SQLiteBatch
~SQLiteBatch() override
Definition: sqlite.h:38
SQLiteBatch::m_read_stmt
sqlite3_stmt * m_read_stmt
Definition: sqlite.h:22
SQLiteDatabase::MakeBatch
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a SQLiteBatch connected to this database.
Definition: sqlite.cpp:328
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
SQLiteDatabase::m_db
sqlite3 * m_db
Definition: sqlite.h:114
SQLiteBatch::Close
void Close() override
Definition: sqlite.cpp:340
WalletDatabase
An instance of this class represents one database.
Definition: db.h:100
SQLiteDatabase::ReloadDbEnv
void ReloadDbEnv() override
Definition: sqlite.h:104
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
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
SQLiteBatch::SQLiteBatch
SQLiteBatch(SQLiteDatabase &database)
Definition: sqlite.cpp:333
SQLiteDatabase::AddRef
void AddRef() override
Indicate the a new database user has began using the database.
Definition: sqlite.h:83
SQLiteBatch::WriteKey
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: sqlite.cpp:429
WalletDatabase::nUpdateCounter
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:156
SQLiteBatch::ReadAtCursor
bool ReadAtCursor(CDataStream &key, CDataStream &value, bool &complete) override
Definition: sqlite.cpp:532
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
db.h
SQLiteBatch::m_delete_stmt
sqlite3_stmt * m_delete_stmt
Definition: sqlite.h:25
DatabaseBatch
RAII class that provides access to a WalletDatabase.
Definition: db.h:24
SQLiteBatch::EraseKey
bool EraseKey(CDataStream &&key) override
Definition: sqlite.cpp:473
SQLiteBatch::TxnBegin
bool TxnBegin() override
Definition: sqlite.cpp:569
SQLiteDatabase::IncrementUpdateCounter
void IncrementUpdateCounter() override
Definition: sqlite.h:106
MakeSQLiteDatabase
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:610
SQLiteBatch::m_insert_stmt
sqlite3_stmt * m_insert_stmt
Definition: sqlite.h:23
SQLiteDatabase::Close
void Close() override
Close the database.
Definition: sqlite.cpp:318
SQLiteDatabaseVersion
std::string SQLiteDatabaseVersion()
Definition: sqlite.cpp:627
SQLiteBatch::TxnAbort
bool TxnAbort() override
Definition: sqlite.cpp:593
SQLiteBatch::CloseCursor
void CloseCursor() override
Definition: sqlite.cpp:564
fs
Filesystem operations and types.
Definition: fs.h:19
SQLiteBatch::HasKey
bool HasKey(CDataStream &&key) override
Definition: sqlite.cpp:501
DatabaseStatus
DatabaseStatus
Definition: db.h:229
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:84
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
SQLiteDatabase::PeriodicFlush
bool PeriodicFlush() override
Definition: sqlite.h:103
SQLiteBatch
RAII class that provides access to a WalletDatabase.
Definition: sqlite.h:16
SQLiteBatch::Flush
void Flush() override
No-op.
Definition: sqlite.h:41
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
SQLiteDatabase::Filename
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: sqlite.h:108
SQLiteDatabase::Cleanup
void Cleanup() noexcept
Definition: sqlite.cpp:122
ExistsSQLiteDatabase
bool ExistsSQLiteDatabase(const fs::path &path)
Definition: sqlite.cpp:605
SQLiteDatabase::Flush
void Flush() override
No-ops.
Definition: sqlite.h:102