Browse Source

Now resetting cached statements before executing a query on them (as well as resetting them at the close of a result set). There was an issue where if you used the same query twice without closing the result set of the first one, you would get results back from the previous query, or maybe an exhausted result set. Thanks to note173 on github for pointing out the problem.

ccgus 13 years ago
parent
commit
8afcebcb4a
3 changed files with 14 additions and 0 deletions
  1. 3 0
      CHANGES_AND_TODO_LIST.txt
  2. 2 0
      src/FMDatabase.m
  3. 9 0
      src/fmdb.m

+ 3 - 0
CHANGES_AND_TODO_LIST.txt

@@ -3,6 +3,9 @@ Zip, nada, zilch.  Got any ideas?
 
 If you would like to contribute some code- awesome!  I just ask that you make it conform to the coding conventions already set in here, and to add a couple of tests for your new code to fmdb.m.  And of course, the code should be of general use to more than just a couple of folks.  Send your patches to gus@flyingmeat.com.
 
+2012.12.17
+    Now resetting cached statements before executing a query on them (as well as resetting them at the close of a result set).  There was an issue where if you used the same query twice without closing the result set of the first one, you would get results back from the previous query, or maybe an exhausted result set.  Thanks to note173 on github for pointing out the problem.
+
 2012.12.13
     Changed up how the binding count is calculated when passing a dictionary for named parameter support.  Thanks to Samuel Chen for pointing out the problem.
 

+ 2 - 0
src/FMDatabase.m

@@ -527,6 +527,7 @@ - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arr
     if (_shouldCacheStatements) {
         statement = [self cachedStatementForQuery:sql];
         pStmt = statement ? [statement statement] : 0x00;
+        [statement reset];
     }
     
     int numberOfRetries = 0;
@@ -705,6 +706,7 @@ - (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArra
     if (_shouldCacheStatements) {
         cachedStmt = [self cachedStatementForQuery:sql];
         pStmt = cachedStmt ? [cachedStmt statement] : 0x00;
+        [cachedStmt reset];
     }
     
     int numberOfRetries = 0;

+ 9 - 0
src/fmdb.m

@@ -793,6 +793,15 @@ int main (int argc, const char * argv[]) {
     }
     
     
+    [db setShouldCacheStatements:true];
+    
+    [db executeUpdate:@"CREATE TABLE testCacheStatements(key INTEGER PRIMARY KEY, value INTEGER)"];
+    [db executeUpdate:@"INSERT INTO testCacheStatements (key, value) VALUES (1, 2)"];
+    [db executeUpdate:@"INSERT INTO testCacheStatements (key, value) VALUES (2, 4)"];
+    
+    FMDBQuickCheck([[db executeQuery:@"SELECT * FROM testCacheStatements WHERE key=1"] next]);
+    FMDBQuickCheck([[db executeQuery:@"SELECT * FROM testCacheStatements WHERE key=1"] next]);
+    
     [db close];