Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Closing a database now closes all open resultsets
Browse files Browse the repository at this point in the history
  • Loading branch information
davedelong committed Mar 12, 2011
1 parent 99112fe commit ae8622d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/FMDatabase.h
Expand Up @@ -15,6 +15,7 @@
int busyRetryTimeout;
BOOL shouldCacheStatements;
NSMutableDictionary *cachedStatements;
NSMutableSet *openResultSets;
}


Expand All @@ -28,6 +29,7 @@
- (BOOL)close;
- (BOOL)goodConnection;
- (void)clearCachedStatements;
- (void)closeOpenResultSets;

// encryption methods. You need to have purchased the sqlite encryption extensions for these to work.
- (BOOL)setKey:(NSString*)key;
Expand Down
25 changes: 25 additions & 0 deletions src/FMDatabase.m
Expand Up @@ -12,6 +12,7 @@ - (id)initWithPath:(NSString*)aPath {

if (self) {
databasePath = [aPath copy];
openResultSets = [[NSMutableSet alloc] init];
db = 0x00;
logsErrors = 0x00;
crashOnErrors = 0x00;
Expand All @@ -29,6 +30,7 @@ - (void)finalize {
- (void)dealloc {
[self close];

[openResultSets release];
[cachedStatements release];
[databasePath release];

Expand Down Expand Up @@ -76,6 +78,7 @@ - (BOOL)openWithFlags:(int)flags {
- (BOOL)close {

[self clearCachedStatements];
[self closeOpenResultSets];

if (!db) {
return YES;
Expand Down Expand Up @@ -118,6 +121,26 @@ - (void)clearCachedStatements {
[cachedStatements removeAllObjects];
}

- (void)closeOpenResultSets {
//Copy the set so we don't get mutation errors
NSSet *resultSets = [[openResultSets copy] autorelease];

NSEnumerator *e = [resultSets objectEnumerator];
NSValue *returnedResultSet = nil;

while((returnedResultSet = [e nextObject])) {
FMResultSet *rs = (FMResultSet *)[returnedResultSet pointerValue];
if ([rs respondsToSelector:@selector(close)]) {
[rs close];
}
}
}

- (void)resultSetDidClose:(FMResultSet *)resultSet {
NSValue *setValue = [NSValue valueWithNonretainedObject:resultSet];
[openResultSets removeObject:setValue];
}

- (FMStatement*)cachedStatementForQuery:(NSString*)query {
return [cachedStatements objectForKey:query];
}
Expand Down Expand Up @@ -383,6 +406,8 @@ - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arr
// the statement gets closed in rs's dealloc or [rs close];
rs = [FMResultSet resultSetWithStatement:statement usingParentDatabase:self];
[rs setQuery:sql];
NSValue *openResultSet = [NSValue valueWithNonretainedObject:rs];
[openResultSets addObject:openResultSet];

statement.useCount = statement.useCount + 1;

Expand Down
7 changes: 6 additions & 1 deletion src/FMResultSet.m
Expand Up @@ -2,6 +2,11 @@
#import "FMDatabase.h"
#import "unistd.h"

@interface FMDatabase ()
- (void)resultSetDidClose:(FMResultSet *)resultSet;
@end


@interface FMResultSet (Private)
- (NSMutableDictionary *)columnNameToIndexMap;
- (void)setColumnNameToIndexMap:(NSMutableDictionary *)value;
Expand Down Expand Up @@ -37,13 +42,13 @@ - (void)dealloc {
}

- (void)close {

[statement reset];
[statement release];
statement = nil;

// we don't need this anymore... (i think)
//[parentDB setInUse:NO];
[parentDB resultSetDidClose:self];
parentDB = nil;
}

Expand Down

0 comments on commit ae8622d

Please sign in to comment.