BDQuestionModule.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // BDQuestionModule.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2022/7/28.
  6. // Copyright © 2022 JCZ. All rights reserved.
  7. //
  8. #import "BDQuestionModule.h"
  9. static NSString *dbNameStr = @"question";
  10. static NSString *dbTypeStr = @"db";
  11. static NSString *questionTableNameStr = @"t_question";
  12. @interface BDQuestionModule ()
  13. @property (nonatomic, readwrite, strong) FMDatabaseQueue *databaseQueue;
  14. @end
  15. @implementation BDQuestionModule
  16. static id rq_BDQuestionModule = nil;
  17. #pragma mark - init
  18. + (instancetype)sharedInstance {
  19. static dispatch_once_t onceToken;
  20. dispatch_once(&onceToken, ^{
  21. rq_BDQuestionModule = [[self alloc] init];
  22. });
  23. return rq_BDQuestionModule;
  24. }
  25. - (instancetype)init {
  26. if (self = [super init]) {
  27. NSError *error;
  28. NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",dbNameStr,dbTypeStr]];
  29. NSString *resourcePath = [[NSBundle mainBundle] pathForResource:dbNameStr ofType:dbTypeStr];
  30. if ([RQFileManager isPathExist:dbPath]) {
  31. self.databaseQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
  32. NSLog(@"数据库path-----%@",dbPath);
  33. if ([self getQuestionVersion] >= [self getQuestionVersionWithResourcePath:resourcePath]) {
  34. } else {
  35. [[RQFileManager fileManager] removeItemAtPath:dbPath error:&error];
  36. [[RQFileManager fileManager] copyItemAtPath:resourcePath toPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",dbNameStr,dbTypeStr]] error:&error];
  37. self.databaseQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
  38. }
  39. } else {
  40. [[RQFileManager fileManager] copyItemAtPath:resourcePath toPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",dbNameStr,dbTypeStr]] error:&error];
  41. self.databaseQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
  42. NSLog(@"数据库path-----%@",dbPath);
  43. }
  44. }
  45. return self;
  46. }
  47. #pragma mark - PublicMethods
  48. - (NSInteger)getQuestionVersion {
  49. __block NSInteger version = 0;
  50. if (_databaseQueue) {
  51. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  52. FMResultSet *resultSet;
  53. resultSet = [db executeQuery:@"SELECT version FROM t_version"];
  54. while ([resultSet next]) {
  55. version = [resultSet longForColumn:@"version"];
  56. }
  57. }];
  58. }
  59. return version;
  60. }
  61. - (NSInteger)getQuestionVersionWithResourcePath:(NSString *)resourcePath {
  62. __block NSInteger version = 0;
  63. [[FMDatabaseQueue databaseQueueWithPath:resourcePath] inDatabase:^(FMDatabase * _Nonnull db) {
  64. FMResultSet *resultSet;
  65. resultSet = [db executeQuery:@"SELECT version FROM t_version"];
  66. while ([resultSet next]) {
  67. version = [resultSet longForColumn:@"version"];
  68. }
  69. }];
  70. return version;
  71. }
  72. - (NSArray *)queryQuestionWithQueryStr:(NSString *)queryStr {
  73. NSMutableArray *arr = @[].mutableCopy;
  74. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  75. // NSString *sqlStr = [NSString stringWithFormat:@"ATTACH DATABASE '%@' AS jsjp_user_db",RQ_YDT_USER_Question_Module.dbPath];
  76. // bool success = [db executeStatements:sqlStr];
  77. // if(!success) {
  78. // NSLog(@"%@", db.lastErrorMessage);
  79. // }
  80. FMResultSet *resultSet = [db executeQuery:queryStr];
  81. // NSInteger num = 0;
  82. while ([resultSet next]) {
  83. RQBDQuestionModel *bdQuestionModel = [RQBDQuestionModel bdQuestionModelWithFMResultSet:resultSet];
  84. // ydtQuestionModel.num = num;
  85. [arr addObject:bdQuestionModel];
  86. // num ++;
  87. }
  88. [resultSet close];
  89. }];
  90. return arr.copy;
  91. }
  92. @end