一、DBOBject 介绍
DBOBject 使用runtime + fmdb 封装
1.自动建表
2.自动检查增删表字段
3.自定义数据库名,文件路径
4.支持一对一对象数据库存储,清缓存简单方便
5.支持多路径,多数据库关联查询
6.一键保存、修改、删除、查找、多级关联查询解析、反序列化
7.支持数据解析序列化、反序列化、json -> model and model ->json
8.使用FMDatabaseQueue队列操作
9.只依赖唯一第三方库FMDB
// // \\ // ======== \\ // // \\ // == \\ // // \\ // == \\// // || == //\\ // || == // \\ // || ======== // \\ // // DBObject.h // YUDBObject // // Created by BruceYu on 15/8/12. // Copyright (c) 2015年 BruceYu. All rights reserved. // /* Database operations DBObject use:*/ /* .Inherited DBObject class*/ /* * Support multiple data using FALASE OR TRUE * Set TRUE customizable dbFolder () and dbName (), custom database location and file name * Folder Path +(NSString*)dbFolder; * * Database name +(NSString*)dbName; */ #import <Foundation/Foundation.h> #import "NSObject+DB.h" #import "DBBaseObject.h" @interface DBObject : DBBaseObject /** * Data creation time */ YU_STATEMENT_Strong NSString *createDate; /** * Unique identifier ID, Warning: Do not modify and ignored */ YU_STATEMENT_Strong NSString *dId; /** * Associated with the parent ID, used to query */ YU_STATEMENT_Strong NSString *parentId; /** * Folder Path * * @return Default Sandbox project folder name */ +(NSString*)dbFolder; /** * Database name * * @return Default bundleName */ +(NSString*)dbName; /** * Model to save data, set the fields you want to ignore * * @return <#return value description#> */ +(NSArray *)dbIgnoreFields; /* */ //-(void)copyTo:(NSObject*)dest; /** * Deserialize json -> Class * * @param _dict <#_dict description#> */ -(void)Deserialize:(NSDictionary*)_dict; -(void)Deserialize:(NSDictionary*)_dict arrayParserObj:(DB_Block_ParserForArray)_parser; -(void)Deserialize:(NSDictionary*)_dict coustom:(DB_Block_Dealize_Parser)_custParser; -(void)Deserialize:(NSDictionary *)_dict arrayObjParser:(DB_Block_ParserForArray)_parser arrayObjCustParser:(DB_Block_Dealize_Parser)_custParser; /** * Deserialize DB data * * @param reslut <#reslut description#> */ -(void)DeserializeFromDBResult:(FMResultSet*)reslut; /** * Serialize Class -> json (depth Analytical Model) * * @return <#return value description#> */ -(NSDictionary *)dictory; /** * Serialize Class -> json (shallow Analytical Model) * * @return <#return value description#> */ -(NSDictionary *)dictoryProperties; /** * save the model with keyName * * @param obj DBObject subClass * @param keyName select tableName where keyName = value,Search results, If present, the update, or the inserted * * @return Save the result */ +(BOOL)save:(id)obj KeyName:(NSString*)keyName; /** * save the model with keyNames * * @param obj DBObject subClass * @param keyNames keyName collection ,select tableName where keyName = value and keyName = value ...,Search results, If present, the update, or the inserted * * @return Save the result */ +(BOOL)saveWtihConstraints:(id)obj KeyNames:(NSArray*)keyNames; /** * save the model * * @param obj DBObject subClass * * @return Save the result */ +(BOOL)save:(id)obj; /** * save the model * * @param objs DBObject subClass collection * * @return Save the result */ +(BOOL)saveObjs:(NSArray *)objs; /** * select * from SELECT * FROM tableName WHERE keyName = value * * @param keyName <#keyName description#> * @param value <#value description#> * * @return select the result (DBObject model) */ +(id)get:(NSString*)keyName value:(NSString*)value; /** * select * from SELECT * FROM class WHERE keyName = value * * @param keyName <#keyName description#> * @param value <#value description#> * * @return select the result (DBObject model array) */ +(NSArray*)getList:(NSString*)keyName value:(NSString*)value; /** * select * from SELECT * FROM class WHERE keyName = value and keyName = value.. * * @param keyValues <#keyValues description#> * * @return select the result (DBObject model array) */ +(NSArray*)getWtihConstraints:(NSDictionary*)keyValues; /** * select * from SELECT * FROM class WHERE keyName = value and keyName = value.. * * @param where <#where description#> * @param groupBy <#groupBy description#> * @param orderBy <#orderBy description#> * @param limit <#limit description#> * * @return select the result (DBObject model array) */ +(NSArray *)selectWhere:(NSString *)where groupBy:(NSString *)groupBy orderBy:(NSString *)orderBy limit:(NSString *)limit; /** * Get all the data into the table * * @return <#return value description#> */ +(NSArray*)getAll; /** * Remove all */ +(void)deleteAll; /** * Delete with keyName * * @param keyName <#keyName description#> */ -(void)deleteWithKey:(NSString*)keyName; /** * Delete with keyNames * * @param keyNames <#keyNames description#> */ -(void)deleteWtihConstraints:(NSArray*)keyNames; /** * Save with keyName * * @param keyName <#keyName description#> * * @return <#return value description#> */ -(BOOL)save:(NSString*)keyName; /** * Save with keyNames * * @param keyNames <#keyNames description#> * * @return <#return value description#> */ -(BOOL)saveWtihConstraints:(NSArray*)keyNames; @end
二、如何使用
#import <YUDBFramework/DBOBject.h>
.对象继承DBOBject 创建模型
2.自定义名字和路径,序列化属性
/Users/apple/Library/Developer/CoreSimulator/Devices/A58191FC-7309-4453-A619-4C85E756BF0B/data/Containers/Data/Application/E6AE1BD6-AAE7-4FD4-A201-92B9B193B19D/Documents/user/User.db
.生成目录
4.关联查询 运行结果
NSArray *userArry = [DBObj getAll]; for (DBObj *info in userArry) { DBLog(@"info %@",[info dictory]); for (UserInfo *obj in info.infoArry) { DBLog(@"obj %@",[obj dictory]); } }
2015-11-09 10:03:50.495 YUDBObjectSample[3762:67175] ||========================================================================|| || \\ // ======== \\ // || || \\ // == \\ // || || \\// == \\// || || || == //\\ Debug DBObject:2015-11-09 || || || == // \\ || || || ======== // \\ || ||========================================================================|| 2015-11-09 10:03:50.504 YUDBObjectSample[3762:67175] 数据库位置:/Users/apple/Library/Developer/CoreSimulator/Devices/A58191FC-7309-4453-A619-4C85E756BF0B/data/Containers/Data/Application/E6AE1BD6-AAE7-4FD4-A201-92B9B193B19D/Documents/user/User.db 2015-11-09 10:03:50.513 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a040df80> time cost: 0.005 2015-11-09 10:03:50.516 YUDBObjectSample[3762:67175] 数据库位置:/Users/apple/Library/Developer/CoreSimulator/Devices/A58191FC-7309-4453-A619-4C85E756BF0B/data/Containers/Data/Application/E6AE1BD6-AAE7-4FD4-A201-92B9B193B19D/Documents/base/Base.db 2015-11-09 10:03:50.521 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a040df80> time cost: 0.003 2015-11-09 10:03:50.521 YUDBObjectSample[3762:67175] 解析 DBObj 关联模型: UserInfo 2015-11-09 10:03:50.522 YUDBObjectSample[3762:67175] 解析 DBObj 关联NSArray: ( "<UserInfo: 0x7fd6a06aa1c0>", "<UserInfo: 0x7fd6a0692e60>", "<UserInfo: 0x7fd6a06a30e0>", "<UserInfo: 0x7fd6a0403c10>", "<UserInfo: 0x7fd6a04036b0>", "<UserInfo: 0x7fd6a040b590>", "<UserInfo: 0x7fd6a0411f20>", "<UserInfo: 0x7fd6a040da10>", "<UserInfo: 0x7fd6a040fa90>", "<UserInfo: 0x7fd6a0415680>", "<UserInfo: 0x7fd6a0403dd0>", "<UserInfo: 0x7fd6a040eda0>", "<UserInfo: 0x7fd6a040c480>", "<UserInfo: 0x7fd6a0425660>", "<UserInfo: 0x7fd6a04136f0>" ) 2015-11-09 10:03:50.536 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a06aa1c0> time cost: 0.003 2015-11-09 10:03:50.540 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a0692e60> time cost: 0.003 2015-11-09 10:03:50.544 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a06a30e0> time cost: 0.004 2015-11-09 10:03:50.548 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a0403c10> time cost: 0.004 2015-11-09 10:03:50.551 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a04036b0> time cost: 0.003 2015-11-09 10:03:50.554 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a040b590> time cost: 0.003 2015-11-09 10:03:50.557 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a0411f20> time cost: 0.003 2015-11-09 10:03:50.561 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a040da10> time cost: 0.003 2015-11-09 10:03:50.564 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a040fa90> time cost: 0.003 2015-11-09 10:03:50.567 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a0415680> time cost: 0.003 2015-11-09 10:03:50.571 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a0403dd0> time cost: 0.003 2015-11-09 10:03:50.576 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a040eda0> time cost: 0.005 2015-11-09 10:03:50.581 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a040c480> time cost: 0.005 2015-11-09 10:03:50.585 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a0425660> time cost: 0.003 2015-11-09 10:03:50.588 YUDBObjectSample[3762:67175] save <UserInfo: 0x7fd6a04136f0> time cost: 0.002 2015-11-09 10:03:50.590 YUDBObjectSample[3762:67175] save <DBObj: 0x7fd6a0699ea0> time cost: 0.073
代码地址