Browse Source

Committing some code that I know I'm going to delete for something completely different.

August Mueller 9 years ago
parent
commit
6fcbcb8a06
2 changed files with 57 additions and 14 deletions
  1. 36 6
      src/fmdb/FMDatabase.m
  2. 21 8
      src/sample/main.m

+ 36 - 6
src/fmdb/FMDatabase.m

@@ -1291,8 +1291,10 @@ - (void)makeFunctionNamed:(NSString*)name maximumArguments:(int)count withBlock:
 }
 
 - (FMDBQ*)u:(NSString*)sql, ... {
-    FMDBQ *q = [FMDBQ new];
     
+    #pragma message "FIXME: this will go kabom if it's not on a queue."
+    
+    FMDBQ *q = [FMDBQ new];
     
     va_list args;
     va_start(args, sql);
@@ -1315,6 +1317,7 @@ - (FMDBQ*)u:(NSString*)sql, ... {
         
         sqlite3_finalize(pStmt);
         
+        return nil;
     }
     
     int idx = 0;
@@ -1384,29 +1387,56 @@ - (NSString*)description {
 
 @implementation FMDBQ
 
-- (void)executeUpdateInBackground:(void (^)(NSError *error))callback {
+- (void)executeUpdateSync:(BOOL)isSync callback:(void (^)(NSError *))callback {
     
+    static dispatch_queue_t FMDBQ_queue;
     
-    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        FMDBQ_queue = dispatch_queue_create([[NSString stringWithFormat:@"fmdb.%@", self] UTF8String], DISPATCH_QUEUE_SERIAL);
+    });
+    
+    
+    void (*dispatch_function)(dispatch_queue_t queue, dispatch_block_t block) = isSync ? dispatch_sync : dispatch_async;
+    
+    dispatch_function(FMDBQ_queue, ^{
         
         NSError *outErr = nil;
         
-        if (![self executeUpdate:&outErr]) {
+        if (![self executeUpdateInt:&outErr]) {
             callback(outErr);
         }
         else {
             callback(nil);
         }
         
-        
-        
     });
+}
+
+- (void)executeUpdateInBackground:(void (^)(NSError *error))callback {
     
+    [self executeUpdateSync:NO callback:callback];
 }
 
 
 - (BOOL)executeUpdate:(NSError * __autoreleasing *)outErr {
     
+    __block BOOL worked = NO;
+    [self executeUpdateSync:YES callback:^(NSError *e) {
+        
+        worked = e == nil;
+        if (e) {
+            *outErr = e;
+        }
+    }];
+    
+    
+    return worked;
+}
+
+
+- (BOOL)executeUpdateInt:(NSError * __autoreleasing *)outErr {
+    
     // FIXME: ref the db here instead of using an ivar.
     BOOL _logsErrors = NO;
     NSString *sql = nil;

+ 21 - 8
src/sample/main.m

@@ -45,7 +45,7 @@ int main (int argc, const char * argv[]) {
     }
     
     // kind of experimentalish.
-    [db setShouldCacheStatements:YES];
+    //[db setShouldCacheStatements:YES];
     
     
     
@@ -67,21 +67,34 @@ int main (int argc, const char * argv[]) {
         
         
         
-        __block BOOL allGood = NO;
+        __block int allGoodCount = NO;
         
         [[db u:@"create table test3 (a text, b text, c integer, d double, e double)"] executeUpdateInBackground:^(NSError *error) {
-            
-            
             FMDBQuickCheck(error != nil);
-            
-            allGood = YES;
+            allGoodCount++;
         }];
-                        
         
         
+        
+        dispatch_apply(20, dispatch_get_global_queue(0, DISPATCH_QUEUE_PRIORITY_HIGH), ^(size_t i) {
+            
+            NSLog(@"idx: %ld", i);
+            
+            [[db u:@"insert into test3 (a, b, c, d, e) values (?, ?, ?, ?, ?)" ,
+              @"hi'", // look!  I put in a ', and I'm not escaping it!
+              [NSString stringWithFormat:@"number %ld", i],
+              @(i),
+              [NSDate date],
+              [NSNumber numberWithFloat:2.2f]] executeUpdateInBackground:^(NSError *error) {
+                
+                FMDBQuickCheck(error == nil);
+                allGoodCount++;
+            }];
+        });
+        
         sleep(2);
         
-        FMDBQuickCheck(allGood);
+        FMDBQuickCheck(allGoodCount == 21);
         
         
         NSLog(@"yay");