FLEXRealmDatabaseManager.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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+FLEX.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. if (![self open]) {
  37. return nil;
  38. }
  39. }
  40. return self;
  41. }
  42. - (BOOL)open {
  43. Class configurationClass = NSClassFromString(@"RLMRealmConfiguration");
  44. if (!RLMRealmClass || !configurationClass) {
  45. return NO;
  46. }
  47. NSError *error = nil;
  48. id configuration = [configurationClass new];
  49. [(RLMRealmConfiguration *)configuration setFileURL:[NSURL fileURLWithPath:self.path]];
  50. self.realm = [RLMRealmClass realmWithConfiguration:configuration error:&error];
  51. return (error == nil);
  52. }
  53. - (NSArray<NSString *> *)queryAllTables {
  54. // Map each schema to its name
  55. NSArray<NSString *> *tableNames = [self.realm.schema.objectSchema flex_mapped:^id(RLMObjectSchema *schema, NSUInteger idx) {
  56. return schema.className ?: nil;
  57. }];
  58. return [tableNames sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
  59. }
  60. - (NSArray<NSString *> *)queryAllColumnsOfTable:(NSString *)tableName {
  61. RLMObjectSchema *objectSchema = [self.realm.schema schemaForClassName:tableName];
  62. // Map each column to its name
  63. return [objectSchema.properties flex_mapped:^id(RLMProperty *property, NSUInteger idx) {
  64. return property.name;
  65. }];
  66. }
  67. - (NSArray<NSArray *> *)queryAllDataInTable:(NSString *)tableName {
  68. RLMObjectSchema *objectSchema = [self.realm.schema schemaForClassName:tableName];
  69. RLMResults *results = [self.realm allObjects:tableName];
  70. if (results.count == 0 || !objectSchema) {
  71. return nil;
  72. }
  73. // Map results to an array of rows
  74. return [NSArray flex_mapped:results block:^id(RLMObject *result, NSUInteger idx) {
  75. // Map each row to an array of the values of its properties
  76. return [objectSchema.properties flex_mapped:^id(RLMProperty *property, NSUInteger idx) {
  77. return [result valueForKey:property.name] ?: NSNull.null;
  78. }];
  79. }];
  80. }
  81. @end