RQDataBaseManager.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // RQDataBaseManager.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2018/12/4.
  6. // Copyright © 2018 JCZ. All rights reserved.
  7. //
  8. #import "RQDataBaseManager.h"
  9. /************************* 下载 *************************/
  10. NSString * const RQDownloadProgressNotification = @"RQDownloadProgressNotification";
  11. NSString * const RQDownloadStateChangeNotification = @"RQDownloadStateChangeNotification";
  12. NSString * const RQDownloadMaxConcurrentCountKey = @"RQDownloadMaxConcurrentCountKey";
  13. NSString * const RQDownloadMaxConcurrentCountChangeNotification = @"RQDownloadMaxConcurrentCountChangeNotification";
  14. NSString * const RQDownloadAllowsCellularAccessKey = @"RQDownloadAllowsCellularAccessKey";
  15. NSString * const RQDownloadAllowsCellularAccessChangeNotification = @"RQDownloadAllowsCellularAccessChangeNotification";
  16. typedef NS_ENUM(NSInteger, RQDBGetDateOption) {
  17. RQDBGetDateOptionAllCacheData = 0, // 所有缓存数据
  18. RQDBGetDateOptionAllDownloadingData, // 所有正在下载的数据
  19. RQDBGetDateOptionAllDownloadedData, // 所有下载完成的数据
  20. RQDBGetDateOptionAllUnDownloadedData, // 所有未下载完成的数据
  21. RQDBGetDateOptionAllWaitingData, // 所有等待下载的数据
  22. RQDBGetDateOptionModelWithUrl, // 通过url获取单条数据
  23. RQDBGetDateOptionWaitingModel, // 第一条等待的数据
  24. RQDBGetDateOptionLastDownloadingModel, // 最后一条正在下载的数据
  25. };
  26. @interface RQDataBaseManager ()
  27. @property (nonatomic, strong) FMDatabaseQueue *dbQueue;
  28. @end
  29. @implementation RQDataBaseManager
  30. + (instancetype)shareManager
  31. {
  32. static RQDataBaseManager *manager = nil;
  33. static dispatch_once_t onceToken;
  34. dispatch_once(&onceToken, ^{
  35. manager = [[self alloc] init];
  36. });
  37. return manager;
  38. }
  39. - (instancetype)init
  40. {
  41. if (self = [super init]) {
  42. [self creatVideoCachesTable];
  43. }
  44. return self;
  45. }
  46. // 创表
  47. - (void)creatVideoCachesTable
  48. {
  49. // 数据库文件路径
  50. NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"RQDownloadVideoCaches.sqlite"];
  51. // 创建队列对象,内部会自动创建一个数据库, 并且自动打开
  52. _dbQueue = [FMDatabaseQueue databaseQueueWithPath:path];
  53. [_dbQueue inDatabase:^(FMDatabase *db) {
  54. // 创表
  55. BOOL result = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_videoCaches (id integer PRIMARY KEY AUTOINCREMENT, vid text, subject text, urlString text, title text, imageUrl text, content text, fileName text, resumeData blob, fileTotalSize integer, fileDownloadSize integer, progress float, status integer, statusText text, lastSpeedTime integer, intervalFileSize integer, lastStateTime integer, destinationPath text)"];
  56. if (result) {
  57. NSLog(@"视频缓存数据表创建成功 --- %@",path);
  58. }else {
  59. NSLog(@"视频缓存数据表创建失败");
  60. }
  61. }];
  62. }
  63. // 插入数据
  64. - (void)insertModel:(RQDownloadModel *)model
  65. {
  66. [_dbQueue inDatabase:^(FMDatabase *db) {
  67. BOOL result = [db executeUpdate:@"INSERT INTO t_videoCaches (vid, subject, urlString, title, imageUrl, content, fileName, resumeData, fileTotalSize, fileDownloadSize, progress, status, statusText, lastSpeedTime, intervalFileSize, lastStateTime, destinationPath) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", model.vid, model.subject, model.urlString, model.title, model.imageUrl, model.content, model.fileName, model.resumeData, [NSNumber numberWithInteger:model.fileTotalSize], [NSNumber numberWithInteger:model.fileDownloadSize], [NSNumber numberWithInteger:model.progress], [NSNumber numberWithFloat:model.status], model.statusText, [NSNumber numberWithInteger:0], [NSNumber numberWithInteger:0], [NSNumber numberWithInteger:0], model.destinationPath];
  68. if (result) {
  69. NSLog(@"插入成功:%@", model.fileName);
  70. }else {
  71. NSLog(@"插入失败:%@", model.fileName);
  72. }
  73. }];
  74. }
  75. // 获取单条数据
  76. - (RQDownloadModel *)getModelWithUrl:(NSString *)url
  77. {
  78. return [self getModelWithOption:RQDBGetDateOptionModelWithUrl url:url];
  79. }
  80. // 获取第一条等待的数据
  81. - (RQDownloadModel *)getWaitingModel
  82. {
  83. return [self getModelWithOption:RQDBGetDateOptionWaitingModel url:nil];
  84. }
  85. // 获取最后一条正在下载的数据
  86. - (RQDownloadModel *)getLastDownloadingModel
  87. {
  88. return [self getModelWithOption:RQDBGetDateOptionLastDownloadingModel url:nil];
  89. }
  90. // 获取所有数据
  91. - (NSArray<RQDownloadModel *> *)getAllCacheData
  92. {
  93. return [self getDateWithOption:RQDBGetDateOptionAllCacheData];
  94. }
  95. // 根据lastStateTime倒叙获取所有正在下载的数据
  96. - (NSArray<RQDownloadModel *> *)getAllDownloadingData
  97. {
  98. return [self getDateWithOption:RQDBGetDateOptionAllDownloadingData];
  99. }
  100. // 获取所有下载完成的数据
  101. - (NSArray<RQDownloadModel *> *)getAllDownloadedData
  102. {
  103. return [self getDateWithOption:RQDBGetDateOptionAllDownloadedData];
  104. }
  105. // 获取所有未下载完成的数据
  106. - (NSArray<RQDownloadModel *> *)getAllUnDownloadedData
  107. {
  108. return [self getDateWithOption:RQDBGetDateOptionAllUnDownloadedData];
  109. }
  110. // 获取所有等待下载的数据
  111. - (NSArray<RQDownloadModel *> *)getAllWaitingData
  112. {
  113. return [self getDateWithOption:RQDBGetDateOptionAllWaitingData];
  114. }
  115. // 获取单条数据
  116. - (RQDownloadModel *)getModelWithOption:(RQDBGetDateOption)option url:(NSString *)url
  117. {
  118. __block RQDownloadModel *model = nil;
  119. [_dbQueue inDatabase:^(FMDatabase *db) {
  120. FMResultSet *resultSet;
  121. switch (option) {
  122. case RQDBGetDateOptionModelWithUrl:
  123. resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches WHERE urlString = ?", url];
  124. break;
  125. case RQDBGetDateOptionWaitingModel:
  126. resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches WHERE status = ? order by lastStateTime asc limit 0,1", [NSNumber numberWithInteger:RQDownloadStatus_Waiting]];
  127. break;
  128. case RQDBGetDateOptionLastDownloadingModel:
  129. resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches WHERE status = ? order by lastStateTime desc limit 0,1", [NSNumber numberWithInteger:RQDownloadStatus_Running]];
  130. break;
  131. default:
  132. break;
  133. }
  134. while ([resultSet next]) {
  135. model = [[RQDownloadModel alloc] initWithFMResultSet:resultSet];
  136. }
  137. }];
  138. return model;
  139. }
  140. // 获取数据集合
  141. - (NSArray<RQDownloadModel *> *)getDateWithOption:(RQDBGetDateOption)option
  142. {
  143. __block NSArray<RQDownloadModel *> *array = nil;
  144. [_dbQueue inDatabase:^(FMDatabase *db) {
  145. FMResultSet *resultSet;
  146. switch (option) {
  147. case RQDBGetDateOptionAllCacheData:
  148. resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches"];
  149. break;
  150. case RQDBGetDateOptionAllDownloadingData:
  151. resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches WHERE status = ? order by lastStateTime desc", [NSNumber numberWithInteger:RQDownloadStatus_Running]];
  152. break;
  153. case RQDBGetDateOptionAllDownloadedData:
  154. resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches WHERE status = ?", [NSNumber numberWithInteger:RQDownloadStatus_Completed]];
  155. break;
  156. case RQDBGetDateOptionAllUnDownloadedData:
  157. resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches WHERE status != ?", [NSNumber numberWithInteger:RQDownloadStatus_Completed]];
  158. break;
  159. case RQDBGetDateOptionAllWaitingData:
  160. resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches WHERE status = ?", [NSNumber numberWithInteger:RQDownloadStatus_Waiting]];
  161. break;
  162. default:
  163. break;
  164. }
  165. NSMutableArray *tmpArr = [NSMutableArray array];
  166. while ([resultSet next]) {
  167. [tmpArr addObject:[[RQDownloadModel alloc] initWithFMResultSet:resultSet]];
  168. }
  169. array = tmpArr;
  170. }];
  171. return array;
  172. }
  173. // 更新数据
  174. - (void)updateWithModel:(RQDownloadModel *)model option:(RQDBUpdateOption)option
  175. {
  176. [_dbQueue inDatabase:^(FMDatabase *db) {
  177. if (option & RQDBUpdateOptionState) {
  178. [self postStateChangeNotificationWithFMDatabase:db model:model];
  179. [db executeUpdate:@"UPDATE t_videoCaches SET status = ? WHERE urlString = ?", [NSNumber numberWithInteger:model.status], model.urlString];
  180. }
  181. if (option & RQDBUpdateOptionLastStateTime) {
  182. [db executeUpdate:@"UPDATE t_videoCaches SET lastStateTime = ? WHERE urlString = ?", [NSNumber numberWithInteger:[RQ_SHARE_FUNCTION getTimeStampWithDate:[NSDate date]]], model.urlString];
  183. }
  184. if (option & RQDBUpdateOptionResumeData) {
  185. [db executeUpdate:@"UPDATE t_videoCaches SET resumeData = ? WHERE urlString = ?", model.resumeData, model.urlString];
  186. }
  187. if (option & RQDBUpdateOptionProgressData) {
  188. [db executeUpdate:@"UPDATE t_videoCaches SET fileDownloadSize = ?, fileTotalSize = ?, progress = ?, lastSpeedTime = ?, intervalFileSize = ? WHERE urlString = ?", [NSNumber numberWithInteger:model.fileDownloadSize], [NSNumber numberWithFloat:model.fileTotalSize], [NSNumber numberWithFloat:model.progress], [NSNumber numberWithInteger:model.lastSpeedTime], [NSNumber numberWithInteger:model.intervalFileSize], model.urlString];
  189. }
  190. if (option & RQDBUpdateOptionAllParam) {
  191. [self postStateChangeNotificationWithFMDatabase:db model:model];
  192. [db executeUpdate:@"UPDATE t_videoCaches SET resumeData = ?, fileTotalSize = ?, fileDownloadSize = ?, progress = ?, status = ?, statusText = ?, lastSpeedTime = ?, intervalFileSize = ?, lastStateTime = ? WHERE urlString = ?", model.resumeData, [NSNumber numberWithInteger:model.fileTotalSize], [NSNumber numberWithInteger:model.fileDownloadSize], [NSNumber numberWithFloat:model.progress], [NSNumber numberWithInteger:model.status], model.statusText, [NSNumber numberWithInteger:model.lastSpeedTime], [NSNumber numberWithInteger:model.intervalFileSize], [NSNumber numberWithInteger:[RQ_SHARE_FUNCTION getTimeStampWithDate:[NSDate date]]], model.urlString];
  193. }
  194. }];
  195. }
  196. // 状态变更通知
  197. - (void)postStateChangeNotificationWithFMDatabase:(FMDatabase *)db model:(RQDownloadModel *)model
  198. {
  199. // 原状态
  200. NSInteger oldState = [db intForQuery:@"SELECT status FROM t_videoCaches WHERE urlString = ?", model.urlString];
  201. if (oldState != model.status) {
  202. // 状态变更通知
  203. [[NSNotificationCenter defaultCenter] postNotificationName:RQDownloadStateChangeNotification object:model];
  204. }
  205. }
  206. // 删除数据
  207. - (void)deleteModelWithUrl:(NSString *)url
  208. {
  209. [_dbQueue inDatabase:^(FMDatabase *db) {
  210. BOOL result = [db executeUpdate:@"DELETE FROM t_videoCaches WHERE urlString = ?", url];
  211. if (result) {
  212. NSLog(@"删除成功:%@", url);
  213. }else {
  214. NSLog(@"删除失败:%@", url);
  215. }
  216. }];
  217. }
  218. @end