FLEXRealmDatabaseManager.m 2.6 KB

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