@@ -57,6 +57,15 @@ static std::optional<int> ReadPragmaInteger(sqlite3* db, const std::string& key,
5757 return result;
5858}
5959
60+ static void SetPragma (sqlite3* db, const std::string& key, const std::string& value, const std::string& err_msg)
61+ {
62+ std::string stmt_text = strprintf (" PRAGMA %s = %s" , key, value);
63+ int ret = sqlite3_exec (db, stmt_text.c_str (), nullptr , nullptr , nullptr );
64+ if (ret != SQLITE_OK) {
65+ throw std::runtime_error (strprintf (" SQLiteDatabase: %s: %s\n " , err_msg, sqlite3_errstr (ret)));
66+ }
67+ }
68+
6069SQLiteDatabase::SQLiteDatabase (const fs::path& dir_path, const fs::path& file_path, bool mock)
6170 : WalletDatabase(), m_mock(mock), m_dir_path(dir_path.string()), m_file_path(file_path.string())
6271{
@@ -211,12 +220,9 @@ void SQLiteDatabase::Open()
211220
212221 // Acquire an exclusive lock on the database
213222 // First change the locking mode to exclusive
214- int ret = sqlite3_exec (m_db, " PRAGMA locking_mode = exclusive" , nullptr , nullptr , nullptr );
215- if (ret != SQLITE_OK) {
216- throw std::runtime_error (strprintf (" SQLiteDatabase: Unable to change database locking mode to exclusive: %s\n " , sqlite3_errstr (ret)));
217- }
223+ SetPragma (m_db, " locking_mode" , " exclusive" , " Unable to change database locking mode to exclusive" );
218224 // Now begin a transaction to acquire the exclusive lock. This lock won't be released until we close because of the exclusive locking mode.
219- ret = sqlite3_exec (m_db, " BEGIN EXCLUSIVE TRANSACTION" , nullptr , nullptr , nullptr );
225+ int ret = sqlite3_exec (m_db, " BEGIN EXCLUSIVE TRANSACTION" , nullptr , nullptr , nullptr );
220226 if (ret != SQLITE_OK) {
221227 throw std::runtime_error (" SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another bitcoind?\n " );
222228 }
@@ -226,18 +232,12 @@ void SQLiteDatabase::Open()
226232 }
227233
228234 // Enable fullfsync for the platforms that use it
229- ret = sqlite3_exec (m_db, " PRAGMA fullfsync = true" , nullptr , nullptr , nullptr );
230- if (ret != SQLITE_OK) {
231- throw std::runtime_error (strprintf (" SQLiteDatabase: Failed to enable fullfsync: %s\n " , sqlite3_errstr (ret)));
232- }
235+ SetPragma (m_db, " fullfsync" , " true" , " Failed to enable fullfsync" );
233236
234237 if (gArgs .GetBoolArg (" -unsafesqlitesync" , false )) {
235238 // Use normal synchronous mode for the journal
236239 LogPrintf (" WARNING SQLite is configured to not wait for data to be flushed to disk. Data loss and corruption may occur.\n " );
237- ret = sqlite3_exec (m_db, " PRAGMA synchronous = OFF" , nullptr , nullptr , nullptr );
238- if (ret != SQLITE_OK) {
239- throw std::runtime_error (strprintf (" SQLiteDatabase: Failed to set synchronous mode to OFF: %s\n " , sqlite3_errstr (ret)));
240- }
240+ SetPragma (m_db, " synchronous" , " OFF" , " Failed to set synchronous mode to OFF" );
241241 }
242242
243243 // Make the table for our key-value pairs
@@ -269,18 +269,12 @@ void SQLiteDatabase::Open()
269269
270270 // Set the application id
271271 uint32_t app_id = ReadBE32 (Params ().MessageStart ());
272- std::string set_app_id = strprintf (" PRAGMA application_id = %d" , static_cast <int32_t >(app_id));
273- ret = sqlite3_exec (m_db, set_app_id.c_str (), nullptr , nullptr , nullptr );
274- if (ret != SQLITE_OK) {
275- throw std::runtime_error (strprintf (" SQLiteDatabase: Failed to set the application id: %s\n " , sqlite3_errstr (ret)));
276- }
272+ SetPragma (m_db, " application_id" , strprintf (" %d" , static_cast <int32_t >(app_id)),
273+ " Failed to set the application id" );
277274
278275 // Set the user version
279- std::string set_user_ver = strprintf (" PRAGMA user_version = %d" , WALLET_SCHEMA_VERSION);
280- ret = sqlite3_exec (m_db, set_user_ver.c_str (), nullptr , nullptr , nullptr );
281- if (ret != SQLITE_OK) {
282- throw std::runtime_error (strprintf (" SQLiteDatabase: Failed to set the wallet schema version: %s\n " , sqlite3_errstr (ret)));
283- }
276+ SetPragma (m_db, " user_version" , strprintf (" %d" , WALLET_SCHEMA_VERSION),
277+ " Failed to set the wallet schema version" );
284278 }
285279}
286280
0 commit comments