版权声明:本文为博主原创文章,未经博主允许不得转载。
(本ORM的源码已经上传到上 (https://github.com/helloclq/BCSqliteORM_FMDB),大家可以下载测试,如发现什么问题或意见,欢迎大家提出并指正,oschina上的地址为: )
0、测试代码及结果
根据前面的UML,我完成了基本的code,先看下测试结果:
测试代码:
int main(int argc, const char * argv[]) { @autoreleasepool { //build two model ClassEntity* classeEntity = [ClassEntity new]; classeEntity.className = @"Software02"; classeEntity.classId = 1; StudentEntity* student = [StudentEntity new]; student.age = 12; student.score = 80; student.classId = 1; student.studentNum = 421125; student.studentName = @"BlockCheng"; //init and mapping ,or open an existing database file, BCORMHelper* helper = [[BCORMHelper alloc]initWithDatabaseName:@"test.db" enties: @[ [ClassEntity class],[StudentEntity class]]]; //insert for (int i = 0 ;i < 10; i ++) { classeEntity.classId = i % 10; classeEntity.className = [NSString stringWithFormat:@"Class E_%d_%@",i,[NSDate date]]; [helper save:classeEntity]; } for (int i = 0 ;i < 100; i ++) { student.studentNum = 421125 + i ; student.classId = i % 10 + 1; student.studentName = [NSString stringWithFormat:@"student_%d_%@",i,[NSDate date]]; [helper save:student]; } //query BCSqlParameter *queryParam = [[BCSqlParameter alloc] init]; queryParam.entityClass = [StudentEntity class]; queryParam.propertyArray = @[@"age",@"classId",@"score",@"studentName",@"studentNum"]; queryParam.selection = @"classId = ? and studentNum=?"; queryParam.selectionArgs = @[@1,@421125]; queryParam.orderBy = @" studentNum asc"; id entity = [helper queryEntityByCondition:queryParam]; NSLog(@"entity:----%@",entity); //another way to query entity = [helper queryEntityByCondition:BCQueryParameterMake([StudentEntity class], @[@"age",@"classId",@"score",@"studentName",@"studentNum"], @"classId = ? and studentNum=?", @[@1,@421128], nil,@" studentNum asc", -1, -1)]; NSLog(@"entity:----%@",entity); //query many models queryParam.propertyArray = nil; queryParam.selection = @"classId = ?"; queryParam.selectionArgs = @[@1]; NSArray* entities = [helper queryEntitiesByCondition:queryParam]; NSLog(@"entities:----%@",entities); //query by condition entities = [helper queryEntitiesByCondition:BCQueryParameterMake([ClassEntity class], nil, @"classId = ?", @[@1], nil, nil, -1, -1)]; NSLog(@"entities:----%@",entities); //update a model student.studentName = @"BlockCheng_Update"; [helper update:student]; //query many model by condition entity = [helper queryEntityByCondition:BCQueryParameterMakeSimple([StudentEntity class], nil, @"studentNum=?", @[@421138])]; NSLog(@"entity:----%@",entity); //delete [helper remove:entity]; //query many model by condition entity = [helper queryEntityByCondition:BCQueryParameterMakeSimple([StudentEntity class], nil, @"studentNum=?", @[@421138])]; NSLog(@"entity:----%@",entity); //update many model by condition [helper updateByCondition:BCUpdateParameterMake([StudentEntity class], @"studentName=?", @[@"new_name"], @"studentNum=?", @[@421125])]; entity = [helper queryEntityByCondition:BCQueryParameterMakeSimple([StudentEntity class], nil, @"studentNum=?", @[@421125])]; NSLog(@"entity:----%@",entity); //delete by condition [helper deleteByCondition:BCDeleteParameterMake([StudentEntity class], @"studentNum < ?", @[@421135])]; } return 0;}
控制台输出:
1、建表
BCORMHelper.m:412 sqlite file path:/Users/BlockCheng/Library/Application Support/test.dbBCORMHelper.m:341 add a new class:ClassEntityBCORMHelper.m:345 execute a sql:SSQL:::[DROP TABLE IF EXISTS "class" ;]BCORMHelper.m:263 execute a sql:SSQL:::[CREATE TABLE 'class' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , 'name' TEXT NOT NULL);]BCORMHelper.m:341 add a new class:StudentEntityBCORMHelper.m:345 execute a sql:SSQL:::[DROP TABLE IF EXISTS "StudentEntity" ;]BCORMHelper.m:263 execute a sql:SSQL:::[CREATE TABLE 'StudentEntity' ('age' INTEGER NOT NULL DEFAULT '22' , 'name' TEXT NOT NULL DEFAULT 'blockcheng' , 'num' INTEGER PRIMARY KEY NOT NULL , 'score' REAL NOT NULL DEFAULT '80.0' , 'classid' INTEGER, CONSTRAINT 'Software01' FOREIGN KEY ('classid') REFERENCES 'class'('id'));]BCORMHelper.m:281 build table index.....BCORMHelper.m:297 execute a sql:SSQL:::[CREATE INDEX idx_StudentEntity_num ON StudentEntity('num');CREATE INDEX idx_StudentEntity_classid ON StudentEntity('classid');]
2、插入insert
BCORMHelper.m:520 save:SSQL:::[INSERT INTO class ( 'id','name') VALUES (:id ,:name )] args:{ id = 2; name = Software02;}
3、查询
BCORMHelper.m:598 queryEntityByCondition:SSQL:::[SELECT age, classid, score, name, num FROM StudentEntity WHERE classid = ? and num=? ORDER BY num asc ] arg:( 1, 421125)
4、更新:
BCORMHelper.m:822 update:SSQL:::[UPDATE StudentEntity SET age = :age,name = :name,num = :num,score = :score,classid = :classid WHERE num ='421224'] args:{ age = 12; classid = 10; name = "BlockCheng_Update"; num = 421224; score = 80;}
5、删除:
BCORMHelper.m:971 remove:SSQL:::[DELETE FROM StudentEntity WHERE num ='421138']
6、条件更新:
BCORMHelper.m:899 updateByCondition:SSQL:::[UPDATE StudentEntity SET name=? WHERE num=? ] args:( "new_name", 421125)
7、条件删除
BCORMHelper.m:1016 deleteByCondition:SSQL:::[DELETE FROM StudentEntity WHERE num < ? ] args:( 421135)
从测试结果看,完全达到了前面我的预期
1、用法_建表_映射
由于本orm是基于fmdb的,故包好FMDB和相应的库是必须的!!
然后,包含本ORM头文件
#import "BCDB.h"
需要和数据库映射的实体需要实现 BCORMEntityProtocol 协议
@protocol BCORMEntityProtocol@required/** *propertyName: databaseInfo; **/+(NSDictionary*)tableEntityMapping;@optional+(NSString*)tableName;/** *propertyName----- table index; **/+(NSArray*)tableIndexArray;@end
例如:
@interface ClassEntity : NSObject@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":BCSqliteTypeMakeText(@"name", NO) };}@end
然后创建数据库或打开现有数据库即可:
BCORMHelper* helper = [[BCORMHelper alloc]initWithDatabaseName:@"test.db" enties: @[ [ClassEntity class],[StudentEntity class]]];
执行后,如果数据库不存在,便可看到建表过程,输入如:上面的【0、测试代码及结果-----1、建表】
2、insert实体到sqlite
完成【1、用法_建表_映射】后,构造一个实体插入:
ClassEntity* classeEntity = [ClassEntity new];classeEntity.className = @"Software02";classeEntity.classId = 1; [helper save:classeEntity];
遍成功地插入了一条数据,或者更新了数据:结果见 上面的【0、测试代码及结果-----2、插入insert】
3、update更新实体
更新一个已存在的数据的方式为:
classeEntity.className = @"Software01"; [helper update:classeEntity];
输入为:
BCORMHelper.m:822 update:SSQL:::[UPDATE class SET id = :id,name = :name WHERE id ='9'] args:{ id = 9; name = Software01;}
也可以根据条件更新:
//update many model by condition [helper updateByCondition:BCUpdateParameterMake([StudentEntity class], @"studentName=?", @[@"new_name"], @"studentNum=?", @[@421125])];
结果见 【0、测试代码及结果-----6、条件更新:】
4、query查询一个或多个
查询可以自己构造参数:
//query BCSqlParameter *queryParam = [[BCSqlParameter alloc] init]; queryParam.entityClass = [StudentEntity class]; queryParam.propertyArray = @[@"age",@"classId",@"score",@"studentName",@"studentNum"]; queryParam.selection = @"classId = ? and studentNum=?"; queryParam.selectionArgs = @[@1,@421125]; queryParam.orderBy = @" studentNum asc"; id entity = [helper queryEntityByCondition:queryParam]; NSLog(@"entity:----%@",entity);
也可以:
//another way to query id entity = [helper queryEntityByCondition:BCQueryParameterMake([StudentEntity class], @[@"age",@"classId",@"score",@"studentName",@"studentNum"], @"classId = ? and studentNum=?", @[@1,@421128], nil,@" studentNum asc", -1, -1)]; NSLog(@"entity:----%@",entity);
输出见【0、测试代码及结果-----3、查询】
5、delete删除实体
可以直接remove一个已存在的实体:
//delete[helper remove:entity];
或者根据条件来删除:
//delete by condition [helper deleteByCondition:BCDeleteParameterMake([StudentEntity class], @"studentNum < ?", @[@421135])];
输出见【0、测试代码及结果-----7、条件删除】
6、结束语
本ORM的源码已经上传到上 (),大家可以下载测试,如发现什么问题或意见,欢迎大家提出并指正,
稍后,我也会传一份到oschina的git上,后面我持续更新该orm源码。
版权声明:本文为博主原创文章,未经博主允许不得转载。