ソースを参照

Added a close method to the database queue

August Mueller 14 年 前
コミット
1aea390a82
3 ファイル変更44 行追加11 行削除
  1. 4 0
      src/FMDatabaseQueue.h
  2. 35 11
      src/FMDatabaseQueue.m
  3. 5 0
      src/fmdb.m

+ 4 - 0
src/FMDatabaseQueue.h

@@ -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;
 

+ 35 - 11
src/FMDatabaseQueue.m

@@ -11,6 +11,8 @@
 
 @implementation FMDatabaseQueue
 
+@synthesize path = _path;
+
 + (id)databaseQueueWithPath:(NSString*)aPath {
     return [[[self alloc] initWithPath:aPath] autorelease];
 }
@@ -29,6 +31,8 @@ - (id)initWithPath:(NSString*)aPath {
             return 0x00;
         }
         
+        _path = [aPath retain];
+        
         _queue = dispatch_queue_create([[NSString stringWithFormat:@"fmdb.%@", self] UTF8String], NULL);
 	}
     
@@ -38,6 +42,7 @@ - (id)initWithPath:(NSString*)aPath {
 - (void)dealloc {
     
     [_db release];
+    [_path release];
     
     if (_queue) {
         dispatch_release(_queue);
@@ -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 {
@@ -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];
         }
     
     });
@@ -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];
             }
             
         }

+ 5 - 0
src/fmdb.m

@@ -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')"]);
+        }];
     }