// // CloudStorageDB.m // zhuxun // // Created by winsoft on 17/6/22. // // #import "CloudStorageDB.h" #import "DBOperation.h" #import #import #import "DBConfigureCenter.h" #import @interface CloudStorageDB() @property (nonatomic , strong) DBOperation *dbOperation; @end @implementation CloudStorageDB static CloudStorageDB *cloudStorageDB; + (instancetype)shareInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ cloudStorageDB = [[self alloc] init]; }); return cloudStorageDB; } - (instancetype)init { if (self = [super init]) { NSString *localPath = [NSPersistentStore MR_defaultLocalStoreUrl].absoluteString.stringByDeletingLastPathComponent; NSString *path = [NSString stringWithFormat:@"%@/%@_cloudStorageDisk.sqlite",localPath,Login_Name]; //创建表 self.dbOperation = [DBOperation dbOperationWithPath:path]; [self.dbOperation creatDBWithTableName:cacheFileList dbModelClass:[CSFileModel class] Path:path autoUpgrade:YES]; [self.dbOperation creatDBWithTableName:downTransFileList dbModelClass:[CSFileDownUpModel class] Path:path autoUpgrade:YES]; [self.dbOperation creatDBWithTableName:upTransFileList dbModelClass:[CSFileDownUpModel class] Path:path autoUpgrade:YES]; } return self; } //建立索引 - (void)createIndexForTableName:(NSString *)name indexCloums:(NSArray *)indexCloums { for (NSString *cloum in indexCloums) { NSString *sql = [NSString stringWithFormat:@"create index %@_%@ on %@(%@)",name,cloum,name,cloum]; [self.dbOperation addDataToDBWithSQL:sql]; } } - (void)addcsFilestoDB:(NSArray *)csFilesArray { for (CSFileModel *csFileModel in csFilesArray) { [self addcsFiletoDB:csFileModel]; } } //增加一条数据到数据库中 - (void)addcsFiletoDB:(CSFileModel *)csFile { if (csFile.isdir) { [self getFileModelWithFileID:csFile.sid resultBlock:^(CSFileModel *csFileModel) { if (!csFileModel) { [self addDataDBWithTableName:@"cacheFileList" model:csFile]; } }]; }else{ //根据pid,md5,name或者sid即可. [self getFileModelWithFileMD5:csFile.md5 resultBlock:^(CSFileModel *csFileModel) { if (!csFileModel) { [self addDataDBWithTableName:@"cacheFileList" model:csFile]; } }]; } } - (void)addCSDownUpFileToDB:(CSFileDownUpModel *)csDownUpFile { if (!csDownUpFile.pid) { csDownUpFile.pid = @"0"; } //外部调用前请自身判断是否由相同的数据存在. if (csDownUpFile.downUpType == DownUpModelDown) { [self addDataDBWithTableName:downTransFileList model:csDownUpFile]; }else [self addDataDBWithTableName:upTransFileList model:csDownUpFile]; //先通过md5和updownmodel判断文件是否在数据库内,如果是则更新数据,否则add. // // if (csDownUpFile.downUpType == DownUpModelDown) { // [self addDataDBWithTableName:downTransFileList model:csDownUpFile]; // }else [self addDataDBWithTableName:upTransFileList model:csDownUpFile]; } - (void)updateCloudFileToDB:(CSFileModel *)csFile { } #pragma mark -- 2017-07-26 文件迁移 serverFullPath未处理 --sql - (void)updateCSFilestoDB:(NSArray *)csFileSids targetid:(NSString *)pid{ NSMutableString * pidsstring = [NSMutableString string]; NSUInteger idx = 0; NSUInteger arraycount = csFileSids.count; for (NSString *str in csFileSids) { [pidsstring appendString:[NSString stringWithFormat:@"'%@'",str]]; idx++; if (idx= csFiles.count - 1 && j >= downUpModels.count - 1) { if (resultBlock) { resultBlock(tempAllFilesArray); } } } j++; } }]; }else{ //删除同name和md5的文件 NSString *sql = [NSString stringWithFormat:@"delete from %@ where md5 = '%@' and name = '%@'",downUpFileModel.downUpType == DownUpModelDown?downTransFileList:upTransFileList,downUpFileModel.md5,downUpFileModel.name]; [self.dbOperation addDataToDBWithSQL:sql]; [tempAllFilesArray addObject:downUpFileModel]; if (i >= csFiles.count - 1) { if (resultBlock) { resultBlock(tempAllFilesArray); } } } i++; } } - (void)updateCSFiletoDB:(CSFileModel *)csFile { } - (void)updateCSDownUpFileStatusWithCSFileDownUpModel:(CSFileDownUpModel *)csFileDownUpModel { #warning md5 和name相同的文件,则会全部更新 NSString *sql = nil; if (!csFileDownUpModel.isdir) { if (csFileDownUpModel.localRootPath) { sql = [NSString stringWithFormat:@"update %@ set downUpStatus = %d,downUpSize = %ld, localRootPath = '%@' where md5 = '%@' and name = '%@';",csFileDownUpModel.downUpType == DownUpModelDown?downTransFileList:upTransFileList,csFileDownUpModel.downUpStatus,csFileDownUpModel.downUpSize,csFileDownUpModel.localRootPath,csFileDownUpModel.md5,csFileDownUpModel.name]; }else sql = [NSString stringWithFormat:@"update %@ set downUpStatus = %d,downUpSize = %ld where md5 = '%@' and name = '%@';",csFileDownUpModel.downUpType == DownUpModelDown?downTransFileList:upTransFileList,csFileDownUpModel.downUpStatus,csFileDownUpModel.downUpSize,csFileDownUpModel.md5,csFileDownUpModel.name]; }else{ sql = [NSString stringWithFormat:@"update %@ set downUpStatus = %d where sid = '%@'",csFileDownUpModel.downUpType == DownUpModelDown?downTransFileList:upTransFileList,csFileDownUpModel.downUpStatus,csFileDownUpModel.sid]; } [self.dbOperation addDataToDBWithSQL:sql]; //同时更新relationlist,如果downupsize不断累加=size-> 累加到父节点上. //获取父节点,如果父节点 } - (void)updateCSDownUpFileIDWithCSFileDownUpModel:(CSFileDownUpModel *)csFileDownUpModel sid:(NSString *)sid { NSString *sql = [NSString stringWithFormat:@"update %@ set sid = '%@' where downUpUUID = '%@';",csFileDownUpModel.downUpType == DownUpModelDown?downTransFileList:upTransFileList,sid,csFileDownUpModel.downUpUUID]; [self.dbOperation addDataToDBWithSQL:sql]; } #warning 待封装到自定义底层数据库. - (void)addDataDBWithTableName:(NSString *)tableName model:(id)model { Class cls = [model class]; unsigned int ivarsCnt = 0; // 获取类成员变量列表,ivarsCnt为类成员数量 Ivar *ivars = class_copyIvarList(cls, &ivarsCnt); // 遍历成员变量列表,其中每个变量都是Ivar类型的结构体 int i = 0; NSMutableArray *keys = [NSMutableArray array]; NSMutableArray *values = [NSMutableArray array]; // NSMutableArray *valueSqlSymbol = [NSMutableArray array]; for (const Ivar *p = ivars; p < ivars + ivarsCnt; ++p) { Ivar const ivar = *p; // 获取变量名 NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)]; // 若此变量未在类结构体中声明而只声明为Property,则变量名加前缀 '_'下划线 // 比如 @property(retain) NSString *abc;则 key == _abc; // 获取变量值 id value = [model valueForKey:key]; // id value = [self valueForKey:key]; // 取得变量类型 // 通过 type[0]可以判断其具体的内置类型 // const char *type = ivar_getTypeEncoding(ivar); if(value) { BOOL strValue = NO; if ([value isKindOfClass:[NSString class]]) { strValue = YES; NSString *strValue = value; if (!strValue.length) { continue; } } [keys addObject:[key substringFromIndex:1]]; //字符串需要新增一个'',如果是字典对象呢? if (strValue) { [values addObject:[NSString stringWithFormat:@"'%@'",value]]; }else [values addObject:value]; // [valueSqlSymbol addObject:@"?"]; } i++; } free(ivars); NSString *fileKeyListStr = [keys componentsJoinedByString:@","]; NSString *fileValueListStr = [values componentsJoinedByString:@","]; // NSString *sqlValueListStr = [valueSqlSymbol componentsJoinedByString:@","]; #warning 2017-06-23... NSString *SQL = [NSString stringWithFormat:@"insert into %@ (%@) values(%@)",tableName,fileKeyListStr,fileValueListStr]; [self.dbOperation addDataToDBWithSQL:SQL]; } - (void)updateDataDBWithTableName:(NSString *)tableName model:(id)model whereSql:(NSString *)whereSql { /* UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition]; */ // sqlite> UPDATE COMPANY SET ADDRESS = 'Texas' WHERE ID = 6; } - (void)deleteDataDBWIthTableName:(NSString *)tableName whereSql:(NSString *)whereSql { //if wheresql = nil, clear all table // sqlite> DELETE FROM COMPANY WHERE SQL; } @end