DAO (Data Access Object) 數據訪問對象是一個面向對象的接口. 直接操作數據庫, 針對數據的增添,刪除,修改,查找,具體為業務邏輯層或表示層提供數據服務.
- (void)saveRemind:(RemindEntity *)remind; // 保存一個提醒 - (void)deleteRemindWithWhere:(NSString *)where; // 刪除一個提醒 - (RemindEntity *) loadRemindWithRemindID:(NSString *)rid; // 讀取一個提醒 - (void)deleteRemindWithRemindID:(NSString *)rid; // 刪除一個提醒 - (void)deleteRemindWithWeihuaId:(NSString *)weihuaId rcdId:(int)rcdId; // 刪除一個聯系人或者微話好友的提醒 - (NSArray *)searchRemindWithWhere:(NSString *)where order:(NSString *)order limit:(NSString *)limit; // 查找提醒 - (NSInteger)remindCountWithWhere:(NSString *)where; // 返回提醒數量 - (NSArray *)allArrivalReminds; // 返回所有未過期, 下次開始時間小於現在時間的提醒 - (NSArray *)allNonOverdueReminds; // 返回所有未過期的提醒 + (void) setupDB; // 初始化數據庫
內部實現是用fmdb,得寫大量的sql.如果我們有多個實體類, 就得寫多個dao.
下面我們來看一個范化的dao, 就不用為每個實體專門寫sql了,具體使用的時候可以繼承這個類或者寫個工廠類.
@protocol XYBaseDaoEntityProtocol@required // 返回表名 + (NSString *)getTableName; // 返回主鍵 + (NSString *)getPrimaryKey; @end // 范化的本地dao類 @interface XYBaseDao : NSObject @property (nonatomic, weak, readonly) Class entityClass; + (instancetype)daoWithEntityClass:(Class)aClass; - (NSError *)saveEntity:(id)entity; - (NSError *)saveEntityWithArray:(NSArray *)array; - (id)loadEntityWithKey:(NSString *)key; - (NSArray *)loadEntityWithWhere:(NSString *)where; - (NSInteger)countWithWhere:(NSString *)where; - (NSError *)deleteEntityWithKey:(NSString *)key; - (NSError *)deleteEntityWithWhere:(NSString *)where; - (void)deleteAllEntity; @end
這個dao是基於LKDBHelper的, 把實體對象的類當初始化參數傳進去.
+ (instancetype)daoWithEntityClass:(Class)aClass { XYBaseDao *dao = [[[self class] alloc] initWithEntityClass:aClass]; return dao; } - (instancetype)initWithEntityClass:(Class)aClass { self = [super init]; if (self) { _entityClass = aClass; _globalHelper = [LKDBHelper getUsingLKDBHelper]; // 創建表 [_globalHelper createTableWithModelClass:[_entityClass class]]; } return self; }
- (NSError *)saveEntity:(id)entity { if (entity == nil) return [NSError errorWithDomain:[NSString stringWithFormat:@"[save error] :%@", entity] code:XYBaseDao_error_code userInfo:nil]; LKDBHelper *globalHelper = [LKDBHelper getUsingLKDBHelper]; if (![globalHelper insertToDB:entity]) { return [NSError errorWithDomain:[NSString stringWithFormat:@"[save error] :%@", entity] code:XYBaseDao_error_code userInfo:nil]; } return nil; }
- (NSError *)saveEntityWithArray:(NSArray *)array { if (array == nil || array.count == 0) return [NSError errorWithDomain:[NSString stringWithFormat:@"[save error] :%@", array] code:XYBaseDao_error_code userInfo:nil]; NSMutableString *str = [NSMutableString stringWithFormat:@"[save error] :"]; NSInteger length = str.length; __weak __typeof(XYBaseDao *) weakSelf = self; // 事務 transaction [_globalHelper executeDB:^(FMDatabase *db) { [db beginTransaction]; for (NSObject *entity in array) { if (![weakSelf.globalHelper insertToDB:entity]) { [str stringByAppendingFormat:@"%@ ", entity]; } } [db commit]; }]; if (str.length > length) { return [NSError errorWithDomain:str code:XYBaseDao_error_code userInfo:nil]; } return nil; }
- (id)loadEntityWithKey:(NSString *)key { if (key.length == 0) return nil; id object = [[_entityClass alloc] init]; NSString *where = nil; if ([key isKindOfClass:[NSString class]]) { where = [NSString stringWithFormat:@"%@ = '%@'", [_entityClass getPrimaryKey], key]; } else if ([key isKindOfClass:[NSNumber class]]) { where = [NSString stringWithFormat:@"%@ = %@", [_entityClass getPrimaryKey], key]; } else if (1) { #pragma mark- todo 多主鍵種類判斷 where = @""; } object = [_entityClass searchSingleWithWhere:where orderBy:nil]; return object; }
- (NSArray *)loadEntityWithWhere:(NSString *)where { if (where.length == 0) return nil; NSArray *array = [_entityClass searchWithWhere:where orderBy:nil offset:0 count:XYBaseDao_load_maxCount]; return array; }
- (NSInteger)countWithWhere:(NSString *)where { return [_entityClass rowCountWithWhere:where]; }
- (NSError *)deleteEntityWithKey:(NSString *)key { id object = [[_entityClass alloc] init]; [object setValue:key forKey:[_entityClass getPrimaryKey]]; if (![object deleteToDB]) { return [NSError errorWithDomain:[NSString stringWithFormat:@"[delete error] : %@ %@", _entityClass, key] code:XYBaseDao_error_code userInfo:nil]; } return nil; }
- (NSError *)deleteEntityWithWhere:(NSString *)where { if (![_entityClass deleteWithWhere:where]) { return [NSError errorWithDomain:[NSString stringWithFormat:@"[delete error] : %@ %@", _entityClass, where] code:XYBaseDao_error_code userInfo:nil]; } return nil; }
- (void)deleteAllEntity { [LKDBHelper clearTableData:[_entityClass class]]; }
最後是demo
- (void)clickTestDao:(id)sender { XYBaseDao *dao = [XYBaseDao daoWithEntityClass:[CarEntity class]]; [dao deleteAllEntity]; CarEntity *car = [[CarEntity alloc] init]; car.name = @"a"; car.brand = @"科魯茲"; car.time = 1; [dao saveEntity:car]; CarEntity *car2 = [[CarEntity alloc] init]; car2.name = @"b"; car2.brand = @"科魯茲"; car2.time = 1; CarEntity *car3 = [[CarEntity alloc] init]; car3.name = @"c"; car3.brand = @"福克斯"; car3.time = 1; CarEntity *car5 = [[CarEntity alloc] init]; car5.name = @"d"; car5.brand = @"高爾夫"; car5.time = 1; NSArray *array = @[car2, car3, car5]; [dao saveEntityWithArray:array]; CarEntity *car4 = [dao loadEntityWithKey:@"c"]; NSLog(@"%@", car4); NSArray *array2 = [dao loadEntityWithWhere:@"brand = '科魯茲'"]; NSLog(@"%@", array2); NSInteger count = [dao countWithWhere:nil]; NSLog(@"%d", count); [dao deleteEntityWithKey:@"c"]; [dao deleteEntityWithKey:@"brand = '科魯茲'"]; }