Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Lazily populate columnNameToIndexMap when accessing so that clients c…

…an use the value without causing setupColumns to be called first.
  • Loading branch information...
commit d72cbb0e44182e77864fed559f93f3a225960cd8 1 parent 4a2fe82
Daniel Dickison danieldickison authored
Showing with 12 additions and 31 deletions.
  1. +1 −2  src/FMResultSet.h
  2. +11 −29 src/FMResultSet.m
3  src/FMResultSet.h
View
@@ -22,11 +22,10 @@
NSString *_query;
NSMutableDictionary *_columnNameToIndexMap;
- BOOL _columnNamesSetup;
}
@property (atomic, retain) NSString *query;
-@property (atomic, retain) NSMutableDictionary *columnNameToIndexMap;
+@property (readonly) NSMutableDictionary *columnNameToIndexMap;
@property (atomic, retain) FMStatement *statement;
+ (id)resultSetWithStatement:(FMStatement *)statement usingParentDatabase:(FMDatabase*)aDB;
40 src/FMResultSet.m
View
@@ -7,14 +7,8 @@ - (void)resultSetDidClose:(FMResultSet *)resultSet;
@end
-@interface FMResultSet (Private)
-- (NSMutableDictionary *)columnNameToIndexMap;
-- (void)setColumnNameToIndexMap:(NSMutableDictionary *)value;
-@end
-
@implementation FMResultSet
@synthesize query=_query;
-@synthesize columnNameToIndexMap=_columnNameToIndexMap;
@synthesize statement=_statement;
+ (id)resultSetWithStatement:(FMStatement *)statement usingParentDatabase:(FMDatabase*)aDB {
@@ -61,20 +55,17 @@ - (int)columnCount {
return sqlite3_column_count([_statement statement]);
}
-- (void)setupColumnNames {
-
+- (NSMutableDictionary *)columnNameToIndexMap {
if (!_columnNameToIndexMap) {
- [self setColumnNameToIndexMap:[NSMutableDictionary dictionary]];
- }
-
- int columnCount = sqlite3_column_count([_statement statement]);
-
- int columnIdx = 0;
- for (columnIdx = 0; columnIdx < columnCount; columnIdx++) {
- [_columnNameToIndexMap setObject:[NSNumber numberWithInt:columnIdx]
- forKey:[[NSString stringWithUTF8String:sqlite3_column_name([_statement statement], columnIdx)] lowercaseString]];
+ int columnCount = sqlite3_column_count([_statement statement]);
+ _columnNameToIndexMap = [[NSMutableDictionary alloc] initWithCapacity:columnCount];
+ int columnIdx = 0;
+ for (columnIdx = 0; columnIdx < columnCount; columnIdx++) {
+ [_columnNameToIndexMap setObject:[NSNumber numberWithInt:columnIdx]
+ forKey:[[NSString stringWithUTF8String:sqlite3_column_name([_statement statement], columnIdx)] lowercaseString]];
+ }
}
- _columnNamesSetup = YES;
+ return _columnNameToIndexMap;
}
- (void)kvcMagic:(id)object {
@@ -105,11 +96,7 @@ - (NSDictionary*)resultDict {
if (num_cols > 0) {
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:num_cols];
- if (!_columnNamesSetup) {
- [self setupColumnNames];
- }
-
- NSEnumerator *columnNames = [_columnNameToIndexMap keyEnumerator];
+ NSEnumerator *columnNames = [self.columnNameToIndexMap keyEnumerator];
NSString *columnName = nil;
while ((columnName = [columnNames nextObject])) {
id objectValue = [self objectForColumnName:columnName];
@@ -219,14 +206,9 @@ - (BOOL)hasAnotherRow {
}
- (int)columnIndexForName:(NSString*)columnName {
-
- if (!_columnNamesSetup) {
- [self setupColumnNames];
- }
-
columnName = [columnName lowercaseString];
- NSNumber *n = [_columnNameToIndexMap objectForKey:columnName];
+ NSNumber *n = [self.columnNameToIndexMap objectForKey:columnName];
if (n) {
return [n intValue];
Please sign in to comment.
Something went wrong with that request. Please try again.