@@ -125,6 +125,7 @@ void SQLiteBatch::SetupSQLStatements()
125125 {&m_insert_stmt, " INSERT INTO main VALUES(?, ?)" },
126126 {&m_overwrite_stmt, " INSERT or REPLACE into main values(?, ?)" },
127127 {&m_delete_stmt, " DELETE FROM main WHERE key = ?" },
128+ {&m_delete_prefix_stmt, " DELETE FROM main WHERE instr(key, ?) = 1" },
128129 };
129130
130131 for (const auto & [stmt_prepared, stmt_text] : statements) {
@@ -375,6 +376,7 @@ void SQLiteBatch::Close()
375376 {&m_insert_stmt, " insert" },
376377 {&m_overwrite_stmt, " overwrite" },
377378 {&m_delete_stmt, " delete" },
379+ {&m_delete_prefix_stmt, " delete prefix" },
378380 };
379381
380382 for (const auto & [stmt_prepared, stmt_description] : statements) {
@@ -441,24 +443,34 @@ bool SQLiteBatch::WriteKey(DataStream&& key, DataStream&& value, bool overwrite)
441443 return res == SQLITE_DONE;
442444}
443445
444- bool SQLiteBatch::EraseKey (DataStream&& key )
446+ bool SQLiteBatch::ExecStatement (sqlite3_stmt* stmt, Span< const std::byte> blob )
445447{
446448 if (!m_database.m_db ) return false ;
447- assert (m_delete_stmt );
449+ assert (stmt );
448450
449451 // Bind: leftmost parameter in statement is index 1
450- if (!BindBlobToStatement (m_delete_stmt , 1 , key , " key" )) return false ;
452+ if (!BindBlobToStatement (stmt , 1 , blob , " key" )) return false ;
451453
452454 // Execute
453- int res = sqlite3_step (m_delete_stmt );
454- sqlite3_clear_bindings (m_delete_stmt );
455- sqlite3_reset (m_delete_stmt );
455+ int res = sqlite3_step (stmt );
456+ sqlite3_clear_bindings (stmt );
457+ sqlite3_reset (stmt );
456458 if (res != SQLITE_DONE) {
457459 LogPrintf (" %s: Unable to execute statement: %s\n " , __func__, sqlite3_errstr (res));
458460 }
459461 return res == SQLITE_DONE;
460462}
461463
464+ bool SQLiteBatch::EraseKey (DataStream&& key)
465+ {
466+ return ExecStatement (m_delete_stmt, key);
467+ }
468+
469+ bool SQLiteBatch::ErasePrefix (Span<const std::byte> prefix)
470+ {
471+ return ExecStatement (m_delete_prefix_stmt, prefix);
472+ }
473+
462474bool SQLiteBatch::HasKey (DataStream&& key)
463475{
464476 if (!m_database.m_db ) return false ;
0 commit comments