FWDatabaseManager


@interface FWDatabaseManager : NSObject

本地数据库管理类

  • 全局数据库模型版本号,默认1.0。如果模型实现了fwDatabaseVersion且不为空,则会忽略全局版本号

    Declaration

    Objective-C

    @property (class, nonatomic, copy) NSString *_Nonnull version;
  • 说明: 保存模型到本地,主键存在时更新,不存在时新增

    Declaration

    Objective-C

    + (BOOL)save:(nullable id)model_object;

    Parameters

    model_object

    模型对象

    Return Value

    是否保存成功

  • 说明: 新增模型数组到本地(事务方式)

    Declaration

    Objective-C

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

    Parameters

    model_array

    模型数组对象(model_array 里对象类型要一致)

    Return Value

    是否插入成功

  • 说明: 新增模型到本地,自动更新主键

    Declaration

    Objective-C

    + (BOOL)insert:(nullable id)model_object;

    Parameters

    model_object

    模型对象

    Return Value

    是否插入成功

  • 说明: 获取模型类表总条数

    Declaration

    Objective-C

    + (NSUInteger)count:(nonnull Class)model_class;

    Parameters

    model_class

    模型类

    Return Value

    总条数

  • 说明: 查询本地模型对象

    Declaration

    Objective-C

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

    Parameters

    model_class

    模型类

    Return Value

    查询模型对象数组

  • 说明: 查询本地模型对象

    Declaration

    Objective-C

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

    Parameters

    model_class

    模型类

    where

    查询条件(查询语法和SQL where 查询语法一样,where为空则查询所有)

    Return Value

    查询模型对象数组

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

    Declaration

    Objective-C

    + (nonnull NSArray *)query:(nonnull Class)model_class
                         order:(nullable NSString *)order;
  • example: [FWDatabase query:[Person class] limit:@“8”]; 对person数据表查询并且并且限制查询数量为8 example: [FWDatabase query:[Person class] limit:@“8 offset 8”]; 对person数据表查询并且对查询列表偏移8并且限制查询数量为8

    Declaration

    Objective-C

    + (nonnull NSArray *)query:(nonnull Class)model_class
                         limit:(nullable NSString *)limit;
  • example: [FWDatabase query:[Person class] where:@“age < 30” order:@“age desc/asc”]; 对person数据表查询age小于30岁并且根据age自动降序或者升序排序

    Declaration

    Objective-C

    + (nonnull NSArray *)query:(nonnull Class)model_class
                         where:(nullable NSString *)where
                         order:(nullable NSString *)order;
  • example: [FWDatabase query:[Person class] where:@“age <= 30” limit:@“8”]; 对person数据表查询age小于30岁并且限制查询数量为8 example: [FWDatabase query:[Person class] where:@“age <= 30” limit:@“8 offset 8”]; 对person数据表查询age小于30岁并且对查询列表偏移8并且限制查询数量为8

    Declaration

    Objective-C

    + (nonnull NSArray *)query:(nonnull Class)model_class
                         where:(nullable NSString *)where
                         limit:(nullable NSString *)limit;
  • example: [FWDatabase query:[Person class] order:@“age desc/asc” limit:@“8”]; 对person数据表查询并且根据age自动降序或者升序排序并且限制查询的数量为8 example: [FWDatabase query:[Person class] order:@“age desc/asc” limit:@“8 offset 8”]; 对person数据表查询并且根据age自动降序或者升序排序并且限制查询的数量为8偏移为8

    Declaration

    Objective-C

    + (nonnull NSArray *)query:(nonnull Class)model_class
                         order:(nullable NSString *)order
                         limit:(nullable NSString *)limit;
  • example: [FWDatabase query:[Person class] where:@“age <= 30” order:@“age desc/asc” limit:@“8”]; 对person数据表查询age小于30岁并且根据age自动降序或者升序排序并且限制查询的数量为8 example: [FWDatabase query:[Person class] where:@“age <= 30” order:@“age desc/asc” limit:@“8 offset 8”]; 对person数据表查询age小于30岁并且根据age自动降序或者升序排序并且限制查询的数量为8偏移为8

    Declaration

    Objective-C

    + (nonnull NSArray *)query:(nonnull Class)model_class
                         where:(nullable NSString *)where
                         order:(nullable NSString *)order
                         limit:(nullable NSString *)limit;
  • example: [FWDatabase query:[Person class] key:1]; /// 获取Person表主键为1的记录

    Declaration

    Objective-C

    + (nullable id)query:(nonnull Class)model_class key:(NSInteger)key;
  • 说明: 自定义sql查询

    /// example: [FWDatabase 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”];

    Declaration

    Objective-C

    + (nonnull NSArray *)query:(nonnull Class)model_class
                           sql:(nonnull NSString *)sql;

    Parameters

    model_class

    接收model类

    sql

    sql语句

    Return Value

    查询模型对象数组

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

    Declaration

    Objective-C

    + (nullable id)query:(nonnull Class)model_class func:(nonnull NSString *)func;

    Parameters

    model_class

    要查询模型类

    func

    sqlite函数例如:(MAX(age),MIN(age),COUNT(*)….)

    Return Value

    返回查询结果(如果结果条数 > 1返回Array , = 1返回单个值 , = 0返回nil) /// example: [FWDatabase query:[Person class] sqliteFunc:@“max(age)”]; /// 获取Person表的最大age值 /// example: [FWDatabase query:[Person class] sqliteFunc:@“count(*)”]; /// 获取Person表的总记录条数

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

    Declaration

    Objective-C

    + (nullable id)query:(nonnull Class)model_class
                    func:(nonnull NSString *)func
               condition:(nullable NSString *)condition;

    Parameters

    model_class

    要查询模型类

    func

    sqlite函数例如:(MAX(age),MIN(age),COUNT(*)….)

    condition

    其他查询条件例如:(where age > 20 order by age desc ….)

    Return Value

    返回查询结果(如果结果条数 > 1返回Array , = 1返回单个值 , = 0返回nil) /// example: [FWDatabase query:[Person class] sqliteFunc:@“max(age)” condition:@“where name = ‘北京’”]; /// 获取Person表name=北京集合中的的最大age值 /// example: [FWDatabase query:[Person class] sqliteFunc:@“count(*)” condition:@“where name = ‘北京’”]; /// 获取Person表name=北京集合中的总记录条数

  • 说明: 更新本地模型对象

    Declaration

    Objective-C

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

    Parameters

    model_object

    模型对象

    where

    查询条件(查询语法和SQL where 查询语法一样,where为空则更新所有)

  • 说明: 更新数据表字段

    Declaration

    Objective-C

    + (BOOL)update:(nonnull Class)model_class
             value:(nonnull NSString *)value
             where:(nullable NSString *)where;

    Parameters

    model_class

    模型类

    value

    更新的值

    where

    更新条件

    Return Value

    是否成功 /// 更新Person表在age字段大于25岁是的name值为whc,age为100岁 /// example: [FWDatabase update:Person.self value:@“name = ‘whc’, age = 100” where:@“age > 25”];

  • 说明: 清空本地模型对象

    Declaration

    Objective-C

    + (BOOL)clear:(nonnull Class)model_class;

    Parameters

    model_class

    模型类

  • 说明: 根据主键删除本地模型对象,主键必须存在

    Declaration

    Objective-C

    + (BOOL)delete:(nonnull id)model_object;

    Parameters

    model_object

    模型对象

    Return Value

    是否删除成功

  • 说明: 删除本地模型对象

    Declaration

    Objective-C

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

    Parameters

    model_class

    模型类

    where

    查询条件(查询语法和SQL where 查询语法一样,where为空则删除所有)

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

    Declaration

    Objective-C

    + (void)removeAllModel;
  • 说明: 清空指定本地模型数据库

    Declaration

    Objective-C

    + (void)removeModel:(nonnull Class)model_class;

    Parameters

    model_class

    模型类

  • 说明: 返回本地模型数据库路径

    Declaration

    Objective-C

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

    Parameters

    model_class

    模型类

    Return Value

    路径

  • 说明: 返回本地模型数据库版本号

    Declaration

    Objective-C

    + (nullable NSString *)versionWithModel:(nonnull Class)model_class;

    Parameters

    model_class

    模型类

    Return Value

    版本号