From ea19cc844e780b29825b26aee321204be981a3ae Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Fri, 2 Apr 2021 15:22:21 +0200 Subject: wallet: refactor: dedup sqlite statement deletions --- src/wallet/sqlite.cpp | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) (limited to 'src/wallet/sqlite.cpp') diff --git a/src/wallet/sqlite.cpp b/src/wallet/sqlite.cpp index f7aa576956..91891c5fc2 100644 --- a/src/wallet/sqlite.cpp +++ b/src/wallet/sqlite.cpp @@ -347,31 +347,22 @@ void SQLiteBatch::Close() } // Free all of the prepared statements - int ret = sqlite3_finalize(m_read_stmt); - if (ret != SQLITE_OK) { - LogPrintf("SQLiteBatch: Batch closed but could not finalize read statement: %s\n", sqlite3_errstr(ret)); - } - ret = sqlite3_finalize(m_insert_stmt); - if (ret != SQLITE_OK) { - LogPrintf("SQLiteBatch: Batch closed but could not finalize insert statement: %s\n", sqlite3_errstr(ret)); - } - ret = sqlite3_finalize(m_overwrite_stmt); - if (ret != SQLITE_OK) { - LogPrintf("SQLiteBatch: Batch closed but could not finalize overwrite statement: %s\n", sqlite3_errstr(ret)); - } - ret = sqlite3_finalize(m_delete_stmt); - if (ret != SQLITE_OK) { - LogPrintf("SQLiteBatch: Batch closed but could not finalize delete statement: %s\n", sqlite3_errstr(ret)); - } - ret = sqlite3_finalize(m_cursor_stmt); - if (ret != SQLITE_OK) { - LogPrintf("SQLiteBatch: Batch closed but could not finalize cursor statement: %s\n", sqlite3_errstr(ret)); + const std::vector> statements{ + {&m_read_stmt, "read"}, + {&m_insert_stmt, "insert"}, + {&m_overwrite_stmt, "overwrite"}, + {&m_delete_stmt, "delete"}, + {&m_cursor_stmt, "cursor"}, + }; + + for (const auto& [stmt_prepared, stmt_description] : statements) { + int res = sqlite3_finalize(*stmt_prepared); + if (res != SQLITE_OK) { + LogPrintf("SQLiteBatch: Batch closed but could not finalize %s statement: %s\n", + stmt_description, sqlite3_errstr(res)); + } + *stmt_prepared = nullptr; } - m_read_stmt = nullptr; - m_insert_stmt = nullptr; - m_overwrite_stmt = nullptr; - m_delete_stmt = nullptr; - m_cursor_stmt = nullptr; } bool SQLiteBatch::ReadKey(CDataStream&& key, CDataStream& value) -- cgit v1.2.3