手動複製《YUDBModel.framework》到工程git
使用Cocoapods管理github
///設置數據庫路徑 extern void YUDBModel_SetupDBPath(NSString *path); ///設置對象歸檔路徑 extern void YUDBModel_SetupObjectPath(NSString *path); ///設置數據庫版本號 extern void YUDBModel_SetupDBVersion(NSString *version); ///打開日誌輸出 extern void YUDBModel_SetDBLog(BOOL on); ///刪除數據庫文件 extern BOOL YUDBModel_ClearDBFile();
struct User { __unsafe_unretained NSString *name; __unsafe_unretained NSString *email; int age; }; @interface BASEDBObj : NSObject @end @interface DBObj : BASEDBObj @property (copy, nonatomic, readonly) NSNumber *objId; @property (nonatomic, assign, readonly)Point *point; @property (nonatomic, assign, readonly)struct User *user; @property (nonatomic, assign, readonly)Method method; @property (nonatomic, assign, readonly)Ivar ivar; @property (nonatomic, assign, readonly)Category category; @property (nonatomic, assign, readonly)objc_property_t property; @property (copy, nonatomic, readonly) void(^Block)(void); @property (copy, nonatomic, readwrite) id obj; @property (copy, nonatomic, readwrite) Class classz; @property (assign, nonatomic, readwrite) SEL sel; @property (copy, nonatomic, readwrite) NSString *name; @property (copy, nonatomic, readonly) UserInfo *info; @property (copy, nonatomic, readonly) NSArray *list; @property (copy, nonatomic, readwrite) NSArray *array; @property (copy, nonatomic, readwrite) NSDictionary *dict; @property (copy, nonatomic, readwrite) NSData *data; @property (copy, nonatomic, readwrite) NSDate *date; @property (copy, nonatomic, readwrite) NSURL *url; @property (copy, nonatomic, readwrite) NSNumber *number; @property (copy, nonatomic, readwrite) NSValue *value; @property (copy, nonatomic, readwrite) UIImage *image; @property (copy, nonatomic, readwrite) UIColor *color; @property (assign, nonatomic, readwrite) char countC; @property (assign, nonatomic, readwrite) unsigned char countUC; @property (assign, nonatomic, readwrite) short countS; @property (assign, nonatomic, readwrite) unsigned short countUS; @property (assign, nonatomic, readwrite) int countI; @property (assign, nonatomic, readwrite) unsigned int countUI; @property (assign, nonatomic, readwrite) long countL; @property (assign, nonatomic, readwrite) unsigned long countUL; @property (assign, nonatomic, readwrite) long long countLL; @property (assign, nonatomic, readwrite) unsigned long long countULL; @property (assign, nonatomic, readwrite) float countF; @property (assign, nonatomic, readwrite) double countD; @property (assign, nonatomic, readwrite) BOOL countB; @property (assign, nonatomic, readwrite) NSInteger integer; @property (assign, nonatomic, readwrite) NSUInteger uinteger; @end
@{ @"name":@"BruceYu", @"id":@"0001", @"countF":@"1.6", @"countD":@"1.6", @"countLL":@"6", @"countUL":@"66", @"countULL":@"666", @"countS":@(100), @"countUS":@(99), @"countB":@"1", @"integer":@"1", @"uinteger":@"1", @"countC":@'c', @"countUC":@'c', @"url":@"https://github.com/c6357/YUDBModel", @"dict":@{ @"name":@"(null)", @"phone":@"18512345678", @"age":@(66), @"sex":@(1), }, @"info":@{ @"name":@"bruce", @"phone":@"(null)", @"age":@(66), @"sex":@(1), @"infoLevel1" : @{ @"infoLevel2" : @{ @"infoLevel3" : @{ @"infoLevel4" : @{ @"infoLevel5" : @{ @"infoLevel6" : @{ @"name6" : @"level6", }, @"name5" : @"level5", }, @"name4" : @"level4", }, @"name3" : @"level3", }, @"name2" : @"level2", }, @"name1" : @"level1", @"phone1" : @"phone1", }, }, @"list":@[ @{ @"name":@"name1", @"phone":@"18500000001", @"age":@(11), @"sex":@(2), }, @{ @"name":@"(null)", @"phone":@"18500000002", @"age":@(22), @"sex":@(2), }, @{ @"name":@"name3", @"phone":@"18500000003", @"age":@(33), @"sex":@(3), }, ], };
///把json轉換成對象 DBObj *dbObj = [DBObj modelWithDictionary:json];
/** 反序列化JSON須要替換的KEY @return @{@"mode key":@"json key"} */ +(NSDictionary <NSString *, NSString*> *)YUDBModel_ReplacePropertyKey; /** 須要過濾在數據庫表中的特殊字段 不儲存的字段 @return e.g. return @[@"desrc"] */ +(NSArray<NSString*> *)YUDBModel_IgnoreFields; /** 設置主鍵 @return 主鍵的 keyName */ +(NSString*)YUDBModel_PrimaryKey; /** 反序列化json自定義操做(一般用於NSArray和特殊處理) @param key key值 @param value value值 @return 根據key和value返回相應的對象 e.g. -(void)deserialize:(NSDictionary *)dictionary { [super deserialize:dictionary arrayParser:^id(NSString *key,id value) { if ([key isEqualToString:@"list"]) { return [UserInfo class]; } else if ([key isEqualToString:@"array"]) { return @[@"1",@"2",@"3"];//自定義數組 } else if ([key isEqualToString:@"name"]) { return @"自定義名字"; } return nil; }]; }
///把對象轉換成json NSDictionary * json = [dbObj dictionary];
///直接插入一條數據 [NSObject insert:dbObj]; ///直接插入多條數據 [NSObject insertObjs:@[dbObj]]; ///保存一條數據(根據rowid查找 if數據存在更新else插入) [NSObject save:dbObj]; ///保存多條數據(根據rowid查找 if數據存在更新else插入) [NSObject saveObjs:@[dbObj]]; ///保存一條數據(根據key值條件查找 if數據存在更新else插入) [NSObject save:dbObj withKey:@"name"]; ///保存一條數據(根據多個key值條件查找 if數據存在更新else插入) [NSObject save:dbObj withKeys:@[@"name"]]; ///更新一條數據(只執行更新語句) [NSObject update:dbObj where:@"name = 'BruceYu'"];
///刪除表dbObj [obj Class] [NSObject delete:[dbObj class]]; ///根據條件key刪除表[object class]數據 [NSObject delete:dbObj WithKey:@"name"]; ///根據多個條件key刪除表[object class]數據 [NSObject delete:dbObj withKeys:@[@"name"]]; ///根據條件刪除表[obj Class]數據 [NSObject delete:dbObj where:@"name = 'BruceYu' "];
///舉個栗子 DBObj *obj = [DBObj queryWithkey:@"name" value:@"BruceYu"]; if (nil == obj) { obj = [[DBObj query:[DBObj class]where:@"info.infoLevel1.infoLevel2.infoLevel3.infoLevel4.name4 = 'level4'"] firstObject]; } if (nil == obj) { obj = [[DBObj query:[DBObj class]] firstObject]; } obj.integer = 100; obj.name = @"hello"; [obj update];
#pragma mark - 簡單查詢 /** 查詢表[self class]全部數據 @return array */ + (NSArray *)query; /** 查詢表[obj Class]全部數據 @param objClass 數據模型 @return array */ + (NSArray *)query:(Class)objClass; /** 查詢表[self class]數據 @param key 查詢條件鍵key @param value 查詢條件鍵valus @return 符合條件的第一條數據 */ + (id)queryWithkey:(NSString*)key value:(NSString*)value; /** 查詢表[self class]數據 @param key 查詢條件鍵key @param value 查詢條件鍵valus @return array */ + (NSArray *)query:(NSString*)key value:(NSString*)value; /** 查詢表[self class]數據 @param keyValues 查詢條件鍵的{key:valus} @return array */ + (NSArray *)queryWithkeyValues:(NSDictionary*)keyValues; #pragma mark - 高級查詢 /** 查詢表[obj Class]數據 @param objClass 數據模型 @param where where條件 @return array */ + (NSArray *)query:(Class)objClass where:(NSString *)where; /** 查詢表[obj Class]數據 @param objClass 數據模型 @param order order by 條件 @return array */ + (NSArray *)query:(Class)objClass order:(NSString *)order; /** 查詢表[obj Class]數據 @param objClass 數據模型 @param limit LIMIT 語句 @return array */ + (NSArray *)query:(Class)objClass limit:(NSString *)limit; /** 查詢表[obj Class]數據 @param objClass 數據模型 @param where where條件 @param order order by @return array */ + (NSArray *)query:(Class)objClass where:(NSString *)where order:(NSString *)order; /** 查詢表[obj Class]數據 @param objClass 數據模型 @param where where條件 @param limit limit @return array */ + (NSArray *)query:(Class)objClass where:(NSString *)where limit:(NSString *)limit; /** 查詢表[obj Class]數據 @param objClass 數據模型 @param order order by @param limit limit @return array */ + (NSArray *)query:(Class)objClass order:(NSString *)order limit:(NSString *)limit; /** 查詢表[obj Class]數據 @param objClass 數據模型 @param where where條件 @param order order by @param limit limit @return array */ + (NSArray *)query:(Class)objClass where:(NSString *)where order:(NSString *)order limit:(NSString *)limit; #pragma mark - 自助高級查詢 /** 查詢數據 @param objClass 須要反序列化的數據模型objClass @param sql 純sql語句 @return array @[objClass,objClass,objClass.....] */ + (NSArray *)query:(Class)objClass sql:(NSString *)sql; /** 執行一條sql更新語句 @param sql 純sql語句 @return 執行結果 */ + (BOOL)executeUpdateWithSql:(NSString *)sql;
///存檔一個對象 static NSString *const archiveName = @"DBObj.archive"; BOOL result = NO; if ((result = [NSObject archiveObject:dbObj toName:archiveName])) { NSLog(@"歸檔成功:%@",NSObject.archivePath); }
/// 解檔一個對象 DBObj *obj = [NSObject unarchiveObjectWithName:archiveName]; NSLog(@"解檔 DBObj---%@",(DBObj*)obj.name);
代碼地址sql