BGDB.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // BGDB.h
  3. // BGFMDB
  4. //
  5. // Created by biao on 2017/10/18.
  6. // Copyright © 2017年 Biao. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "BGFMDBConfig.h"
  10. #import "FMDB.h"
  11. @interface BGDB : NSObject
  12. //信号量.
  13. @property(nonatomic, strong)dispatch_semaphore_t _Nullable semaphore;
  14. /**
  15. 调试标志
  16. */
  17. @property(nonatomic,assign)BOOL debug;
  18. /**
  19. 自定义数据库名称
  20. */
  21. @property(nonatomic,copy)NSString* _Nonnull sqliteName;
  22. /**
  23. 设置操作过程中不可关闭数据库(即closeDB函数无效).
  24. */
  25. @property(nonatomic,assign)BOOL disableCloseDB;
  26. /**
  27. 获取单例函数.
  28. */
  29. +(_Nonnull instancetype)shareManager;
  30. /**
  31. 关闭数据库.
  32. */
  33. -(void)closeDB;
  34. /**
  35. 删除数据库文件.
  36. */
  37. +(BOOL)deleteSqlite:(NSString*_Nonnull)sqliteName;
  38. /**
  39. 事务操作.
  40. */
  41. -(void)inTransaction:(BOOL (^_Nonnull)())block;
  42. /**
  43. 添加操作到线程池
  44. */
  45. -(void)addToThreadPool:(void (^_Nonnull)())block;
  46. /**
  47. 注册数据变化监听.
  48. @claName 注册监听的类名.
  49. @name 注册名称,此字符串唯一,不可重复,移除监听的时候使用此字符串移除.
  50. @return YES: 注册监听成功; NO: 注册监听失败.
  51. */
  52. -(BOOL)registerChangeWithName:(NSString* const _Nonnull)name block:(bg_changeBlock)block;
  53. /**
  54. 移除数据变化监听.
  55. @name 注册监听的时候使用的名称.
  56. @return YES: 移除监听成功; NO: 移除监听失败.
  57. */
  58. -(BOOL)removeChangeWithName:(NSString* const _Nonnull)name;
  59. #pragma mark --> 以下是直接存储一个对象的API
  60. /**
  61. 存储一个对象.
  62. @object 将要存储的对象.
  63. @ignoreKeys 忽略掉模型中的哪些key(即模型变量)不要存储,nil时全部存储.
  64. @complete 回调的block.
  65. */
  66. -(void)saveObject:(id _Nonnull)object ignoredKeys:(NSArray* const _Nullable)ignoredKeys complete:(bg_complete_B)complete;
  67. /**
  68. 批量存储.
  69. */
  70. -(void)saveObjects:(NSArray* _Nonnull)array ignoredKeys:(NSArray* const _Nullable)ignoredKeys complete:(bg_complete_B)complete;
  71. /**
  72. 批量更新.
  73. over
  74. */
  75. -(void)updateObjects:(NSArray* _Nonnull)array ignoredKeys:(NSArray* const _Nullable)ignoredKeys complete:(bg_complete_B)complete;
  76. /**
  77. 批量插入或更新.
  78. */
  79. -(void)bg_saveOrUpateArray:(NSArray* _Nonnull)array ignoredKeys:(NSArray* const _Nullable)ignoredKeys complete:(bg_complete_B)complete;
  80. /**
  81. 根据条件查询对象.
  82. @tablename 要操作的表名称.
  83. @cla 代表对应的类.
  84. @where 条件参数.
  85. @complete 回调的block.
  86. */
  87. -(void)queryObjectWithTableName:(NSString* _Nonnull)tablename class:(__unsafe_unretained _Nonnull Class)cla where:(NSString* _Nullable)where complete:(bg_complete_A)complete;
  88. /**
  89. 根据条件改变对象数据.
  90. @object 要更新的对象.
  91. @where 数组的形式 @[@"key",@"=",@"value",@"key",@">=",@"value"],为nil时设置全部.
  92. @complete 回调的block
  93. */
  94. -(void)updateWithObject:(id _Nonnull)object where:(NSArray* _Nullable)where ignoreKeys:(NSArray* const _Nullable)ignoreKeys complete:(bg_complete_B)complete;
  95. /**
  96. 根据keyPath改变对象数据.
  97. @keyPathValues数组,形式@[@"user.student.name",Equal,@"小芳",@"user.student.conten",Contains,@"书"]
  98. 即更新user.student.name=@"小芳" 和 user.student.content中包含@“书”这个字符串的对象.
  99. */
  100. -(void)updateWithObject:(id _Nonnull)object forKeyPathAndValues:(NSArray* _Nonnull)keyPathValues ignoreKeys:(NSArray* const _Nullable)ignoreKeys complete:(bg_complete_B)complete;
  101. /**
  102. 直接传入条件sql语句更新对象.
  103. */
  104. -(void)updateObject:(id _Nonnull)object ignoreKeys:(NSArray* const _Nullable)ignoreKeys conditions:(NSString* _Nonnull)conditions complete:(bg_complete_B)complete;
  105. /**
  106. 根据类删除此类所有数据.
  107. @complete 回调的block
  108. */
  109. -(void)clearWithObject:(id _Nonnull)object complete:(bg_complete_B)complete;
  110. /**
  111. 根据类,删除这个类的表.
  112. @complete 回调的block
  113. */
  114. -(void)dropWithTableName:(NSString* _Nonnull)tablename complete:(bg_complete_B)complete;
  115. /**
  116. 将某表的数据拷贝给另一个表
  117. @srcTable 源表.
  118. @destTable 目标表.
  119. @keyDict 拷贝的对应key集合,形式@{@"srcKey1":@"destKey1",@"srcKey2":@"destKey2"},即将源类srcCla中的变量值拷贝给目标类destCla中的变量destKey1,srcKey2和destKey2同理对应,以此推类.
  120. @append YES: 不会覆盖destCla的原数据,在其末尾继续添加;NO: 覆盖掉原数据,即将原数据删掉,然后将新数据拷贝过来.
  121. */
  122. -(void)copyTable:(NSString* _Nonnull)srcTable to:(NSString* _Nonnull)destTable keyDict:(NSDictionary* const _Nonnull)keydict append:(BOOL)append complete:(bg_complete_I)complete;
  123. /**----------------------------------华丽分割线---------------------------------------------*/
  124. #pragma mark --> 以下是非直接存储一个对象的API
  125. /**
  126. 数据库中是否存在表.
  127. @name 表名称.
  128. @complete 回调的block
  129. */
  130. - (void)isExistWithTableName:( NSString* _Nonnull)name complete:(bg_complete_B)complete;
  131. - (BOOL)bg_isExistWithTableName:( NSString* _Nonnull)name;
  132. /**
  133. 创建表(如果存在则不创建)
  134. @name 表名称.
  135. @keys 数据存放要求@[字段名称1,字段名称2].
  136. @uniqueKeys '唯一约束'集合.
  137. @complete 回调的block
  138. */
  139. -(void)createTableWithTableName:(NSString* _Nonnull)name keys:(NSArray<NSString*>* _Nonnull)keys unionPrimaryKeys:(NSArray* _Nullable)unionPrimaryKeys uniqueKeys:(NSArray* _Nullable)uniqueKeys complete:(bg_complete_B)complete;
  140. /**
  141. 插入数据.
  142. @name 表名称.
  143. @dict 插入的数据,只关心key和value 即@{key:value,key:value}.
  144. @complete 回调的block
  145. */
  146. -(void)insertIntoTableName:(NSString* _Nonnull)name Dict:(NSDictionary* _Nonnull)dict complete:(bg_complete_B)complete;
  147. /**
  148. 直接传入条件sql语句查询.
  149. @name 表名称.
  150. @conditions 条件语句.例如:@"where BG_name = '标哥' or BG_name = '小马哥' and BG_age = 26 order by BG_age desc limit 6" 即查询BG_name等于标哥或小马哥和BG_age等于26的数据通过BG_age降序输出,只查询前面6条.
  151. 更多条件语法,请查询sql的基本使用语句.
  152. */
  153. -(void)queryWithTableName:(NSString* _Nonnull)name conditions:(NSString* _Nullable)conditions complete:(bg_complete_A)complete;
  154. /**
  155. 根据条件查询字段.
  156. @name 表名称.
  157. @keys 存放的是要查询的哪些key,为nil时代表查询全部.
  158. @where 形式 @[@"key",@"=",@"value",@"key",@">=",@"value"],条件key属性只能是系统自带的属性,暂不支持自定义类.
  159. @complete 回调的block,返回的数组元素是字典 @[@{key:value},@{key:value}] .
  160. */
  161. -(void)queryWithTableName:(NSString* _Nonnull)name keys:(NSArray<NSString*>* _Nullable)keys where:(NSArray* _Nullable)where complete:(bg_complete_A)complete;
  162. /**
  163. 全部查询.
  164. @name 表名称.
  165. @where 条件参数 例如排序 @"order by BG_bg_id desc" 等.
  166. @complete 回调的block,返回的数组元素是字典 @[@{key:value},@{key:value}] .
  167. */
  168. -(void)queryWithTableName:(NSString* _Nonnull)name where:(NSString* _Nullable)where complete:(bg_complete_A)complete;
  169. /**
  170. 直接传入条件sql语句更新.
  171. */
  172. -(void)updateWithObject:(id _Nonnull)object valueDict:(NSDictionary* _Nullable)valueDict conditions:(NSString* _Nonnull)conditions complete:(bg_complete_B)complete;
  173. /**
  174. 根据表名和条件删除表内容.
  175. @name 表名称.
  176. @where 条件数组,形式 @[@"key",@"=",@"value",@"key",@">=",@"value"],where要非空.
  177. @complete 回调的block.
  178. */
  179. -(void)deleteWithTableName:(NSString* _Nonnull)name where:(NSArray* _Nonnull)where complete:(bg_complete_B)complete;
  180. /**
  181. 直接传入条件sql语句删除.
  182. */
  183. -(void)deleteWithTableName:(NSString* _Nonnull)name conditions:(NSString* _Nullable)conditions complete:(bg_complete_B)complete;
  184. /**
  185. 根据keypath删除表内容.
  186. @name 表名称.
  187. @keyPathValues数组,形式@[@"user.student.name",Equal,@"小芳",@"user.student.conten",Contains,@"书"]
  188. 即删除user.student.name=@"小芳" 和 user.student.content中包含@“书”这个字符串的对象.
  189. */
  190. -(void)deleteWithTableName:(NSString* _Nonnull)name forKeyPathAndValues:(NSArray* _Nonnull)keyPathValues complete:(bg_complete_B)complete;
  191. /**
  192. 根据表名删除表格全部内容.
  193. @name 表名称.
  194. @complete 回调的block.
  195. */
  196. -(void)clearTable:(NSString* _Nonnull)name complete:(bg_complete_B)complete;
  197. /**
  198. 删除表.
  199. @name 表名称.
  200. @complete 回调的block.
  201. */
  202. -(void)dropTable:(NSString* _Nonnull)name complete:(bg_complete_B)complete;
  203. /**
  204. 删除表(线程安全).
  205. */
  206. -(void)dropSafeTable:(NSString* _Nonnull)name complete:(bg_complete_B)complete;
  207. /**
  208. 动态添加表字段.
  209. @name 表名称.
  210. @key 将要增加的字段.
  211. @complete 回调的block.
  212. */
  213. -(void)addTable:(NSString* _Nonnull)name key:(NSString* _Nonnull)key complete:(bg_complete_B)complete;
  214. /**
  215. 查询该表中有多少条数据
  216. @name 表名称.
  217. @where 条件数组,形式 @[@"key",@"=",@"value",@"key",@">=",@"value"],为nil时返回全部数据的条数.
  218. */
  219. -(NSInteger)countForTable:(NSString* _Nonnull)name where:(NSArray* _Nullable)where;
  220. /**
  221. 直接传入条件sql语句查询数据条数.
  222. */
  223. -(NSInteger)countForTable:(NSString* _Nonnull)name conditions:(NSString* _Nullable)conditions;
  224. /**
  225. keyPath查询数据条数.
  226. */
  227. -(NSInteger)countForTable:(NSString* _Nonnull)name forKeyPathAndValues:(NSArray* _Nonnull)keyPathValues;
  228. /**
  229. 直接调用sqliteb的原生函数计算sun,min,max,avg等.
  230. */
  231. -(double)sqliteMethodForTable:(NSString* _Nonnull)name type:(bg_sqliteMethodType)methodType key:(NSString* _Nonnull)key where:(NSString* _Nullable)where;
  232. /**
  233. 刷新数据库,即将旧数据库的数据复制到新建的数据库,这是为了去掉没用的字段.
  234. @name 表名称.
  235. @keys 新表的数组字段.
  236. */
  237. -(void)refreshTable:(NSString* _Nonnull)name class:(__unsafe_unretained _Nonnull Class)cla keys:(NSArray<NSString*>* const _Nonnull)keys complete:(bg_complete_I)complete;
  238. -(void)refreshTable:(NSString* _Nonnull)name class:(__unsafe_unretained _Nonnull Class)cla keys:(NSArray* const _Nonnull)keys keyDict:(NSDictionary* const _Nonnull)keyDict complete:(bg_complete_I)complete;
  239. /**
  240. 直接执行sql语句.
  241. @tablename 要操作的表名.
  242. @cla 要操作的类.
  243. */
  244. -(id _Nullable)bg_executeSql:(NSString* const _Nonnull)sql tablename:(NSString* _Nonnull)tablename class:(__unsafe_unretained _Nonnull Class)cla;
  245. #pragma mark 存储数组.
  246. /**
  247. 直接存储数组.
  248. */
  249. -(void)saveArray:(NSArray* _Nonnull)array name:(NSString* _Nonnull)name complete:(bg_complete_B)complete;
  250. /**
  251. 读取数组.
  252. */
  253. -(void)queryArrayWithName:(NSString* _Nonnull)name complete:(bg_complete_A)complete;
  254. /**
  255. 读取数组某个元素.
  256. */
  257. -(id _Nullable)queryArrayWithName:(NSString* _Nonnull)name index:(NSInteger)index;
  258. /**
  259. 更新数组某个元素.
  260. */
  261. -(BOOL)updateObjectWithName:(NSString* _Nonnull)name object:(id _Nonnull)object index:(NSInteger)index;
  262. /**
  263. 删除数组某个元素.
  264. */
  265. -(BOOL)deleteObjectWithName:(NSString* _Nonnull)name index:(NSInteger)index;
  266. #pragma mark 存储字典.
  267. /**
  268. 直接存储字典.
  269. */
  270. -(void)saveDictionary:(NSDictionary* _Nonnull)dictionary complete:(bg_complete_B)complete;
  271. /**
  272. 添加字典元素.
  273. */
  274. -(BOOL)bg_setValue:(id _Nonnull)value forKey:(NSString* const _Nonnull)key;
  275. /**
  276. 更新字典元素.
  277. */
  278. -(BOOL)bg_updateValue:(id _Nonnull)value forKey:(NSString* const _Nonnull)key;
  279. /**
  280. 遍历字典元素.
  281. */
  282. -(void)bg_enumerateKeysAndObjectsUsingBlock:(void (^ _Nonnull)(NSString* _Nonnull key, id _Nonnull value,BOOL *stop))block;
  283. /**
  284. 获取字典元素.
  285. */
  286. -(id _Nullable)bg_valueForKey:(NSString* const _Nonnull)key;
  287. /**
  288. 删除字典元素.
  289. */
  290. -(BOOL)bg_deleteValueForKey:(NSString* const _Nonnull)key;
  291. @end