Code Monkey home page Code Monkey logo

vvsequelize's Introduction

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, [email protected]

License

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

vvsequelize's People

Contributors

pozi119 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.