diff --git a/YTKKeyValueStore.xcodeproj/project.pbxproj b/YTKKeyValueStore.xcodeproj/project.pbxproj index 653a894..722d47f 100644 --- a/YTKKeyValueStore.xcodeproj/project.pbxproj +++ b/YTKKeyValueStore.xcodeproj/project.pbxproj @@ -464,6 +464,7 @@ 4597AE7919DEDFB30028ECAC /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 4597AE7A19DEDFB30028ECAC /* Build configuration list for PBXNativeTarget "YTKKeyValueStoreTests" */ = { isa = XCConfigurationList; @@ -472,6 +473,7 @@ 4597AE7C19DEDFB30028ECAC /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/YTKKeyValueStore/AppDelegate.m b/YTKKeyValueStore/AppDelegate.m index 28041aa..4630ff3 100644 --- a/YTKKeyValueStore/AppDelegate.m +++ b/YTKKeyValueStore/AppDelegate.m @@ -17,15 +17,23 @@ @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - // Demo + + NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; + path = [path stringByAppendingPathComponent:@"test.db"]; + YTKKeyValueStore *store = [[YTKKeyValueStore alloc] initWithDBWithPath:path]; + NSString *tableName = @"user_table"; - YTKKeyValueStore *store = [[YTKKeyValueStore alloc] initDBWithName:@"test.db"]; [store createTableWithName:tableName]; - NSString *key = @"1"; - NSDictionary *user = @{@"id": @1, @"name": @"tangqiao", @"age": @30}; - [store putObject:user withId:key intoTable:tableName]; - NSDictionary *queryUser = [store getObjectById:key fromTable:tableName]; + NSString *key = @"YourDefineKey"; + NSDictionary *user = @{@"id": @1, + @"name": @"tangqiao", + @"age": @30}; + + + [store putValue:user forKey:key intoTable:tableName]; + + NSDictionary *queryUser = [store valueForKey:key fromTable:tableName]; NSLog(@"query data result: %@", queryUser); return YES; diff --git a/YTKKeyValueStore/FMDB/FMDatabase.m b/YTKKeyValueStore/FMDB/FMDatabase.m index f58b7b4..e010cac 100644 --- a/YTKKeyValueStore/FMDB/FMDatabase.m +++ b/YTKKeyValueStore/FMDB/FMDatabase.m @@ -313,7 +313,7 @@ - (void)clearCachedStatements { - (FMStatement*)cachedStatementForQuery:(NSString*)query { - NSMutableSet* statements = [_cachedStatements objectForKey:query]; + NSMutableSet* statements = [_cachedStatements valueForKey:query]; return [[statements objectsPassingTest:^BOOL(FMStatement* statement, BOOL *stop) { @@ -329,7 +329,7 @@ - (void)setCachedStatement:(FMStatement*)statement forQuery:(NSString*)query { query = [query copy]; // in case we got handed in a mutable string... [statement setQuery:query]; - NSMutableSet* statements = [_cachedStatements objectForKey:query]; + NSMutableSet* statements = [_cachedStatements valueForKey:query]; if (!statements) { statements = [NSMutableSet set]; } @@ -787,7 +787,7 @@ - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arr NSString *parameterName = [[NSString alloc] initWithFormat:@":%@", dictionaryKey]; if (_traceExecution) { - NSLog(@"%@ = %@", parameterName, [dictionaryArgs objectForKey:dictionaryKey]); + NSLog(@"%@ = %@", parameterName, [dictionaryArgs valueForKey:dictionaryKey]); } // Get the index for the parameter name. @@ -797,7 +797,7 @@ - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arr if (namedIdx > 0) { // Standard binding from here. - [self bindObject:[dictionaryArgs objectForKey:dictionaryKey] toColumn:namedIdx inStatement:pStmt]; + [self bindObject:[dictionaryArgs valueForKey:dictionaryKey] toColumn:namedIdx inStatement:pStmt]; // increment the binding count, so our check below works out idx++; } @@ -969,7 +969,7 @@ - (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArra NSString *parameterName = [[NSString alloc] initWithFormat:@":%@", dictionaryKey]; if (_traceExecution) { - NSLog(@"%@ = %@", parameterName, [dictionaryArgs objectForKey:dictionaryKey]); + NSLog(@"%@ = %@", parameterName, [dictionaryArgs valueForKey:dictionaryKey]); } // Get the index for the parameter name. int namedIdx = sqlite3_bind_parameter_index(pStmt, [parameterName UTF8String]); @@ -978,7 +978,7 @@ - (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArra if (namedIdx > 0) { // Standard binding from here. - [self bindObject:[dictionaryArgs objectForKey:dictionaryKey] toColumn:namedIdx inStatement:pStmt]; + [self bindObject:[dictionaryArgs valueForKey:dictionaryKey] toColumn:namedIdx inStatement:pStmt]; // increment the binding count, so our check below works out idx++; diff --git a/YTKKeyValueStore/FMDB/FMResultSet.m b/YTKKeyValueStore/FMDB/FMResultSet.m index 3b1edb7..3d98708 100644 --- a/YTKKeyValueStore/FMDB/FMResultSet.m +++ b/YTKKeyValueStore/FMDB/FMResultSet.m @@ -184,7 +184,7 @@ - (BOOL)hasAnotherRow { - (int)columnIndexForName:(NSString*)columnName { columnName = [columnName lowercaseString]; - NSNumber *n = [[self columnNameToIndexMap] objectForKey:columnName]; + NSNumber *n = [[self columnNameToIndexMap] valueForKey:columnName]; if (n) { return [n intValue]; diff --git a/YTKKeyValueStore/YTKKeyValueStore.h b/YTKKeyValueStore/YTKKeyValueStore.h index 88e0e32..1719e62 100644 --- a/YTKKeyValueStore/YTKKeyValueStore.h +++ b/YTKKeyValueStore/YTKKeyValueStore.h @@ -8,50 +8,33 @@ #import -@interface YTKKeyValueItem : NSObject - -@property (strong, nonatomic) NSString *itemId; -@property (strong, nonatomic) id itemObject; -@property (strong, nonatomic) NSDate *createdTime; - -@end - - +// Help you store anything string data to sqlite. @interface YTKKeyValueStore : NSObject -- (id)initDBWithName:(NSString *)dbName; +// init & close - (id)initWithDBWithPath:(NSString *)dbPath; - - (void)createTableWithName:(NSString *)tableName; - -- (void)clearTable:(NSString *)tableName; - +- (void)clearTableWithName:(NSString *)tableName; - (void)close; -///************************ Put&Get methods ***************************************** - -- (void)putObject:(id)object withId:(NSString *)objectId intoTable:(NSString *)tableName; - -- (id)getObjectById:(NSString *)objectId fromTable:(NSString *)tableName; - -- (YTKKeyValueItem *)getYTKKeyValueItemById:(NSString *)objectId fromTable:(NSString *)tableName; - -- (void)putString:(NSString *)string withId:(NSString *)stringId intoTable:(NSString *)tableName; - -- (NSString *)getStringById:(NSString *)stringId fromTable:(NSString *)tableName; -- (void)putNumber:(NSNumber *)number withId:(NSString *)numberId intoTable:(NSString *)tableName; +// use default table +- (void)putValue:(id)object forKey:(NSString *)key; +- (id)valueForKey:(NSString *)key; -- (NSNumber *)getNumberById:(NSString *)numberId fromTable:(NSString *)tableName; -- (NSArray *)getAllItemsFromTable:(NSString *)tableName; +// use custom table +- (void)putValue:(id)object forKey:(NSString *)key intoTable:(NSString *)tableName; +- (id)valueForKey:(NSString *)key fromTable:(NSString *)tableName; -- (void)deleteObjectById:(NSString *)objectId fromTable:(NSString *)tableName; -- (void)deleteObjectsByIdArray:(NSArray *)objectIdArray fromTable:(NSString *)tableName; +// clear +- (void)removeValueForKey:(NSString *)key fromTable:(NSString *)tableName; +- (void)removeValuesForKeys:(NSArray *)keys fromTable:(NSString *)tableName; -- (void)deleteObjectsByIdPrefix:(NSString *)objectIdPrefix fromTable:(NSString *)tableName; +// 模糊前缀删除, SQL = delete + like prefix%% +- (void)removeValuesByKeyPrefix:(NSString *)keyPrefix fromTable:(NSString *)tableName; @end diff --git a/YTKKeyValueStore/YTKKeyValueStore.m b/YTKKeyValueStore/YTKKeyValueStore.m index ea57d34..5bab88f 100644 --- a/YTKKeyValueStore/YTKKeyValueStore.m +++ b/YTKKeyValueStore/YTKKeyValueStore.m @@ -9,6 +9,7 @@ #import "YTKKeyValueStore.h" #import "FMDatabase.h" #import "FMDatabaseQueue.h" +#import "FMDatabaseAdditions.h" #ifdef DEBUG #define debugLog(...) NSLog(__VA_ARGS__) @@ -20,47 +21,61 @@ #define debugError() #endif -#define PATH_OF_DOCUMENT [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] +#pragma mark - +typedef NS_ENUM(NSUInteger, StoreValueType) { + kStoreValueType_String, + kStoreValueType_Number, + kStoreValueType_Collection, // JSON, NSArray, NSDictionary, NSData ... + kStoreValueType_Error, // 非法数据 +}; + +@interface YTKKeyValueItem : NSObject +@property (strong, nonatomic) NSString *key; +@property (strong, nonatomic) id value; +@property (assign, nonatomic) StoreValueType type; +@property (strong, nonatomic) NSDate *createdTime; +@end @implementation YTKKeyValueItem - (NSString *)description { - return [NSString stringWithFormat:@"id=%@, value=%@, timeStamp=%@", _itemId, _itemObject, _createdTime]; + return [NSString stringWithFormat:@"id=%@, \nvalue=%@, \ntimeStamp=%@", _key, _value, _createdTime]; } @end +#pragma mark - @interface YTKKeyValueStore() @property (strong, nonatomic) FMDatabaseQueue * dbQueue; @end + @implementation YTKKeyValueStore -static NSString *const DEFAULT_DB_NAME = @"database.sqlite"; +static NSString *const kDefault_DB_Name = @"database.sqlite"; +static NSString *const kDefault_Table_Name = @"store_table"; -static NSString *const CREATE_TABLE_SQL = +static NSString *const kCreate_Table_SQL = @"CREATE TABLE IF NOT EXISTS %@ ( \ id TEXT NOT NULL, \ json TEXT NOT NULL, \ +type integer NOT NULL, \ createdTime TEXT NOT NULL, \ PRIMARY KEY(id)) \ "; -static NSString *const UPDATE_ITEM_SQL = @"REPLACE INTO %@ (id, json, createdTime) values (?, ?, ?)"; - -static NSString *const QUERY_ITEM_SQL = @"SELECT json, createdTime from %@ where id = ? Limit 1"; - -static NSString *const SELECT_ALL_SQL = @"SELECT * from %@"; +static NSString *const kUpdate_Item_SQL = @"REPLACE INTO %@ (id, json, type, createdTime) values (?, ?, ?, ?)"; +static NSString *const kQuery_Item_SQL = @"SELECT json, type, createdTime from %@ where id = ? Limit 1"; -static NSString *const CLEAR_ALL_SQL = @"DELETE from %@"; +static NSString *const kSelect_All_SQL = @"SELECT * from %@"; +static NSString *const kClear_All_SQL = @"DELETE from %@"; -static NSString *const DELETE_ITEM_SQL = @"DELETE from %@ where id = ?"; +static NSString *const kDelete_Item_SQL = @"DELETE from %@ where id = ?"; +static NSString *const kDelete_Items_SQL = @"DELETE from %@ where id in ( %@ )"; +static NSString *const kDelete_Items_With_Prefix_SQL = @"DELETE from %@ where id like ? "; -static NSString *const DELETE_ITEMS_SQL = @"DELETE from %@ where id in ( %@ )"; - -static NSString *const DELETE_ITEMS_WITH_PREFIX_SQL = @"DELETE from %@ where id like ? "; + (BOOL)checkTableName:(NSString *)tableName { if (tableName == nil || tableName.length == 0 || [tableName rangeOfString:@" "].location != NSNotFound) { @@ -70,40 +85,25 @@ + (BOOL)checkTableName:(NSString *)tableName { return YES; } -- (id)init { - return [self initDBWithName:DEFAULT_DB_NAME]; -} - -- (id)initDBWithName:(NSString *)dbName { - self = [super init]; - if (self) { - NSString * dbPath = [PATH_OF_DOCUMENT stringByAppendingPathComponent:dbName]; - debugLog(@"dbPath = %@", dbPath); - if (_dbQueue) { - [self close]; - } - _dbQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath]; - } - return self; -} - - (id)initWithDBWithPath:(NSString *)dbPath { - self = [super init]; - if (self) { + + if (self = [super init]) { debugLog(@"dbPath = %@", dbPath); if (_dbQueue) { [self close]; } _dbQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath]; + [self createTableWithName:kDefault_Table_Name]; } return self; } - (void)createTableWithName:(NSString *)tableName { + if ([YTKKeyValueStore checkTableName:tableName] == NO) { return; } - NSString * sql = [NSString stringWithFormat:CREATE_TABLE_SQL, tableName]; + NSString * sql = [NSString stringWithFormat:kCreate_Table_SQL, tableName]; __block BOOL result; [_dbQueue inDatabase:^(FMDatabase *db) { result = [db executeUpdate:sql]; @@ -113,11 +113,12 @@ - (void)createTableWithName:(NSString *)tableName { } } -- (void)clearTable:(NSString *)tableName { - if ([YTKKeyValueStore checkTableName:tableName] == NO) { +- (void)clearTableWithName:(NSString *)tableName { + + if ([self tableExists:tableName]) { return; } - NSString * sql = [NSString stringWithFormat:CLEAR_ALL_SQL, tableName]; + NSString * sql = [NSString stringWithFormat:kClear_All_SQL, tableName]; __block BOOL result; [_dbQueue inDatabase:^(FMDatabase *db) { result = [db executeUpdate:sql]; @@ -127,155 +128,199 @@ - (void)clearTable:(NSString *)tableName { } } -- (void)putObject:(id)object withId:(NSString *)objectId intoTable:(NSString *)tableName { +- (BOOL)tableExists:(NSString *)tableName { if ([YTKKeyValueStore checkTableName:tableName] == NO) { + return NO; + } + + __block BOOL result; + [_dbQueue inDatabase:^(FMDatabase *db) { + result = [db tableExists:tableName]; + }]; + + if (!result) { + debugLog(@"ERROR, table: %@ not exists in current DB", tableName); + } + + return result; +} + +- (StoreValueType)typeWithValue:(id)value { + if ([value isKindOfClass:[NSString class]]) { + return kStoreValueType_String; + }else if ([value isKindOfClass:[NSNumber class]]) { + return kStoreValueType_Number; + }else{ + return kStoreValueType_Collection; + } +} + +#pragma mark - default table +- (void)putValue:(id)value forKey:(NSString *)key { + [self putValue:value forKey:key intoTable:kDefault_Table_Name]; +} +- (id)valueForKey:(NSString *)key { + return [self valueForKey:key fromTable:kDefault_Table_Name]; +} + +#pragma mark - custom table +- (void)putValue:(id)value forKey:(NSString *)key intoTable:(NSString *)tableName { + + if ([self tableExists:tableName]) { return; } - NSError * error; - NSData * data = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error]; - if (error) { - debugLog(@"ERROR, faild to get json data"); + + StoreValueType type = [self typeWithValue:value]; + if (type == kStoreValueType_Error) { return; } - NSString * jsonString = [[NSString alloc] initWithData:data encoding:(NSUTF8StringEncoding)]; + + // number => array + if (type == kStoreValueType_Number) { + value = @[value]; + } + + // NSData, NSNumber, NSArray, NSDictionary => JSON String + if (type == kStoreValueType_Number || type == kStoreValueType_Collection) { + NSError * error; + NSData * data = [NSJSONSerialization dataWithJSONObject:value options:0 error:&error]; + if (error) { + debugLog(@"ERROR, faild to get json data"); + return; + } + value = [[NSString alloc] initWithData:data encoding:(NSUTF8StringEncoding)]; + } + NSDate * createdTime = [NSDate date]; - NSString * sql = [NSString stringWithFormat:UPDATE_ITEM_SQL, tableName]; + NSString * sql = [NSString stringWithFormat:kUpdate_Item_SQL, tableName]; + __block BOOL result; [_dbQueue inDatabase:^(FMDatabase *db) { - result = [db executeUpdate:sql, objectId, jsonString, createdTime]; + result = [db executeUpdate:sql, key, value, @(type), createdTime]; }]; if (!result) { debugLog(@"ERROR, failed to insert/replace into table: %@", tableName); } } -- (id)getObjectById:(NSString *)objectId fromTable:(NSString *)tableName { - YTKKeyValueItem * item = [self getYTKKeyValueItemById:objectId fromTable:tableName]; +- (id)valueForKey:(NSString *)key fromTable:(NSString *)tableName { + YTKKeyValueItem * item = [self itemForKey:key fromTable:tableName]; if (item) { - return item.itemObject; + return item.value; } else { return nil; } } -- (YTKKeyValueItem *)getYTKKeyValueItemById:(NSString *)objectId fromTable:(NSString *)tableName { - if ([YTKKeyValueStore checkTableName:tableName] == NO) { +- (YTKKeyValueItem *)itemForKey:(NSString *)key fromTable:(NSString *)tableName { + + if ([self tableExists:tableName]) { return nil; } - NSString * sql = [NSString stringWithFormat:QUERY_ITEM_SQL, tableName]; - __block NSString * json = nil; + + NSString * sql = [NSString stringWithFormat:kQuery_Item_SQL, tableName]; + __block NSString * content = nil; __block NSDate * createdTime = nil; + __block NSInteger type = 0; [_dbQueue inDatabase:^(FMDatabase *db) { - FMResultSet * rs = [db executeQuery:sql, objectId]; + FMResultSet * rs = [db executeQuery:sql, key]; if ([rs next]) { - json = [rs stringForColumn:@"json"]; + type = [rs intForColumn:@"type"]; + content = [rs stringForColumn:@"json"]; createdTime = [rs dateForColumn:@"createdTime"]; } [rs close]; }]; - if (json) { - NSError * error; - id result = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] - options:(NSJSONReadingAllowFragments) error:&error]; - if (error) { - debugLog(@"ERROR, faild to prase to json"); - return nil; + + if (content) { + + id result = nil; + if (type == kStoreValueType_String) { + result = content; } + + // numuber = array[0] + if (type == kStoreValueType_Collection || type == kStoreValueType_Number) { + NSError * error; + result = [NSJSONSerialization JSONObjectWithData:[content dataUsingEncoding:NSUTF8StringEncoding] + options:(NSJSONReadingAllowFragments) error:&error]; + if (error) { + debugLog(@"ERROR, faild to prase to json"); + return nil; + } + } + + if (type == kStoreValueType_Number) { + result = [result objectAtIndex:0]; + } + YTKKeyValueItem * item = [[YTKKeyValueItem alloc] init]; - item.itemId = objectId; - item.itemObject = result; + item.key = key; + item.value = result; item.createdTime = createdTime; + item.type = type; return item; } else { return nil; } } -- (void)putString:(NSString *)string withId:(NSString *)stringId intoTable:(NSString *)tableName { - if (string == nil) { - debugLog(@"error, string is nil"); - return; - } - [self putObject:@[string] withId:stringId intoTable:tableName]; -} - -- (NSString *)getStringById:(NSString *)stringId fromTable:(NSString *)tableName { - NSArray * array = [self getObjectById:stringId fromTable:tableName]; - if (array && [array isKindOfClass:[NSArray class]]) { - return array[0]; - } - return nil; -} - -- (void)putNumber:(NSNumber *)number withId:(NSString *)numberId intoTable:(NSString *)tableName { - if (number == nil) { - debugLog(@"error, number is nil"); - return; - } - [self putObject:@[number] withId:numberId intoTable:tableName]; -} - -- (NSNumber *)getNumberById:(NSString *)numberId fromTable:(NSString *)tableName { - NSArray * array = [self getObjectById:numberId fromTable:tableName]; - if (array && [array isKindOfClass:[NSArray class]]) { - return array[0]; - } - return nil; -} - -- (NSArray *)getAllItemsFromTable:(NSString *)tableName { - if ([YTKKeyValueStore checkTableName:tableName] == NO) { +- (NSArray *)allItemsFromTable:(NSString *)tableName { + + if ([self tableExists:tableName]) { return nil; } - NSString * sql = [NSString stringWithFormat:SELECT_ALL_SQL, tableName]; + NSString * sql = [NSString stringWithFormat:kSelect_All_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"]; - item.itemObject = [rs stringForColumn:@"json"]; + item.key = [rs stringForColumn:@"id"]; + item.value = [rs stringForColumn:@"json"]; item.createdTime = [rs dateForColumn:@"createdTime"]; [result addObject:item]; } [rs close]; }]; - // parse json string to object + // parse json string to value NSError * error; for (YTKKeyValueItem * item in result) { error = nil; - id object = [NSJSONSerialization JSONObjectWithData:[item.itemObject dataUsingEncoding:NSUTF8StringEncoding] + id value = [NSJSONSerialization JSONObjectWithData:[item.value dataUsingEncoding:NSUTF8StringEncoding] options:(NSJSONReadingAllowFragments) error:&error]; if (error) { debugLog(@"ERROR, faild to prase to json."); } else { - item.itemObject = object; + item.value = value; } } return result; } -- (void)deleteObjectById:(NSString *)objectId fromTable:(NSString *)tableName { - if ([YTKKeyValueStore checkTableName:tableName] == NO) { +- (void)removeValueForKey:(NSString *)key fromTable:(NSString *)tableName { + + if ([self tableExists:tableName]) { return; } - NSString * sql = [NSString stringWithFormat:DELETE_ITEM_SQL, tableName]; + NSString * sql = [NSString stringWithFormat:kDelete_Item_SQL, tableName]; __block BOOL result; [_dbQueue inDatabase:^(FMDatabase *db) { - result = [db executeUpdate:sql, objectId]; + result = [db executeUpdate:sql, key]; }]; if (!result) { debugLog(@"ERROR, failed to delete item from table: %@", tableName); } } -- (void)deleteObjectsByIdArray:(NSArray *)objectIdArray fromTable:(NSString *)tableName { - if ([YTKKeyValueStore checkTableName:tableName] == NO) { +- (void)removeValuesForKeys:(NSArray *)keys fromTable:(NSString *)tableName { + + if ([self tableExists:tableName]) { return; } NSMutableString *stringBuilder = [NSMutableString string]; - for (id objectId in objectIdArray) { - NSString *item = [NSString stringWithFormat:@" '%@' ", objectId]; + for (id key in keys) { + NSString *item = [NSString stringWithFormat:@" '%@' ", key]; if (stringBuilder.length == 0) { [stringBuilder appendString:item]; } else { @@ -283,7 +328,7 @@ - (void)deleteObjectsByIdArray:(NSArray *)objectIdArray fromTable:(NSString *)ta [stringBuilder appendString:item]; } } - NSString *sql = [NSString stringWithFormat:DELETE_ITEMS_SQL, tableName, stringBuilder]; + NSString *sql = [NSString stringWithFormat:kDelete_Items_SQL, tableName, stringBuilder]; __block BOOL result; [_dbQueue inDatabase:^(FMDatabase *db) { result = [db executeUpdate:sql]; @@ -293,12 +338,13 @@ - (void)deleteObjectsByIdArray:(NSArray *)objectIdArray fromTable:(NSString *)ta } } -- (void)deleteObjectsByIdPrefix:(NSString *)objectIdPrefix fromTable:(NSString *)tableName { - if ([YTKKeyValueStore checkTableName:tableName] == NO) { +- (void)removeValuesByKeyPrefix:(NSString *)keyPrefix fromTable:(NSString *)tableName { + + if ([self tableExists:tableName]) { return; } - NSString *sql = [NSString stringWithFormat:DELETE_ITEMS_WITH_PREFIX_SQL, tableName]; - NSString *prefixArgument = [NSString stringWithFormat:@"%@%%", objectIdPrefix]; + NSString *sql = [NSString stringWithFormat:kDelete_Items_With_Prefix_SQL, tableName]; + NSString *prefixArgument = [NSString stringWithFormat:@"%@%%", keyPrefix]; __block BOOL result; [_dbQueue inDatabase:^(FMDatabase *db) { result = [db executeUpdate:sql, prefixArgument]; diff --git a/YTKKeyValueStoreTests/YTKKeyValueStoreTests.m b/YTKKeyValueStoreTests/YTKKeyValueStoreTests.m index 606ae8e..8975dcf 100644 --- a/YTKKeyValueStoreTests/YTKKeyValueStoreTests.m +++ b/YTKKeyValueStoreTests/YTKKeyValueStoreTests.m @@ -24,50 +24,84 @@ - (void)setUp { _tableName = @"test_table"; _store = [[YTKKeyValueStore alloc] init]; [_store createTableWithName:_tableName]; - [_store clearTable:_tableName]; + [_store clearTableWithName:_tableName]; } - (void)tearDown { - [_store clearTable:_tableName]; + [_store clearTableWithName:_tableName]; [_store close]; _store = nil; [super tearDown]; } - (void)testSaveNSString { +// NSString *str1 = @"abc"; +// NSString *key1 = @"key1"; +// NSString *str2 = @"abc2"; +// NSString *key2 = @"key2"; +// [_store putString:str1 forKey:key1 intoTable:_tableName]; +// [_store putString:str2 forKey:key2 intoTable:_tableName]; +// +// NSString *result; +// result = [_store getStringByID:key1 fromTable:_tableName]; +// XCTAssertEqualObjects(str1, result); +// result = [_store getStringByID:key2 fromTable:_tableName]; +// XCTAssertEqualObjects(str2, result); +// +// result = [_store getStringByID:@"key3" fromTable:_tableName]; +// XCTAssertNil(result); + NSString *str1 = @"abc"; NSString *key1 = @"key1"; NSString *str2 = @"abc2"; NSString *key2 = @"key2"; - [_store putString:str1 withId:key1 intoTable:_tableName]; - [_store putString:str2 withId:key2 intoTable:_tableName]; + [_store putValue:str1 forKey:key1 intoTable:_tableName]; + [_store putValue:str2 forKey:key2 intoTable:_tableName]; NSString *result; - result = [_store getStringById:key1 fromTable:_tableName]; + result = [_store valueForKey:key1 fromTable:_tableName]; XCTAssertEqualObjects(str1, result); - result = [_store getStringById:key2 fromTable:_tableName]; + result = [_store valueForKey:key2 fromTable:_tableName]; XCTAssertEqualObjects(str2, result); - result = [_store getStringById:@"key3" fromTable:_tableName]; + result = [_store valueForKey:@"key3" fromTable:_tableName]; XCTAssertNil(result); } - (void)testSaveNumber { +// NSNumber *number1 = @1; +// NSString *key1 = @"key1"; +// NSNumber *number2 = @2; +// NSString *key2 = @"key2"; +// [_store putNumber:number1 forKey:key1 intoTable:_tableName]; +// [_store putNumber:number2 forKey:key2 intoTable:_tableName]; +// +// NSNumber *result; +// result = [_store getNumberByID:key1 fromTable:_tableName]; +// XCTAssertEqualObjects(number1, result); +// result = [_store getNumberByID:key2 fromTable:_tableName]; +// XCTAssertEqualObjects(number2, result); +// +// result = [_store getNumberByID:@"key3" fromTable:_tableName]; +// XCTAssertNil(result); + NSNumber *number1 = @1; NSString *key1 = @"key1"; NSNumber *number2 = @2; NSString *key2 = @"key2"; - [_store putNumber:number1 withId:key1 intoTable:_tableName]; - [_store putNumber:number2 withId:key2 intoTable:_tableName]; + + [_store putValue:number1 forKey:key1 intoTable:_tableName]; + [_store putValue:number2 forKey:key2 intoTable:_tableName]; NSNumber *result; - result = [_store getNumberById:key1 fromTable:_tableName]; + result = [_store valueForKey:key1 fromTable:_tableName]; XCTAssertEqualObjects(number1, result); - result = [_store getNumberById:key2 fromTable:_tableName]; + result = [_store valueForKey:key2 fromTable:_tableName]; XCTAssertEqualObjects(number2, result); - result = [_store getNumberById:@"key3" fromTable:_tableName]; + result = [_store valueForKey:@"key3" fromTable:_tableName]; XCTAssertNil(result); + } - (void)testPerformanceExample {