diff --git a/changes.txt b/changes.txt index c1401cc3ad..b4565b22c1 100644 --- a/changes.txt +++ b/changes.txt @@ -1,6 +1,7 @@ 2014-03-07 ========== * Adding appendRow to TightdbTable. +* Adding method removeColumn on table. 2014-03-05 ========== diff --git a/src/tightdb/objc/table.h b/src/tightdb/objc/table.h index b389070d7f..74c04b24f3 100644 --- a/src/tightdb/objc/table.h +++ b/src/tightdb/objc/table.h @@ -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; diff --git a/src/tightdb/objc/table_objc.mm b/src/tightdb/objc/table_objc.mm index e162e176de..6ef8eb86b2 100644 --- a/src/tightdb/objc/table_objc.mm +++ b/src/tightdb/objc/table_objc.mm @@ -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]; @@ -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 { diff --git a/src/tightdb/objc/test/dynamic_table.m b/src/tightdb/objc/test/dynamic_table.m index a4814d164f..071d9165f7 100644 --- a/src/tightdb/objc/test/dynamic_table.m +++ b/src/tightdb/objc/test/dynamic_table.m @@ -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]; diff --git a/src/tightdb/objc/util.hpp b/src/tightdb/objc/util.hpp index c717a71206..691e2a9a9a 100644 --- a/src/tightdb/objc/util.hpp +++ b/src/tightdb/objc/util.hpp @@ -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) { \