FLEXRealmDatabaseManager.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. NSError *error = nil;
  30. RLMRealmConfiguration *configuration = [[RLMRealmConfiguration alloc] init];
  31. configuration.path = self.path;
  32. self.realm = [RLMRealm realmWithConfiguration:configuration error:&error];
  33. return (error == nil);
  34. }
  35. - (NSArray *)queryAllTables
  36. {
  37. NSMutableArray *allTables = [NSMutableArray array];
  38. RLMSchema *schema = self.realm.schema;
  39. for (RLMObjectSchema *objectSchema in schema.objectSchema) {
  40. if (objectSchema.className == nil) {
  41. continue;
  42. }
  43. NSDictionary *dictionary = @{@"name":objectSchema.className};
  44. [allTables addObject:dictionary];
  45. }
  46. return allTables;
  47. }
  48. - (NSArray *)queryAllColumnsWithTableName:(NSString *)tableName
  49. {
  50. RLMObjectSchema *objectSchema = [self.realm.schema schemaForClassName:tableName];
  51. if (objectSchema == nil) {
  52. return nil;
  53. }
  54. NSMutableArray *columnNames = [NSMutableArray array];
  55. for (RLMProperty *property in objectSchema.properties) {
  56. [columnNames addObject:property.name];
  57. }
  58. return columnNames;
  59. }
  60. - (NSArray *)queryAllDataWithTableName:(NSString *)tableName
  61. {
  62. RLMObjectSchema *objectSchema = [self.realm.schema schemaForClassName:tableName];
  63. RLMResults *results = [self.realm allObjects:tableName];
  64. if (results.count == 0 || objectSchema == nil) {
  65. return nil;
  66. }
  67. NSMutableArray *allDataEntries = [NSMutableArray array];
  68. for (RLMObject *result in results) {
  69. NSMutableDictionary *entry = [NSMutableDictionary dictionary];
  70. for (RLMProperty *property in objectSchema.properties) {
  71. entry[property.name] = [result valueForKey:property.name];
  72. }
  73. [allDataEntries addObject:entry];
  74. }
  75. return allDataEntries;
  76. }
  77. #endif
  78. @end