Skip to content

Commit c0160ea

Browse files
committed
Merge #21540: wallet: refactor: dedup sqlite statement preparations/deletions
ea19cc8 wallet: refactor: dedup sqlite statement deletions (Sebastian Falbesoner) 9a36709 wallet: refactor: dedup sqlite statement preparations (Sebastian Falbesoner) Pull request description: This refactoring PR deduplicates repeated SQLite statement preparation calls (`sqlite3_prepare_v2(...)`) / deletions (`sqlite3_finalize(...)`) and its surrounding logic by putting each prepared statement and its corresponding text representation into a ~std::map~ ~`std::array`~ `std::vector`. This should be more readable and less error-prone, e.g. in case an additional statement needs to be added in the future or the error handling has to be adapted. ACKs for top commit: achow101: ACK ea19cc8 meshcollider: utACK ea19cc8 Tree-SHA512: ced89869b2147e088e7a4cda2acbbdd4a806f66dbc2d6999953d0d702c0655aa53c0eb699cc7e5e3732f2d24206d577a9d9e1b5de7f439100dead2696ade1092
2 parents 41a8d2b + ea19cc8 commit c0160ea

File tree

1 file changed

+33
-48
lines changed

1 file changed

+33
-48
lines changed

src/wallet/sqlite.cpp

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include <sqlite3.h>
1717
#include <stdint.h>
1818

19+
#include <utility>
20+
#include <vector>
21+
1922
static constexpr int32_t WALLET_SCHEMA_VERSION = 0;
2023

2124
static Mutex g_sqlite_mutex;
@@ -69,30 +72,21 @@ SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_pa
6972

7073
void SQLiteBatch::SetupSQLStatements()
7174
{
72-
int res;
73-
if (!m_read_stmt) {
74-
if ((res = sqlite3_prepare_v2(m_database.m_db, "SELECT value FROM main WHERE key = ?", -1, &m_read_stmt, nullptr)) != SQLITE_OK) {
75-
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
76-
}
77-
}
78-
if (!m_insert_stmt) {
79-
if ((res = sqlite3_prepare_v2(m_database.m_db, "INSERT INTO main VALUES(?, ?)", -1, &m_insert_stmt, nullptr)) != SQLITE_OK) {
80-
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
81-
}
82-
}
83-
if (!m_overwrite_stmt) {
84-
if ((res = sqlite3_prepare_v2(m_database.m_db, "INSERT or REPLACE into main values(?, ?)", -1, &m_overwrite_stmt, nullptr)) != SQLITE_OK) {
85-
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
86-
}
87-
}
88-
if (!m_delete_stmt) {
89-
if ((res = sqlite3_prepare_v2(m_database.m_db, "DELETE FROM main WHERE key = ?", -1, &m_delete_stmt, nullptr)) != SQLITE_OK) {
90-
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
91-
}
92-
}
93-
if (!m_cursor_stmt) {
94-
if ((res = sqlite3_prepare_v2(m_database.m_db, "SELECT key, value FROM main", -1, &m_cursor_stmt, nullptr)) != SQLITE_OK) {
95-
throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements : %s\n", sqlite3_errstr(res)));
75+
const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{
76+
{&m_read_stmt, "SELECT value FROM main WHERE key = ?"},
77+
{&m_insert_stmt, "INSERT INTO main VALUES(?, ?)"},
78+
{&m_overwrite_stmt, "INSERT or REPLACE into main values(?, ?)"},
79+
{&m_delete_stmt, "DELETE FROM main WHERE key = ?"},
80+
{&m_cursor_stmt, "SELECT key, value FROM main"},
81+
};
82+
83+
for (const auto& [stmt_prepared, stmt_text] : statements) {
84+
if (*stmt_prepared == nullptr) {
85+
int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, stmt_prepared, nullptr);
86+
if (res != SQLITE_OK) {
87+
throw std::runtime_error(strprintf(
88+
"SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res)));
89+
}
9690
}
9791
}
9892
}
@@ -353,31 +347,22 @@ void SQLiteBatch::Close()
353347
}
354348

355349
// Free all of the prepared statements
356-
int ret = sqlite3_finalize(m_read_stmt);
357-
if (ret != SQLITE_OK) {
358-
LogPrintf("SQLiteBatch: Batch closed but could not finalize read statement: %s\n", sqlite3_errstr(ret));
359-
}
360-
ret = sqlite3_finalize(m_insert_stmt);
361-
if (ret != SQLITE_OK) {
362-
LogPrintf("SQLiteBatch: Batch closed but could not finalize insert statement: %s\n", sqlite3_errstr(ret));
363-
}
364-
ret = sqlite3_finalize(m_overwrite_stmt);
365-
if (ret != SQLITE_OK) {
366-
LogPrintf("SQLiteBatch: Batch closed but could not finalize overwrite statement: %s\n", sqlite3_errstr(ret));
367-
}
368-
ret = sqlite3_finalize(m_delete_stmt);
369-
if (ret != SQLITE_OK) {
370-
LogPrintf("SQLiteBatch: Batch closed but could not finalize delete statement: %s\n", sqlite3_errstr(ret));
371-
}
372-
ret = sqlite3_finalize(m_cursor_stmt);
373-
if (ret != SQLITE_OK) {
374-
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;
375365
}
376-
m_read_stmt = nullptr;
377-
m_insert_stmt = nullptr;
378-
m_overwrite_stmt = nullptr;
379-
m_delete_stmt = nullptr;
380-
m_cursor_stmt = nullptr;
381366
}
382367

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

0 commit comments

Comments
 (0)