2 Star 3 Fork 2

辉兔狼 / BCSqliteORM_FMDB

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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.

The MIT License (MIT) Copyright (c) 2015 辉兔狼 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

利用objective-c的runtime特性,结合现有的操作sqlite的FMDB库,实现一个轻量级的ORM框架(an objective-c ORM base on FMDB(https://github.com/ccgus/fmdb) and objective-c runtime.) 展开 收起
Objective-C
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Objective-C
1
https://gitee.com/BlockCheng/BCSqliteORM_FMDB.git
git@gitee.com:BlockCheng/BCSqliteORM_FMDB.git
BlockCheng
BCSqliteORM_FMDB
BCSqliteORM_FMDB
master

搜索帮助