FLEXRealmDatabaseManager.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #import "NSArray+Functional.h"
  10. #if __has_include(<Realm/Realm.h>)
  11. #import <Realm/Realm.h>
  12. #import <Realm/RLMRealm_Dynamic.h>
  13. #else
  14. #import "FLEXRealmDefines.h"
  15. #endif
  16. @interface FLEXRealmDatabaseManager ()
  17. @property (nonatomic, copy) NSString *path;
  18. @property (nonatomic) RLMRealm * realm;
  19. @end
  20. @implementation FLEXRealmDatabaseManager
  21. static Class RLMRealmClass = nil;
  22. + (void)load {
  23. RLMRealmClass = NSClassFromString(@"RLMRealm");
  24. }
  25. + (instancetype)managerForDatabase:(NSString *)path {
  26. return [[self alloc] initWithPath:path];
  27. }
  28. - (instancetype)initWithPath:(NSString *)path {
  29. if (!RLMRealmClass) {
  30. return nil;
  31. }
  32. self = [super init];
  33. if (self) {
  34. _path = path;
  35. }
  36. return self;
  37. }
  38. - (BOOL)open {
  39. Class configurationClass = NSClassFromString(@"RLMRealmConfiguration");
  40. if (!RLMRealmClass || !configurationClass) {
  41. return NO;
  42. }
  43. NSError *error = nil;
  44. id configuration = [configurationClass new];
  45. [(RLMRealmConfiguration *)configuration setFileURL:[NSURL fileURLWithPath:self.path]];
  46. self.realm = [RLMRealmClass realmWithConfiguration:configuration error:&error];
  47. return (error == nil);
  48. }
  49. - (NSArray<NSString *> *)queryAllTables {
  50. // Map each schema to its name
  51. return [self.realm.schema.objectSchema flex_mapped:^id(RLMObjectSchema *schema, NSUInteger idx) {
  52. return schema.className ?: nil;
  53. }];
  54. }
  55. - (NSArray<NSString *> *)queryAllColumnsWithTableName:(NSString *)tableName {
  56. RLMObjectSchema *objectSchema = [self.realm.schema schemaForClassName:tableName];
  57. // Map each column to its name
  58. return [objectSchema.properties flex_mapped:^id(RLMProperty *property, NSUInteger idx) {
  59. return property.name;
  60. }];
  61. }
  62. - (NSArray<NSArray *> *)queryAllDataWithTableName:(NSString *)tableName {
  63. RLMObjectSchema *objectSchema = [self.realm.schema schemaForClassName:tableName];
  64. RLMResults *results = [self.realm allObjects:tableName];
  65. if (results.count == 0 || !objectSchema) {
  66. return nil;
  67. }
  68. // Map results to an array of rows
  69. return [NSArray flex_mapped:results block:^id(RLMObject *result, NSUInteger idx) {
  70. // Map each row to an array of the values of its properties
  71. return [objectSchema.properties flex_mapped:^id(RLMProperty *property, NSUInteger idx) {
  72. return [result valueForKey:property.name] ?: NSNull.null;
  73. }];
  74. }];
  75. }
  76. @end