diff options
author | Sebastian Falbesoner <sebastian.falbesoner@gmail.com> | 2021-03-28 14:52:10 +0200 |
---|---|---|
committer | Sebastian Falbesoner <sebastian.falbesoner@gmail.com> | 2021-04-02 15:47:11 +0200 |
commit | 9a3670930eaf6b495f81ef9c5f6e68883a3a2750 (patch) | |
tree | 5a5143d91481955ca780db28c678517f2d6388ac /src/wallet/sqlite.cpp | |
parent | e658b0e49bee257e197dd8ca37ee3ffa73092d92 (diff) |
wallet: refactor: dedup sqlite statement preparations
Diffstat (limited to 'src/wallet/sqlite.cpp')
-rw-r--r-- | src/wallet/sqlite.cpp | 42 |
1 files changed, 18 insertions, 24 deletions
diff --git a/src/wallet/sqlite.cpp b/src/wallet/sqlite.cpp index 975974cb6a..f7aa576956 100644 --- a/src/wallet/sqlite.cpp +++ b/src/wallet/sqlite.cpp @@ -16,6 +16,9 @@ #include <sqlite3.h> #include <stdint.h> +#include <utility> +#include <vector> + static constexpr int32_t WALLET_SCHEMA_VERSION = 0; static Mutex g_sqlite_mutex; @@ -69,30 +72,21 @@ SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_pa void SQLiteBatch::SetupSQLStatements() { - int res; - if (!m_read_stmt) { - if ((res = sqlite3_prepare_v2(m_database.m_db, "SELECT value FROM main WHERE key = ?", -1, &m_read_stmt, nullptr)) != SQLITE_OK) { - throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res))); - } - } - if (!m_insert_stmt) { - if ((res = sqlite3_prepare_v2(m_database.m_db, "INSERT INTO main VALUES(?, ?)", -1, &m_insert_stmt, nullptr)) != SQLITE_OK) { - throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res))); - } - } - if (!m_overwrite_stmt) { - if ((res = sqlite3_prepare_v2(m_database.m_db, "INSERT or REPLACE into main values(?, ?)", -1, &m_overwrite_stmt, nullptr)) != SQLITE_OK) { - throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res))); - } - } - if (!m_delete_stmt) { - if ((res = sqlite3_prepare_v2(m_database.m_db, "DELETE FROM main WHERE key = ?", -1, &m_delete_stmt, nullptr)) != SQLITE_OK) { - throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res))); - } - } - if (!m_cursor_stmt) { - if ((res = sqlite3_prepare_v2(m_database.m_db, "SELECT key, value FROM main", -1, &m_cursor_stmt, nullptr)) != SQLITE_OK) { - throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements : %s\n", sqlite3_errstr(res))); + const std::vector<std::pair<sqlite3_stmt**, const char*>> statements{ + {&m_read_stmt, "SELECT value FROM main WHERE key = ?"}, + {&m_insert_stmt, "INSERT INTO main VALUES(?, ?)"}, + {&m_overwrite_stmt, "INSERT or REPLACE into main values(?, ?)"}, + {&m_delete_stmt, "DELETE FROM main WHERE key = ?"}, + {&m_cursor_stmt, "SELECT key, value FROM main"}, + }; + + for (const auto& [stmt_prepared, stmt_text] : statements) { + if (*stmt_prepared == nullptr) { + int res = sqlite3_prepare_v2(m_database.m_db, stmt_text, -1, stmt_prepared, nullptr); + if (res != SQLITE_OK) { + throw std::runtime_error(strprintf( + "SQLiteDatabase: Failed to setup SQL statements: %s\n", sqlite3_errstr(res))); + } } } } |