DAO layer Implementation code under iOS What is the DAO layer
The DAO data Access object is an object-oriented interface. Direct operation of the database, for the addition of data, delete, modify, find, specifically for the business logic layer or presentation layer to provide data services.
An example of a DAO
-(void) Saveremind: (Remindentity *) remind; Save a reminder-(void) Deleteremindwithwhere: (NSString *) where; Delete a reminder-(remindentity *) Loadremindwithremindid: (NSString *) rid; Read a reminder-(void) Deleteremindwithremindid: (NSString *) rid; Delete a reminder-(void) Deleteremindwithweihuaid: (NSString *) Weihuaid rcdid: (int) Rcdid; Delete a contact or a WeChat friend's reminder-(Nsarray *) Searchremindwithwhere: (NSString *) where order: (NSString *) Order limit: ( NSString *) limit; Find reminders-(Nsinteger) Remindcountwithwhere: (NSString *) where; Returns the number of reminders-(Nsarray *) allarrivalreminds; Return all non-expiring, next start time less than the current time reminder-(Nsarray *) allnonoverduereminds; Returns all non-expired reminders + (void) setupdb; Initializing the database
The internal implementation is using Fmdb to write a lot of SQL. If we have multiple entity classes, we have to write multiple DAO.
DAO of standardizing
Let's look at a generic DAO, you don't have to write SQL for each entity, you can inherit this class or write a factory class when you use it.
@protocol xybasedaoentityprotocol <NSObject> @required//Return table name + (NSString *) gettablename;//return primary key + (NSString *) GetPrimaryKey; @end//standardizing native DAO class @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
This DAO is based on Lkdbhelper, which passes the class of the entity object into the initialization parameter.
+ (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]; CREATE TABLE [_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 US Erinfo:nil]; nsmutablestring *str = [nsmutablestring stringwithformat:@ "[Save Error]:"]; Nsinteger length = str.length; __weak __typeof (Xybasedao *) weakself = self; Things 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 ge Tprimarykey], key]; } else if (1) {#pragma mark-todo multiple primary key types to determine 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];}
and finally the demo.
-(void) Clicktestdao: (ID) sender{Xybasedao *dao = [Xybasedao daowithentityclass:[carentity class]]; [DAO deleteallentity]; carentity *car = [[Carentity alloc] init]; Car.name = @ "a"; Car.brand = @ "Cruze"; Car.time = 1; [DAO Saveentity:car]; carentity *car2 = [[Carentity alloc] init]; Car2.name = @ "B"; Car2.brand = @ "Cruze"; Car2.time = 1; carentity *CAR3 = [[Carentity alloc] init]; Car3.name = @ "C"; Car3.brand = @ "Fox"; Car3.time = 1; carentity *CAR5 = [[Carentity alloc] init]; Car5.name = @ "D"; Car5.brand = @ "Golf"; Car5.time = 1; Nsarray *array = @[car2, Car3, CAR5]; [DAO Saveentitywitharray:array]; carentity *CAR4 = [dao loadentitywithkey:@ "C"]; NSLog (@ "%@", CAR4); Nsarray *array2 = [DAO loadentitywithwhere:@ "brand = ' Cruze '"]; NSLog (@ "%@", array2); Nsinteger count = [dao Countwithwhere:nil]; NSLog (@ "%d", count); [DAO deleteentitywithkey:@ "C"]; [DAO deleteentitywithkey:@ "brand = ' Cruze '"];}
DAO layer Implementation code under iOS