FMDB 存储请求

#import "RequestTool.h"
#import "RequestParams.h"
#import 

@implementation RequestTool

// static 保证了 _queue 无法被外部调用
static FMDatabaseQueue *_queue;

+ (void)initialize {
    //获取沙盒路径
    NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"FMDB_Request.sqlite"];
    NSLog(@"dbPath = %@",dbPath);
    //使用此方式可以保证线程安全
    _queue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
    
    [_queue inDatabase:^(FMDatabase * _Nonnull db) {
        //创建 t_request 表, 其中 id 为主键
        BOOL result = [db executeUpdate:@"create table if not exists t_request (id integer primary key autoincrement, dbKey text, url text, params blob);"];
        if (result) {
            NSLog(@"创建表 t_request 成功");
        } else {
            NSLog(@"创建表 t_request 失败");
        }
    }];
}

+ (void)insertStudent:(RequestParams *)params {
    [_queue inDatabase:^(FMDatabase * _Nonnull db) {
        NSData *paramsData = [NSKeyedArchiver archivedDataWithRootObject:params.params];
        BOOL result = [db executeUpdate:@"insert into t_request (dbKey, url, params) values (?, ?, ?);",params.dbKey, params.url, paramsData];
        if (result) {
            NSLog(@"成功插入数据");
        } else {
            NSLog(@"插入数据失败");
        }
    }];
}

+ (void)insertStudents:(NSArray *)params {
    for (RequestParams *param in params) {
        [self insertStudent:param];
    }
}

+ (void)updateStudent:(RequestParams *)params {
    [_queue inDatabase:^(FMDatabase * _Nonnull db) {
        
        NSData *paramsData = [NSKeyedArchiver archivedDataWithRootObject:params.params];
        BOOL result = [db executeUpdate:@"update t_request set url = ? params = ? where dbKey = ? ;",params.url, paramsData, params.dbKey];
        if (result) {
            NSLog(@"更新数据成功");
        } else {
            NSLog(@"更新数据失败");
        }
    }];
}

+ (void)deleteStudent:(RequestParams *)params {
    [_queue inDatabase:^(FMDatabase * _Nonnull db) {
        BOOL result = [db executeUpdate:@"delete from t_request where dbKey = ?;",params.dbKey];
        if (result) {
            NSLog(@"删除数据成功");
        } else {
            NSLog(@"删除数据失败");
        }
    }];
}

+ (NSArray *)queryStudents:(RequestParams *)params {
    
    __block NSMutableArray *requestParams = nil;
    
    //inDatabase 内部是一个同步线程,所以在 block 执行完毕之前,查询方法不会被提前 return
    [_queue inDatabase:^(FMDatabase * _Nonnull db) {
        requestParams = [[NSMutableArray alloc] init];
        
        FMResultSet *rs = [db executeQuery:@"select * from t_request where dbKey = ?;",params.dbKey];
        //用 while
        while (rs.next) {
            NSString *dbKey = [rs stringForColumn:@"dbKey"];
            NSString *url   = [rs stringForColumn:@"url"];
            NSData   *data  = [rs dataForColumn:@"params"];
            
            RequestParams *params = [[RequestParams alloc] init];
            params.dbKey = dbKey;
            params.url = url;
            params.params = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            
            [requestParams addObject:params];
        }
    }];
    
    return requestParams;
}


@end

你可能感兴趣的:(FMDB 存储请求)