FFDataBaseModel+Custom.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // FFDataBaseModel+Custom.m
  3. // FFDB
  4. //
  5. // Created by Fidetro on 2017/7/16.
  6. // Copyright © 2017年 Fidetro. All rights reserved.
  7. //
  8. // https://github.com/Fidetro/FFDB
  9. #import "FFDataBaseModel+Custom.h"
  10. #import <objc/runtime.h>
  11. #import "NSObject+FIDProperty.h"
  12. NSString *const kDatabaseHeadname = @"FID";
  13. @implementation FFDataBaseModel (Custom)
  14. + (NSString *)tableName
  15. {
  16. return [NSString stringWithFormat:@"%@%@",kDatabaseHeadname,NSStringFromClass([self class])];
  17. }
  18. + (NSArray *)memoryPropertys
  19. {
  20. return nil;
  21. }
  22. + (NSDictionary *)columnsType
  23. {
  24. return nil;
  25. }
  26. + (NSArray *)columnsOfSelf
  27. {
  28. unsigned int count = 0;
  29. Ivar *ivarList = class_copyIvarList(self, &count);
  30. NSMutableArray *propertyNames = [NSMutableArray array];
  31. for (int i = 0; i < count; i++)
  32. {
  33. Ivar ivar = ivarList[i];
  34. NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
  35. NSString *key;
  36. if ([[name substringToIndex:1] isEqualToString:@"_"])
  37. {
  38. key = [name substringFromIndex:1];
  39. }
  40. else
  41. {
  42. key = [name substringFromIndex:0];
  43. }
  44. if ([[self memoryPropertys]containsObject:key])
  45. {
  46. continue;
  47. }
  48. NSString *customColumn = [self customColumns][key];
  49. if (customColumn != nil)
  50. {
  51. key = customColumn;
  52. }
  53. [propertyNames addObject:key];
  54. }
  55. free(ivarList);
  56. return [propertyNames copy];
  57. }
  58. + (NSDictionary *)customColumns
  59. {
  60. return nil;
  61. }
  62. - (id)sendGetMethodWithPropertyName:(NSString *)propertyName
  63. {
  64. NSArray *propertyNames = [[[self class]customColumns] allKeysForObject:propertyName];
  65. if (propertyNames.count == 0)
  66. {
  67. return [super sendGetMethodWithPropertyName:propertyName];;
  68. }
  69. else
  70. {
  71. NSString *customColumn = [propertyNames lastObject];
  72. return [super sendGetMethodWithPropertyName:customColumn];;
  73. }
  74. }
  75. - (void)setPropertyWithName:(NSString *)propertyName object:(id)object
  76. {
  77. NSArray *propertyNames = [[[self class]customColumns] allKeysForObject:propertyName];
  78. if (propertyNames.count == 0)
  79. {
  80. [super setPropertyWithName:propertyName object:object];
  81. }
  82. else
  83. {
  84. NSString *customColumn = [propertyNames lastObject];
  85. [super setPropertyWithName:customColumn object:object];
  86. }
  87. }
  88. @end