diff options
author | Ryan Ofsky <ryan@ofsky.org> | 2020-04-12 13:40:43 -0400 |
---|---|---|
committer | Ryan Ofsky <ryan@ofsky.org> | 2023-04-12 05:30:43 -0400 |
commit | 5938ad0bdb013953861c7cd15a95f00998a06f44 (patch) | |
tree | 5c760ab363b8c3ee305f1762766af2007cd65b00 /src/wallet/sqlite.cpp | |
parent | cae0608ad4b195652e0cfcc905e19b50197d43ab (diff) | |
download | bitcoin-5938ad0bdb013953861c7cd15a95f00998a06f44.tar.xz |
wallet: Add DatabaseBatch::ErasePrefix method
This new function is not used yet this commit, but next commit adds usages and
test coverage for both BDB and sqlite.
Diffstat (limited to 'src/wallet/sqlite.cpp')
-rw-r--r-- | src/wallet/sqlite.cpp | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/src/wallet/sqlite.cpp b/src/wallet/sqlite.cpp index d6259e095e..77e8a4e9c1 100644 --- a/src/wallet/sqlite.cpp +++ b/src/wallet/sqlite.cpp @@ -125,6 +125,7 @@ void SQLiteBatch::SetupSQLStatements() {&m_insert_stmt, "INSERT INTO main VALUES(?, ?)"}, {&m_overwrite_stmt, "INSERT or REPLACE into main values(?, ?)"}, {&m_delete_stmt, "DELETE FROM main WHERE key = ?"}, + {&m_delete_prefix_stmt, "DELETE FROM main WHERE instr(key, ?) = 1"}, }; for (const auto& [stmt_prepared, stmt_text] : statements) { @@ -375,6 +376,7 @@ void SQLiteBatch::Close() {&m_insert_stmt, "insert"}, {&m_overwrite_stmt, "overwrite"}, {&m_delete_stmt, "delete"}, + {&m_delete_prefix_stmt, "delete prefix"}, }; for (const auto& [stmt_prepared, stmt_description] : statements) { @@ -441,24 +443,34 @@ bool SQLiteBatch::WriteKey(DataStream&& key, DataStream&& value, bool overwrite) return res == SQLITE_DONE; } -bool SQLiteBatch::EraseKey(DataStream&& key) +bool SQLiteBatch::ExecStatement(sqlite3_stmt* stmt, Span<const std::byte> blob) { if (!m_database.m_db) return false; - assert(m_delete_stmt); + assert(stmt); // Bind: leftmost parameter in statement is index 1 - if (!BindBlobToStatement(m_delete_stmt, 1, key, "key")) return false; + if (!BindBlobToStatement(stmt, 1, blob, "key")) return false; // Execute - int res = sqlite3_step(m_delete_stmt); - sqlite3_clear_bindings(m_delete_stmt); - sqlite3_reset(m_delete_stmt); + int res = sqlite3_step(stmt); + sqlite3_clear_bindings(stmt); + sqlite3_reset(stmt); if (res != SQLITE_DONE) { LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res)); } return res == SQLITE_DONE; } +bool SQLiteBatch::EraseKey(DataStream&& key) +{ + return ExecStatement(m_delete_stmt, key); +} + +bool SQLiteBatch::ErasePrefix(Span<const std::byte> prefix) +{ + return ExecStatement(m_delete_prefix_stmt, prefix); +} + bool SQLiteBatch::HasKey(DataStream&& key) { if (!m_database.m_db) return false; |