DBManager.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // DBManager.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by 王昭威 on 2023/1/29.
  6. //
  7. #import "DBManager.h"
  8. #import <sqlite3.h>
  9. #import "LenzCachedResourceModel.h"
  10. #import "LenzHeader.h"
  11. @interface LenzDBManager ()
  12. @property (nonatomic, strong) dispatch_queue_t serialQueue;
  13. @end
  14. @implementation LenzDBManager{
  15. sqlite3 *_db; // 句柄
  16. }
  17. + (instancetype)shared{
  18. static dispatch_once_t token;
  19. static LenzDBManager* instance = nil;
  20. dispatch_once(&token, ^{
  21. instance = [[self alloc] init];
  22. });
  23. return instance;
  24. }
  25. - (instancetype)init{
  26. self = [super init];
  27. if(self){
  28. _serialQueue = dispatch_queue_create("db", DISPATCH_QUEUE_SERIAL);
  29. }
  30. return self;
  31. }
  32. // 打开数据库
  33. - (void)openSqlDataBase {
  34. dispatch_sync(self.serialQueue, ^{
  35. // _db是数据库的句柄,即数据库的象征,如果对数据库进行增删改查,就得操作这个示例
  36. // 获取数据库文件的路径
  37. NSString *docPath = [PCSTools shared].documentPath;
  38. NSString *fileName = [docPath stringByAppendingPathComponent:@"image_disk_cache.sqlite"];
  39. NSLog(@"fileNamePath = %@",fileName);
  40. // 将 OC 字符串转换为 C 语言的字符串
  41. const char *cFileName = fileName.UTF8String;
  42. // 打开数据库文件(如果数据库文件不存在,那么该函数会自动创建数据库文件)
  43. int result = sqlite3_open(cFileName, &_db);
  44. if (result != SQLITE_OK) { // 打开成功
  45. NSLog(@"打开数据库失败");
  46. return;
  47. }
  48. NSLog(@"打开数据库成功");
  49. // 创建表
  50. /**
  51. type
  52. 1: photo
  53. 2: movie
  54. 3: others
  55. */
  56. const char *sql = "CREATE TABLE IF NOT EXISTS t_cached_resources (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, type integer NOT NULL, unique (name, type));";
  57. char *errMsg = NULL;
  58. result = sqlite3_exec(_db, sql, NULL, NULL, &errMsg);
  59. if (result == SQLITE_OK) {
  60. NSLog(@"创建表成功");
  61. }
  62. else {
  63. NSLog(@"创建表失败");
  64. printf("创表失败---%s----%s---%d",errMsg,__FILE__,__LINE__);
  65. }
  66. });
  67. }
  68. - (BOOL)insertWithName:(NSString*)name type: (int)type{
  69. __block BOOL success = NO;
  70. dispatch_sync(self.serialQueue, ^{
  71. if(_db == NULL){
  72. return;
  73. }
  74. const char *sql = "INSERT INTO t_cached_resources (name, type) VALUES (?, ?);";
  75. sqlite3_stmt* stmt = NULL;
  76. if(sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL) == SQLITE_OK){
  77. sqlite3_bind_text(stmt, 1, name.UTF8String, -1, SQLITE_TRANSIENT);
  78. sqlite3_bind_int(stmt, 2, type);
  79. int ret = sqlite3_step(stmt);
  80. success = ret == SQLITE_DONE;
  81. if(ret == SQLITE_DONE){
  82. }
  83. else{
  84. NSLog(@"insert error");
  85. }
  86. }
  87. sqlite3_finalize(stmt);
  88. });
  89. return success;
  90. }
  91. - (LenzCachedResourceModel*)queryByName: (NSString*)name type: (int)type{
  92. __block LenzCachedResourceModel* ret = nil;
  93. dispatch_sync(self.serialQueue, ^{
  94. const char* sql = "SELECT * FROM t_cached_resources where name=? and type =?;";
  95. sqlite3_stmt* stmt = NULL;
  96. NSInteger state = sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL);
  97. sqlite3_bind_text(stmt, 1, name.UTF8String, -1, SQLITE_TRANSIENT);
  98. sqlite3_bind_int(stmt, 2, type);
  99. if (state == SQLITE_OK)
  100. // 成功指向一条记录
  101. while (sqlite3_step(stmt) == SQLITE_ROW) {
  102. // 封装的实体
  103. LenzCachedResourceModel *model = [[LenzCachedResourceModel alloc] init];
  104. const unsigned char* str = sqlite3_column_text(stmt, 1);
  105. if(str != NULL){
  106. model.name = [NSString stringWithUTF8String:(const char*)str];
  107. }
  108. model.type = sqlite3_column_int(stmt, 2);
  109. ret = model;
  110. }
  111. else
  112. NSLog(@"----- 读取数据失败 -----");
  113. sqlite3_finalize(stmt);
  114. });
  115. return ret;
  116. }
  117. - (void)deleteByName: (NSString*)name type: (int)type{
  118. dispatch_sync(self.serialQueue, ^{
  119. const char *sql = "DELETE FROM t_cached_resources where name=? and type =?;";
  120. sqlite3_stmt* stmt = NULL;
  121. sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL);
  122. sqlite3_bind_text(stmt, 1, name.UTF8String, -1, SQLITE_TRANSIENT);
  123. sqlite3_bind_int(stmt, 2, type);
  124. sqlite3_step(stmt);
  125. sqlite3_finalize(stmt);
  126. });
  127. }
  128. - (void)deleteModels:(NSArray<LenzCachedResourceModel *> *)models{
  129. dispatch_sync(self.serialQueue, ^{
  130. const char *sql = "DELETE FROM t_cached_resources where name=? and type =?;";
  131. sqlite3_stmt* stmt = NULL;
  132. sqlite3_exec(_db, "BEGIN EXCLUSIVE TRANSACTION", 0, 0, 0);
  133. if(sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL) == SQLITE_OK){
  134. [models enumerateObjectsUsingBlock:^(LenzCachedResourceModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  135. sqlite3_bind_text(stmt, 1, obj.name.UTF8String, -1, SQLITE_TRANSIENT);
  136. sqlite3_bind_int(stmt, 2, (int)obj.type);
  137. sqlite3_step(stmt);
  138. sqlite3_reset(stmt);
  139. }];
  140. }
  141. if(sqlite3_finalize(stmt) != SQLITE_OK){
  142. NSLog(@"SQL Error: %s",sqlite3_errmsg(_db));
  143. }
  144. if (sqlite3_exec(_db, "COMMIT TRANSACTION", 0, 0, 0) != SQLITE_OK)
  145. NSLog(@"SQL Error: %s",sqlite3_errmsg(_db));
  146. });
  147. }
  148. - (NSArray<LenzCachedResourceModel*>*)fetchAll{
  149. NSMutableArray<LenzCachedResourceModel*>* ret = nil;
  150. @autoreleasepool {
  151. ret = [NSMutableArray array];
  152. dispatch_sync(self.serialQueue, ^{
  153. const char* sql = "SELECT * FROM t_cached_resources;";
  154. sqlite3_stmt* stmt = NULL;
  155. NSInteger state = sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL);
  156. if (state == SQLITE_OK)
  157. // 成功指向一条记录
  158. while (sqlite3_step(stmt) == SQLITE_ROW) {
  159. // 封装的实体
  160. LenzCachedResourceModel *model = [[LenzCachedResourceModel alloc] init];
  161. const unsigned char* str = sqlite3_column_text(stmt, 1);
  162. if(str != NULL){
  163. model.name = [NSString stringWithUTF8String:(const char*)str];
  164. }
  165. model.type = sqlite3_column_int(stmt, 2);
  166. [ret addObject:model];
  167. }
  168. else
  169. NSLog(@"----- 读取数据失败 -----");
  170. sqlite3_finalize(stmt);
  171. });
  172. }
  173. return [ret copy];
  174. }
  175. - (BOOL)close{
  176. __block BOOL success = NO;
  177. dispatch_sync(self.serialQueue, ^{
  178. int ret = sqlite3_close(_db);
  179. success = SQLITE_OK == ret;
  180. });
  181. return !!success;
  182. }
  183. @end