@@ -57,6 +57,15 @@ static std::optional<int> ReadPragmaInteger(sqlite3* db, const std::string& key,
57
57
return result;
58
58
}
59
59
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
+
60
69
SQLiteDatabase::SQLiteDatabase (const fs::path& dir_path, const fs::path& file_path, bool mock)
61
70
: WalletDatabase(), m_mock(mock), m_dir_path(dir_path.string()), m_file_path(file_path.string())
62
71
{
@@ -211,12 +220,9 @@ void SQLiteDatabase::Open()
211
220
212
221
// Acquire an exclusive lock on the database
213
222
// 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" );
218
224
// 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 );
220
226
if (ret != SQLITE_OK) {
221
227
throw std::runtime_error (" SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another bitcoind?\n " );
222
228
}
@@ -226,18 +232,12 @@ void SQLiteDatabase::Open()
226
232
}
227
233
228
234
// 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" );
233
236
234
237
if (gArgs .GetBoolArg (" -unsafesqlitesync" , false )) {
235
238
// Use normal synchronous mode for the journal
236
239
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" );
241
241
}
242
242
243
243
// Make the table for our key-value pairs
@@ -269,18 +269,12 @@ void SQLiteDatabase::Open()
269
269
270
270
// Set the application id
271
271
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" );
277
274
278
275
// 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" );
284
278
}
285
279
}
286
280
0 commit comments