2 Star 3 Fork 2

辉兔狼 / BCSqliteORM_FMDB

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 4.33 KB
一键复制 编辑 原始数据 按行查看 历史
辉兔狼 提交于 2015-09-11 11:44 . Update 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.

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

搜索帮助