You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.0 KiB

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