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
12struct bilingual_str;
13class SQLiteDatabase;
14
17{
18private:
20
21 bool m_cursor_init = false;
22
23 sqlite3_stmt* m_read_stmt{nullptr};
24 sqlite3_stmt* m_insert_stmt{nullptr};
25 sqlite3_stmt* m_overwrite_stmt{nullptr};
26 sqlite3_stmt* m_delete_stmt{nullptr};
27 sqlite3_stmt* m_cursor_stmt{nullptr};
28
29 void SetupSQLStatements();
30
31 bool ReadKey(CDataStream&& key, CDataStream& value) override;
32 bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite = true) override;
33 bool EraseKey(CDataStream&& key) override;
34 bool HasKey(CDataStream&& key) override;
35
36public:
37 explicit SQLiteBatch(SQLiteDatabase& database);
38 ~SQLiteBatch() override { Close(); }
39
40 /* No-op. See comment on SQLiteDatabase::Flush */
41 void Flush() override {}
42
43 void Close() override;
44
45 bool StartCursor() override;
46 bool ReadAtCursor(CDataStream& key, CDataStream& value, bool& complete) override;
47 void CloseCursor() override;
48 bool TxnBegin() override;
49 bool TxnCommit() override;
50 bool TxnAbort() override;
51};
52
56{
57private:
58 const bool m_mock{false};
59
60 const std::string m_dir_path;
61
62 const std::string m_file_path;
63
64 void Cleanup() noexcept;
65
66public:
67 SQLiteDatabase() = delete;
68
70 SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, bool mock = false);
71
73
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
91 bool Backup(const std::string& dest) const override;
92
101 void Flush() override {}
102 bool PeriodicFlush() override { return false; }
103 void ReloadDbEnv() override {}
104
106
107 std::string Filename() override { return m_file_path; }
108 std::string Format() override { return "sqlite"; }
109
111 std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
112
113 sqlite3* m_db{nullptr};
114};
115
116std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
117
118std::string SQLiteDatabaseVersion();
119
120#endif // BITCOIN_WALLET_SQLITE_H
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:205
RAII class that provides access to a WalletDatabase.
Definition: db.h:25
RAII class that provides access to a WalletDatabase.
Definition: sqlite.h:17
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
Definition: sqlite.cpp:406
sqlite3_stmt * m_read_stmt
Definition: sqlite.h:23
bool TxnAbort() override
Definition: sqlite.cpp:549
bool m_cursor_init
Definition: sqlite.h:21
sqlite3_stmt * m_cursor_stmt
Definition: sqlite.h:27
void SetupSQLStatements()
Definition: sqlite.cpp:104
bool HasKey(CDataStream &&key) override
Definition: sqlite.cpp:469
sqlite3_stmt * m_insert_stmt
Definition: sqlite.h:24
sqlite3_stmt * m_overwrite_stmt
Definition: sqlite.h:25
bool StartCursor() override
Definition: sqlite.cpp:489
bool EraseKey(CDataStream &&key) override
Definition: sqlite.cpp:445
bool ReadAtCursor(CDataStream &key, CDataStream &value, bool &complete) override
Definition: sqlite.cpp:497
bool TxnBegin() override
Definition: sqlite.cpp:529
~SQLiteBatch() override
Definition: sqlite.h:38
void Close() override
Definition: sqlite.cpp:343
sqlite3_stmt * m_delete_stmt
Definition: sqlite.h:26
bool ReadKey(CDataStream &&key, CDataStream &value) override
Definition: sqlite.cpp:373
bool TxnCommit() override
Definition: sqlite.cpp:539
void Flush() override
Definition: sqlite.h:41
SQLiteDatabase & m_database
Definition: sqlite.h:19
void CloseCursor() override
Definition: sqlite.cpp:523
SQLiteBatch(SQLiteDatabase &database)
Definition: sqlite.cpp:334
An instance of this class represents one SQLite3 database.
Definition: sqlite.h:56
void Close() override
Close the database.
Definition: sqlite.cpp:319
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: sqlite.h:107
void Open() override
Open the database if it is not already opened.
Definition: sqlite.cpp:200
void ReloadDbEnv() override
Definition: sqlite.h:103
sqlite3 * m_db
Definition: sqlite.h:113
void IncrementUpdateCounter() override
Definition: sqlite.h:105
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: sqlite.h:84
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a SQLiteBatch connected to this database.
Definition: sqlite.cpp:328
std::string Format() override
Definition: sqlite.h:108
const std::string m_dir_path
Definition: sqlite.h:60
void Cleanup() noexcept
Definition: sqlite.cpp:130
bool Verify(bilingual_str &error)
Definition: sqlite.cpp:143
void AddRef() override
Indicate the a new database user has began using the database.
Definition: sqlite.h:83
void Flush() override
No-ops.
Definition: sqlite.h:101
const bool m_mock
Definition: sqlite.h:58
bool PeriodicFlush() override
Definition: sqlite.h:102
const std::string m_file_path
Definition: sqlite.h:62
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
Definition: sqlite.cpp:292
bool Rewrite(const char *skip=nullptr) override
Rewrite the entire database on disk.
Definition: sqlite.cpp:285
An instance of this class represents one database.
Definition: db.h:104
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:148
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:34
DatabaseStatus
Definition: db.h:212
Filesystem operations and types.
Definition: fs.h:19
std::string SQLiteDatabaseVersion()
Definition: sqlite.cpp:577
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:559
Bilingual messages:
Definition: translation.h:16
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
assert(!tx.IsCoinBase())