Skip to content

Provides a beautiful, simple ActiveRecord implementation to interact with the database.

License

Notifications You must be signed in to change notification settings

GeniusBrother/HZORM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HZORM

License MIT  CocoaPods  CocoaPods  Support 

Provides a beautiful, simple ActiveRecord implementation to interact with the database.
(It's a component of HZExtend)

Contact

QQ Group:32272635

Installation

CocoaPods

  1. Add pod 'HZORM to your Podfile.
  2. Run pod install or pod update.
  3. Import <HZORM/HZORM.h>.

Documentation

Full API documentation is available on CocoaDocs.

Requirements

This library requires iOS 8.0+ and Xcode 8.0+.

Usage

DB Config

NSString *dbPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/HZDatabase.db"];
[HZDatabaseManager sharedManager].dbPath = dbPath;

Structure

@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 Operation

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

Search

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

Query builder

//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 Operation

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

About

Provides a beautiful, simple ActiveRecord implementation to interact with the database.

Resources

License

Stars

Watchers

Forks

Packages

No packages published