diff --git a/Classes/FMDTKeyValueStorage.h b/Classes/FMDTKeyValueStorage.h new file mode 100644 index 0000000..067933b --- /dev/null +++ b/Classes/FMDTKeyValueStorage.h @@ -0,0 +1,83 @@ +// +// FMDTKeyValueStorage.h +// FMDataTable +// +// Created by bing.hao on 2017/2/22. +// Copyright © 2017年 bing.hao. All rights reserved. +// + +#import +#import "FMDTManager.h" + +#define STORE [FMDTKeyValueStorage shared] + +@interface FMDTKeyValueStorage : FMDTManager + +/** + 添加键值存储 + + @param key 关键字 + @param value 具体的值,可以是NSString, NSNumber, Dictionary, Array等类型 + */ +- (void)set:(NSString *)key value:(id)value; + + +/** + 获取值 + + @param key 关键字 + @return 返回值 + */ +- (id)objectForKey:(NSString *)key; + + +/** + 获取值 + + @param key 关键字 + @return 返回值 + */ +- (NSString *)stringForKey:(NSString *)key; + + +/** + 获取值 + + @param key 关键字 + @return 返回值 + */ +- (NSNumber *)numberForKey:(NSString *)key; + + +/** + 获取值 + + @param key 关键字 + @return 返回值 + */ +- (NSDictionary *)dictionaryForKey:(NSString *)key; + + +/** + 获取值 + + @param key 关键字 + @return 返回值 + */ +- (NSArray *)arrayForKey:(NSString *)key; + + +/** + 删除 + + @param key 关键字 + */ +- (void)removeForKey:(NSString *)key; + + +/** + 删除所有 + */ +- (void)removeAll; + +@end diff --git a/Classes/FMDTKeyValueStorage.m b/Classes/FMDTKeyValueStorage.m new file mode 100644 index 0000000..573b80f --- /dev/null +++ b/Classes/FMDTKeyValueStorage.m @@ -0,0 +1,128 @@ +// +// FMDTKeyValueStorage.m +// FMDataTable +// +// Created by bing.hao on 2017/2/22. +// Copyright © 2017年 bing.hao. All rights reserved. +// + +#import "FMDTKeyValueStorage.h" +#import "FMDTObject.h" + +@interface FMDTKeyValueObject : FMDTObject + +@property (nonatomic, strong) NSString *m_key; +@property (nonatomic, strong) NSDictionary *m_value; +//@property (nonatomic, strong) NSString *m_type; + +@end + +@implementation FMDTKeyValueObject + ++ (NSString *)primaryKeyFieldName { + return @"m_key"; +} + +@end + +@interface FMDTKeyValueStorage () + +@property (nonatomic, strong) FMDTContext *kv; + +@end + +@implementation FMDTKeyValueStorage + ++ (instancetype)shared { + static id _staticObject = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + _staticObject = [FMDTKeyValueStorage new]; + }); + return _staticObject; +} + +- (FMDTContext *)kv { +// NSMutableDictionary *s; +// [s set] + NSString *path = [NSString stringWithFormat:@"%@/kv.db", [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]]; + return [self cacheWithClass:[FMDTKeyValueObject class] dbPath:path]; +} + +//- (void)set:(NSString *)key valueForString:(NSString *)value { +// +//} +// +//- (void)set:(NSString *)key valueForNumber:(NSNumber *)value { +// +//} +// +//- (void)set:(NSString *)key valueForDictionary:(NSDictionary *)value { +// +//} +// +//- (void)set:(NSString *)key valueForArray:(NSArray *)value { +// +//} +// + +- (void)set:(NSString *)key value:(id)value { + if (key == nil || value == nil) { + return; + } + FMDTKeyValueObject *obj = [[FMDTKeyValueObject alloc] init]; + obj.m_key = key; + obj.m_value = @{ @"value": value }; + FMDTInsertCommand *cmd = [self.kv createInsertCommand]; + [cmd add:obj]; + [cmd setRelpace:YES]; + [cmd saveChanges]; +} + +- (id)objectForKey:(NSString *)key { + + if (key == nil) { + return nil; + } + + FMDTSelectCommand *cmd = [self.kv createSelectCommand]; + [cmd where:@"m_key" equalTo:key]; + FMDTKeyValueObject *obj = [cmd fetchObject]; + if (obj) { + return [obj.m_value objectForKey:@"value"]; + } + return nil; +} + +- (NSString *)stringForKey:(NSString *)key { + return [self objectForKey:key]; +} + +- (NSNumber *)numberForKey:(NSString *)key { + return [self objectForKey:key]; +} + +- (NSDictionary *)dictionaryForKey:(NSString *)key { + return [self objectForKey:key]; +}; + +- (NSArray *)arrayForKey:(NSString *)key { + return [self objectForKey:key]; +} + +- (void)removeForKey:(NSString *)key { + + if (key == nil) { + return; + } + FMDTDeleteCommand *cmd = [self.kv createDeleteCommand]; + [cmd where:@"m_key" equalTo:key]; + [cmd saveChanges]; +} + +- (void)removeAll { + FMDTDeleteCommand *cmd = [self.kv createDeleteCommand]; + [cmd saveChanges]; +} + +@end diff --git a/FMDBDataTable.podspec b/FMDBDataTable.podspec index 90b66ee..73cae3a 100644 --- a/FMDBDataTable.podspec +++ b/FMDBDataTable.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |s| # s.name = "FMDBDataTable" - s.version = "2.1.6" + s.version = "2.2" s.summary = "基于FMDB的一个ORM解决方案" s.description = <<-DESC @@ -78,7 +78,7 @@ Pod::Spec.new do |s| # Supports git, hg, bzr, svn and HTTP. # - s.source = { :git => "https://github.com/bing6/FMDBDataTable.git", :tag => "2.1.6" } + s.source = { :git => "https://github.com/bing6/FMDBDataTable.git", :tag => "2.2" } # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # diff --git a/FMDataTable.xcodeproj/project.pbxproj b/FMDataTable.xcodeproj/project.pbxproj index c092263..d365aea 100644 --- a/FMDataTable.xcodeproj/project.pbxproj +++ b/FMDataTable.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ 37477D521C8FC1E500924741 /* FMDTUpdateObjectCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = 37477D511C8FC1E500924741 /* FMDTUpdateObjectCommand.m */; }; 37477D551C8FE14C00924741 /* Users.m in Sources */ = {isa = PBXBuildFile; fileRef = 37477D541C8FE14C00924741 /* Users.m */; }; 37477D581C8FE1E600924741 /* DBSet.m in Sources */ = {isa = PBXBuildFile; fileRef = 37477D571C8FE1E600924741 /* DBSet.m */; }; + 374AEB451E5D4DAA00828FDA /* FMDTKeyValueStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = 374AEB441E5D4DAA00828FDA /* FMDTKeyValueStorage.m */; }; 3765CCA81C8E755100CB32B2 /* FMDTContext.m in Sources */ = {isa = PBXBuildFile; fileRef = 3765CCA71C8E755100CB32B2 /* FMDTContext.m */; }; 3765CCAB1C8E755D00CB32B2 /* FMDTSchema.m in Sources */ = {isa = PBXBuildFile; fileRef = 3765CCAA1C8E755D00CB32B2 /* FMDTSchema.m */; }; 3765CCAE1C8E756800CB32B2 /* FMDTObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 3765CCAD1C8E756800CB32B2 /* FMDTObject.m */; }; @@ -64,6 +65,9 @@ 37477D541C8FE14C00924741 /* Users.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Users.m; sourceTree = ""; }; 37477D561C8FE1E600924741 /* DBSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DBSet.h; sourceTree = ""; }; 37477D571C8FE1E600924741 /* DBSet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DBSet.m; sourceTree = ""; }; + 374AEB431E5D4DAA00828FDA /* FMDTKeyValueStorage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FMDTKeyValueStorage.h; sourceTree = ""; }; + 374AEB441E5D4DAA00828FDA /* FMDTKeyValueStorage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FMDTKeyValueStorage.m; sourceTree = ""; }; + 374AEB461E5D5FD200828FDA /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 3765CCA61C8E755100CB32B2 /* FMDTContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FMDTContext.h; sourceTree = ""; }; 3765CCA71C8E755100CB32B2 /* FMDTContext.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FMDTContext.m; sourceTree = ""; }; 3765CCA91C8E755D00CB32B2 /* FMDTSchema.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FMDTSchema.h; sourceTree = ""; }; @@ -135,6 +139,7 @@ 371FF0041B93E0DD001D59BB = { isa = PBXGroup; children = ( + 374AEB461E5D5FD200828FDA /* README.md */, 37FC779E1CFFD23F009F7FA1 /* FMDBDataTable.podspec */, 371FF0491B93F3C4001D59BB /* Classes */, 371FF00F1B93E0DD001D59BB /* FMDataTable */, @@ -219,6 +224,8 @@ 3765CCB91C8E77A300CB32B2 /* FMDTDeleteCommand.m */, 3765CCBE1C8ED00300CB32B2 /* FMDTManager.h */, 3765CCBF1C8ED00300CB32B2 /* FMDTManager.m */, + 374AEB431E5D4DAA00828FDA /* FMDTKeyValueStorage.h */, + 374AEB441E5D4DAA00828FDA /* FMDTKeyValueStorage.m */, ); path = Classes; sourceTree = ""; @@ -396,6 +403,7 @@ 3765CCB71C8E777300CB32B2 /* FMDTUpdateCommand.m in Sources */, 3765CCB41C8E774400CB32B2 /* FMDTInsertCommand.m in Sources */, 3765CCBA1C8E77A300CB32B2 /* FMDTDeleteCommand.m in Sources */, + 374AEB451E5D4DAA00828FDA /* FMDTKeyValueStorage.m in Sources */, 37B305EE1C910B510079B2B1 /* Message.m in Sources */, 3765CCB11C8E757600CB32B2 /* FMDTSelectCommand.m in Sources */, 3765CCC01C8ED00300CB32B2 /* FMDTManager.m in Sources */, @@ -529,6 +537,7 @@ baseConfigurationReference = DAAD600BEDF629331279BE95 /* Pods.debug.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = FMDataTable/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 7.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; @@ -541,6 +550,7 @@ baseConfigurationReference = B248441247E193317B6F9625 /* Pods.release.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = FMDataTable/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 7.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; diff --git a/FMDataTable/Info.plist b/FMDataTable/Info.plist index d31dabb..3e5b98a 100644 --- a/FMDataTable/Info.plist +++ b/FMDataTable/Info.plist @@ -15,11 +15,11 @@ CFBundlePackageType APPL CFBundleShortVersionString - 2.1.6 + 2.2 CFBundleSignature ???? CFBundleVersion - 2.1.6 + 2.2 LSRequiresIPhoneOS UILaunchStoryboardName diff --git a/FMDataTable/ViewController.m b/FMDataTable/ViewController.m index 7828544..b053e12 100644 --- a/FMDataTable/ViewController.m +++ b/FMDataTable/ViewController.m @@ -8,6 +8,7 @@ #import "ViewController.h" #import "DBSet.h" +#import "FMDTKeyValueStorage.h" @interface ViewController () @@ -190,18 +191,45 @@ - (void)dyInsertData { } +- (void)testKeyValue { + + //字符串存储 + [STORE set:@"a1" value:@"我是字符串"]; + //数值存储 + [STORE set:@"a2" value:@(1)]; + //字典存储 + [STORE set:@"a3" value:@{@"test1": @"abc", @"test2": @"abc222"}]; + //数组存储 + [STORE set:@"a4" value:@[@(1),@(2),@(3),@(4)]]; + + //读取字符串 + NSLog(@"%@", [STORE stringForKey:@"a1"]); + //读取数值 + NSLog(@"%@", [STORE stringForKey:@"a2"]); + //读取字典 + NSLog(@"%@", [STORE stringForKey:@"a3"]); + //读取数组 + NSLog(@"%@", [STORE stringForKey:@"a4"]); + + //删除键值 + [STORE removeForKey:@"a1"]; + //清除所有的键值 + [STORE removeAll]; +} + - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%@", [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]); - [self insertData]; //插入数据 +// [self insertData]; //插入数据 // [self updateData]; //更新数据 // [self deleteData]; //删除数据 // [self selectData]; //查询数据 // // [self dyInsertData]; //向动态表里添加数据 + [self testKeyValue]; } - (void)didReceiveMemoryWarning { diff --git a/README.md b/README.md index ac2aa4b..2578c2c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # FMDBDataTable 这是一个简易的ORM工具,只需要几步就可以完成对单表的CRUD操作. - + + # 建立模型 @interface Users : FMDTObject @@ -181,6 +182,31 @@ [cmd setSkip:10]; [cmd fetchArray]; +# 基于KeyValue的存储功能 + + //字符串存储 + [STORE set:@"a1" value:@"我是字符串"]; + //数值存储 + [STORE set:@"a2" value:@(1)]; + //字典存储 + [STORE set:@"a3" value:@{@"test1": @"abc", @"test2": @"abc222"}]; + //数组存储 + [STORE set:@"a4" value:@[@(1),@(2),@(3),@(4)]]; + + //读取字符串 + NSLog(@"%@", [STORE stringForKey:@"a1"]); + //读取数值 + NSLog(@"%@", [STORE stringForKey:@"a2"]); + //读取字典 + NSLog(@"%@", [STORE stringForKey:@"a3"]); + //读取数组 + NSLog(@"%@", [STORE stringForKey:@"a4"]); + + //删除键值 + [STORE removeForKey:@"a1"]; + //清除所有的键值 + [STORE removeAll]; + # Installation pod 'FMDBDataTable'