FLEXRealmDatabaseManager.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // FLEXRealmDatabaseManager.m
  3. // FLEX
  4. //
  5. // Created by Tim Oliver on 28/01/2016.
  6. // Copyright © 2016 Realm. All rights reserved.
  7. //
  8. #import "FLEXRealmDatabaseManager.h"
  9. #if __has_include(<Realm/Realm.h>)
  10. #import <Realm/Realm.h>
  11. #import <Realm/RLMRealm_Dynamic.h>
  12. #else
  13. #import "FLEXRealmDefines.h"
  14. #endif
  15. @interface FLEXRealmDatabaseManager ()
  16. @property (nonatomic, copy) NSString *path;
  17. @property (nonatomic) RLMRealm * realm;
  18. @end
  19. //#endif
  20. @implementation FLEXRealmDatabaseManager
  21. - (instancetype)initWithPath:(NSString*)aPath {
  22. Class realmClass = NSClassFromString(@"RLMRealm");
  23. if (realmClass == nil) {
  24. return nil;
  25. }
  26. self = [super init];
  27. if (self) {
  28. _path = aPath;
  29. }
  30. return self;
  31. }
  32. - (BOOL)open {
  33. Class realmClass = NSClassFromString(@"RLMRealm");
  34. Class configurationClass = NSClassFromString(@"RLMRealmConfiguration");
  35. if (realmClass == nil || configurationClass == nil) {
  36. return NO;
  37. }
  38. NSError *error = nil;
  39. id configuration = [configurationClass new];
  40. [(RLMRealmConfiguration *)configuration setFileURL:[NSURL fileURLWithPath:self.path]];
  41. self.realm = [realmClass realmWithConfiguration:configuration error:&error];
  42. return (error == nil);
  43. }
  44. - (NSArray<NSDictionary<NSString *, id> *> *)queryAllTables {
  45. NSMutableArray<NSDictionary<NSString *, id> *> *allTables = [NSMutableArray array];
  46. RLMSchema *schema = [self.realm schema];
  47. for (RLMObjectSchema *objectSchema in schema.objectSchema) {
  48. if (objectSchema.className == nil) {
  49. continue;
  50. }
  51. NSDictionary<NSString *, id> *dictionary = @{@"name":objectSchema.className};
  52. [allTables addObject:dictionary];
  53. }
  54. return allTables;
  55. }
  56. - (NSArray<NSString *> *)queryAllColumnsWithTableName:(NSString *)tableName {
  57. RLMObjectSchema *objectSchema = [[self.realm schema] schemaForClassName:tableName];
  58. if (objectSchema == nil) {
  59. return nil;
  60. }
  61. NSMutableArray<NSString *> *columnNames = [NSMutableArray array];
  62. for (RLMProperty *property in objectSchema.properties) {
  63. [columnNames addObject:property.name];
  64. }
  65. return columnNames;
  66. }
  67. - (NSArray<NSDictionary<NSString *, id> *> *)queryAllDataWithTableName:(NSString *)tableName {
  68. RLMObjectSchema *objectSchema = [[self.realm schema] schemaForClassName:tableName];
  69. RLMResults *results = [self.realm allObjects:tableName];
  70. if (results.count == 0 || objectSchema == nil) {
  71. return nil;
  72. }
  73. NSMutableArray<NSDictionary<NSString *, id> *> *allDataEntries = [NSMutableArray array];
  74. for (RLMObject *result in results) {
  75. NSMutableDictionary<NSString *, id> *entry = [NSMutableDictionary dictionary];
  76. for (RLMProperty *property in objectSchema.properties) {
  77. id value = [result valueForKey:property.name];
  78. entry[property.name] = (value) ? (value) : [NSNull null];
  79. }
  80. [allDataEntries addObject:entry];
  81. }
  82. return allDataEntries;
  83. }
  84. @end