FLEXRealmDatabaseManager.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. @interface FLEXRealmDatabaseManager ()
  13. @property (nonatomic, copy) NSString *path;
  14. @property (nonatomic, strong) RLMRealm *realm;
  15. @end
  16. #endif
  17. @implementation FLEXRealmDatabaseManager
  18. #if __has_include("<Realm/Realm.h>")
  19. - (instancetype)initWithPath:(NSString*)aPath
  20. {
  21. self = [super init];
  22. if (self) {
  23. _path = aPath;
  24. }
  25. return self;
  26. }
  27. - (BOOL)open
  28. {
  29. RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
  30. configuration.dynamic = YES;
  31. configuration.path = self.path;
  32. NSError *error = nil;
  33. self.realm = [RLMRealm realmWithConfiguration:configuration error:&error];
  34. return (error == nil);
  35. }
  36. - (NSArray *)queryAllTables
  37. {
  38. NSMutableArray *allTables = [NSMutableArray array];
  39. RLMSchema *schema = self.realm.schema;
  40. for (RLMObjectSchema *objectSchema in schema.objectSchema) {
  41. if (objectSchema.className == nil) {
  42. continue;
  43. }
  44. NSDictionary *dictionary = @{@"name":objectSchema.className};
  45. [allTables addObject:dictionary];
  46. }
  47. return allTables;
  48. }
  49. - (NSArray *)queryAllColumnsWithTableName:(NSString *)tableName
  50. {
  51. RLMObjectSchema *objectSchema = [self.realm.schema schemaForClassName:tableName];
  52. if (objectSchema == nil) {
  53. return nil;
  54. }
  55. NSMutableArray *columnNames = [NSMutableArray array];
  56. for (RLMProperty *property in objectSchema.properties) {
  57. [columnNames addObject:property.name];
  58. }
  59. return columnNames;
  60. }
  61. - (NSArray *)queryAllDataWithTableName:(NSString *)tableName
  62. {
  63. RLMObjectSchema *objectSchema = [self.realm.schema schemaForClassName:tableName];
  64. RLMResults *results = [self.realm allObjects:tableName];
  65. if (results.count == 0 || objectSchema == nil) {
  66. return nil;
  67. }
  68. NSMutableArray *allDataEntries = [NSMutableArray array];
  69. for (RLMObject *result in results) {
  70. NSMutableDictionary *entry = [NSMutableDictionary dictionary];
  71. for (RLMProperty *property in objectSchema.properties) {
  72. entry[property.name] = [result valueForKey:property.name];
  73. }
  74. [allDataEntries addObject:entry];
  75. }
  76. return allDataEntries;
  77. }
  78. #endif
  79. @end