DWDatabase 1.1.1.6

DWDatabase 1.1.1.6

codewicky 维护。



 
依赖关系
FMDB~> 2.7.2
DWKit/DWCategory/DWObjectUtils~> 0.0.0.18
 

DWDatabase 1.1.1.6

  • 作者
  • codeWicky

DWDatabase

描述

这是一个基于 FMDB 的非侵入性 ORM 数据库方案。

利用它可以直接使用模型操作数据库,而不需要考虑 SQL 语句该如何编写,你也不需要继承特定的模型,这样可以保证代码的无侵入性。如果你遵循了协议,你甚至可以指定每个模型存储在数据库中的属性及其对应的字段名转换。而且所有操作都是线程安全的。

Description

This is a non-intrusive ORM database schema based on FMDB.

You can manage the database directly through models instead of writing SQL statements. And you don't need to inherit from a specific model, thus ensuring the code's non-intrusiveness. If you follow the protocol, you can even specify which properties to store and their corresponding field name transformations in the database for each property. Moreover, all operations are thread-safe.

功能

  • 根据模型创建表
  • 使用模型进行数据库操作
  • 自定义模型与数据库字段名映射
  • 模型属性白名单/黑名单

Func

  • Create table with the model.
  • Manage database through the model.
  • Customize the mapping between the model and the table.
  • White / black list for properties to save.

如何使用

首先,您应该将所需的文件拖入项目中,或者您也可以使用 Cocoapods 进行集成。

pod 'DWDatabase', '~> 1.0.0'

在使用过程中,你应该确保在使用前初始化数据库,我们一般建议你在AppDelegate中调用它。初始化数据库主要是为了读取本地已存在的数据库信息。

DWDatabase * db = [DWDatabase shareDB];
NSError * err;
if ([db initializeDBWithError:nil]) {
    NSLog(@"%@",db.allDBs);
} else {
    NSLog(@"%@",err);
}

初始化之后,你可以调用API来创建一个新的本地数据库。

BOOL success = [db configDBIfNeededWithClass:cls name:name tableName:tblName path:path error:&err];

本地数据库创建成功后,你就可以开始操作数据库了,但首先要获取操作所需的数据库句柄,所有的数据库操作你都需要获取库名数据库句柄,所有的表操作你都需要获取表名数据库句柄。

///库名数据库句柄
DWDatabaseConfiguration * conf = [db fetchDBConfigurationWithName:name error:&err];

///表名数据库句柄
DWDatabaseConfiguration * conf = [db fetchDBConfigurationWithName:name tableName:tblName error:&err];

最后,你就可以使用数据库句柄进行数据库操作或表操作了。

///插入数据
success = [db insertTableWithModel:model keys:keys configuration:conf error:&err];

当然,如果你觉得这一套流程太过麻烦,我还提供了组合API,你可以一键调用完成数据库操作,比如:

BOOL success = [[DWDatabase shareDB] insertTableAutomaticallyWithModel:model name:name tableName:tblName path:path keys:keys error:&error];

这个API组合了所有上述操作,可以直接调用,但作者还是建议在了解数据库状态的情况下尽可能调用单独的API而不是组合API,这样可以避免很多不必要的判断操作。

使用方法

首先,将其拖入你的项目或使用cocoapods。

pod 'DWDatabase', '~> 1.0.0'

在使用前,你应该确保已经初始化了数据库。你最好在AppDelegate中这么做。初始化操作是用来加载本地数据库信息的。

DWDatabase * db = [DWDatabase shareDB];
NSError * err;
if ([db initializeDBWithError:nil]) {
    NSLog(@"%@",db.allDBs);
} else {
    NSLog(@"%@",err);
}

初始化之后,你可以创建一个新的本地数据库。

BOOL success = [db configDBIfNeededWithClass:cls name:name tableName:tblName path:path error:&err];

如果你成功地创建了一个数据库,你可以开始管理数据库了,但你必须使用配置来管理数据库。你应使用数据库配置进行所有数据库操作以及使用表配置进行所有表操作。

///Database-configuration
DWDatabaseConfiguration * conf = [db fetchDBConfigurationWithName:name error:&err];

///Table-configuration
DWDatabaseConfiguration * conf = [db fetchDBConfigurationWithName:name tableName:tblName error:&err];

最后,使用配置来管理数据库。

///Insert model
success = [db insertTableWithModel:model keys:keys configuration:conf error:&err];

当然,如果你觉得这样很麻烦,我还提供了一个组合API,让你可以轻松地管理数据库,比如:

BOOL success = [[DWDatabase shareDB] insertTableAutomaticallyWithModel:model name:name tableName:tblName path:path keys:keys error:&error];

这个API包括了所有上述操作,可以直接调用。但我建议你尽可能多地调用单独的API而不是组合API,因为这样可以避免很多不必要的操作。

联系作者

你可以在我的GitHub上给我留言或发送电子邮件 [email protected] 以提出建议或指出我的bug,我将不胜感激。

如果你喜欢这个小工具,记得给我点个star吧,么么哒~

与我联系

你可以在我的GitHub上给我提交issue或通过电子邮件 [email protected] 给我提建议或指出我的bug,我会非常感激。

如果你喜欢它,请给我点个star。