Skip to content

Commit

Permalink
Cleanup and little changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccgus committed Apr 23, 2014
1 parent 5cd7302 commit 6d090b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 44 deletions.
7 changes: 3 additions & 4 deletions src/fmdb/FMDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
63 changes: 23 additions & 40 deletions src/fmdb/FMDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,7 +39,6 @@ - (instancetype)initWithPath:(NSString*)aPath {
_logsErrors = YES;
_crashOnErrors = NO;
_maxBusyRetryTimeInterval = 2;
execCallbackBlock = nil;
}

return self;
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 6d090b8

Please sign in to comment.