From a72fc5fa0c9811ff5a57af90bf4440023c94ac5a Mon Sep 17 00:00:00 2001 From: Nsable-sh Date: Mon, 17 Aug 2026 10:50:00 -0300 Subject: wallet: add typed SQLite wallet records --- src/wallet_sqlite.cpp | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) (limited to 'src/wallet_sqlite.cpp') diff --git a/src/wallet_sqlite.cpp b/src/wallet_sqlite.cpp index ca81573..f4b4b37 100644 --- a/src/wallet_sqlite.cpp +++ b/src/wallet_sqlite.cpp @@ -156,7 +156,8 @@ bool CWalletDBSQLite::InitSchema(string& strError) bool CWalletDBSQLite::WriteRecord(const vector& vchKey, const vector& vchValue, - string& strError) + string& strError, + bool fOverwrite) { if (!pdb) { @@ -170,8 +171,9 @@ bool CWalletDBSQLite::WriteRecord(const vector& vchKey, } sqlite3_stmt* stmt = NULL; - const char* pszSql = - "INSERT OR REPLACE INTO wallet_records(key, value) VALUES(?, ?);"; + const char* pszSql = fOverwrite + ? "INSERT OR REPLACE INTO wallet_records(key, value) VALUES(?, ?);" + : "INSERT INTO wallet_records(key, value) VALUES(?, ?);"; int ret = sqlite3_prepare_v2(pdb, pszSql, -1, &stmt, NULL); if (ret != SQLITE_OK) { @@ -250,6 +252,41 @@ bool CWalletDBSQLite::ReadRecord(const vector& vchKey, return false; } +bool CWalletDBSQLite::EraseRecord(const vector& vchKey, + string& strError) +{ + if (!pdb) + { + strError = "SQLite wallet is not open"; + return false; + } + if (vchKey.empty()) + { + strError = "SQLite wallet record key is empty"; + return false; + } + + sqlite3_stmt* stmt = NULL; + const char* pszSql = "DELETE FROM wallet_records WHERE key = ?;"; + int ret = sqlite3_prepare_v2(pdb, pszSql, -1, &stmt, NULL); + if (ret != SQLITE_OK) + { + SetSQLiteError(pdb, "cannot prepare SQLite wallet erase", strError); + return false; + } + + ret = sqlite3_bind_blob(stmt, 1, &vchKey[0], (int)vchKey.size(), + SQLITE_TRANSIENT); + if (ret == SQLITE_OK) + ret = sqlite3_step(stmt); + + bool fOk = (ret == SQLITE_DONE); + if (!fOk) + SetSQLiteError(pdb, "cannot erase SQLite wallet record", strError); + sqlite3_finalize(stmt); + return fOk; +} + bool CWalletDBSQLite::ScanRecords(CWalletRecordVisitor& visitor, string& strError) { -- cgit v1.2.3