Browse Source

Cleanup and little changes.

August Mueller 11 years ago
parent
commit
6d090b8b0b
2 changed files with 26 additions and 44 deletions
  1. 3 4
      src/fmdb/FMDatabase.h
  2. 23 40
      src/fmdb/FMDatabase.m

+ 3 - 4
src/fmdb/FMDatabase.h

@@ -388,10 +388,9 @@ typedef int(^FMDBExecuteBulkSQLCallbackBlock)(NSDictionary *resultsDictionary);
 
  @param sql       The SQL to be performed.
  @param block     A block that will be called for any result sets returned by any SQL statements. 
-                  Note, if you supply this block, it must return integer value, zero upon success,
-                  non-zero value upon failure (which will stop the bulk execution of the SQL. This block
-                  takes two parameters, the `void *userInfo` and a `NSDictionary *resultsDictionary`. 
-                  This may be `nil`.
+                  Note, if you supply this block, it must return integer value, zero upon success (this would be a good opertunity to use SQLITE_OK),
+                  non-zero value upon failure (which will stop the bulk execution of the SQL).  If a statement returns values, the block will be called with the results from the query in NSDictionary *resultsDictionary.
+                  This may be `nil` if you don't care to recive any results.
 
  @return          `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`,
                   `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.

+ 23 - 40
src/fmdb/FMDatabase.m

@@ -2,9 +2,6 @@
 #import "unistd.h"
 #import <objc/runtime.h>
 
-
-static FMDBExecuteBulkSQLCallbackBlock execCallbackBlock;
-
 @interface FMDatabase ()
 
 - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args;
@@ -42,7 +39,6 @@ - (instancetype)initWithPath:(NSString*)aPath {
         _logsErrors                 = YES;
         _crashOnErrors              = NO;
         _maxBusyRetryTimeInterval   = 2;
-        execCallbackBlock           = nil;
     }
     
     return self;
@@ -1096,56 +1092,43 @@ - (BOOL)executeUpdateWithFormat:(NSString*)format, ... {
     return [self executeUpdate:sql withArgumentsInArray:arguments];
 }
 
-int FMDBExecuteBulkSQLCallback(void *userInfo, int columns, char **values, char**names)
-{
-    if (!execCallbackBlock) {
-        return 0;
-    }
-
-    NSString *key;
-    id        value;
 
+int FMDBExecuteBulkSQLCallback(void *theBlockAsVoid, int columns, char **values, char **names); // shhh clang.
+int FMDBExecuteBulkSQLCallback(void *theBlockAsVoid, int columns, char **values, char **names) {
+    
+    if (!theBlockAsVoid) {
+        return SQLITE_OK;
+    }
+    
+    int (^execCallbackBlock)(NSDictionary *resultsDictionary) = (__bridge int (^)(NSDictionary *__strong))(theBlockAsVoid);
+    
     NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:columns];
+    
     for (NSInteger i = 0; i < columns; i++) {
-        key = [NSString stringWithUTF8String:names[i]];
-
-        if (values[i] == NULL)
-            value = [NSNull null];
-        else
-            value = [NSString stringWithUTF8String:values[i]];
-
+        NSString *key = [NSString stringWithUTF8String:names[i]];
+        id value = values[i] ? [NSString stringWithUTF8String:values[i]] : [NSNull null];
         [dictionary setObject:value forKey:key];
     }
-
+    
     return execCallbackBlock(dictionary);
 }
 
-- (BOOL)executeBulkSQL:(NSString *)sql
-{
+- (BOOL)executeBulkSQL:(NSString *)sql {
     return [self executeBulkSQL:sql block:nil];
 }
 
-- (BOOL)executeBulkSQL:(NSString *)sql block:(FMDBExecuteBulkSQLCallbackBlock)block
-{
+- (BOOL)executeBulkSQL:(NSString *)sql block:(FMDBExecuteBulkSQLCallbackBlock)block {
+    
     int rc;
-
-    if (execCallbackBlock) {
-        if (_logsErrors) {
-            NSLog(@"Currently already executing sqlite3_exec");
-        }
-        return NO;
-    }
-
-    execCallbackBlock = block;
-
-    if (execCallbackBlock) {
-        rc = sqlite3_exec(self.sqliteHandle, [sql UTF8String], FMDBExecuteBulkSQLCallback, NULL, NULL);
-    } else {
-        rc = sqlite3_exec(self.sqliteHandle, [sql UTF8String], NULL, NULL, NULL);
+    char *errmsg = nil;
+    
+    rc = sqlite3_exec([self sqliteHandle], [sql UTF8String], block ? FMDBExecuteBulkSQLCallback : nil, (__bridge void *)(block), &errmsg);
+    
+    if (errmsg && [self logsErrors]) {
+        NSLog(@"Error inserting batch: %s", errmsg);
+        sqlite3_free(errmsg);
     }
 
-    execCallbackBlock = nil;
-
     return (rc == SQLITE_OK);
 }