Branch data Line data Source code
# 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 : : # 17 : : /** RAII class that provides access to a WalletDatabase */ # 18 : : class SQLiteBatch : public DatabaseBatch # 19 : : { # 20 : : private: # 21 : : SQLiteDatabase& m_database; # 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 : 83232 : ~SQLiteBatch() override { Close(); } # 41 : : # 42 : : /* No-op. See comment on SQLiteDatabase::Flush */ # 43 : 46 : 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 : : # 55 : : /** An instance of this class represents one SQLite3 database. # 56 : : **/ # 57 : : class SQLiteDatabase : public WalletDatabase # 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 : : # 71 : : /** Create DB handle to real database */ # 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 : : # 78 : : /** Open the database if it is not already opened */ # 79 : : void Open() override; # 80 : : # 81 : : /** Close the database */ # 82 : : void Close() override; # 83 : : # 84 : : /* These functions are unused */ # 85 : 0 : void AddRef() override { assert(false); } # 86 : 0 : void RemoveRef() override { assert(false); } # 87 : : # 88 : : /** Rewrite the entire database on disk */ # 89 : : bool Rewrite(const char* skip = nullptr) override; # 90 : : # 91 : : /** Back up the entire database to a file. # 92 : : */ # 93 : : bool Backup(const std::string& dest) const override; # 94 : : # 95 : : /** No-ops # 96 : : * # 97 : : * SQLite always flushes everything to the database file after each transaction # 98 : : * (each Read/Write/Erase that we do is its own transaction unless we called # 99 : : * TxnBegin) so there is no need to have Flush or Periodic Flush. # 100 : : * # 101 : : * There is no DB env to reload, so ReloadDbEnv has nothing to do # 102 : : */ # 103 : 549 : void Flush() override {} # 104 : 3140 : bool PeriodicFlush() override { return false; } # 105 : 15 : void ReloadDbEnv() override {} # 106 : : # 107 : 94846 : void IncrementUpdateCounter() override { ++nUpdateCounter; } # 108 : : # 109 : 304 : std::string Filename() override { return m_file_path; } # 110 : 347 : std::string Format() override { return "sqlite"; } # 111 : : # 112 : : /** Make a SQLiteBatch connected to this database */ # 113 : : std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override; # 114 : : # 115 : : sqlite3* m_db{nullptr}; # 116 : : bool m_use_unsafe_sync; # 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