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

将 String 和 Number 接口都合并到 Object 中 #6

Open
wants to merge 5 commits 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.xcodeproj/project.pbxproj
Expand Up @@ -464,6 +464,7 @@
4597AE7919DEDFB30028ECAC /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
4597AE7A19DEDFB30028ECAC /* Build configuration list for PBXNativeTarget "YTKKeyValueStoreTests" */ = {
isa = XCConfigurationList;
Expand All @@ -472,6 +473,7 @@
4597AE7C19DEDFB30028ECAC /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
20 changes: 14 additions & 6 deletions YTKKeyValueStore/AppDelegate.m
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions YTKKeyValueStore/FMDB/FMDatabase.m
Expand Up @@ -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) {

Expand All @@ -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];
}
Expand Down Expand Up @@ -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.
Expand All @@ -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++;
}
Expand Down Expand Up @@ -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]);
Expand All @@ -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++;
Expand Down
2 changes: 1 addition & 1 deletion YTKKeyValueStore/FMDB/FMResultSet.m
Expand Up @@ -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];
Expand Down
45 changes: 14 additions & 31 deletions YTKKeyValueStore/YTKKeyValueStore.h
Expand Up @@ -8,50 +8,33 @@

#import <Foundation/Foundation.h>

@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