Skip to content

Commit ea19cc8

Browse files
committed
wallet: refactor: dedup sqlite statement deletions
1 parent 9a36709 commit ea19cc8

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

src/wallet/sqlite.cpp

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -347,31 +347,22 @@ void SQLiteBatch::Close()
347347
}
348348

349349
// Free all of the prepared statements
350-
int ret = sqlite3_finalize(m_read_stmt);
351-
if (ret != SQLITE_OK) {
352-
LogPrintf("SQLiteBatch: Batch closed but could not finalize read statement: %s\n", sqlite3_errstr(ret));
353-
}
354-
ret = sqlite3_finalize(m_insert_stmt);
355-
if (ret != SQLITE_OK) {
356-
LogPrintf("SQLiteBatch: Batch closed but could not finalize insert statement: %s\n", sqlite3_errstr(ret));
357-
}
358-
ret = sqlite3_finalize(m_overwrite_stmt);
359-
if (ret != SQLITE_OK) {
360-
LogPrintf("SQLiteBatch: Batch closed but could not finalize overwrite statement: %s\n", sqlite3_errstr(ret));
361-
}
362-
ret = sqlite3_finalize(m_delete_stmt);
363-
if (ret != SQLITE_OK) {
364-
LogPrintf("SQLiteBatch: Batch closed but could not finalize delete statement: %s\n", sqlite3_errstr(ret));
365-
}
366-
ret = sqlite3_finalize(m_cursor_stmt);
367-
if (ret != SQLITE_OK) {
368-
LogPrintf("SQLiteBatch: Batch closed but could not finalize cursor statement: %s\n", sqlite3_errstr(ret));
350+
const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{
351+
{&m_read_stmt, "read"},
352+
{&m_insert_stmt, "insert"},
353+
{&m_overwrite_stmt, "overwrite"},
354+
{&m_delete_stmt, "delete"},
355+
{&m_cursor_stmt, "cursor"},
356+
};
357+
358+
for (const auto& [stmt_prepared, stmt_description] : statements) {
359+
int res = sqlite3_finalize(*stmt_prepared);
360+
if (res != SQLITE_OK) {
361+
LogPrintf("SQLiteBatch: Batch closed but could not finalize %s statement: %s\n",
362+
stmt_description, sqlite3_errstr(res));
363+
}
364+
*stmt_prepared = nullptr;
369365
}
370-
m_read_stmt = nullptr;
371-
m_insert_stmt = nullptr;
372-
m_overwrite_stmt = nullptr;
373-
m_delete_stmt = nullptr;
374-
m_cursor_stmt = nullptr;
375366
}
376367

377368
bool SQLiteBatch::ReadKey(CDataStream&& key, CDataStream& value)

0 commit comments

Comments
 (0)