Score
0
Watch 2 Star 3 Fork 1

辉兔狼 / BCSqliteORM_FMDBObjective-CMIT

Create your Gitee Account
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
利用objective-c的runtime特性,结合现有的操作sqlite的FMDB库,实现一个轻量级的ORM框架(an objective-c ORM base on FMDB(https://github.com/ccgus/fmdb) and objective-c runtime.) spread retract

http://my.oschina.net/chengliqun/blog/504020

Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

BCSqliteORM v1.0

an objective-c ORM base on FMDB(https://github.com/ccgus/fmdb) and objective-c runtime.

Usage

Setup

I am using FMDB as SQLite wrapper.

Implement BCORMEntityProtocol

make your model entity implement BCORMEntityProtocol protocol

@interface ClassEntity : NSObject<BCORMEntityProtocol>
@property (nonatomic,assign)NSInteger classId;
@property (nonatomic,copy)NSString* className;
@end
@implementation ClassEntity
- (NSString *)description
{
    return [NSString stringWithFormat:@"[Class:id(%ld) name:(%@)]:%p",self.classId,self.className,self];
}

+(NSString*)tableName
{
    return @"class";
}

+(NSDictionary*)tableEntityMapping
{
    return @{ @"classId":BCSqliteTypeMakeIntPrimaryKey(@"id", YES),
              @"className":BCSqliteTypeMakeTextDefault(@"name", NO,@"Software01")
              };
}
@end

it will build a table like this.or just make a mapping 输入图片说明

it will build a sql like this:

CREATE TABLE class (
  id integer PRIMARY KEY AUTOINCREMENT NOT NULL,
  name text NOT NULL DEFAULT('Software01')
);

Open OR create a database file

you can create a new datase file ,or open an existing file like this

BCORMHelper* helper = [[BCORMHelper alloc]initWithDatabaseName:@"test.db" 
				           enties: @[ [ClassEntity class],[StudentEntity class]]];

OR

BCORMHelper* helper = [[BCORMHelper alloc]													initWithDatabasePath:@"/Users/BlockCheng/Library/Application Support/test.db" 
			enties: @[ [ClassEntity class],[StudentEntity class]]];

Save an Model

an example model:

ClassEntity* classeEntity = [ClassEntity new];
classeEntity.className = @"Software02";
classeEntity.classId = 2;

StudentEntity* student = [StudentEntity new];
student.age = 12;
student.score = 80;
student.classId = 2;
student.studentNum = 421125;
student.studentName = @"BlockCheng";
[helper save:classeEntity];

[helper save:student];

query a model

BCSqlParameter *queryParam  = [[BCSqlParameter  alloc] init];
queryParam.entityClass = [StudentEntity class];
queryParam.propertyArray = @[@"age",@"classId",@"score",@"studentName",@"studentNum"];
queryParam.selection = @"classId = ? and studentNum=?";
queryParam.selectionArgs = @[@1,@421128];
queryParam.orderBy = @" studentNum  asc";
id entity  = [helper queryEntityByCondition:queryParam];
NSLog(@"entity:----%@",entity);

OR simply use

entity  = [helper queryEntityByCondition:BCQueryParameterMake([StudentEntity class],
						@[@"age",@"classId",@"score",@"studentName",@"studentNum"],@"classId = ? and studentNum=?",
						@[@1,@421128], 
						@" studentNum  asc", nil, -1, -1)];
NSLog(@"entity:----%@",entity);

query many models

NSArray* entities  = [helper queryEntitiesByCondition:
				BCQueryParameterMake([ClassEntity class],
				nil, @"classId = ?", @[@1],
				nil, nil, -1, -1)];
NSLog(@"entities:----%@",entities);

OR

BCSqlParameter *queryParam  = [[BCSqlParameter  alloc] init];
queryParam.entityClass = [StudentEntity class];
queryParam.selection = @"classId = ?";
queryParam.selectionArgs = @[@1];
NSArray* entities  = [helper queryEntitiesByCondition:queryParam];

update

[helper update:student];
    

OR with a update condition

[helper updateByCondition:BCUpdateParameterMake([StudentEntity class],@"studentName=?", @[@"new_name"],@"studentNum=?", @[@421125])];

delete

[helper remove:entity];
    

OR with delete condition

[helper deleteByCondition:BCDeleteParameterMake([StudentEntity class],@"studentNum < ?", @[@421135])];
    

architecture 输入图片说明

License

The license for BCSqliteORM is contained in the "License.txt" file.

Comments ( 0 )

Sign in for post a comment

Objective-C
1
https://gitee.com/BlockCheng/BCSqliteORM_FMDB.git
git@gitee.com:BlockCheng/BCSqliteORM_FMDB.git
BlockCheng
BCSqliteORM_FMDB
BCSqliteORM_FMDB
master

Search

231008 48f1a665 1899542 231017 9a6720c6 1899542