|
16 | 16 | #include <sqlite3.h>
|
17 | 17 | #include <stdint.h>
|
18 | 18 |
|
| 19 | +#include <utility> |
| 20 | +#include <vector> |
| 21 | + |
19 | 22 | static constexpr int32_t WALLET_SCHEMA_VERSION = 0;
|
20 | 23 |
|
21 | 24 | static Mutex g_sqlite_mutex;
|
@@ -69,30 +72,21 @@ SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_pa
|
69 | 72 |
|
70 | 73 | void SQLiteBatch::SetupSQLStatements()
|
71 | 74 | {
|
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 | + } |
96 | 90 | }
|
97 | 91 | }
|
98 | 92 | }
|
@@ -353,31 +347,22 @@ void SQLiteBatch::Close()
|
353 | 347 | }
|
354 | 348 |
|
355 | 349 | // 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; |
375 | 365 | }
|
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; |
381 | 366 | }
|
382 | 367 |
|
383 | 368 | bool SQLiteBatch::ReadKey(CDataStream&& key, CDataStream& value)
|
|
0 commit comments