Browse Source

Added ability to retrieve a column value as an arbitrary object

Dave DeLong 15 years ago
parent
commit
4d12c96fde
2 changed files with 28 additions and 0 deletions
  1. 4 0
      src/FMResultSet.h
  2. 24 0
      src/FMResultSet.m

+ 4 - 0
src/FMResultSet.h

@@ -71,6 +71,10 @@
 - (const unsigned char *)UTF8StringForColumnIndex:(int)columnIdx;
 - (const unsigned char *)UTF8StringForColumnIndex:(int)columnIdx;
 - (const unsigned char *)UTF8StringForColumnName:(NSString*)columnName;
 - (const unsigned char *)UTF8StringForColumnName:(NSString*)columnName;
 
 
+// returns one of NSNumber, NSString, NSData, or NSNull
+- (id)objectForColumnIndex:(int)columnIdx;
+- (id)objectForColumnName:(NSString*)columnName;
+
 /*
 /*
 If you are going to use this data after you iterate over the next row, or after you close the
 If you are going to use this data after you iterate over the next row, or after you close the
 result set, make sure to make a copy of the data first (or just use dataForColumn:/dataForColumnIndex:)
 result set, make sure to make a copy of the data first (or just use dataForColumn:/dataForColumnIndex:)

+ 24 - 0
src/FMResultSet.m

@@ -353,6 +353,30 @@ - (const unsigned char *)UTF8StringForColumnName:(NSString*)columnName {
     return [self UTF8StringForColumnIndex:[self columnIndexForName:columnName]];
     return [self UTF8StringForColumnIndex:[self columnIndexForName:columnName]];
 }
 }
 
 
+- (id)objectForColumnIndex:(int)columnIdx {
+	int columnType = sqlite3_column_type(statement.statement, columnIdx);
+	id returnValue = nil;
+	if (columnType == SQLITE_INTEGER) {
+		returnValue = [NSNumber numberWithLongLong:[self longLongIntForColumnIndex:columnIdx]];
+	} else if (columnType == SQLITE_FLOAT) {
+		returnValue = [NSNumber numberWithDouble:[self doubleForColumnIndex:columnIdx]];
+	} else if (columnType == SQLITE_BLOB) {
+		returnValue = [self dataForColumnIndex:columnIdx];
+	} else {
+		//default to a string for everything else
+		returnValue = [self stringForColumnIndex:columnIdx];
+	}
+	
+	if (returnValue == nil) {
+		returnValue = [NSNull null];
+	}
+	return returnValue;
+}
+
+- (id)objectForColumnName:(NSString*)columnName {
+	return [self objectForColumnIndex:[self columnIndexForName:columnName]];
+}
+
 
 
 // returns autoreleased NSString containing the name of the column in the result set
 // returns autoreleased NSString containing the name of the column in the result set
 - (NSString*)columnNameForIndex:(int)columnIdx {
 - (NSString*)columnNameForIndex:(int)columnIdx {