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

MOD: add getAllItemIDsFromTable:tableName and test #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions YTKKeyValueStore/YTKKeyValueStore.h
Expand Up @@ -51,6 +51,8 @@

- (NSArray *)getAllItemsFromTable:(NSString *)tableName;

- (NSArray *)getAllItemIDsFromTable:(NSString *)tableName;

- (NSUInteger)getCountFromTable:(NSString *)tableName;

- (void)deleteObjectById:(NSString *)objectId fromTable:(NSString *)tableName;
Expand Down
20 changes: 20 additions & 0 deletions YTKKeyValueStore/YTKKeyValueStore.m
Expand Up @@ -55,6 +55,8 @@ PRIMARY KEY(id)) \

static NSString *const SELECT_ALL_SQL = @"SELECT * from %@";

static NSString *const SELECT_ALL_ID_SQL = @"SELECT id from %@";

static NSString *const COUNT_ALL_SQL = @"SELECT count(*) as num from %@";

static NSString *const CLEAR_ALL_SQL = @"DELETE from %@";
Expand Down Expand Up @@ -288,6 +290,24 @@ - (NSArray *)getAllItemsFromTable:(NSString *)tableName {
return result;
}

- (NSArray *)getAllItemIDsFromTable:(NSString *)tableName {
if ([YTKKeyValueStore checkTableName:tableName] == NO) {
return nil;
}
NSString * sql = [NSString stringWithFormat:SELECT_ALL_ID_SQL, tableName];
__block NSMutableArray * result = [NSMutableArray array];
[_dbQueue inDatabase:^(FMDatabase *db) {
FMResultSet * rs = [db executeQuery:sql];
while ([rs next]) {
YTKKeyValueItem * item = [[YTKKeyValueItem alloc] init];
item.itemId = [rs stringForColumn:@"id"];
[result addObject:item.itemId];
}
[rs close];
}];
return result;
}

- (NSUInteger)getCountFromTable:(NSString *)tableName
{
if ([YTKKeyValueStore checkTableName:tableName] == NO) {
Expand Down
14 changes: 14 additions & 0 deletions YTKKeyValueStoreTests/YTKKeyValueStoreTests.m
Expand Up @@ -70,6 +70,20 @@ - (void)testSaveNumber {
XCTAssertNil(result);
}

- (void)testGetAllItemIDsFromTable {
NSNumber *number1 = @1;
NSString *key1 = @"key1";
NSNumber *number2 = @2;
NSString *key2 = @"key2";
NSArray *IDs = @[key1, key2];
[_store putNumber:number1 withId:key1 intoTable:_tableName];
[_store putNumber:number2 withId:key2 intoTable:_tableName];

NSArray *result;
result = [_store getAllItemIDsFromTable:_tableName];
XCTAssertEqualObjects(result, IDs);
}

- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
Expand Down