3 Star 1 Fork 0

Gitee 极速下载 / whc-modelsqlitekit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/netyouli/WHC_ModelSqliteKit
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

WHC_ModelSqliteKit

Build Status Pod Version Pod Platform Pod License

架构图

  • Professional database storage solutions, thread safe, high-performance model object storage Sqlite open source library, realize one line of code database operation, simple database storage
  • 专业的数据库存储解决方案,线程安全,高性能模型对象存储Sqlite开源库,真正实现一行代码操作数据库,让数据库存储变得简单

简介

  • 架构: 采用runtime和Sqlite完美结合打造的强大数据库ORM操作引擎开源库
  • 安全: 支持数据库级别加密
  • 易用: 真正实现一行代码操作数据库
  • 目标: 替代直接使用Sqlite和CoreData以及FMDB低效率方式
  • 支持: (NSMutableDictionary,NSMutableArray,NSArray,NSDictionary,NSDate,NSData,NSString,NSNumber,Int,double,float,Bool,char)类型
  • 灵活: 支持使用Sqlite函数进行查询,支持忽略模型类属性存储数据表中
  • 兼容: 支持模型嵌套继承模型类存储到数据库和多表嵌套复杂查询
  • 智能: 根据数据库模型类实现的WHC_SqliteInfo协议返回的版本号来智能更新数据库字段(动态删除/添加)

修复升级: 修复子模型对象为nil访问子模型属性崩溃bug

修复升级: 兼容支持存储NSMutableDictionary, NSMutableArray类型

修复升级: 支持自定义数据库存储路径

升级功能: 支持WHCSqlite操作使用其他方式创建的数据库比如FMDB,只需要指定数据库存储路径。 如果表名不是模型类名称需要指定表名, 详细使用可以参考提供demo,和WHC_SqliteInfo协议

多表嵌套复杂查询

/// 查询person名称为吴海超,并且person的汽车对象的名称为宝马或者person对象学校对象的所在城市对象的名称为北京
NSArray * result = [WHCSqlite query:[Person class] 
        where:@"name = '吴海超' and car.name = '宝马' or school.city.name = '北京'"];

自定义sql查询

persons = [WHCSqlite query:Person.self sql:@"select * from Person"];

/// 来个复杂的例如:
[WHCSqlite query:Model.self sql:@"select cc.* from 
     ( select tt.*,(select count(*)+1 from Chapter where chapter_id =tt.chapter_id and updateTime<tt.updateTime ) as group_id from Chapter tt)
     cc where cc.group_id<=7 order by updateTime desc"];

要求

  • iOS 5.0 or later
  • Xcode 8.0 or later

集成

  • 不需要加密使用: pod 'WHC_ModelSqliteKit'
  • 需要加密数据库使用: pod 'WHC_ModelSqliteKit/SQLCipher'

注意

  • 在需要对数据表自定义信息需要model类实现WHC_SqliteInfo协议
  • 当模型类有新增/删除属性的时候需要在模型类里定义类方法whc_SqliteVersion方法修改模型类(数据库)版本号来表明有字段更新操作,库会根据这个VERSION变更智能检查并自动更新数据库字段,无需手动更新数据库字段
  • 当存储NSArray/NSDictionary属性并且里面是自定义模型对象时,模型对象必须实现NSCoding协议,可以使用WHC_Model库一行代码实现NSCoding相关代码
  • 当需要模型类忽略属性存储数据表时实现whc_IgnorePropertys协议方法即可return要忽略属性名称数组
  • 好用的Mac开源工具:Json生成Class,扫描无用图片,扫描无用类,keyborad
  • 如果要获取主键id值需要在model里声明属性:@property (nonatomic, assign) NSInteger whcId; 或者自定义主键名称的属性
/// 数据库协议信息
@protocol WHC_SqliteInfo <NSObject>
@optional
/**
自定义数据存储路径
@return 自定义数据库路径(目录即可)
*/
+ (NSString *)whc_SqlitePath;

/// 自定义模型类数据库版本号
/** 注意:
***该返回值在改变数据模型属性类型/增加/删除属性时需要更改否则无法自动更新原来模型数据表字段以及类型***
*/
+ (NSString *)whc_SqliteVersion;

/// 自定义数据库加密密码
/** 注意:
***该加密功能需要引用SQLCipher三方库才支持***
/// 引入方式有:
*** 手动引入 ***
*** CocoaPods: pod 'WHC_ModelSqlite/SQLCipher' ***
*/
+ (NSString *)whc_SqlitePasswordKey;

/// 自定义数据表主键名称
/**
*** 返回自定义主键名称默认主键:_id ***
*/
+ (NSString *)whc_SqliteMainkey;

/**
忽略属性集合

@return 返回忽略属性集合
*/
+ (NSArray *)whc_IgnorePropertys;


/**
引入使用其他方式创建的数据库存储路径比如:FMDB
来使用WHC_Sqlite进行操作其他方式创建的数据库

@return 存储路径
*/
+ (NSString *)whc_OtherSqlitePath;


/**
指定自定义表名

在指定引入其他方式创建的数据库时,这个时候如果表名不是模型类名需要实现该方法指定表名称

@return 表名
*/
+ (NSString *)whc_TableName;

@end

用法

1.存储嵌套模型对象到数据库演示

Person * whc = [Person new];
whc.name = @"吴海超";
whc.age = 25;
whc.height = 180.0;
whc.weight = 140.0;
whc.isDeveloper = YES;
whc.sex = 'm';

// 嵌套car对象
whc.car = [Car new];
whc.car.name = @"撼路者";
whc.car.brand = @"大路虎";

// 嵌套school对象
whc.school = [School new];
whc.school.name = @"北京大学";
whc.school.personCount = 5000;

// school对象嵌套city对象
whc.school.city = [City new];
whc.school.city.name = @"北京";
whc.school.city.personCount = 1000;

/// 测试NSArray属性存储
Car * tempCar = [Car new];
tempCar.name = @"宝马";
tempCar.brand = @"林肯";
whc.array = @[@"1",@"2"];
whc.carArray = @[tempCar];

/// 测试NSDictionary属性存储
whc.dict = @{@"1":@"2"};
whc.dictCar = @{@"car": tempCar};

[WHCSqlite insert:whc];

2.存储批量模型对象到数据库演示

NSArray * persons = [self makeArrayPerson];
[WHCSqlite inserts:persons];

3.无条件查询(查询所有记录)数据库中模型类演示

NSArray * personArray = [WHCSqlite query:[Person class]];

3.1.使用Sqlite函数查询数据库演示

/// 获取Person表所有name和name长度
NSArray * nameArray = [WHCSqlite query:[Person class] func:@"name, length(name)"];
NSLog(@"nameArray = %@",nameArray);

/// 获取Person表最大age值
NSNumber * maxAge = [WHCSqlite query:[Person class] func:@"max(age)"];
NSLog(@"maxAge = %@",maxAge);

/// 获取Person表总记录数
NSNumber * sumCount = [WHCSqlite query:[Person class] func:@"count(*)"];
NSLog(@"sumCount = %@",sumCount);

/// 获取Person表字段school.city.name = 北京--0,总记录数
sumCount = [WHCSqlite query:[Person class] func:@"count(*)" condition:@"where school.city.name = '北京--0'"];
NSLog(@"sumCount = %@",sumCount);

4.条件查询数据库中模型类演示(where 条件查询语法和sql where条件查询语法一样)

NSArray * personArray = [WHCSqlite query:[Person class] where:@"name = '吴海超2' OR age <= 18"];

5.查询数据库并对结果排序

///对person数据表查询并且根据age自动降序或者升序排序
[WHCSqlite query:[Person class] order:@"by age desc/asc"];

6.查询数据库并对结果限制查询条数

/// 对person数据表查询并且并且限制查询数量为8
[WHCSqlite query:[Person class] limit:@"8"];

/// 对person数据表查询并且对查询列表偏移8并且限制查询数量为8
[WHCSqlite query:[Person class] limit:@"8 offset 8"];

7.修改数据库中模型对象演示(where 条件查询语法和sql where条件查询语法一样)

/// 更新整条记录
[WHCSqlite update:whc where:@"name = '吴海超2' OR age <= 18"];
/// 更新整条记录中的指定字段(更新Person表在age字段大于25岁时name值为whc,age为100岁)
[WHCSqlite update:Person.self value:@"name = 'whc', age = 100" where:@"age > 25"];

8.删除数据库中模型对象演示(where条件查询为空则删除所有)

[WHCSqlite delete:[Person class] where:@"age = 25 AND name = '吴海超'"];

9.清空指定数据库演示

[WHCSqlite clear:[Person class]];

10.删除数据库演示

[WHCSqlite removeModel:[Person class]];

11.删除所有数据库演示

[WHCSqlite removeAllModel];

12.获取数据库本地路径演示

NSString * path = [WHCSqlite localPathWithModel:[Person class]];

13.获取数据库本地版本号演示

NSString * path = [WHCSqlite versionWithModel:[Person class]];

期待

  • 如果您在使用过程中有任何问题,欢迎issue me! 很乐意为您解答任何相关问题!
  • 与其给我点star,不如向我狠狠地抛来一个BUG!
  • 如果您想要更多的接口来自定义或者建议/意见,欢迎issue me!我会根据大家的需求提供更多的接口!

Api文档

/**
* 说明: 存储模型数组到本地(事务方式)
* @param model_array 模型数组对象(model_array 里对象类型要一致)
*/

+ (BOOL)inserts:(NSArray *)model_array;

/**
* 说明: 存储模型到本地
* @param model_object 模型对象
*/

+ (BOOL)insert:(id)model_object;


/**
* 说明: 获取模型类表总条数
* @param model_class 模型类
* @return 总条数
*/
+ (NSUInteger)count:(Class)model_class;

/**
* 说明: 查询本地模型对象
* @param model_class 模型类
* @return 查询模型对象数组
*/

+ (NSArray *)query:(Class)model_class;

/**
* 说明: 查询本地模型对象
* @param model_class 模型类
* @param where 查询条件(查询语法和SQL where 查询语法一样,where为空则查询所有)
* @return 查询模型对象数组
*/

+ (NSArray *)query:(Class)model_class where:(NSString *)where;

/**
* 说明: 查询本地模型对象
* @param model_class 模型类
* @param order 排序条件(排序语法和SQL order 查询语法一样,order为空则不排序)
* @return 查询模型对象数组
*/

/// example: [WHCSqlite query:[Person class] order:@"by age desc/asc"];
/// 对person数据表查询并且根据age自动降序或者升序排序

+ (NSArray *)query:(Class)model_class order:(NSString *)order;

/**
* 说明: 查询本地模型对象
* @param model_class 模型类
* @param limit 限制条件(限制语法和SQL limit 查询语法一样,limit为空则不限制查询)
* @return 查询模型对象数组
*/

/// example: [WHCSqlite query:[Person class] limit:@"8"];
/// 对person数据表查询并且并且限制查询数量为8
/// example: [WHCSqlite query:[Person class] limit:@"8 offset 8"];
/// 对person数据表查询并且对查询列表偏移8并且限制查询数量为8

+ (NSArray *)query:(Class)model_class limit:(NSString *)limit;

/**
* 说明: 查询本地模型对象
* @param model_class 模型类
* @param where 查询条件(查询语法和SQL where 查询语法一样,where为空则查询所有)
* @param order 排序条件(排序语法和SQL order 查询语法一样,order为空则不排序)
* @return 查询模型对象数组
*/

/// example: [WHCSqlite query:[Person class] where:@"age < 30" order:@"by age desc/asc"];
/// 对person数据表查询age小于30岁并且根据age自动降序或者升序排序

+ (NSArray *)query:(Class)model_class where:(NSString *)where order:(NSString *)order;

/**
* 说明: 查询本地模型对象
* @param model_class 模型类
* @param where 查询条件(查询语法和SQL where 查询语法一样,where为空则查询所有)
* @param limit 限制条件(限制语法和SQL limit 查询语法一样,limit为空则不限制查询)
* @return 查询模型对象数组
*/

/// example: [WHCSqlite query:[Person class] where:@"age <= 30" limit:@"8"];
/// 对person数据表查询age小于30岁并且限制查询数量为8
/// example: [WHCSqlite query:[Person class] where:@"age <= 30" limit:@"8 offset 8"];
/// 对person数据表查询age小于30岁并且对查询列表偏移8并且限制查询数量为8

+ (NSArray *)query:(Class)model_class where:(NSString *)where limit:(NSString *)limit;

/**
* 说明: 查询本地模型对象
* @param model_class 模型类
* @param order 排序条件(排序语法和SQL order 查询语法一样,order为空则不排序)
* @param limit 限制条件(限制语法和SQL limit 查询语法一样,limit为空则不限制查询)
* @return 查询模型对象数组
*/

/// example: [WHCSqlite query:[Person class] order:@"by age desc/asc" limit:@"8"];
/// 对person数据表查询并且根据age自动降序或者升序排序并且限制查询的数量为8
/// example: [WHCSqlite query:[Person class] order:@"by age desc/asc" limit:@"8 offset 8"];
/// 对person数据表查询并且根据age自动降序或者升序排序并且限制查询的数量为8偏移为8

+ (NSArray *)query:(Class)model_class order:(NSString *)order limit:(NSString *)limit;

/**
* 说明: 查询本地模型对象
* @param model_class 模型类
* @param where 查询条件(查询语法和SQL where 查询语法一样,where为空则查询所有)
* @param order 排序条件(排序语法和SQL order 查询语法一样,order为空则不排序)
* @param limit 限制条件(限制语法和SQL limit 查询语法一样,limit为空则不限制查询)
* @return 查询模型对象数组
*/

/// example: [WHCSqlite query:[Person class] where:@"age <= 30" order:@"by age desc/asc" limit:@"8"];
/// 对person数据表查询age小于30岁并且根据age自动降序或者升序排序并且限制查询的数量为8
/// example: [WHCSqlite query:[Person class] where:@"age <= 30" order:@"by age desc/asc" limit:@"8 offset 8"];
/// 对person数据表查询age小于30岁并且根据age自动降序或者升序排序并且限制查询的数量为8偏移为8

+ (NSArray *)query:(Class)model_class where:(NSString *)where order:(NSString *)order limit:(NSString *)limit;


/**
说明: 自定义sql查询

@param model_class 接收model类
@param sql sql语句
@return 查询模型对象数组

/// example: [WHCSqlite query:Model.self sql:@"select cc.* from ( select tt.*,(select count(*)+1 from Chapter where chapter_id =tt.chapter_id and updateTime<tt.updateTime ) as group_id from Chapter tt) cc where cc.group_id<=7 order by updateTime desc"];
*/
+ (NSArray *)query:(Class)model_class sql:(NSString *)sql;

/**
* 说明: 利用sqlite 函数进行查询

* @param model_class 要查询模型类
* @param sqliteFunc sqlite函数例如:(MAX(age),MIN(age),COUNT(*)....)
* @return 返回查询结果(如果结果条数 > 1返回Array , = 1返回单个值 , = 0返回nil)
* /// example: [WHCSqlite query:[Person class] sqliteFunc:@"max(age)"];  /// 获取Person表的最大age值
* /// example: [WHCSqlite query:[Person class] sqliteFunc:@"count(*)"];  /// 获取Person表的总记录条数
*/
+ (id)query:(Class)model_class func:(NSString *)func;

/**
* 说明: 利用sqlite 函数进行查询

* @param model_class 要查询模型类
* @param sqliteFunc sqlite函数例如:(MAX(age),MIN(age),COUNT(*)....)
* @param condition 其他查询条件例如:(where age > 20 order by age desc ....)
* @return 返回查询结果(如果结果条数 > 1返回Array , = 1返回单个值 , = 0返回nil)
* /// example: [WHCSqlite query:[Person class] sqliteFunc:@"max(age)" condition:@"where name = '北京'"];  /// 获取Person表name=北京集合中的的最大age值
* /// example: [WHCSqlite query:[Person class] sqliteFunc:@"count(*)" condition:@"where name = '北京'"];  /// 获取Person表name=北京集合中的总记录条数
*/
+ (id)query:(Class)model_class func:(NSString *)func condition:(NSString *)condition;

/**
* 说明: 更新本地模型对象
* @param model_object 模型对象
* @param where 查询条件(查询语法和SQL where 查询语法一样,where为空则更新所有)
*/

+ (BOOL)update:(id)model_object where:(NSString *)where;


/**
说明: 更新数据表字段

@param model_class 模型类
@param value 更新的值
@param where 更新条件
@return 是否成功
/// 更新Person表在age字段大于25岁是的name值为whc,age为100岁
/// example: [WHCSqlite update:Person.self value:@"name = 'whc', age = 100" where:@"age > 25"];
*/
+ (BOOL)update:(Class)model_class value:(NSString *)value where:(NSString *)where;

/**
* 说明: 清空本地模型对象
* @param model_class 模型类
*/

+ (BOOL)clear:(Class)model_class;


/**
* 说明: 删除本地模型对象
* @param model_class 模型类
* @param where 查询条件(查询语法和SQL where 查询语法一样,where为空则删除所有)
*/

+ (BOOL)delete:(Class)model_class where:(NSString *)where;

/**
* 说明: 清空所有本地模型数据库
*/

+ (void)removeAllModel;

/**
* 说明: 清空指定本地模型数据库
* @param model_class 模型类
*/

+ (void)removeModel:(Class)model_class;

/**
* 说明: 返回本地模型数据库路径
* @param model_class 模型类
* @return 路径
*/

+ (NSString *)localPathWithModel:(Class)model_class;

/**
* 说明: 返回本地模型数据库版本号
* @param model_class 模型类
* @return 版本号
*/
+ (NSString *)versionWithModel:(Class)model_class;

Licenses

All source code is licensed under the MIT License.

The MIT License (MIT) Copyright (c) 2016 吴海超 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 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/mirrors/whc-modelsqlitekit.git
git@gitee.com:mirrors/whc-modelsqlitekit.git
mirrors
whc-modelsqlitekit
whc-modelsqlitekit
master

搜索帮助