Provides a beautiful, simple ActiveRecord implementation to interact with the database.
(It's a component of HZExtend)
Email:[email protected]
- Add
pod 'HZORM
to your Podfile. - Run
pod install
orpod update
. - Import <HZORM/HZORM.h>.
Full API documentation is available on CocoaDocs.
This library requires iOS 8.0+
and Xcode 8.0+
.
NSString *dbPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/HZDatabase.db"];
[HZDatabaseManager sharedManager].dbPath = dbPath;
@interface Person : NSObject
@property(nonatomic, assign) NSInteger id;
@property(nonatomic, copy) NSString *pName;
@property(nonatomic, assign) NSInteger pAge;
@property(nonatomic, copy) NSArray *pBooks;
@end
//create table
NSString *createTableSql = @"create table Person(id integer primary key autoincrement autoincremen, name text not null, age integer, books text)";
[HZDBManager executeUpdate:createTableSql withParams:nil];
//implement structure methods
@implementation Person
+ (NSString *)getTabelName
{
return @"Person";
}
+ (NSDictionary<NSString *,NSString *> *)getColumnMap
{
return @{
@"id":@"id",
@"age":@"pAge",
@"name":@"pName",
@"books":@"pBooks"
};
}
+ (NSDictionary<NSString *,NSString *> *)getCasts
{
return @{
@"pBooks":@"NSArray"
};
}
+ (NSArray<NSString *> *)getPrimaryKeys
{
return @[@"id"];
}
@end
//insert or update
Person *p = [[Person alloc] init];
p.pName = @"GeniusBrother";
p.pAge = 23;
p.pBooks = @[@"IOS",@"PHP",@"JAVA"];
[p save];
//remove
[p remove];
//insert multiple models
[Person insert:@[p1,p2,p3]];
//Removes all models in table
[Person remove];
//Retrieves a model by its primary key.
Person *p = [Person find:@1];
//Get the first record matching the attributes.
Person *p = [Person firstWithKeyValues:@{@"name":@"GeniusBrother"}];
//Gets the all ORM models in table
NSArray *models = [Person all];
//Gets eligible models containing all columns
NSArray *models = [[[Person search:@[@"*"]] where:@{@"age",@23}] get];
//Gets first 10 eligible models.
NSArray *models = [[[[Person search:@[@"name",@"age"]] whereRaw:@"age > 23"] take:10] get];
//Order By
NSArray *models = [[[[Person search:@[@"*"]] where:@{@"age",@23}] orderby:@"name" desc:YES] get];
//join
NSArray *models = [[[Person search:@[@"*"]] where:@{@"age",@23}] join:@"Role" withFirstColumn:@"Person.id" operator:@"=" secondColumn:@"Role.uid"];
//Executes update
BOOL rs = [HZDBManager executeUpdate:@"update Person set name = ? where id = ?" withParams:@[@"GeniusBrother",@"1"]];
//Executes query
NSArray *resultDicArray =[HZDBManager executeQuery:@"select * from Person where id = ?" withParams:@[@1]];
//Batch
[HZDBManager executeStatements:@"update Person set name = 'GeniusBrother' where id = 1;select * from Person where id = 1" withResultBlock:^int(NSDictionary * _Nonnull resultsDictionary) {
//do something
return SQLITE_OK;
}];
//Transaction
[HZDBManager beginTransactionWithBlock:^BOOL(HZDatabaseManager * _Nonnull db) {
BOOL rs = [db executeUpdate:@"" withParams:@[]];
if (rs) {
return YES; //commit
}else {
return NO; //rollback
}
}];