Skip to content

Commit

Permalink
Decimal numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
un1v3rse committed Mar 25, 2013
1 parent f41fd71 commit 0e67da1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/FMDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,9 @@ - (void)bindObject:(id)obj toColumn:(int)idx inStatement:(sqlite3_stmt*)pStmt {
else
sqlite3_bind_double(pStmt, idx, [obj timeIntervalSince1970]);
}
else if ([obj isKindOfClass:[NSDecimalNumber class]]) {
sqlite3_bind_text(pStmt, idx, [[obj description] UTF8String], -1, SQLITE_STATIC);
}
else if ([obj isKindOfClass:[NSNumber class]]) {

if (strcmp([obj objCType], @encode(BOOL)) == 0) {
Expand Down
13 changes: 13 additions & 0 deletions src/FMDatabaseAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ - (NSNumber*)numberForQuery:(NSString*)query, ... {
return result;
}

- (NSDecimalNumber*)decimalNumberForQuery:(NSString*)query, ... {
NSDecimalNumber *result = nil;
va_list args;
va_start(args, query);
FMResultSet *rs = [self executeQuery:query withArgumentsInArray:nil orDictionary:nil orVAList:args];
va_end(args);
if ([rs next]) {
result = [rs decimalNumberForColumnIndex:0];
}
[rs close];
return result;
}


- (BOOL)tableExists:(NSString*)tableName {

Expand Down
3 changes: 3 additions & 0 deletions src/FMResultSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
- (NSNumber*)numberForColumn:(NSString*)columnName;
- (NSNumber*)numberForColumnIndex:(int)columnIdx;

- (NSDecimalNumber*)decimalNumberForColumn:(NSString*)columnName;
- (NSDecimalNumber*)decimalNumberForColumnIndex:(int)columnIdx;

- (const unsigned char *)UTF8StringForColumnIndex:(int)columnIdx;
- (const unsigned char *)UTF8StringForColumnName:(NSString*)columnName;

Expand Down
11 changes: 11 additions & 0 deletions src/FMResultSet.m
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,17 @@ - (NSNumber*)numberForColumnIndex:(int)columnIdx {
}


- (NSDecimalNumber *)decimalNumberForColumn:(NSString*)columnName {
return [self decimalNumberForColumnIndex:[self columnIndexForName:columnName]];
}

- (NSDecimalNumber *)decimalNumberForColumnIndex:(int)columnIdx {

NSString *s = [self stringForColumnIndex:columnIdx];
return s ? [NSDecimalNumber decimalNumberWithString:s] : nil;
}


- (NSData*)dataNoCopyForColumn:(NSString*)columnName {
return [self dataNoCopyForColumnIndex:[self columnIndexForName:columnName]];
}
Expand Down

0 comments on commit 0e67da1

Please sign in to comment.