Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add -[FMResultSet typeInformation] to mitigate missing type information in FMResultSet. #142

Open
wants to merge 1 commit into from

2 participants

@jjthrash

The FMResultSet API destroys column information in the database,
particularly in the float vs integer case. As a result, the
-[FMResultSet resultDictionary] API smashes both ints and floats
together as NSNumber*, making it difficult to introspect.

This API allows a minimal amount of introspection to work around
this problem.

In my case, I needed this API to deal with a fairly run-of-the-mill floating point formatting issue, since SQLite doesn't have actual decimal types.

@jjthrash jjthrash Add -[FMResultSet columnTypeForColumn(Index):]
The FMResultSet API destroys column information in the database,
particularly in the float vs integer case. As a result, the
-[FMResultSet resultDictionary] API smashes both ints and floats
together as NSNumber*, making it difficult to introspect.

This API allows a minimal amount of introspection to work around
this problem.
6756f65
@ccgus
Owner

Why not just use SQLITE_INTEGER and such?

Not sure what you're asking. The point is that if I have a floating point column, I want to know it's floating point so I can handle it differently from integers. As it is now, both integers and floating points are returned as NSNumber. Is there another way around what I'm doing?

Obviously for columns I already know about, I have the information I need. But I have a few columns that I treat generically and it seems a shame not to use info that's built into the DB already.

Owner

Well, my question was why are there some new enums, when SQLite already provides some for us and a method to get at the data. But I've got another question - are you using objectForColumnIndex? You can find out how your number was created (via float or int) via: [NSNumber objCType] as described here: http://www.cocoabuilder.com/archive/cocoa/289696-how-tell-tell-if-an-nsnumber-was-initialized-from-float-or-int.html

Does that make sense? I guess I don't see the need for this patch when there's already two different ways to get at this data :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 3, 2013
  1. @jjthrash

    Add -[FMResultSet columnTypeForColumn(Index):]

    jjthrash authored
    The FMResultSet API destroys column information in the database,
    particularly in the float vs integer case. As a result, the
    -[FMResultSet resultDictionary] API smashes both ints and floats
    together as NSNumber*, making it difficult to introspect.
    
    This API allows a minimal amount of introspection to work around
    this problem.
This page is out of date. Refresh to see the latest.
Showing with 31 additions and 0 deletions.
  1. +10 −0 src/FMResultSet.h
  2. +21 −0 src/FMResultSet.m
View
10 src/FMResultSet.h
@@ -13,6 +13,13 @@
#endif
#endif
+typedef enum {
+ FMColumnTypeOther,
+ FMColumnTypeInteger,
+ FMColumnTypeFloat,
+ FMColumnTypeBlob
+} FMColumnType;
+
@class FMDatabase;
@class FMStatement;
@@ -42,6 +49,9 @@
- (int)columnIndexForName:(NSString*)columnName;
- (NSString*)columnNameForIndex:(int)columnIdx;
+- (FMColumnType)columnTypeForColumn:(NSString*)columnName;
+- (FMColumnType)columnTypeForColumnIndex:(int)columnIdx;
+
- (int)intForColumn:(NSString*)columnName;
- (int)intForColumnIndex:(int)columnIdx;
View
21 src/FMResultSet.m
@@ -219,6 +219,27 @@ - (int)columnIndexForName:(NSString*)columnName {
return -1;
}
+- (FMColumnType)columnTypeForColumn:(NSString*)columnName {
+ return [self columnTypeForColumnIndex:[self columnIndexForName:columnName]];
+}
+
+- (FMColumnType)columnTypeForColumnIndex:(int)columnIdx {
+ int columnType = sqlite3_column_type([_statement statement], columnIdx);
+
+ switch (columnType) {
+ case SQLITE_INTEGER:
+ return FMColumnTypeInteger;
+
+ case SQLITE_FLOAT:
+ return FMColumnTypeFloat;
+
+ case SQLITE_BLOB:
+ return FMColumnTypeBlob;
+
+ default:
+ return FMColumnTypeOther;
+ }
+}
- (int)intForColumn:(NSString*)columnName {
Something went wrong with that request. Please try again.