Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Object Subscripting on cursors #64

Merged
merged 1 commit into from
Mar 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/tightdb/objc/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
-(void)setNdx:(NSUInteger)ndx;
-(NSUInteger)index;

/* object subscripting */
-(id)objectAtIndexedSubscript:(NSUInteger)colNdx;
-(id)objectForKeyedSubscript:(id <NSCopying>)key;
-(void)setObject:(id)obj atIndexedSubscript:(NSUInteger)colNdx;
-(void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;

-(void)setInt:(int64_t)value inColumn:(NSUInteger)colNdx;
-(void)setString:(NSString *)value inColumn:(NSUInteger)colNdx;
-(void)setBool:(BOOL)value inColumn:(NSUInteger)colNdx;
Expand Down
78 changes: 78 additions & 0 deletions src/tightdb/objc/cursor_objc.mm
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,84 @@ -(void)dealloc
_table = nil;
}

-(id)objectAtIndexedSubscript:(NSUInteger)colNdx
{
TightdbType columnType = [_table getColumnType:colNdx];
switch (columnType) {
case tightdb_Bool:
return [NSNumber numberWithBool:[_table getBoolInColumn:colNdx atRow:_ndx]];
case tightdb_Int:
return [NSNumber numberWithLongLong:[_table getIntInColumn:colNdx atRow:_ndx]];
case tightdb_Float:
return [NSNumber numberWithFloat:[_table getFloatInColumn:colNdx atRow:_ndx]];
case tightdb_Double:
return [NSNumber numberWithLongLong:[_table getDoubleInColumn:colNdx atRow:_ndx]];
case tightdb_String:
return [_table getStringInColumn:colNdx atRow:_ndx];
case tightdb_Date:
return [NSDate dateWithTimeIntervalSince1970:[_table getDateInColumn:colNdx atRow:_ndx]];
case tightdb_Binary:
return [_table getBinaryInColumn:colNdx atRow:_ndx];
case tightdb_Table:
return [_table getTableInColumn:colNdx atRow:_ndx];
case tightdb_Mixed:
return [_table getMixedInColumn:colNdx atRow:_ndx];
}
}

- (id)objectForKeyedSubscript:(id <NSCopying>)key
{
NSUInteger colNdx = [_table getColumnIndex:(NSString*)key];
return [self objectAtIndexedSubscript:colNdx];
}

-(void)setObject:(id)obj atIndexedSubscript:(NSUInteger)colNdx
{
TightdbType columnType = [_table getColumnType:colNdx];

// TODO: Verify obj type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably use verify_cellwhich is found in my object literal branch.


switch (columnType) {
case tightdb_Bool:
[_table setBool:[obj boolValue] inColumn:colNdx atRow:_ndx];
break;
case tightdb_Int:
[_table setInt:[obj longLongValue] inColumn:colNdx atRow:_ndx];
break;
case tightdb_Float:
[_table setFloat:[obj floatValue] inColumn:colNdx atRow:_ndx];
break;
case tightdb_Double:
[_table setDouble:[obj doubleValue] inColumn:colNdx atRow:_ndx];
break;
case tightdb_String:
if (![obj isKindOfClass:[NSString class]])
[NSException raise:@"TypeException" format:@"Inserting non-string obj into string column"];
[_table setString:(NSString*)obj inColumn:colNdx atRow:_ndx];
break;
case tightdb_Date:
if ([obj isKindOfClass:[NSDate class]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding long or even a double(aka NSNumber) as permitted classes.

[NSException raise:@"TypeException" format:@"Inserting non-date obj into date column"];
[_table setDate:time_t([obj timeIntervalSince1970]) inColumn:colNdx atRow:_ndx];
break;
case tightdb_Binary:
[_table setBinary:(TightdbBinary *)obj inColumn:colNdx atRow:_ndx];
break;
case tightdb_Table:
[_table setTable:(TightdbTable *)obj inColumn:colNdx atRow:_ndx];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens here if the specified object is not a TightdbTable?

break;
case tightdb_Mixed:
[_table setMixed:(TightdbMixed *)obj inColumn:colNdx atRow:_ndx];
break;
}
}

-(void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key
{
NSUInteger colNdx = [_table getColumnIndex:(NSString*)key];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if column key does not exist?

[self setObject:obj atIndexedSubscript:colNdx];
}

-(int64_t)getIntInColumn:(NSUInteger)colNdx
{
return [_table getIntInColumn:colNdx atRow:_ndx];
Expand Down
4 changes: 3 additions & 1 deletion src/tightdb/objc/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@
-(BOOL)removeLastRow;
-(BOOL)removeLastRowWithError:(NSError *__autoreleasing *)error;

-(TightdbCursor *)objectAtIndexedSubscript:(NSUInteger)ndx; /* object subscripting */
/* object subscripting */
-(TightdbCursor *)objectAtIndexedSubscript:(NSUInteger)ndx;

-(TightdbCursor *)cursorAtIndex:(NSUInteger)ndx;
-(TightdbCursor *)cursorAtLastIndex;

Expand Down
39 changes: 39 additions & 0 deletions src/tightdb/objc/test/dynamic_table.m
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,47 @@ - (void)testTableDynamic_Subscripting
// Same but used directly
STAssertEquals([_table[0] getIntInColumn:0], (int64_t)506, @"table[0].first");
STAssertTrue([[_table[0] getStringInColumn:1] isEqual:@"test"], @"table[0].second");
}

- (void)testTableDynamic_Cursor_Subscripting
{
TightdbTable* _table = [[TightdbTable alloc] init];
STAssertNotNil(_table, @"Table is nil");

// 1. Add two columns
[_table addColumnWithType:tightdb_Int andName:@"first"];
[_table addColumnWithType:tightdb_String andName:@"second"];

TightdbCursor* c;

// Add some rows
c = [_table addEmptyRow];
c[0] = @506;
c[1] = @"test";

c = [_table addEmptyRow];
c[@"first"] = @4;
c[@"second"] = @"more test";

// Get values from cursor by object subscripting
c = _table[0];
STAssertTrue([c[0] isEqual:@506], @"table[0].first");
STAssertTrue([c[1] isEqual:@"test"], @"table[0].second");

// Same but used with column name
STAssertTrue([c[@"first"] isEqual:@506], @"table[0].first");
STAssertTrue([c[@"second"] isEqual:@"test"], @"table[0].second");

// Combine with subscripting for rows
STAssertTrue([_table[0][0] isEqual:@506], @"table[0].first");
STAssertTrue([_table[0][1] isEqual:@"test"], @"table[0].second");
STAssertTrue([_table[0][@"first"] isEqual:@506], @"table[0].first");
STAssertTrue([_table[0][@"second"] isEqual:@"test"], @"table[0].second");

STAssertTrue([_table[1][0] isEqual:@4], @"table[1].first");
STAssertTrue([_table[1][1] isEqual:@"more test"], @"table[1].second");
STAssertTrue([_table[1][@"first"] isEqual:@4], @"table[1].first");
STAssertTrue([_table[1][@"second"] isEqual:@"more test"], @"table[1].second");
}

@end