Browse Source

executeBatch:withRowResultBlock:

August Mueller 11 years ago
parent
commit
de7e71a32b
3 changed files with 71 additions and 0 deletions
  1. 32 0
      Tests/FMDatabaseTests.m
  2. 3 0
      src/fmdb/FMDatabase.h
  3. 36 0
      src/fmdb/FMDatabase.m

+ 32 - 0
Tests/FMDatabaseTests.m

@@ -833,4 +833,36 @@ - (void)testApplicationID
 #endif
 
 
+- (void)testBatch {
+    
+    NSString *sql = @"create table exectest (foo text);\n"
+                    @"insert into exectest values ('hello')\n;"
+                    @"insert into exectest values ('hi');\n"
+                    @"insert into exectest values ('not h!');\n"
+                    @"insert into exectest values ('definitely not h!');\n"
+                    @"select * from exectest where foo like 'h%';";
+    
+    __block int callbackRowCount = 0;
+    
+    int errorCode = [[self db] executeBatch:sql withRowResultBlock:^(int columnCount, char **columnValues, char **columnNames, BOOL *stop) {
+        
+        NSLog(@"Inserted %d columns for row %d", columnCount, callbackRowCount);
+        
+        callbackRowCount++;
+    }];
+    
+    XCTAssert(callbackRowCount == 2, @"Wrong number of rows inserted (got %d)!", callbackRowCount);
+    XCTAssert(errorCode == SQLITE_OK, @"Got an error back from executeBatch: %d", errorCode);
+    
+    int selectRowCount = 0;
+    FMResultSet *rs = [[self db] executeQuery:@"select * from exectest"];
+    while ([rs next]) {
+        selectRowCount++;
+    }
+    
+    XCTAssert(selectRowCount == 4, @"Wrong number of rows queried!");
+    
+}
+
+
 @end

+ 3 - 0
src/fmdb/FMDatabase.h

@@ -916,6 +916,9 @@
 
 - (NSString *)stringFromDate:(NSDate *)date;
 
+
+- (int)executeBatch:(NSString*)sql withRowResultBlock:(void (^)(int columnCount, char **columnValues, char **columnNames, BOOL *stop))callbackBlock;
+
 @end
 
 

+ 36 - 0
src/fmdb/FMDatabase.m

@@ -1239,6 +1239,42 @@ - (void)makeFunctionNamed:(NSString*)name maximumArguments:(int)count withBlock:
 #endif
 }
 
+int FMDBSQLiteExecCallBackFunction(void *theBlockAsVoid, int columnCount, char **columnValues, char **columnNames);
+int FMDBSQLiteExecCallBackFunction(void *theBlockAsVoid, int columnCount, char **columnValues, char **columnNames) {
+    
+    void (^callbackBlock)(int columnCount, char **columnValues, char **columnNames, BOOL *stop) = theBlockAsVoid;
+    
+    if (callbackBlock) {
+        BOOL shouldStop = NO;
+        callbackBlock(columnCount, columnValues, columnNames, &shouldStop);
+        
+        if (shouldStop) {
+            return 1;
+        }
+    }
+    
+    return 0;
+}
+
+- (int)executeBatch:(NSString*)sql withRowResultBlock:(void (^)(int columnCount, char **columnValues, char **columnNames, BOOL *stop))callbackBlock {
+    
+    
+    char *errmsg = nil;
+    
+    int errorCode = sqlite3_exec([self sqliteHandle], [sql UTF8String], &FMDBSQLiteExecCallBackFunction, callbackBlock, &errmsg);
+    
+    
+    if (errmsg) {
+        NSLog(@"Error inserting batch: %s", errmsg);
+        errorCode = [self lastErrorCode];
+        sqlite3_free(errmsg);
+    }
+    
+    return errorCode;
+}
+
+
+
 @end