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

added sort on view and getColumnType on view #47

Merged
merged 10 commits into from
Mar 7, 2014
Merged
4 changes: 4 additions & 0 deletions TightDbObjcDyn/TightDbObjcDyn.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
4D01A9EF18C75B4100527AD5 /* table_view.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D01A9EE18C75B4100527AD5 /* table_view.m */; };
4D79966018BDFC3E009EB0C0 /* table_macros.h in Headers */ = {isa = PBXBuildFile; fileRef = 4D79965F18BDFC3E009EB0C0 /* table_macros.h */; };
4DB1DEFB18BF70AE005A7234 /* dynamic_table.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DB1DEF818BF70AE005A7234 /* dynamic_table.m */; };
4DB1DEFC18BF70AE005A7234 /* functional.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DB1DEF918BF70AE005A7234 /* functional.m */; };
Expand Down Expand Up @@ -59,6 +60,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
4D01A9EE18C75B4100527AD5 /* table_view.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = table_view.m; sourceTree = "<group>"; };
4D79965F18BDFC3E009EB0C0 /* table_macros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = table_macros.h; path = ../../src/tightdb/objc/table_macros.h; sourceTree = "<group>"; };
4DB1DEF818BF70AE005A7234 /* dynamic_table.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = dynamic_table.m; path = ../../src/tightdb/objc/test/dynamic_table.m; sourceTree = "<group>"; };
4DB1DEF918BF70AE005A7234 /* functional.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = functional.m; path = ../../src/tightdb/objc/test/functional.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -206,6 +208,7 @@
4DB1DEF818BF70AE005A7234 /* dynamic_table.m */,
4DB1DEF918BF70AE005A7234 /* functional.m */,
4DB1DEFA18BF70AE005A7234 /* typed_table.m */,
4D01A9EE18C75B4100527AD5 /* table_view.m */,
E91890E7177B71E400653D7A /* data_type.mm */,
E91890E8177B71E400653D7A /* enumerator.m */,
E91890E9177B71E400653D7A /* err_handling.mm */,
Expand Down Expand Up @@ -378,6 +381,7 @@
E91890FE177B71E400653D7A /* subtable.m in Sources */,
4DB1DEFC18BF70AE005A7234 /* functional.m in Sources */,
4DB1DEFD18BF70AE005A7234 /* typed_table.m in Sources */,
4D01A9EF18C75B4100527AD5 /* table_view.m in Sources */,
E91890FF177B71E400653D7A /* table_delete_all.m in Sources */,
4DB1DEFB18BF70AE005A7234 /* dynamic_table.m in Sources */,
E9189101177B71E400653D7A /* template.m in Sources */,
Expand Down
234 changes: 234 additions & 0 deletions TightDbObjcDyn/TightDbObjcDynTests/table_view.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
/*************************************************************************
*
* TIGHTDB CONFIDENTIAL
* __________________
*
* [2011] - [2014] TightDB Inc
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of TightDB Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to TightDB Incorporated
* and its suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from TightDB Incorporated.
*
**************************************************************************/

#import <SenTestingKit/SenTestingKit.h>

#import <tightdb/objc/tightdb.h>


@interface table_view : SenTestCase

@end

@implementation table_view

- (void)setUp
{
[super setUp];
// Put setup code here; it will be run once, before the first test case.
}

- (void)tearDown
{
// Put teardown code here; it will be run once, after the last test case.
[super tearDown];
}

- (void)testColumnTypesOnView
{
TightdbTable *t = [[TightdbTable alloc] init];

NSUInteger boolCol = [t addColumnWithType:tightdb_Bool andName:@"boolCol"];
NSUInteger binaryCol = [t addColumnWithType:tightdb_Binary andName:@"binaryCol"];
NSUInteger dateCol = [t addColumnWithType:tightdb_Date andName:@"dateCol"];
NSUInteger doubleCol = [t addColumnWithType:tightdb_Double andName:@"doubleCol"];
NSUInteger floatCol = [t addColumnWithType:tightdb_Float andName:@"floatCol"];
NSUInteger intCol = [t addColumnWithType:tightdb_Int andName:@"intCol"];
NSUInteger mixedCol = [t addColumnWithType:tightdb_Mixed andName:@"MixedCol"];
NSUInteger stringCol = [t addColumnWithType:tightdb_String andName:@"stringCol"];
NSUInteger tableCol = [t addColumnWithType:tightdb_Table andName:@"tableCol"];


TightdbQuery *q = [t where];
TightdbView *v = [q findAll];

STAssertTrue([v getColumnType:boolCol] == tightdb_Bool, @"Column types matches");
STAssertTrue([v getColumnType:binaryCol] == tightdb_Binary, @"Column types matches");
STAssertTrue([v getColumnType:dateCol] == tightdb_Date, @"Column types matches");
STAssertTrue([v getColumnType:doubleCol] == tightdb_Double, @"Column types matches");
STAssertTrue([v getColumnType:floatCol] == tightdb_Float, @"Column types matches");
STAssertTrue([v getColumnType:intCol] == tightdb_Int, @"Column types matches");
STAssertTrue([v getColumnType:mixedCol] == tightdb_Mixed, @"Column types matches");
STAssertTrue([v getColumnType:stringCol] == tightdb_String, @"Column types matches");
STAssertTrue([v getColumnType:tableCol] == tightdb_Table, @"Column types matches");
}

- (void)testSortOnViewIntColumn
{
TightdbTable *t = [[TightdbTable alloc] init];
NSUInteger intCol = [t addColumnWithType:tightdb_Int andName:@"intCol"];

TightdbCursor *row = [t addEmptyRow];
[row setInt:2 inColumn:intCol];

row = [t addEmptyRow];
[row setInt:1 inColumn:intCol];

row = [t addEmptyRow];
[row setInt:0 inColumn:intCol];

TightdbQuery *q = [t where];
TightdbView *v = [q findAll];

// Not yet sorted
STAssertTrue([v get:intCol ndx:0] == 2, @"matcing value after no sort");
STAssertTrue([v get:intCol ndx:1] == 1, @"matcing value after no sort");
STAssertTrue([v get:intCol ndx:2] == 0, @"matcing value after no sort");

// Sort same way without order specified. Ascending default
[v sortColumnWithIndex:intCol];
STAssertTrue([v get:intCol ndx:0] == 0, @"matcing value after default sort");
STAssertTrue([v get:intCol ndx:1] == 1, @"matcing value after default sort");
STAssertTrue([v get:intCol ndx:2] == 2, @"matcing value after default sort");

// Sort same way
[v sortColumnWithIndex:intCol inOrder:tightdb_ascending];
STAssertTrue([v get:intCol ndx:0] == 0, @"matcing value after ascending sort");
STAssertTrue([v get:intCol ndx:1] == 1, @"matcing value after ascending sort");
STAssertTrue([v get:intCol ndx:2] == 2, @"matcing value after ascending sort");

// Sort descending
[v sortColumnWithIndex:intCol inOrder: tightdb_descending];
STAssertTrue([v get:intCol ndx:0] == 2, @"matcing value after descending sort");
STAssertTrue([v get:intCol ndx:1] == 1, @"matcing value after descending sort");
STAssertTrue([v get:intCol ndx:2] == 0, @"matcing value after descending sort");
}

- (void)testSortOnViewBoolColumn
{
TightdbTable *t = [[TightdbTable alloc] init];
NSUInteger boolCol = [t addColumnWithType:tightdb_Bool andName:@"boolCol"];

TightdbCursor *row = [t addEmptyRow];
[row setBool:YES inColumn:boolCol];

row = [t addEmptyRow];
[row setBool:YES inColumn:boolCol];

row = [t addEmptyRow];
[row setBool:NO inColumn:boolCol];

TightdbQuery *q = [t where];
TightdbView *v = [q findAll];

// Not yet sorted
STAssertTrue([v getBool:boolCol ndx:0] == YES, @"matcing value after no sort");
STAssertTrue([v getBool:boolCol ndx:1] == YES, @"matcing value after no sort");
STAssertTrue([v getBool:boolCol ndx:2] == NO, @"matcing value after no sort");

// Sort same way without order specified. Ascending default
[v sortColumnWithIndex:boolCol];
STAssertTrue([v getBool:boolCol ndx:0] == NO, @"matcing value after default sort");
STAssertTrue([v getBool:boolCol ndx:1] == YES, @"matcing value after default sort");
STAssertTrue([v getBool:boolCol ndx:2] == YES, @"matcing value after default sort");

// Sort same way
[v sortColumnWithIndex:boolCol inOrder:tightdb_ascending];
STAssertTrue([v getBool:boolCol ndx:0] == NO, @"matcing value after ascending sort");
STAssertTrue([v getBool:boolCol ndx:1] == YES, @"matcing value after ascending sort");
STAssertTrue([v getBool:boolCol ndx:2] == YES, @"matcing value after ascending sort");

// Sort descending
[v sortColumnWithIndex:boolCol inOrder: tightdb_descending];
STAssertTrue([v getBool:boolCol ndx:0] == YES, @"matcing value after descending sort");
STAssertTrue([v getBool:boolCol ndx:1] == YES, @"matcing value after descending sort");
STAssertTrue([v getBool:boolCol ndx:2] == NO, @"matcing value after descending sort");
}


- (void)testSortOnViewDateColumn
{
TightdbTable *t = [[TightdbTable alloc] init];
NSUInteger dateCol = [t addColumnWithType:tightdb_Date andName:@"dateCol"];


NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/yyyy hh:mm a"];

NSDate *dateFirst = [formatter dateFromString:@"01/01/2014 10:10 PM"];
NSDate *dateMiddle = [formatter dateFromString:@"02/01/2014 10:10 PM"];
NSDate *dateLast = [formatter dateFromString:@"03/01/2014 10:10 PM"];

TightdbCursor *row = [t addEmptyRow];
[row setDate:[dateLast timeIntervalSince1970] inColumn:dateCol];

row = [t addEmptyRow];
[row setDate:[dateMiddle timeIntervalSince1970] inColumn:dateCol];

row = [t addEmptyRow];
[row setDate:[dateFirst timeIntervalSince1970] inColumn:dateCol];

TightdbQuery *q = [t where];
TightdbView *v = [q findAll];

// Not yet sorted
STAssertTrue([v getDate:dateCol ndx:0] == [dateLast timeIntervalSince1970], @"matcing value after no sort");
STAssertTrue([v getDate:dateCol ndx:1] == [dateMiddle timeIntervalSince1970], @"matcing value after no sort");
STAssertTrue([v getDate:dateCol ndx:2] == [dateFirst timeIntervalSince1970], @"matcing value after no sort");

// Sort same way without order specified. Ascending default
[v sortColumnWithIndex:dateCol];
STAssertTrue([v getDate:dateCol ndx:0] == [dateFirst timeIntervalSince1970], @"matcing value after default sort");
STAssertTrue([v getDate:dateCol ndx:1] == [dateMiddle timeIntervalSince1970], @"matcing value after default sort");
STAssertTrue([v getDate:dateCol ndx:2] == [dateLast timeIntervalSince1970], @"matcing value after default sort");

// Sort same way
[v sortColumnWithIndex:dateCol inOrder:tightdb_ascending];
STAssertTrue([v getDate:dateCol ndx:0] == [dateFirst timeIntervalSince1970], @"matcing value after ascending sort");
STAssertTrue([v getDate:dateCol ndx:1] == [dateMiddle timeIntervalSince1970], @"matcing value after ascending sort");
STAssertTrue([v getDate:dateCol ndx:2] == [dateLast timeIntervalSince1970], @"matcing value after ascending sort");

// Sort descending
[v sortColumnWithIndex:dateCol inOrder: tightdb_descending];
STAssertTrue([v getDate:dateCol ndx:0] == [dateLast timeIntervalSince1970], @"matcing value after descending sort");
STAssertTrue([v getDate:dateCol ndx:1] == [dateMiddle timeIntervalSince1970], @"matcing value after descending sort");
STAssertTrue([v getDate:dateCol ndx:2] == [dateFirst timeIntervalSince1970], @"matcing value after descending sort");
}


- (void)testSortOnAllColumnTypes
{
TightdbTable *t = [[TightdbTable alloc] init];

NSUInteger boolCol = [t addColumnWithType:tightdb_Bool andName:@"boolCol"];
NSUInteger binaryCol = [t addColumnWithType:tightdb_Binary andName:@"binaryCol"];
NSUInteger dateCol = [t addColumnWithType:tightdb_Date andName:@"dateCol"];
NSUInteger doubleCol = [t addColumnWithType:tightdb_Double andName:@"doubleCol"];
NSUInteger floatCol = [t addColumnWithType:tightdb_Float andName:@"floatCol"];
NSUInteger intCol = [t addColumnWithType:tightdb_Int andName:@"intCol"];
NSUInteger mixedCol = [t addColumnWithType:tightdb_Mixed andName:@"MixedCol"];
NSUInteger stringCol = [t addColumnWithType:tightdb_String andName:@"stringCol"];
NSUInteger tableCol = [t addColumnWithType:tightdb_Table andName:@"tableCol"];

TightdbQuery *q = [t where];
TightdbView *v = [q findAll];

[v sortColumnWithIndex:boolCol]; // bool is supported
STAssertThrows([v sortColumnWithIndex:binaryCol], @"Not supported on binary column");
[v sortColumnWithIndex:dateCol]; // bool is supported
STAssertThrows([v sortColumnWithIndex:doubleCol], @"Not supported on double column");
STAssertThrows([v sortColumnWithIndex:floatCol], @"Not supported on float column");
[v sortColumnWithIndex:intCol]; // int is supported
STAssertThrows([v sortColumnWithIndex:mixedCol], @"Not supported on mixed column");
STAssertThrows([v sortColumnWithIndex:stringCol], @"Not supported on string column");
STAssertThrows([v sortColumnWithIndex:tableCol], @"Not supported on table column");
}

@end
5 changes: 5 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2014-03-05
==========
+ tableview now supports sort on column with column type bool, date and int
+ tableview has method for checking the column type of a specified column

2014-02-27
==========
! BREAKING CHANGE: addRow renamed to addEmptyRow
Expand Down
3 changes: 3 additions & 0 deletions src/tightdb/objc/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@

-(size_t)count;
-(BOOL)isEmpty;
-(TightdbType)getColumnType:(size_t)colNdx;
-(void) sortColumnWithIndex: (size_t)columnIndex;
-(void) sortColumnWithIndex: (size_t)columnIndex inOrder: (TightdbSortOrder)order;
-(int64_t)get:(size_t)colNdx ndx:(size_t)ndx;
-(BOOL)getBool:(size_t)colNdx ndx:(size_t)ndx;
-(time_t)getDate:(size_t)colNdx ndx:(size_t)ndx;
Expand Down
21 changes: 21 additions & 0 deletions src/tightdb/objc/table_objc.mm
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,27 @@ -(BOOL)isEmpty
{
return m_view->is_empty();
}
-(TightdbType)getColumnType:(size_t)colNdx
{
return TightdbType(m_view->get_column_type(colNdx));
}
-(void) sortColumnWithIndex: (size_t)columnIndex
{
[self sortColumnWithIndex:columnIndex inOrder:tightdb_ascending];
}
-(void) sortColumnWithIndex: (size_t)columnIndex inOrder: (TightdbSortOrder)order
{
TightdbType columnType = [self getColumnType:columnIndex];

if(columnType != tightdb_Int && columnType != tightdb_Bool && columnType != tightdb_Date) {
NSException* exception = [NSException exceptionWithName:@"tightdb:sort_on_column_with_type_not_supported" \
reason:@"Sort is currently only supported on Integer, Boolean and Date columns." \
userInfo:[NSMutableDictionary dictionary]]; \
[exception raise];
}

m_view->sort(columnIndex, order == 0);
}
-(int64_t)get:(size_t)col_ndx ndx:(size_t)ndx
{
return m_view->get_int(col_ndx, ndx);
Expand Down
7 changes: 7 additions & 0 deletions src/tightdb/objc/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ typedef enum {
tightdb_Mixed = 6,
} TightdbType;


typedef enum {
tightdb_ascending = 0,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm, perhaps we should call these tightdb_sortAscending for better autocompletion and not to mix of when doing autocom for looking up column types

tightdb_descending = 1,

} TightdbSortOrder;

#endif /* TIGHTDB_OBJC_TYPE_H */