6 Star 25 Fork 3

ValoLee / VVSequelize

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

VVSequelize

Version License Platform

改动(0.4.7)

  1. 移除accessible. 不建议访问appgroup中的数据库, 所以去掉此功能.
  2. 移除cache. 因为sqlite3_update_hook()不能完全hook所有delete, drop操作, cache将不能被有效更新,会导致查询到错误数据, 所以去掉此功能, 由用户自行管理缓存.

功能

  • 根据Class生成数据表
  • 增删改查,insert,update,upsert,delele,drop...
  • Where语句生成,可满足大部分常规场景
  • 数据库加解密(SQLCipher)
  • 原生SQL语句支持
  • 常规查询函数支持,max(),min(),sum(),count()...
  • 支持主键,可多主键,单主键可自增.
  • 支持唯一性约束
  • Transaction支持
  • Object直接处理
  • 数据存储,OC类型支持: NSData, NSURL, NSSelector, NSValue, NSDate, NSArray, NSDictionary, NSSet,...
  • 数据存储,C类型支持: char *, struct, union
  • 子对象存储为Json字符串
  • OrmModel查询缓存
  • FTS5全文搜索(不支持FTS3/4)
  • 自定义FTS分词器
  • 支持拼音分词,简繁互搜
  • 支持外键,约束
  • 支持View

结构

安装

pod 'VVSequelize', '~> 0.4.7'

使用测试版本:

    pod 'VVSequelize', :git => 'https://github.com/pozi119/VVSequelize.git'

注意

  1. 子对象会保存成为Json字符串,子对象内的NSData也会保存为16进制字符串.
  2. 含有子对象时,请确保不会循环引用,否则Dictionary/Object互转会死循环,请将相应的循环引用加入互转黑名单.
  3. VVKeyValue仅用于本工具,不适用常规的Json转对象.

用法

此处主要列出一些基本用法,详细用法请阅读代码注释.

打开/创建数据库文件

    self.vvdb = [[VVDatabase alloc] initWithPath:dbPath];

定义ORM配置

  1. 手动创建VVOrmConfig.
  2. 普通表适配协议VVOrmable, fts表适配协议VVFtsable

注意: 已移除fts3/4的支持,仅使用fts5

定义ORM模型

可自定义表名和存放的数据库文件. 生成的模型将不在保存在ModelPool中,防止表过多导致内存占用大,需要请自行实现.

示例如下:

item.orm = [VVOrm ormWithClass:VVMessage.class name:item.tableName database:item.db];
        
item.ftsOrm = [VVOrm ormWithFtsClass:VVMessage.class name:item.tableName database:item.ftsDb];

增删改查

使用ORM模型进行增删改查等操作.

示例如下:

    NSInteger count = [self.mobileModel count:nil];
    
    BOOL ret = [self.mobileModel increase:nil field:@"times" value:-1];
    
    NSArray *array = [self.mobileModel findAll:nil orderBy:nil limit:10 offset:0];
    
...

生成SQL子句

现在仅支持非套嵌的字典或字典数组,转换方式如下:

//where/having :
{field1:val1,field2:val2} --> field1 = "val1" AND field2 = "val2"
[{field1:val1,field2:val2},{field3:val3}] --> (field1 = "val1" AND field2 = "val2") OR (field3 = "val3")
//group by:
[filed1,field2] --> "field1","field2"
//order by
[filed1,field2] --> "field1","field2" ASC
[filed1,field2].desc --> "field1","field2" DESC

示例:

- (void)testClause
  {
    VVSelect *select =  [VVSelect new];
    select.table(@"mobiles");
    select.where(@"relative".lt(@(0.3))
                 .and(@"mobile".gte(@(1600000000)))
                 .and(@"times".gte(@(0))));
    NSLog(@"%@", select.sql);
    select.where(@{ @"city": @"西安", @"relative": @(0.3) });
    NSLog(@"%@", select.sql);
    select.where(@[@{ @"city": @"西安", @"relative": @(0.3) }, @{ @"relative": @(0.7) }]);
    NSLog(@"%@", select.sql);
    select.where(@"relative".lt(@(0.3)));
    NSLog(@"%@", select.sql);
    select.where(@"     where relative < 0.3");
    NSLog(@"%@", select.sql);
    select.groupBy(@"city");
    NSLog(@"%@", select.sql);
    select.groupBy(@[@"city", @"carrier"]);
    NSLog(@"%@", select.sql);
    select.groupBy(@" group by city carrier");
    NSLog(@"%@", select.sql);
    select.having(@"relative".lt(@(0.2)));
    NSLog(@"%@", select.sql);
    select.groupBy(nil);
    NSLog(@"%@", select.sql);
    select.orderBy(@[@"city", @"carrier"]);
    NSLog(@"%@", select.sql);
    select.orderBy(@" order by relative");
    NSLog(@"%@", select.sql);
    select.limit(10);
    NSLog(@"%@", select.sql);
    select.distinct(YES);
    NSLog(@"%@", select.sql);
}

原生语句查询

- (NSArray<NSDictionary *> *)query:(NSString *)sql;

- (NSArray *)query:(NSString *)sql clazz:(Class)clazz;

加密数据数据转换(sqlcipher 3.x->4.x)

    VVDatabase *database = [VVDatabase databaseWithPath:path flags:0 encrypt:@"XXXXX"];
    database.cipherOptions = @[
        @"pragma cipher_page_size = 4096;", ///<3.x的cipher_page_size,默认为1024
        @"pragma kdf_iter = 64000;",
        @"pragma cipher_hmac_algorithm = HMAC_SHA1;",
        @"pragma cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;"
    ];

Author

Valo Lee, pozi119@163.com

License

VVSequelize is available under the MIT license. See the LICENSE file for more info.

Copyright (c) 2018 Valo Lee <pozi119@163.com> 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.

简介

数据库模型映射,自动建表, 自动更新表,数据增删改查, FTS全文搜索, 支持自定义fts3,4,5分词器,可拼音分词. sql,fmdb,wcdb,sqlite3,orm,fts,fts3,fts4,fts5 展开 收起
Objective-C 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

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

搜索帮助