LKDBHelper.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // LKDBHelper.h
  3. // upin
  4. //
  5. // Created by Fanhuan on 12-12-6.
  6. // Copyright (c) 2012年 linggan. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "FMDatabaseQueue.h"
  10. #import "FMDatabase.h"
  11. #import "LKDBUtils.h"
  12. #import "LKDB+Manager.h"
  13. #import "LKDB+Mapping.h"
  14. #import "NSObject+LKModel.h"
  15. #import "NSObject+LKDBHelper.h"
  16. #ifdef DEBUG
  17. #ifdef NSLog
  18. #define LKLog(fmt, ...) NSLog(@"#LKLog:\n" fmt,##__VA_ARGS__);
  19. #else
  20. #define LKLog(fmt, ...) NSLog(@"\n#LKLog: %s [Line %d] \n" fmt, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
  21. #endif
  22. #else
  23. # define LKLog(...)
  24. #endif
  25. @interface LKDBHelper : NSObject
  26. // you can use [LKDBHelper getUsingLKDBHelper]
  27. #pragma mark- deprecated
  28. +(LKDBHelper*)sharedDBHelper DEPRECATED_ATTRIBUTE;
  29. #pragma mark-
  30. -(id)initWithDBName:(NSString*)dbname;
  31. /**
  32. * @brief change database , filepath the use of : "documents/db/" + fileName + ".db"
  33. */
  34. -(void)setDBName:(NSString*)fileName;
  35. /**
  36. * @brief execute database operations synchronously,not afraid of recursive deadlock 同步执行数据库操作 可递归调用
  37. */
  38. -(void)executeDB:(void (^)(FMDatabase *db))block;
  39. -(BOOL)executeSQL:(NSString *)sql arguments:(NSArray *)args;
  40. -(NSString *)executeScalarWithSQL:(NSString *)sql arguments:(NSArray *)args;
  41. @end
  42. @interface LKDBHelper(DatabaseManager)
  43. //create table with entity class
  44. -(BOOL)createTableWithModelClass:(Class)model;
  45. //drop all table
  46. -(void)dropAllTable;
  47. //drop table with entity class
  48. -(BOOL)dropTableWithClass:(Class)modelClass;
  49. @end
  50. @interface LKDBHelper(DatabaseExecute)
  51. /**
  52. * @brief The number of rows query table
  53. *
  54. * @param modelClass entity class
  55. * @param where can use NSString or NSDictionary or nil
  56. *
  57. * @return rows number
  58. */
  59. -(int)rowCount:(Class)modelClass where:(id)where;
  60. -(void)rowCount:(Class)modelClass where:(id)where callback:(void(^)(int rowCount))callback;
  61. /**
  62. * @brief query table
  63. *
  64. * @param modelClass entity class
  65. * @param where can use NSString or NSDictionary or nil
  66. * @param orderBy The Sort: Ascending "name asc",Descending "name desc"
  67. For example: @"rowid desc" or @"rowid asc"
  68. * @param offset Skip how many rows
  69. * @param count Limit the number
  70. *
  71. * @return query finished result is an array(model instance collection)
  72. */
  73. -(NSMutableArray*)search:(Class)modelClass where:(id)where orderBy:(NSString*)orderBy offset:(int)offset count:(int)count;
  74. -(void)search:(Class)modelClass where:(id)where orderBy:(NSString*)orderBy offset:(int)offset count:(int)count callback:(void(^)(NSMutableArray* array))block;
  75. //return first model or nil
  76. -(id)searchSingle:(Class)modelClass where:(id)where orderBy:(NSString*)orderBy;
  77. /**
  78. * @brief insert table
  79. *
  80. * @param model you want to insert the entity
  81. *
  82. * @return the inserted was successful
  83. */
  84. -(BOOL)insertToDB:(NSObject*)model;
  85. -(void)insertToDB:(NSObject*)model callback:(void(^)(BOOL result))block;
  86. /**
  87. * @brief insert when the entity primary key does not exist
  88. *
  89. * @param model you want to insert the entity
  90. *
  91. * @return the inserted was successful
  92. */
  93. -(BOOL)insertWhenNotExists:(NSObject*)model;
  94. -(void)insertWhenNotExists:(NSObject*)model callback:(void(^)(BOOL result))block;
  95. /**
  96. * @brief update table
  97. *
  98. * @param model you want to update the entity
  99. * @param where can use NSString or NSDictionary or nil
  100. when "where" is nil : update the value based on rowid colume or primary key colume
  101. *
  102. * @return the updated was successful
  103. */
  104. -(BOOL)updateToDB:(NSObject *)model where:(id)where;
  105. -(void)updateToDB:(NSObject *)model where:(id)where callback:(void (^)(BOOL result))block;
  106. -(BOOL)updateToDB:(Class)modelClass set:(NSString*)sets where:(id)where;
  107. /**
  108. * @brief delete table
  109. *
  110. * @param model you want to delete entity
  111. when entity property "rowid" == 0 based on the primary key to delete
  112. *
  113. * @return the deleted was successful
  114. */
  115. -(BOOL)deleteToDB:(NSObject*)model;
  116. -(void)deleteToDB:(NSObject*)model callback:(void(^)(BOOL result))block;
  117. /**
  118. * @brief delete table with "where" constraint
  119. *
  120. * @param modelClass entity class
  121. * @param where can use NSString or NSDictionary, can not is nil
  122. *
  123. * @return the deleted was successful
  124. */
  125. -(BOOL)deleteWithClass:(Class)modelClass where:(id)where;
  126. -(void)deleteWithClass:(Class)modelClass where:(id)where callback:(void (^)(BOOL result))block;
  127. /**
  128. * @brief entity exists?
  129. * for primary key colume
  130. (if rowid > 0 would certainly exist so we do not rowid judgment)
  131. * @param model entity
  132. *
  133. * @return YES: entity presence , NO: entity not exist
  134. */
  135. -(BOOL)isExistsModel:(NSObject*)model;
  136. -(BOOL)isExistsClass:(Class)modelClass where:(id)where;
  137. /**
  138. * @brief Clear data based on the entity class
  139. *
  140. * @param modelClass entity class
  141. */
  142. +(void)clearTableData:(Class)modelClass;
  143. /**
  144. * @brief Clear Unused Data File
  145. if you property has UIImage or NSData, will save their data in the (documents dir)
  146. *
  147. * @param modelClass entity class
  148. * @param columes UIImage or NSData Colume Name
  149. */
  150. +(void)clearNoneImage:(Class)modelClass columes:(NSArray*)columes;
  151. +(void)clearNoneData:(Class)modelClass columes:(NSArray*)columes;
  152. @end