LKDB+Mapping.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // LKDBProperty+KeyMapping.m
  3. // LKDBHelper
  4. //
  5. // Created by upin on 13-6-17.
  6. // Copyright (c) 2013年 ljh. All rights reserved.
  7. //
  8. #import "LKDB+Mapping.h"
  9. #import "NSObject+LKModel.h"
  10. inline NSString *LKSQLTypeFromObjcType(NSString* objcType)
  11. {
  12. if([LKSQLIntType rangeOfString:objcType].location != NSNotFound){
  13. return LKSQLInt;
  14. }
  15. if ([LKSQLFloatType rangeOfString:objcType].location != NSNotFound) {
  16. return LKSQLDouble;
  17. }
  18. if ([LKSQLBlobType rangeOfString:objcType].location != NSNotFound) {
  19. return LKSQLBlob;
  20. }
  21. return LKSQLText;
  22. }
  23. #pragma mark- 声明
  24. @interface LKDBProperty()
  25. {
  26. __strong NSString* _type;
  27. __strong NSString* _sqlColumeName;
  28. __strong NSString* _sqlColumeType;
  29. __strong NSString* _propertyName;
  30. __strong NSString* _propertyType;
  31. }
  32. -(id)initWithType:(NSString*)type cname:(NSString*)cname ctype:(NSString*)ctype pname:(NSString*)pname ptype:(NSString*)ptype;
  33. @end
  34. @interface LKModelInfos()
  35. {
  36. __strong NSMutableDictionary* _proNameDic;
  37. __strong NSMutableDictionary* _sqlNameDic;
  38. __strong NSMutableArray* _primaryKeys;
  39. }
  40. -(void)removeWithColumeName:(NSString*)columeName;
  41. @end
  42. #pragma mark- LKDBProperty
  43. @implementation LKDBProperty
  44. -(id)initWithType:(NSString *)type cname:(NSString *)cname ctype:(NSString *)ctype pname:(NSString *)pname ptype:(NSString *)ptype
  45. {
  46. self = [super init];
  47. if(self)
  48. {
  49. _type = [type copy];
  50. _sqlColumeName = [cname copy];
  51. _sqlColumeType = [ctype copy];
  52. _propertyName = [pname copy];
  53. _propertyType = [ptype copy];
  54. }
  55. return self;
  56. }
  57. -(void)enableUserCalculate
  58. {
  59. _type = LKSQLUserCalculate;
  60. }
  61. -(BOOL)isUserCalculate
  62. {
  63. return ([_type isEqualToString:LKSQLUserCalculate] || _propertyName == nil || [_propertyName isEqualToString:LKSQLUserCalculate]);
  64. }
  65. @end
  66. #pragma mark- NSObject - TableMapping
  67. @implementation NSObject(TableMapping)
  68. +(NSDictionary *)getTableMapping
  69. {
  70. return nil;
  71. }
  72. +(void)setUserCalculateForCN:(NSString *)columename
  73. {
  74. LKDBProperty* property = [[self getModelInfos] objectWithSqlColumeName:columename];
  75. [property enableUserCalculate];
  76. }
  77. +(void)setUserCalculateForPTN:(NSString *)propertyTypeName
  78. {
  79. LKModelInfos* infos = [self getModelInfos];
  80. for (int i=0; i<infos.count; i++) {
  81. LKDBProperty* property = [infos objectWithIndex:i];
  82. if([property.propertyType isEqualToString:propertyTypeName])
  83. {
  84. [property enableUserCalculate];
  85. }
  86. }
  87. }
  88. +(void)removePropertyWithColumeName:(NSString *)columename
  89. {
  90. [[self getModelInfos] removeWithColumeName:columename];
  91. }
  92. @end
  93. #pragma mark- LKModelInfos
  94. @implementation LKModelInfos
  95. - (id)initWithKeyMapping:(NSDictionary *)keyMapping propertyNames:(NSArray *)propertyNames propertyType:(NSArray *)propertyType primaryKeys:(NSArray *)primaryKeys
  96. {
  97. self = [super init];
  98. if (self) {
  99. _primaryKeys = [primaryKeys copy];
  100. _proNameDic = [[NSMutableDictionary alloc]init];
  101. _sqlNameDic = [[NSMutableDictionary alloc]init];
  102. NSString *type,*colume_name,*colume_type,*property_name,*property_type;
  103. if(keyMapping.count>0)
  104. {
  105. NSArray* sql_names = keyMapping.allKeys;
  106. for (int i =0; i< sql_names.count; i++) {
  107. type = colume_name = colume_type = property_name = property_type = nil;
  108. colume_name = [sql_names objectAtIndex:i];
  109. NSString* mappingValue = [keyMapping objectForKey:colume_name];
  110. //如果 设置的 属性名 是空白的 自动转成 使用ColumeName
  111. if([LKDBUtils checkStringIsEmpty:mappingValue])
  112. {
  113. NSLog(@"#ERROR sql colume name %@ mapping value is empty,automatically converted LKDBInherit",colume_name);
  114. mappingValue = LKSQLInherit;
  115. }
  116. if([mappingValue isEqualToString:LKSQLUserCalculate])
  117. {
  118. type = LKSQLUserCalculate;
  119. colume_type = LKSQLText;
  120. }
  121. else
  122. {
  123. if([mappingValue isEqualToString:LKSQLInherit] || [mappingValue isEqualToString:LKSQLBinding])
  124. {
  125. type = LKSQLInherit;
  126. property_name = colume_name;
  127. }
  128. else
  129. {
  130. type = LKSQLBinding;
  131. property_name = mappingValue;
  132. }
  133. int index = [propertyNames indexOfObject:property_name];
  134. NSAssert(index != NSNotFound, @"#ERROR TableMapping SQL colume name %@ not fount %@ property name",colume_name,property_name);
  135. property_type = [propertyType objectAtIndex:index];
  136. colume_type = LKSQLTypeFromObjcType(property_type);
  137. }
  138. [self addDBPropertyWithType:type cname:colume_name ctype:colume_type pname:property_name ptype:property_type];
  139. }
  140. }
  141. else
  142. {
  143. for (int i=0; i<propertyNames.count; i++) {
  144. type = LKSQLInherit;
  145. property_name = [propertyNames objectAtIndex:i];
  146. colume_name = property_name;
  147. property_type = [propertyType objectAtIndex:i];
  148. colume_type = LKSQLTypeFromObjcType(property_type);
  149. [self addDBPropertyWithType:type cname:colume_name ctype:colume_type pname:property_name ptype:property_type];
  150. }
  151. }
  152. }
  153. return self;
  154. }
  155. -(void)addDBPropertyWithType:(NSString *)type cname:(NSString *)colume_name ctype:(NSString *)ctype pname:(NSString *)pname ptype:(NSString *)ptype
  156. {
  157. LKDBProperty* db_property = [[LKDBProperty alloc]initWithType:type cname:colume_name ctype:ctype pname:pname ptype:ptype];
  158. if(db_property.propertyName)
  159. {
  160. [_proNameDic setObject:db_property forKey:db_property.propertyName];
  161. }
  162. if(db_property.sqlColumeName){
  163. [_sqlNameDic setObject:db_property forKey:db_property.sqlColumeName];
  164. }
  165. }
  166. -(NSArray *)primaryKeys
  167. {
  168. return _primaryKeys;
  169. }
  170. -(int)count
  171. {
  172. return _sqlNameDic.count;
  173. }
  174. -(LKDBProperty *)objectWithIndex:(int)index
  175. {
  176. if(index < _sqlNameDic.count)
  177. {
  178. id key = [_sqlNameDic.allKeys objectAtIndex:index];
  179. return [_sqlNameDic objectForKey:key];
  180. }
  181. return nil;
  182. }
  183. -(LKDBProperty *)objectWithPropertyName:(NSString *)propertyName
  184. {
  185. return [_proNameDic objectForKey:propertyName];
  186. }
  187. -(LKDBProperty *)objectWithSqlColumeName:(NSString *)columeName
  188. {
  189. return [_sqlNameDic objectForKey:columeName];
  190. }
  191. -(void)removeWithColumeName:(NSString*)columeName
  192. {
  193. if(columeName == nil)
  194. return;
  195. LKDBProperty* property = [_sqlNameDic objectForKey:columeName];
  196. if(property.propertyName)
  197. {
  198. [_proNameDic removeObjectForKey:property.propertyName];
  199. }
  200. [_sqlNameDic removeObjectForKey:columeName];
  201. }
  202. @end