HZORM 0.1.1

HZORM 0.1.1

GeniusBrother 维护。



HZORM 0.1.1

  • GeniusBrother

HZORM

License MIT  CocoaPods  CocoaPods  Support 

提供一个美观、简单的 ActiveRecord 实现来与数据库交互。
(它是 HZExtend 的组件)

联系

QQ 群组:32272635

邮箱:[email protected]

安装

CocoaPods

  1. 在Podfile中添加pod 'HZORM'
  2. 运行pod installpod update
  3. 导入

文档

完整的API文档可在CocoaDocs找到。

要求

此库需要iOS 8.0+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

ORM操作

//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"];

SQL操作

//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
    }
}];