Skip to content

Commit

Permalink
Added a close method to the database queue
Browse files Browse the repository at this point in the history
  • Loading branch information
ccgus committed Nov 29, 2011
1 parent 3ee000b commit 1aea390
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/FMDatabaseQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
@class FMDatabase;

@interface FMDatabaseQueue : NSObject {
NSString *_path;
dispatch_queue_t _queue;
FMDatabase *_db;
}

@property (retain) NSString *path;

+ (id)databaseQueueWithPath:(NSString*)aPath;
- (id)initWithPath:(NSString*)aPath;
- (void)close;

- (void)inDatabase:(void (^)(FMDatabase *db))block;

Expand Down
46 changes: 35 additions & 11 deletions src/FMDatabaseQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

@implementation FMDatabaseQueue

@synthesize path = _path;

+ (id)databaseQueueWithPath:(NSString*)aPath {
return [[[self alloc] initWithPath:aPath] autorelease];
}
Expand All @@ -29,6 +31,8 @@ - (id)initWithPath:(NSString*)aPath {
return 0x00;
}

_path = [aPath retain];

_queue = dispatch_queue_create([[NSString stringWithFormat:@"fmdb.%@", self] UTF8String], NULL);
}

Expand All @@ -38,6 +42,7 @@ - (id)initWithPath:(NSString*)aPath {
- (void)dealloc {

[_db release];
[_path release];

if (_queue) {
dispatch_release(_queue);
Expand All @@ -47,9 +52,28 @@ - (void)dealloc {
[super dealloc];
}

- (void)inDatabase:(void (^)(FMDatabase *db))block {
- (void)close {
[_db close];
[_db release];
_db = 0x00;
}

- (FMDatabase*)db {
if (!_db) {
_db = [[FMDatabase databaseWithPath:_path] retain];
if (![_db open]) {
NSLog(@"FMDatabaseQueue could not reopen database for path %@", _path);
[_db release];
_db = 0x00;
return 0x00;
}
}

dispatch_sync(_queue, ^() { block(_db); });
return _db;
}

- (void)inDatabase:(void (^)(FMDatabase *db))block {
dispatch_sync(_queue, ^() { block([self db]); });
}

- (void)beginTransaction:(BOOL)useDeferred withBlock:(void (^)(FMDatabase *db, BOOL *rollback))block {
Expand All @@ -59,19 +83,19 @@ - (void)beginTransaction:(BOOL)useDeferred withBlock:(void (^)(FMDatabase *db, B
BOOL shouldRollback = NO;

if (useDeferred) {
[_db beginDeferredTransaction];
[[self db] beginDeferredTransaction];
}
else {
[_db beginTransaction];
[[self db] beginTransaction];
}

block(_db, &shouldRollback);
block([self db], &shouldRollback);

if (shouldRollback) {
[_db rollback];
[[self db] rollback];
}
else {
[_db commit];
[[self db] commit];
}

});
Expand All @@ -97,15 +121,15 @@ - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block {

BOOL shouldRollback = NO;

if ([_db startSavePointWithName:name error:&err]) {
if ([[self db] startSavePointWithName:name error:&err]) {

block(_db, &shouldRollback);
block([self db], &shouldRollback);

if (shouldRollback) {
[_db rollbackToSavePointWithName:name error:&err];
[[self db] rollbackToSavePointWithName:name error:&err];
}
else {
[_db releaseSavePointWithName:name error:&err];
[[self db] releaseSavePointWithName:name error:&err];
}

}
Expand Down
5 changes: 5 additions & 0 deletions src/fmdb.m
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,11 @@ int main (int argc, const char * argv[]) {
}];
});

[queue close];

[queue inDatabase:^(FMDatabase *db) {
FMDBQuickCheck([db executeUpdate:@"insert into likefoo values ('1')"]);
}];
}


Expand Down

0 comments on commit 1aea390

Please sign in to comment.