BGTool.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // BGTool.h
  3. // BGFMDB
  4. //
  5. // Created by huangzhibiao on 17/2/16.
  6. // Copyright © 2017年 Biao. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <objc/runtime.h>
  10. /**
  11. 日志输出
  12. */
  13. #ifdef DEBUG
  14. #define bg_log(...) HLog(__VA_ARGS__)
  15. #else
  16. #define bg_log(...)
  17. #endif
  18. #define bg_completeBlock(obj) !complete?:complete(obj);
  19. #define BG @"BG_"
  20. #define bg_tableNameKey @"bg_tableName"
  21. #define bg_rowid @"rowid"
  22. #define bg_uniqueKeysSelector NSSelectorFromString(@"bg_uniqueKeys")
  23. #define bg_ignoreKeysSelector NSSelectorFromString(@"bg_ignoreKeys")
  24. #define bg_unionPrimaryKeysSelector NSSelectorFromString(@"bg_unionPrimaryKeys")
  25. typedef NS_ENUM(NSInteger,bg_getModelInfoType){//过滤数据类型
  26. bg_ModelInfoInsert,//插入过滤
  27. bg_ModelInfoSingleUpdate,//单条更新过滤
  28. bg_ModelInfoArrayUpdate,//批量更新过滤
  29. bg_ModelInfoNone//无过滤
  30. };
  31. @interface BGTool : NSObject
  32. /**
  33. json字符转json格式数据 .
  34. */
  35. +(id _Nonnull)jsonWithString:(NSString* _Nonnull)jsonString;
  36. /**
  37. 字典转json字符 .
  38. */
  39. +(NSString* _Nonnull)dataToJson:(id _Nonnull)data;
  40. /**
  41. 根据类获取变量名列表
  42. @onlyKey YES:紧紧返回key,NO:在key后面添加type.
  43. */
  44. +(NSArray* _Nonnull)getClassIvarList:(__unsafe_unretained _Nonnull Class)cla Object:(_Nullable id)object onlyKey:(BOOL)onlyKey;
  45. /**
  46. 抽取封装条件数组处理函数.
  47. */
  48. +(NSArray* _Nonnull)where:(NSArray* _Nonnull)where;
  49. /**
  50. 封装like语句获取函数
  51. */
  52. +(NSString* _Nonnull)getLikeWithKeyPathAndValues:(NSArray* _Nonnull)keyPathValues where:(BOOL)where;
  53. /**
  54. 判断是不是主键.
  55. */
  56. +(BOOL)isUniqueKey:(NSString* _Nonnull)uniqueKey with:(NSString* _Nonnull)param;
  57. /**
  58. 判断并获取字段类型.
  59. */
  60. +(NSString* _Nonnull)keyAndType:(NSString* _Nonnull)param;
  61. /**
  62. 根据类属性类型返回数据库存储类型.
  63. */
  64. +(NSString* _Nonnull)getSqlType:(NSString* _Nonnull)type;
  65. //NSDate转字符串,格式: yyyy-MM-dd HH:mm:ss
  66. +(NSString* _Nonnull)stringWithDate:(NSDate* _Nonnull)date;
  67. /**
  68. 根据传入的对象获取表名.
  69. */
  70. +(NSString* _Nonnull)getTableNameWithObject:(id _Nonnull)object;
  71. /**
  72. 根据类属性值和属性类型返回数据库存储的值.
  73. @value 数值.
  74. @type 数组value的类型.
  75. @encode YES:编码 , NO:解码.
  76. */
  77. +(id _Nonnull)getSqlValue:(id _Nonnull)value type:(NSString* _Nonnull)type encode:(BOOL)encode;
  78. /**
  79. 转换从数据库中读取出来的数据.
  80. @tableName 表名(即类名).
  81. @array 传入要转换的数组数据.
  82. */
  83. +(NSArray* _Nonnull)tansformDataFromSqlDataWithTableName:(NSString* _Nonnull)tableName class:(__unsafe_unretained _Nonnull Class)cla array:(NSArray* _Nonnull)array;
  84. /**
  85. 转换从数据库中读取出来的数据.
  86. @claName 类名.
  87. @valueDict 传入要转换的字典数据.
  88. */
  89. +(id _Nonnull)objectFromJsonStringWithTableName:(NSString* _Nonnull)tablename class:(__unsafe_unretained _Nonnull Class)cla valueDict:(NSDictionary* _Nonnull)valueDict;
  90. /**
  91. 字典或json格式字符转模型用的处理函数.
  92. */
  93. +(id _Nonnull)bg_objectWithClass:(__unsafe_unretained _Nonnull Class)cla value:(id _Nonnull)value;
  94. /**
  95. 模型转字典.
  96. */
  97. +(NSMutableDictionary* _Nonnull)bg_keyValuesWithObject:(id _Nonnull)object ignoredKeys:(NSArray* _Nullable)ignoredKeys;
  98. /**
  99. 判断并执行类方法.
  100. */
  101. +(id _Nonnull)executeSelector:(SEL _Nonnull)selector forClass:(__unsafe_unretained _Nonnull Class)cla;
  102. /**
  103. 判断并执行对象方法.
  104. */
  105. +(id _Nonnull)executeSelector:(SEL _Nonnull)selector forObject:(id _Nonnull)object;
  106. /**
  107. 根据对象获取要更新或插入的字典.
  108. */
  109. +(NSDictionary* _Nonnull)getDictWithObject:(id _Nonnull)object ignoredKeys:(NSArray* const _Nullable)ignoredKeys filtModelInfoType:(bg_getModelInfoType)filtModelInfoType;
  110. /**
  111. 过滤建表的key.
  112. */
  113. +(NSArray* _Nonnull)bg_filtCreateKeys:(NSArray* _Nonnull)createkeys ignoredkeys:(NSArray* _Nonnull)ignoredkeys;
  114. /**
  115. 如果表格不存在就新建.
  116. */
  117. +(BOOL)ifNotExistWillCreateTableWithObject:(id _Nonnull)object ignoredKeys:(NSArray* const _Nullable)ignoredKeys;
  118. /**
  119. 整形判断
  120. */
  121. + (BOOL)isPureInt:(NSString* _Nonnull)string;
  122. /**
  123. 浮点形判断
  124. */
  125. + (BOOL)isPureFloat:(NSString* _Nonnull)string;
  126. /**
  127. NSUserDefaults封装使用函数.
  128. */
  129. +(BOOL)getBoolWithKey:(NSString* _Nonnull)key;
  130. +(void)setBoolWithKey:(NSString* _Nonnull)key value:(BOOL)value;
  131. +(NSString* _Nonnull)getStringWithKey:(NSString* _Nonnull)key;
  132. +(void)setStringWithKey:(NSString* _Nonnull)key value:(NSString* _Nonnull)value;
  133. +(NSInteger)getIntegerWithKey:(NSString* _Nonnull)key;
  134. +(void)setIntegerWithKey:(NSString* _Nonnull)key value:(NSInteger)value;
  135. @end