DBConfigureCenter.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // DBConfigureCenter.m
  3. // zhuxun
  4. //
  5. // Created by winsoft on 17/4/24.
  6. //
  7. //
  8. #import "DBConfigureCenter.h"
  9. #import <objc/runtime.h>
  10. @implementation DBConfigureCenter
  11. /*
  12. 数据库升级,字段变更核心语法:
  13. if (![db columnExists:@"需要增加的字段" inTableWithName:@"表名"]){
  14. //插入
  15. NSString *alertStr = [NSString stringWithFormat:@"ALTER TABLE %@ ADD %@ %@",@"表名",@"字段名",@"类型(eg:integer)"]
  16. worked = [db executeUpdate:alertStr];
  17. FMDBQuickCheck(worked);
  18. }else{
  19. 暂sqlite原生不支持删除字段.
  20. //删除
  21. ALTER->DELETE?
  22. }
  23. */
  24. //自动升级
  25. + (NSArray<DBStructModel *> *)sqlCloumnCheckWithClassName:(Class )className
  26. {
  27. //字段及name校验
  28. Class cls = className;
  29. // 遍历成员变量列表,其中每个变量都是Ivar类型的结构体
  30. NSMutableArray *standardSQLArray = [NSMutableArray array];
  31. if (className == NULL) {
  32. return nil;
  33. }
  34. unsigned int outCount, j;
  35. objc_property_t *properties = class_copyPropertyList(cls, &outCount);
  36. for (j = 0; j < outCount; j++) {
  37. objc_property_t property = properties[j];
  38. const char *propName = property_getName(property);
  39. if(propName) {
  40. const char *propType = getPropertyType(property);
  41. if (!propType || propType == NULL) {
  42. continue;
  43. }
  44. NSString *propertyName = [NSString stringWithUTF8String:propName];
  45. NSString *propertyType = [NSString stringWithUTF8String:propType];
  46. NSString *sqlType = [self getValueTypeWithValue:propertyType];
  47. [standardSQLArray addObject:[DBStructModel dbStructModelWithName:propertyName type:sqlType]];
  48. }
  49. }
  50. free(properties);
  51. return standardSQLArray;
  52. }
  53. + (NSString *)getDBTableWithTableName:(NSString *)tableName className:(Class )className
  54. {
  55. Class cls = className;
  56. // 遍历成员变量列表,其中每个变量都是Ivar类型的结构体
  57. NSMutableArray *standardSQLArray = [NSMutableArray array];
  58. if (className == NULL) {
  59. return nil;
  60. }
  61. unsigned int outCount, j;
  62. objc_property_t *properties = class_copyPropertyList(cls, &outCount);
  63. for (j = 0; j < outCount; j++) {
  64. objc_property_t property = properties[j];
  65. const char *propName = property_getName(property);
  66. if(propName) {
  67. const char *propType = getPropertyType(property);
  68. if (!property || propType == NULL) {
  69. continue;
  70. }
  71. NSString *propertyName = [NSString stringWithUTF8String:propName];
  72. NSString *propertyType = [NSString stringWithUTF8String:propType];
  73. NSString *oneStandardSQL = [NSString stringWithFormat:@"%@ %@",propertyName,[self getValueTypeWithValue:propertyType]];
  74. NSLog(@"propertyName %@ propertyType %@", propertyName, propertyType);
  75. [standardSQLArray addObject:oneStandardSQL];
  76. }
  77. }
  78. free(properties);
  79. NSString *fullSql = [standardSQLArray componentsJoinedByString:@","];
  80. return [NSString stringWithFormat:@"create table if not exists %@(%@);",tableName,fullSql];
  81. }
  82. + (NSString *)getValueTypeWithValue:(NSString *)valueStr
  83. {
  84. if ([valueStr isEqualToString:@"B"]) {
  85. return @"bool";
  86. }else if ([valueStr isEqualToString:@"q"] || [valueStr isEqualToString:@"NSNumber"] || [valueStr isEqualToString:@"i"])
  87. {
  88. return @"integer";
  89. }else if ([valueStr isEqualToString:@"NSString"])
  90. {
  91. return @"text";
  92. }else if ([valueStr isEqualToString:@"d"])
  93. {
  94. return @"float";
  95. }else if ([valueStr isEqualToString:@"NSDate"])
  96. {
  97. return @"date";
  98. }else return @"bolb";
  99. }
  100. + (NSString *)getKeysArrayStrWithClassName:(Class)className
  101. {
  102. Class cls = className;
  103. // 遍历成员变量列表,其中每个变量都是Ivar类型的结构体
  104. NSMutableArray *standardSQLArray = [NSMutableArray array];
  105. if (className == NULL) {
  106. return nil;
  107. }
  108. unsigned int outCount, j;
  109. objc_property_t *properties = class_copyPropertyList(cls, &outCount);
  110. for (j = 0; j < outCount; j++) {
  111. objc_property_t property = properties[j];
  112. const char *propName = property_getName(property);
  113. const char *propType = getPropertyType(property);
  114. if (!property || propType == NULL) {
  115. continue;
  116. }
  117. if(propName) {
  118. NSString *propertyName = [NSString stringWithUTF8String:propName];
  119. [standardSQLArray addObject:propertyName];
  120. }
  121. }
  122. free(properties);
  123. NSString *fullSql = [standardSQLArray componentsJoinedByString:@","];
  124. return fullSql;
  125. }
  126. //获取属性的方法
  127. static const char *getPropertyType(objc_property_t property) {
  128. const char *attributes = property_getAttributes(property);
  129. //printf("attributes=%s\n", attributes);
  130. char buffer[1 + strlen(attributes)];
  131. strcpy(buffer, attributes);
  132. char *state = buffer, *attribute;
  133. while ((attribute = strsep(&state, ",")) != NULL) {
  134. if (attribute[0] == 'T' && attribute[1] != '@') {
  135. // it's a C primitive type:
  136. // if you want a list of what will be returned for these primitives, search online for
  137. // "objective-c" "Property Attribute Description Examples"
  138. // apple docs list plenty of examples of what you get for int "i", long "l", unsigned "I", struct, etc.
  139. NSString *name = [[NSString alloc] initWithBytes:attribute + 1 length:strlen(attribute) - 1 encoding:NSASCIIStringEncoding];
  140. return (const char *)[name cStringUsingEncoding:NSASCIIStringEncoding];
  141. }
  142. else if (attribute[0] == 'T' && attribute[1] == '@' && strlen(attribute) == 2) {
  143. // it's an ObjC id type:
  144. return "id";
  145. }
  146. else if (attribute[0] == 'T' && attribute[1] == '@') {
  147. // it's another ObjC object type:
  148. NSString *name = [[NSString alloc] initWithBytes:attribute + 3 length:strlen(attribute) - 4 encoding:NSASCIIStringEncoding];
  149. return (const char *)[name cStringUsingEncoding:NSASCIIStringEncoding];
  150. }
  151. }
  152. return "";
  153. }
  154. @end