Pārlūkot izejas kodu

Cleanup of some code + notes on what is going on

August Mueller 13 gadi atpakaļ
vecāks
revīzija
5a463d036d
4 mainītis faili ar 31 papildinājumiem un 5 dzēšanām
  1. 3 0
      CHANGES_AND_TODO_LIST.txt
  2. 1 0
      CONTRIBUTORS.txt
  3. 3 3
      src/FMResultSet.m
  4. 24 2
      src/fmdb.m

+ 3 - 0
CHANGES_AND_TODO_LIST.txt

@@ -3,6 +3,9 @@ Zip, nada, zilch.  Got any ideas?
 
 If you would like to contribute some code- awesome!  I just ask that you make it conform to the coding conventions already set in here, and to add a couple of tests for your new code to fmdb.m.  And of course, the code should be of general use to more than just a couple of folks.  Send your patches to gus@flyingmeat.com.
 
+2013.01.31
+    Lazy init of columnNameToIndexMap in FMResultSet, and you are now able to use it before asking for any rows first.  Thanks to Daniel Dickison for the patch!
+
 2012.12.17
     Now resetting cached statements before executing a query on them (as well as resetting them at the close of a result set).  There was an issue where if you used the same query twice without closing the result set of the first one, you would get results back from the previous query, or maybe an exhausted result set.  Thanks to note173 on github for pointing out the problem.
 

+ 1 - 0
CONTRIBUTORS.txt

@@ -34,5 +34,6 @@ Sriram Patil
 Geoffrey Foster
 Robert Ryan
 Samuel Chen
+Daniel Dickison
 
 Aaaaannnd, Gus Mueller (that's me!)

+ 3 - 3
src/FMResultSet.m

@@ -58,7 +58,7 @@ - (int)columnCount {
 - (NSMutableDictionary *)columnNameToIndexMap {
     if (!_columnNameToIndexMap) {
         int columnCount = sqlite3_column_count([_statement statement]);
-        _columnNameToIndexMap = [[NSMutableDictionary alloc] initWithCapacity:columnCount];
+        _columnNameToIndexMap = [[NSMutableDictionary alloc] initWithCapacity:(NSUInteger)columnCount];
         int columnIdx = 0;
         for (columnIdx = 0; columnIdx < columnCount; columnIdx++) {
             [_columnNameToIndexMap setObject:[NSNumber numberWithInt:columnIdx]
@@ -96,7 +96,7 @@ - (NSDictionary*)resultDict {
     if (num_cols > 0) {
         NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:num_cols];
         
-        NSEnumerator *columnNames = [self.columnNameToIndexMap keyEnumerator];
+        NSEnumerator *columnNames = [[self columnNameToIndexMap] keyEnumerator];
         NSString *columnName = nil;
         while ((columnName = [columnNames nextObject])) {
             id objectValue = [self objectForColumnName:columnName];
@@ -208,7 +208,7 @@ - (BOOL)hasAnotherRow {
 - (int)columnIndexForName:(NSString*)columnName {
     columnName = [columnName lowercaseString];
     
-    NSNumber *n = [self.columnNameToIndexMap objectForKey:columnName];
+    NSNumber *n = [[self columnNameToIndexMap] objectForKey:columnName];
     
     if (n) {
         return [n intValue];

+ 24 - 2
src/fmdb.m

@@ -890,6 +890,28 @@ int main (int argc, const char * argv[]) {
         }];
     }
     
+    {
+        
+        
+        [queue inDatabase:^(FMDatabase *adb) {
+            [adb executeUpdate:@"create table colNameTest (a, b, c, d)"];
+            FMDBQuickCheck([adb executeUpdate:@"insert into colNameTest values (1, 2, 3, 4)"]);
+            
+            FMResultSet *ars = [adb executeQuery:@"select * from colNameTest"];
+            
+            NSDictionary *d = [ars columnNameToIndexMap];
+            FMDBQuickCheck([d count] == 4);
+            
+            FMDBQuickCheck([[d objectForKey:@"a"] intValue] == 0);
+            FMDBQuickCheck([[d objectForKey:@"b"] intValue] == 1);
+            FMDBQuickCheck([[d objectForKey:@"c"] intValue] == 2);
+            FMDBQuickCheck([[d objectForKey:@"d"] intValue] == 3);
+            
+            [ars close];
+            
+        }];
+        
+    }
     
     
     {
@@ -1214,7 +1236,7 @@ void testPool(NSString *dbPath) {
         
         size_t ops = 128;
         
-        dispatch_queue_t dqueue = dispatch_get_global_queue(0, DISPATCH_QUEUE_PRIORITY_HIGH);
+        dispatch_queue_t dqueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
         
         dispatch_apply(ops, dqueue, ^(size_t nby) {
             
@@ -1249,7 +1271,7 @@ void testPool(NSString *dbPath) {
         
         int ops = 16;
         
-        dispatch_queue_t dqueue = dispatch_get_global_queue(0, DISPATCH_QUEUE_PRIORITY_HIGH);
+        dispatch_queue_t dqueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
         
         dispatch_apply(ops, dqueue, ^(size_t nby) {