由于项目中用到多用户登录,需要根据不同的用户创建本地的数据库表,我的做法就是根据用户ID判断,创建属于该ID的不同的数据库表,包括个人信息,聊天,语音,图片,下载文件等等。
XMDataModelHelp
这个用来根据传入的参数来创建表,需要传入的参数为一个字典,{“tableId”:“123”,“tablePath”:path,“tableName”:name} 分别为表id,表的存储路径跟表名。
#import <Foundation/Foundation.h>
#import "FMDB.h"
@interface XMDataModelHelp : NSObject
@property (nonatomic, retain, readonly) FMDatabaseQueue *dbQueue;
/**
{"tableId":"123","tablePath":path,"tableName":name}
*/
@property (nonatomic, strong) NSDictionary *tableParams;
/**
用于动态创建数据库
@param tableParams {"tableId":"123","tablePath":path,"tableName":name}
@return path
*/
+ (NSString *)dbPathWithTableID:(NSDictionary *)tableParams;
@end
#import "XMDataModelHelp.h"
@interface XMDataModelHelp()
@property (nonatomic, retain) FMDatabaseQueue *dbQueue;
@end
@implementation XMDataModelHelp
+ (NSString *)dbPathWithTableID:(NSDictionary *)tableParams{
NSString *docsdir = tableParams[@"tablePath"];
NSFileManager *filemanage = [NSFileManager defaultManager];
BOOL isDir;
BOOL exit =[filemanage fileExistsAtPath:docsdir isDirectory:&isDir];
if (!exit || !isDir) {
[filemanage createDirectoryAtPath:docsdir withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *dbpath = [NSString stringWithFormat:@"%@/%@_%@.sqlite",docsdir,tableParams[@"tableName"],tableParams[@"tableId"]];
return dbpath;
}
- (FMDatabaseQueue *)dbQueue{
self.dbQueue = [[FMDatabaseQueue alloc] initWithPath:[self.class dbPathWithTableID:self.tableParams]];
return _dbQueue;
}
@end
XMDataModel
所有创建的模型model都可以继承于此model,并且可以进行增删查改操作。
#import <Foundation/Foundation.h>
/** SQLite五种数据类型 */
#define SQLTEXT @"TEXT"
#define SQLINTEGER @"INTEGER"
#define SQLREAL @"REAL"
#define SQLBLOB @"BLOB"
#define SQLNULL @"NULL"
#define PrimaryKey @"primary key"
#define primaryId @"pk"
@interface XMDataModel : NSObject
/** 主键 id */
@property (nonatomic, assign) int pk;
/** 列名 */
@property (retain, readonly, nonatomic) NSMutableArray *columeNames;
/** 列类型 */
@property (retain, readonly, nonatomic) NSMutableArray *columeTypes;
/**
* 获取该类的所有属性
*/
+ (NSDictionary *)getPropertys;
/** 获取所有属性,包括主键 */
+ (NSDictionary *)getAllProperties;
/** 数据库中是否存在表 */
+ (BOOL)isExistInTableWithID:(NSDictionary *)tableParams;
/** 表中的字段*/
+ (NSArray *)getColumnsWithID:(NSDictionary *)tableParams;
/** 保存或更新
* 如果不存在主键,保存,
* 有主键,则更新
*/
- (BOOL)saveOrUpdateWithID:(NSDictionary *)tableParams;
/** 保存单个数据 */
- (BOOL)saveWithID:(NSDictionary *)tableParams;
/** 批量保存数据 */
+ (BOOL)saveObjects:(NSArray *)array withID:(NSDictionary *)tableParams;
/** 更新单个数据 */
- (BOOL)updateWithID:(NSDictionary *)tableParams;
/** 批量更新数据*/
+ (BOOL)updateObjects:(NSArray *)array withID:(NSDictionary *)tableParams;
/** 删除单个数据 */
- (BOOL)deleteObjectWithID:(NSDictionary *)tableParams;
/** 批量删除数据 */
+ (BOOL)deleteObjects:(NSArray *)array withID:(NSDictionary *)tableParams;
/** 通过条件删除数据 */
+ (BOOL)deleteObjectsByCriteria:(NSString *)criteria withID:(NSDictionary *)tableParams;
/** 清空表 */
+ (BOOL)clearTableWithID:(NSDictionary *)tableParams;
/** 查询全部数据 */
+ (NSArray *)findAllWithID:(NSDictionary *)tableParams;
/** 通过主键查询 */
+ (instancetype)findByPK:(int)inPk withID:(NSDictionary *)tableParams;
/** 查找某条数据 */
+ (instancetype)findFirstByCriteria:(NSString *)criteria withID:(NSDictionary *)tableParams;
/** 通过条件查找数据
* 这样可以进行分页查询 @" WHERE pk > 5 limit 10"
*/
+ (NSArray *)findByCriteria:(NSString *)criteria withID:(NSDictionary *)tableParams;
#pragma mark - must be override method
/**
* 创建表
* 如果已经创建,返回YES
*/
+ (BOOL)createTableWithID:(NSDictionary *)tableParams;
/** 如果子类中有一些property不需要创建数据库字段,那么这个方法必须在子类中重写
*/
+ (NSArray *)transients;
/**
*数据库中的数据条数
*/
+ (int)numOfMessagesWithID:(NSDictionary *)tableParams;
/**
给数据库新增加字段
@param column 字段
@param type 字段类型
@param tableParams {"tableId":"123","tablePath":path,"tableName":name}
@return 添加结果
*/
+ (BOOL)addColumn:(NSString *)column type:(NSString *)type tableId:(NSDictionary *)tableParams;
/**
查找某条数据的最大值最小值,平均值
@param criteria 条件用语
(1)查找最大值 select max(age) maxAge from workTable
(2)查找平均值 select avg(age) avgAge from workTable
(3)查找数据之和 select sum(age) sumAge from workTable
(4)查找最小值 select min(age) minAge from workTable
@param tableParams tableParams {"tableId":"123","tablePath":path,"tableName":name}
@return self
*/
+ (instancetype)findOneByCriteria:(NSString *)criteria withID:(NSDictionary *)tableParams;
@end
#import "XMDataModel.h"
#import "XMDataModelHelp.h"
@implementation XMDataModel
- (instancetype)init
{
self = [super init];
if (self) {
NSDictionary *dic = [self.class getAllProperties];
_columeNames = [[NSMutableArray alloc] initWithArray:[dic objectForKey:@"name"]];
_columeTypes = [[NSMutableArray alloc] initWithArray:[dic objectForKey:@"type"]];
}
return self;
}
#pragma mark - base method
/**
* 获取该类的所有属性
*/
+ (NSDictionary *)getPropertys
{
NSMutableArray *proNames = [NSMutableArray array];
NSMutableArray *proTypes = [NSMutableArray array];
NSArray *theTransients = [[self class] transients];
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([self class], &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
//获取属性名
NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
if ([theTransients containsObject:propertyName]) {
continue;