Skip to content

Commit

Permalink
Merge pull request #49 from mekjaer/mk-remove-column
Browse files Browse the repository at this point in the history
added remove column method on table
  • Loading branch information
mekjaer committed Mar 7, 2014
2 parents ba13172 + 1445a4c commit 3deb596
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
2014-03-07
==========
* Adding appendRow to TightdbTable.
* Adding method removeColumn on table.

2014-03-05
==========
Expand Down
2 changes: 2 additions & 0 deletions src/tightdb/objc/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@
-(size_t)addColumnWithType:(TightdbType)type andName:(NSString *)name;
-(size_t)addColumnWithType:(TightdbType)type andName:(NSString *)name error:(NSError *__autoreleasing *)error;

-(void)removeColumnWithIndex:(size_t)columnIndex;

/* Searching */
/* FIXME: Should be findBool:(BOOL)value inColumn:(size_t)colNdx; */
-(size_t)findBool:(size_t)colNdx value:(BOOL)value;
Expand Down
17 changes: 17 additions & 0 deletions src/tightdb/objc/table_objc.mm
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ -(BOOL)addColumnWithType:(TightdbType)type andName:(NSString*)name error:(NSErro
return YES;
}




-(TightdbDescriptor*)addColumnTable:(NSString*)name
{
return [self addColumnTable:name error:nil];
Expand Down Expand Up @@ -1219,6 +1222,20 @@ -(size_t)addColumnWithType:(TightdbType)type andName:(NSString*)name error:(NSEr
0);
}

-(void)removeColumnWithIndex:(size_t)columnIndex
{
TIGHTDB_EXCEPTION_HANDLER_COLUMN_INDEX_VALID(columnIndex);

try {
m_table->remove_column(columnIndex);
}
catch(std::exception& ex) {
NSException* exception = [NSException exceptionWithName:@"tightdb:core_exception"
reason:[NSString stringWithUTF8String:ex.what()]
userInfo:[NSMutableDictionary dictionary]];
[exception raise];
}
}

-(size_t)findBool:(size_t)col_ndx value:(BOOL)value
{
Expand Down
31 changes: 31 additions & 0 deletions src/tightdb/objc/test/dynamic_table.m
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,37 @@ - (void)testTable
STFail(@"1 row excepted");
}

-(void)testRemoveColumns
{

TightdbTable *t = [[TightdbTable alloc] init];
[t addColumnWithType:tightdb_Int andName:@"col0"];
STAssertTrue([t getColumnCount] == 1,@"1 column added" );

[t removeColumnWithIndex:0];
STAssertTrue([t getColumnCount] == 0, @"Colum removed");

for (int i=0;i<10;i++) {
[t addColumnWithType:tightdb_Int andName:@"name"];
}

STAssertThrows([t removeColumnWithIndex:10], @"Out of bounds");
STAssertThrows([t removeColumnWithIndex:-1], @"Less than zero colIndex");

STAssertTrue([t getColumnCount] == 10, @"10 columns added");

for (int i=0;i<10;i++) {
[t removeColumnWithIndex:0];
}

STAssertTrue([t getColumnCount] == 0, @"Colums removed");

STAssertThrows([t removeColumnWithIndex:1], @"No columns added");
STAssertThrows([t removeColumnWithIndex:-1], @"Less than zero colIndex");


}

- (void)testDataTypes_Dynamic
{
TightdbTable* table = [[TightdbTable alloc] init];
Expand Down
8 changes: 8 additions & 0 deletions src/tightdb/objc/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ catch(std::exception& ex) { \
[exception raise]; \
}

#define TIGHTDB_EXCEPTION_HANDLER_COLUMN_INDEX_VALID(columnIndex) \
if (columnIndex >= [self getColumnCount]) { \
NSException* exception = [NSException exceptionWithName:@"tightdb:column_index_out_of_bounds" \
reason:@"The specified column index is not within the table bounds" \
userInfo:[NSMutableDictionary dictionary]]; \
[exception raise]; \
} \

#define TIGHTDB_EXCEPTION_HANDLER_CORE_EXCEPTION(action) \
try { action } \
catch(std::exception& ex) { \
Expand Down

0 comments on commit 3deb596

Please sign in to comment.