@@ -169,7 +169,10 @@ - (BOOL)_dbInitialize {
169
169
- (void )_dbCheckpoint {
170
170
if (![self _dbCheck ]) return ;
171
171
// Cause a checkpoint to occur, merge `sqlite-wal` file to `sqlite` file.
172
- sqlite3_wal_checkpoint (_db, NULL );
172
+ int result = sqlite3_wal_checkpoint (_db, NULL );
173
+ if (result != SQLITE_OK && _errorLogsEnabled) {
174
+ NSLog (@" %s line:%d sqlite WAL checkpoint error (%d )" , __FUNCTION__, __LINE__, result);
175
+ }
173
176
}
174
177
175
178
- (BOOL )_dbExecute : (NSString *)sql {
@@ -197,7 +200,14 @@ - (sqlite3_stmt *)_dbPrepareStmt:(NSString *)sql {
197
200
}
198
201
CFDictionarySetValue (_dbStmtCache, (__bridge const void *)(sql), stmt);
199
202
} else {
200
- sqlite3_reset (stmt);
203
+ if (sqlite3_stmt_busy (stmt)) {
204
+ // just in case someone will forget to sqlite3_reset cached statement
205
+ // causing WAL file lock
206
+ if (_errorLogsEnabled) {
207
+ NSLog (@" %s line:%d WARN: cached statement for query \" %@ \" was not reset." , __FUNCTION__, __LINE__, sql);
208
+ }
209
+ sqlite3_reset (stmt);
210
+ }
201
211
}
202
212
return stmt;
203
213
}
@@ -239,6 +249,7 @@ - (BOOL)_dbSaveWithKey:(NSString *)key value:(NSData *)value fileName:(NSString
239
249
sqlite3_bind_blob (stmt, 7 , extendedData.bytes , (int )extendedData.length , 0 );
240
250
241
251
int result = sqlite3_step (stmt);
252
+ sqlite3_reset (stmt);
242
253
if (result != SQLITE_DONE) {
243
254
if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite insert error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
244
255
return NO ;
@@ -253,6 +264,7 @@ - (BOOL)_dbUpdateAccessTimeWithKey:(NSString *)key {
253
264
sqlite3_bind_int (stmt, 1 , (int )time (NULL ));
254
265
sqlite3_bind_text (stmt, 2 , key.UTF8String , -1 , NULL );
255
266
int result = sqlite3_step (stmt);
267
+ sqlite3_reset (stmt);
256
268
if (result != SQLITE_DONE) {
257
269
if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite update error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
258
270
return NO ;
@@ -289,6 +301,7 @@ - (BOOL)_dbDeleteItemWithKey:(NSString *)key {
289
301
sqlite3_bind_text (stmt, 1 , key.UTF8String , -1 , NULL );
290
302
291
303
int result = sqlite3_step (stmt);
304
+ sqlite3_reset (stmt);
292
305
if (result != SQLITE_DONE) {
293
306
if (_errorLogsEnabled) NSLog (@" %s line:%d db delete error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
294
307
return NO ;
@@ -322,6 +335,7 @@ - (BOOL)_dbDeleteItemsWithSizeLargerThan:(int)size {
322
335
if (!stmt) return NO ;
323
336
sqlite3_bind_int (stmt, 1 , size);
324
337
int result = sqlite3_step (stmt);
338
+ sqlite3_reset (stmt);
325
339
if (result != SQLITE_DONE) {
326
340
if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite delete error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
327
341
return NO ;
@@ -335,6 +349,7 @@ - (BOOL)_dbDeleteItemsWithTimeEarlierThan:(int)time {
335
349
if (!stmt) return NO ;
336
350
sqlite3_bind_int (stmt, 1 , time );
337
351
int result = sqlite3_step (stmt);
352
+ sqlite3_reset (stmt);
338
353
if (result != SQLITE_DONE) {
339
354
if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite delete error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
340
355
return NO ;
@@ -380,6 +395,7 @@ - (YYKVStorageItem *)_dbGetItemWithKey:(NSString *)key excludeInlineData:(BOOL)e
380
395
if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite query error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
381
396
}
382
397
}
398
+ sqlite3_reset (stmt);
383
399
return item;
384
400
}
385
401
@@ -428,12 +444,14 @@ - (NSData *)_dbGetValueWithKey:(NSString *)key {
428
444
if (result == SQLITE_ROW) {
429
445
const void *inline_data = sqlite3_column_blob (stmt, 0 );
430
446
int inline_data_bytes = sqlite3_column_bytes (stmt, 0 );
447
+ sqlite3_reset (stmt);
431
448
if (!inline_data || inline_data_bytes <= 0 ) return nil ;
432
449
return [NSData dataWithBytes: inline_data length: inline_data_bytes];
433
450
} else {
434
451
if (result != SQLITE_DONE) {
435
452
if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite query error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
436
453
}
454
+ sqlite3_reset (stmt);
437
455
return nil ;
438
456
}
439
457
}
@@ -447,13 +465,15 @@ - (NSString *)_dbGetFilenameWithKey:(NSString *)key {
447
465
if (result == SQLITE_ROW) {
448
466
char *filename = (char *)sqlite3_column_text (stmt, 0 );
449
467
if (filename && *filename != 0 ) {
468
+ sqlite3_reset (stmt);
450
469
return [NSString stringWithUTF8String: filename];
451
470
}
452
471
} else {
453
472
if (result != SQLITE_DONE) {
454
473
if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite query error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
455
474
}
456
475
}
476
+ sqlite3_reset (stmt);
457
477
return nil ;
458
478
}
459
479
@@ -512,6 +532,7 @@ - (NSMutableArray *)_dbGetFilenamesWithSizeLargerThan:(int)size {
512
532
break ;
513
533
}
514
534
} while (1 );
535
+ sqlite3_reset (stmt);
515
536
return filenames;
516
537
}
517
538
@@ -538,6 +559,7 @@ - (NSMutableArray *)_dbGetFilenamesWithTimeEarlierThan:(int)time {
538
559
break ;
539
560
}
540
561
} while (1 );
562
+ sqlite3_reset (stmt);
541
563
return filenames;
542
564
}
543
565
@@ -570,6 +592,7 @@ - (NSMutableArray *)_dbGetItemSizeInfoOrderByTimeAscWithLimit:(int)count {
570
592
break ;
571
593
}
572
594
} while (1 );
595
+ sqlite3_reset (stmt);
573
596
return items;
574
597
}
575
598
@@ -581,9 +604,12 @@ - (int)_dbGetItemCountWithKey:(NSString *)key {
581
604
int result = sqlite3_step (stmt);
582
605
if (result != SQLITE_ROW) {
583
606
if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite query error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
607
+ sqlite3_reset (stmt);
584
608
return -1 ;
585
609
}
586
- return sqlite3_column_int (stmt, 0 );
610
+ int count = sqlite3_column_int (stmt, 0 );
611
+ sqlite3_reset (stmt);
612
+ return count;
587
613
}
588
614
589
615
- (int )_dbGetTotalItemSize {
@@ -593,9 +619,12 @@ - (int)_dbGetTotalItemSize {
593
619
int result = sqlite3_step (stmt);
594
620
if (result != SQLITE_ROW) {
595
621
if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite query error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
622
+ sqlite3_reset (stmt);
596
623
return -1 ;
597
624
}
598
- return sqlite3_column_int (stmt, 0 );
625
+ int size = sqlite3_column_int (stmt, 0 );
626
+ sqlite3_reset (stmt);
627
+ return size;
599
628
}
600
629
601
630
- (int )_dbGetTotalItemCount {
@@ -607,7 +636,9 @@ - (int)_dbGetTotalItemCount {
607
636
if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite query error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
608
637
return -1 ;
609
638
}
610
- return sqlite3_column_int (stmt, 0 );
639
+ int count = sqlite3_column_int (stmt, 0 );
640
+ sqlite3_reset (stmt);
641
+ return count;
611
642
}
612
643
613
644
0 commit comments