@@ -37,6 +37,22 @@ static void ErrorLogCallback(void* arg, int code, const char* msg)
37
37
LogPrintf (" SQLite Error. Code: %d. Message: %s\n " , code, msg);
38
38
}
39
39
40
+ static bool BindBlobToStatement (sqlite3_stmt* stmt,
41
+ int index,
42
+ Span<const std::byte> blob,
43
+ const std::string& description)
44
+ {
45
+ int res = sqlite3_bind_blob (stmt, index, blob.data (), blob.size (), SQLITE_STATIC);
46
+ if (res != SQLITE_OK) {
47
+ LogPrintf (" Unable to bind %s to statement: %s\n " , description, sqlite3_errstr (res));
48
+ sqlite3_clear_bindings (stmt);
49
+ sqlite3_reset (stmt);
50
+ return false ;
51
+ }
52
+
53
+ return true ;
54
+ }
55
+
40
56
static std::optional<int > ReadPragmaInteger (sqlite3* db, const std::string& key, const std::string& description, bilingual_str& error)
41
57
{
42
58
std::string stmt_text = strprintf (" PRAGMA %s" , key);
@@ -377,14 +393,8 @@ bool SQLiteBatch::ReadKey(CDataStream&& key, CDataStream& value)
377
393
assert (m_read_stmt);
378
394
379
395
// Bind: leftmost parameter in statement is index 1
380
- int res = sqlite3_bind_blob (m_read_stmt, 1 , key.data (), key.size (), SQLITE_STATIC);
381
- if (res != SQLITE_OK) {
382
- LogPrintf (" %s: Unable to bind statement: %s\n " , __func__, sqlite3_errstr (res));
383
- sqlite3_clear_bindings (m_read_stmt);
384
- sqlite3_reset (m_read_stmt);
385
- return false ;
386
- }
387
- res = sqlite3_step (m_read_stmt);
396
+ if (!BindBlobToStatement (m_read_stmt, 1 , key, " key" )) return false ;
397
+ int res = sqlite3_step (m_read_stmt);
388
398
if (res != SQLITE_ROW) {
389
399
if (res != SQLITE_DONE) {
390
400
// SQLITE_DONE means "not found", don't log an error in that case.
@@ -418,23 +428,11 @@ bool SQLiteBatch::WriteKey(CDataStream&& key, CDataStream&& value, bool overwrit
418
428
419
429
// Bind: leftmost parameter in statement is index 1
420
430
// Insert index 1 is key, 2 is value
421
- int res = sqlite3_bind_blob (stmt, 1 , key.data (), key.size (), SQLITE_STATIC);
422
- if (res != SQLITE_OK) {
423
- LogPrintf (" %s: Unable to bind key to statement: %s\n " , __func__, sqlite3_errstr (res));
424
- sqlite3_clear_bindings (stmt);
425
- sqlite3_reset (stmt);
426
- return false ;
427
- }
428
- res = sqlite3_bind_blob (stmt, 2 , value.data (), value.size (), SQLITE_STATIC);
429
- if (res != SQLITE_OK) {
430
- LogPrintf (" %s: Unable to bind value to statement: %s\n " , __func__, sqlite3_errstr (res));
431
- sqlite3_clear_bindings (stmt);
432
- sqlite3_reset (stmt);
433
- return false ;
434
- }
431
+ if (!BindBlobToStatement (stmt, 1 , key, " key" )) return false ;
432
+ if (!BindBlobToStatement (stmt, 2 , value, " value" )) return false ;
435
433
436
434
// Execute
437
- res = sqlite3_step (stmt);
435
+ int res = sqlite3_step (stmt);
438
436
sqlite3_clear_bindings (stmt);
439
437
sqlite3_reset (stmt);
440
438
if (res != SQLITE_DONE) {
@@ -449,16 +447,10 @@ bool SQLiteBatch::EraseKey(CDataStream&& key)
449
447
assert (m_delete_stmt);
450
448
451
449
// Bind: leftmost parameter in statement is index 1
452
- int res = sqlite3_bind_blob (m_delete_stmt, 1 , key.data (), key.size (), SQLITE_STATIC);
453
- if (res != SQLITE_OK) {
454
- LogPrintf (" %s: Unable to bind statement: %s\n " , __func__, sqlite3_errstr (res));
455
- sqlite3_clear_bindings (m_delete_stmt);
456
- sqlite3_reset (m_delete_stmt);
457
- return false ;
458
- }
450
+ if (!BindBlobToStatement (m_delete_stmt, 1 , key, " key" )) return false ;
459
451
460
452
// Execute
461
- res = sqlite3_step (m_delete_stmt);
453
+ int res = sqlite3_step (m_delete_stmt);
462
454
sqlite3_clear_bindings (m_delete_stmt);
463
455
sqlite3_reset (m_delete_stmt);
464
456
if (res != SQLITE_DONE) {
@@ -473,18 +465,11 @@ bool SQLiteBatch::HasKey(CDataStream&& key)
473
465
assert (m_read_stmt);
474
466
475
467
// Bind: leftmost parameter in statement is index 1
476
- bool ret = false ;
477
- int res = sqlite3_bind_blob (m_read_stmt, 1 , key.data (), key.size (), SQLITE_STATIC);
478
- if (res == SQLITE_OK) {
479
- res = sqlite3_step (m_read_stmt);
480
- if (res == SQLITE_ROW) {
481
- ret = true ;
482
- }
483
- }
484
-
468
+ if (!BindBlobToStatement (m_read_stmt, 1 , key, " key" )) return false ;
469
+ int res = sqlite3_step (m_read_stmt);
485
470
sqlite3_clear_bindings (m_read_stmt);
486
471
sqlite3_reset (m_read_stmt);
487
- return ret ;
472
+ return res == SQLITE_ROW ;
488
473
}
489
474
490
475
bool SQLiteBatch::StartCursor ()
0 commit comments