LKDB+Mapping.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // LKDBProperty+KeyMapping.m
  3. // LKDBHelper
  4. //
  5. // Created by LJH on 13-6-17.
  6. // Copyright (c) 2013年 ljh. All rights reserved.
  7. //
  8. #import "LKDB+Mapping.h"
  9. #import "NSObject+LKModel.h"
  10. @interface LKModelInfos () {
  11. __strong NSMutableDictionary *_proNameDic;
  12. __strong NSMutableDictionary *_sqlNameDic;
  13. __strong NSArray *_primaryKeys;
  14. }
  15. - (void)removeWithColumnName:(NSString *)columnName;
  16. - (void)addDBPropertyWithType:(NSString *)type cname:(NSString *)column_name ctype:(NSString *)ctype pname:(NSString *)pname ptype:(NSString *)ptype;
  17. - (void)updateProperty:(LKDBProperty *)property sqlColumnName:(NSString *)columnName;
  18. - (void)updateProperty:(LKDBProperty *)property propertyName:(NSString *)propertyName;
  19. @end
  20. #pragma mark - 声明属性
  21. @interface LKDBProperty ()
  22. @property (nonatomic, copy) NSString *type;
  23. @property (nonatomic, copy) NSString *sqlColumnName;
  24. @property (nonatomic, copy) NSString *sqlColumnType;
  25. @property (nonatomic, copy) NSString *propertyName;
  26. @property (nonatomic, copy) NSString *propertyType;
  27. - (id)initWithType:(NSString *)type cname:(NSString *)cname ctype:(NSString *)ctype pname:(NSString *)pname ptype:(NSString *)ptype;
  28. @end
  29. #pragma mark - LKDBProperty
  30. @implementation LKDBProperty
  31. - (id)initWithType:(NSString *)type cname:(NSString *)cname ctype:(NSString *)ctype pname:(NSString *)pname ptype:(NSString *)ptype {
  32. self = [super init];
  33. if (self) {
  34. _type = [type copy];
  35. _sqlColumnName = [cname copy];
  36. _sqlColumnType = [ctype copy];
  37. _propertyName = [pname copy];
  38. _propertyType = [ptype copy];
  39. }
  40. return self;
  41. }
  42. - (void)enableUserCalculate {
  43. _type = LKSQL_Mapping_UserCalculate;
  44. }
  45. - (BOOL)isUserCalculate {
  46. return ([_type isEqualToString:LKSQL_Mapping_UserCalculate] || _propertyName == nil || [_propertyName isEqualToString:LKSQL_Mapping_UserCalculate]);
  47. }
  48. @end
  49. #pragma mark - NSObject - TableMapping
  50. @implementation NSObject (TableMapping)
  51. + (NSDictionary *)getTableMapping {
  52. return nil;
  53. }
  54. + (void)setUserCalculateForCN:(NSString *)columnName {
  55. if ([LKDBUtils checkStringIsEmpty:columnName]) {
  56. LKErrorLog(@"columnName is null");
  57. return;
  58. }
  59. LKModelInfos *infos = [self getModelInfos];
  60. LKDBProperty *property = [infos objectWithSqlColumnName:columnName];
  61. if (property) {
  62. [property enableUserCalculate];
  63. } else {
  64. [infos addDBPropertyWithType:LKSQL_Mapping_UserCalculate cname:columnName ctype:LKSQL_Type_Text pname:columnName ptype:@"NSString"];
  65. }
  66. }
  67. + (void)setUserCalculateForPTN:(NSString *)propertyTypeName {
  68. if ([LKDBUtils checkStringIsEmpty:propertyTypeName]) {
  69. LKErrorLog(@"propertyTypeName is null");
  70. return;
  71. }
  72. Class clazz = NSClassFromString(propertyTypeName);
  73. LKModelInfos *infos = [self getModelInfos];
  74. for (NSInteger i = 0; i < infos.count; i++) {
  75. LKDBProperty *property = [infos objectWithIndex:i];
  76. Class p_cls = NSClassFromString(property.propertyType);
  77. BOOL isSubClass = ((p_cls && clazz) && [p_cls isSubclassOfClass:clazz]);
  78. BOOL isNameEqual = [property.propertyType isEqualToString:propertyTypeName];
  79. if (isSubClass || isNameEqual) {
  80. [property enableUserCalculate];
  81. }
  82. }
  83. }
  84. + (void)setTableColumnName:(NSString *)columnName bindingPropertyName:(NSString *)propertyName {
  85. if ([LKDBUtils checkStringIsEmpty:columnName] || [LKDBUtils checkStringIsEmpty:propertyName])
  86. return;
  87. LKModelInfos *infos = [self getModelInfos];
  88. LKDBProperty *property = [infos objectWithPropertyName:propertyName];
  89. if (property == nil) {
  90. return;
  91. }
  92. LKDBProperty *column = [infos objectWithSqlColumnName:columnName];
  93. if (column) {
  94. [infos updateProperty:column propertyName:propertyName];
  95. column.propertyType = property.propertyType;
  96. } else if ([property.sqlColumnName isEqualToString:property.propertyName]) {
  97. [infos updateProperty:property sqlColumnName:columnName];
  98. } else {
  99. [infos addDBPropertyWithType:LKSQL_Mapping_Binding cname:columnName ctype:LKSQL_Type_Text pname:propertyName ptype:property.propertyType];
  100. }
  101. }
  102. + (void)removePropertyWithColumnNameArray:(NSArray *)columnNameArray {
  103. LKModelInfos *infos = [self getModelInfos];
  104. for (NSString *columnName in columnNameArray) {
  105. [infos removeWithColumnName:columnName];
  106. }
  107. }
  108. + (void)removePropertyWithColumnName:(NSString *)columnName {
  109. [[self getModelInfos] removeWithColumnName:columnName];
  110. }
  111. @end
  112. #pragma mark - LKModelInfos
  113. @implementation LKModelInfos
  114. - (id)initWithKeyMapping:(NSDictionary *)keyMapping propertyNames:(NSArray *)propertyNames propertyType:(NSArray *)propertyType primaryKeys:(NSArray *)primaryKeys {
  115. self = [super init];
  116. if (self) {
  117. _primaryKeys = [NSArray arrayWithArray:primaryKeys];
  118. _proNameDic = [[NSMutableDictionary alloc] init];
  119. _sqlNameDic = [[NSMutableDictionary alloc] init];
  120. NSString *type, *column_name, *column_type, *property_name, *property_type;
  121. if (keyMapping.count > 0) {
  122. NSArray *sql_names = keyMapping.allKeys;
  123. for (NSInteger i = 0; i < sql_names.count; i++) {
  124. type = column_name = column_type = property_name = property_type = nil;
  125. column_name = [sql_names objectAtIndex:i];
  126. NSString *mappingValue = [keyMapping objectForKey:column_name];
  127. //如果 设置的 属性名 是空白的 自动转成 使用ColumnName
  128. if ([LKDBUtils checkStringIsEmpty:mappingValue]) {
  129. MOLogV(@"#ERROR sql column name %@ mapping value is empty,automatically converted LKDBInherit", column_name);
  130. mappingValue = LKSQL_Mapping_Inherit;
  131. }
  132. if ([mappingValue isEqualToString:LKSQL_Mapping_UserCalculate]) {
  133. type = LKSQL_Mapping_UserCalculate;
  134. column_type = LKSQL_Type_Text;
  135. } else {
  136. if ([mappingValue isEqualToString:LKSQL_Mapping_Inherit] || [mappingValue isEqualToString:LKSQL_Mapping_Binding]) {
  137. type = LKSQL_Mapping_Inherit;
  138. property_name = column_name;
  139. } else {
  140. type = LKSQL_Mapping_Binding;
  141. property_name = mappingValue;
  142. }
  143. NSUInteger index = [propertyNames indexOfObject:property_name];
  144. NSAssert(index != NSNotFound, @"#ERROR TableMapping SQL column name %@ not fount %@ property name", column_name, property_name);
  145. property_type = [propertyType objectAtIndex:index];
  146. column_type = LKSQLTypeFromObjcType(property_type);
  147. }
  148. [self addDBPropertyWithType:type cname:column_name ctype:column_type pname:property_name ptype:property_type];
  149. }
  150. } else {
  151. for (NSInteger i = 0; i < propertyNames.count; i++) {
  152. type = LKSQL_Mapping_Inherit;
  153. property_name = [propertyNames objectAtIndex:i];
  154. column_name = property_name;
  155. property_type = [propertyType objectAtIndex:i];
  156. column_type = LKSQLTypeFromObjcType(property_type);
  157. [self addDBPropertyWithType:type cname:column_name ctype:column_type pname:property_name ptype:property_type];
  158. }
  159. }
  160. if (_primaryKeys.count == 0) {
  161. _primaryKeys = [NSArray arrayWithObject:@"rowid"];
  162. }
  163. for (NSString *pkname in _primaryKeys) {
  164. if ([pkname.lowercaseString isEqualToString:@"rowid"]) {
  165. if ([self objectWithSqlColumnName:pkname] == nil) {
  166. [self addDBPropertyWithType:LKSQL_Mapping_Inherit cname:pkname ctype:LKSQL_Type_Int pname:pkname ptype:@"int"];
  167. }
  168. }
  169. }
  170. }
  171. return self;
  172. }
  173. - (void)addDBPropertyWithType:(NSString *)type cname:(NSString *)column_name ctype:(NSString *)ctype pname:(NSString *)pname ptype:(NSString *)ptype {
  174. LKDBProperty *db_property = [[LKDBProperty alloc] initWithType:type cname:column_name ctype:ctype pname:pname ptype:ptype];
  175. if (db_property.propertyName) {
  176. _proNameDic[db_property.propertyName] = db_property;
  177. }
  178. if (db_property.sqlColumnName) {
  179. _sqlNameDic[db_property.sqlColumnName] = db_property;
  180. }
  181. }
  182. - (NSArray *)primaryKeys {
  183. return _primaryKeys;
  184. }
  185. - (NSUInteger)count {
  186. return _sqlNameDic.count;
  187. }
  188. - (LKDBProperty *)objectWithIndex:(NSInteger)index {
  189. if (index < _sqlNameDic.count) {
  190. id key = [_sqlNameDic.allKeys objectAtIndex:index];
  191. return [_sqlNameDic objectForKey:key];
  192. }
  193. return nil;
  194. }
  195. - (LKDBProperty *)objectWithPropertyName:(NSString *)propertyName {
  196. return [_proNameDic objectForKey:propertyName];
  197. }
  198. - (LKDBProperty *)objectWithSqlColumnName:(NSString *)columnName {
  199. return [_sqlNameDic objectForKey:columnName];
  200. }
  201. - (void)updateProperty:(LKDBProperty *)property propertyName:(NSString *)propertyName {
  202. [_proNameDic removeObjectForKey:property.propertyName];
  203. property.propertyName = propertyName;
  204. _proNameDic[propertyName] = property;
  205. }
  206. - (void)updateProperty:(LKDBProperty *)property sqlColumnName:(NSString *)columnName {
  207. [_sqlNameDic removeObjectForKey:property.sqlColumnName];
  208. property.sqlColumnName = columnName;
  209. _sqlNameDic[columnName] = property;
  210. }
  211. - (void)removeWithColumnName:(NSString *)columnName {
  212. if ([LKDBUtils checkStringIsEmpty:columnName])
  213. return;
  214. LKDBProperty *property = [_sqlNameDic objectForKey:columnName];
  215. if (property.propertyName) {
  216. [_proNameDic removeObjectForKey:property.propertyName];
  217. }
  218. [_sqlNameDic removeObjectForKey:columnName];
  219. }
  220. @end