@@ -101,14 +101,21 @@ - (BOOL)_dbOpen {
101101 }
102102}
103103
104+ static void _finalizeStatement (const void *key, const void *value, void *context) {
105+ sqlite3_finalize ((sqlite3_stmt *)value);
106+ }
107+
104108- (BOOL )_dbClose {
105109 if (!_db) return YES ;
106110
107111 int result = 0 ;
108112 BOOL retry = NO ;
109113 BOOL stmtFinalized = NO ;
110114
111- if (_dbStmtCache) CFRelease (_dbStmtCache);
115+ if (_dbStmtCache) {
116+ CFDictionaryApplyFunction (_dbStmtCache, _finalizeStatement, NULL );
117+ CFRelease (_dbStmtCache);
118+ }
112119 _dbStmtCache = NULL ;
113120
114121 do {
@@ -153,7 +160,10 @@ - (BOOL)_dbInitialize {
153160- (void )_dbCheckpoint {
154161 if (![self _dbCheck ]) return ;
155162 // Cause a checkpoint to occur, merge `sqlite-wal` file to `sqlite` file.
156- sqlite3_wal_checkpoint (_db, NULL );
163+ int result = sqlite3_wal_checkpoint (_db, NULL );
164+ if (result != SQLITE_OK && _errorLogsEnabled) {
165+ NSLog (@" %s line:%d sqlite WAL checkpoint error (%d )" , __FUNCTION__, __LINE__, result);
166+ }
157167}
158168
159169- (BOOL )_dbExecute : (NSString *)sql {
@@ -181,7 +191,14 @@ - (sqlite3_stmt *)_dbPrepareStmt:(NSString *)sql {
181191 }
182192 CFDictionarySetValue (_dbStmtCache, (__bridge const void *)(sql), stmt);
183193 } else {
184- sqlite3_reset (stmt);
194+ if (sqlite3_stmt_busy (stmt)) {
195+ // just in case someone will forget to sqlite3_reset cached statement
196+ // causing WAL file lock
197+ if (_errorLogsEnabled) {
198+ NSLog (@" %s line:%d WARN: cached statement for query \" %@ \" was not reset." , __FUNCTION__, __LINE__, sql);
199+ }
200+ sqlite3_reset (stmt);
201+ }
185202 }
186203 return stmt;
187204}
@@ -223,6 +240,7 @@ - (BOOL)_dbSaveWithKey:(NSString *)key value:(NSData *)value fileName:(NSString
223240 sqlite3_bind_blob (stmt, 7 , extendedData.bytes , (int )extendedData.length , 0 );
224241
225242 int result = sqlite3_step (stmt);
243+ sqlite3_reset (stmt);
226244 if (result != SQLITE_DONE) {
227245 if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite insert error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
228246 return NO ;
@@ -237,6 +255,7 @@ - (BOOL)_dbUpdateAccessTimeWithKey:(NSString *)key {
237255 sqlite3_bind_int (stmt, 1 , (int )time (NULL ));
238256 sqlite3_bind_text (stmt, 2 , key.UTF8String , -1 , NULL );
239257 int result = sqlite3_step (stmt);
258+ sqlite3_reset (stmt);
240259 if (result != SQLITE_DONE) {
241260 if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite update error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
242261 return NO ;
@@ -273,6 +292,7 @@ - (BOOL)_dbDeleteItemWithKey:(NSString *)key {
273292 sqlite3_bind_text (stmt, 1 , key.UTF8String , -1 , NULL );
274293
275294 int result = sqlite3_step (stmt);
295+ sqlite3_reset (stmt);
276296 if (result != SQLITE_DONE) {
277297 if (_errorLogsEnabled) NSLog (@" %s line:%d db delete error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
278298 return NO ;
@@ -306,6 +326,7 @@ - (BOOL)_dbDeleteItemsWithSizeLargerThan:(int)size {
306326 if (!stmt) return NO ;
307327 sqlite3_bind_int (stmt, 1 , size);
308328 int result = sqlite3_step (stmt);
329+ sqlite3_reset (stmt);
309330 if (result != SQLITE_DONE) {
310331 if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite delete error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
311332 return NO ;
@@ -319,6 +340,7 @@ - (BOOL)_dbDeleteItemsWithTimeEarlierThan:(int)time {
319340 if (!stmt) return NO ;
320341 sqlite3_bind_int (stmt, 1 , time);
321342 int result = sqlite3_step (stmt);
343+ sqlite3_reset (stmt);
322344 if (result != SQLITE_DONE) {
323345 if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite delete error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
324346 return NO ;
@@ -364,6 +386,7 @@ - (YYKVStorageItem *)_dbGetItemWithKey:(NSString *)key excludeInlineData:(BOOL)e
364386 if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite query error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
365387 }
366388 }
389+ sqlite3_reset (stmt);
367390 return item;
368391}
369392
@@ -412,12 +435,14 @@ - (NSData *)_dbGetValueWithKey:(NSString *)key {
412435 if (result == SQLITE_ROW) {
413436 const void *inline_data = sqlite3_column_blob (stmt, 0 );
414437 int inline_data_bytes = sqlite3_column_bytes (stmt, 0 );
438+ sqlite3_reset (stmt);
415439 if (!inline_data || inline_data_bytes <= 0 ) return nil ;
416440 return [NSData dataWithBytes: inline_data length: inline_data_bytes];
417441 } else {
418442 if (result != SQLITE_DONE) {
419443 if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite query error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
420444 }
445+ sqlite3_reset (stmt);
421446 return nil ;
422447 }
423448}
@@ -431,13 +456,15 @@ - (NSString *)_dbGetFilenameWithKey:(NSString *)key {
431456 if (result == SQLITE_ROW) {
432457 char *filename = (char *)sqlite3_column_text (stmt, 0 );
433458 if (filename && *filename != 0 ) {
459+ sqlite3_reset (stmt);
434460 return [NSString stringWithUTF8String: filename];
435461 }
436462 } else {
437463 if (result != SQLITE_DONE) {
438464 if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite query error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
439465 }
440466 }
467+ sqlite3_reset (stmt);
441468 return nil ;
442469}
443470
@@ -496,6 +523,7 @@ - (NSMutableArray *)_dbGetFilenamesWithSizeLargerThan:(int)size {
496523 break ;
497524 }
498525 } while (1 );
526+ sqlite3_reset (stmt);
499527 return filenames;
500528}
501529
@@ -522,6 +550,7 @@ - (NSMutableArray *)_dbGetFilenamesWithTimeEarlierThan:(int)time {
522550 break ;
523551 }
524552 } while (1 );
553+ sqlite3_reset (stmt);
525554 return filenames;
526555}
527556
@@ -554,6 +583,7 @@ - (NSMutableArray *)_dbGetItemSizeInfoOrderByTimeAscWithLimit:(int)count {
554583 break ;
555584 }
556585 } while (1 );
586+ sqlite3_reset (stmt);
557587 return items;
558588}
559589
@@ -565,9 +595,12 @@ - (int)_dbGetItemCountWithKey:(NSString *)key {
565595 int result = sqlite3_step (stmt);
566596 if (result != SQLITE_ROW) {
567597 if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite query error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
598+ sqlite3_reset (stmt);
568599 return -1 ;
569600 }
570- return sqlite3_column_int (stmt, 0 );
601+ int count = sqlite3_column_int (stmt, 0 );
602+ sqlite3_reset (stmt);
603+ return count;
571604}
572605
573606- (int )_dbGetTotalItemSize {
@@ -577,9 +610,12 @@ - (int)_dbGetTotalItemSize {
577610 int result = sqlite3_step (stmt);
578611 if (result != SQLITE_ROW) {
579612 if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite query error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
613+ sqlite3_reset (stmt);
580614 return -1 ;
581615 }
582- return sqlite3_column_int (stmt, 0 );
616+ int size = sqlite3_column_int (stmt, 0 );
617+ sqlite3_reset (stmt);
618+ return size;
583619}
584620
585621- (int )_dbGetTotalItemCount {
@@ -591,7 +627,9 @@ - (int)_dbGetTotalItemCount {
591627 if (_errorLogsEnabled) NSLog (@" %s line:%d sqlite query error (%d ): %s " , __FUNCTION__, __LINE__, result, sqlite3_errmsg (_db));
592628 return -1 ;
593629 }
594- return sqlite3_column_int (stmt, 0 );
630+ int count = sqlite3_column_int (stmt, 0 );
631+ sqlite3_reset (stmt);
632+ return count;
595633}
596634
597635
0 commit comments