diff options
author | August Mueller <gus@flyingmeat.com> | 2014-04-23 20:54:24 +0400 |
---|---|---|
committer | August Mueller <gus@flyingmeat.com> | 2014-04-23 20:54:24 +0400 |
commit | 6d090b8b0bdffdc4bc3a51f580c11091e87927f4 (patch) | |
tree | 57d9d60784fc1debefe843138c1f9d80dbc4e61b | |
parent | 5cd73023f1252c2b5e9521ce53664749dae66f41 (diff) |
Cleanup and little changes.
-rw-r--r-- | src/fmdb/FMDatabase.h | 7 | ||||
-rw-r--r-- | src/fmdb/FMDatabase.m | 63 |
2 files changed, 26 insertions, 44 deletions
diff --git a/src/fmdb/FMDatabase.h b/src/fmdb/FMDatabase.h index 2f950a1..99cb900 100644 --- a/src/fmdb/FMDatabase.h +++ b/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. diff --git a/src/fmdb/FMDatabase.m b/src/fmdb/FMDatabase.m index 23f57f1..deb42d7 100644 --- a/src/fmdb/FMDatabase.m +++ b/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 @@ static FMDBExecuteBulkSQLCallbackBlock execCallbackBlock; _logsErrors = YES; _crashOnErrors = NO; _maxBusyRetryTimeInterval = 2; - execCallbackBlock = nil; } return self; @@ -1096,56 +1092,43 @@ static int FMDBDatabaseBusyHandler(void *f, int count) { 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); } |