RLMClassInfo.mm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import "RLMClassInfo.hpp"
  19. #import "RLMRealm_Private.hpp"
  20. #import "RLMObjectSchema_Private.h"
  21. #import "RLMSchema.h"
  22. #import "RLMProperty_Private.h"
  23. #import "RLMQueryUtil.hpp"
  24. #import "RLMUtil.hpp"
  25. #import <realm/object-store/object_schema.hpp>
  26. #import <realm/object-store/object_store.hpp>
  27. #import <realm/object-store/schema.hpp>
  28. #import <realm/object-store/shared_realm.hpp>
  29. #import <realm/table.hpp>
  30. using namespace realm;
  31. RLMClassInfo::RLMClassInfo(RLMRealm *realm, RLMObjectSchema *rlmObjectSchema,
  32. const realm::ObjectSchema *objectSchema)
  33. : realm(realm), rlmObjectSchema(rlmObjectSchema), objectSchema(objectSchema) { }
  34. realm::TableRef RLMClassInfo::table() const {
  35. if (auto key = objectSchema->table_key) {
  36. return realm.group.get_table(objectSchema->table_key);
  37. }
  38. return nullptr;
  39. }
  40. RLMProperty *RLMClassInfo::propertyForTableColumn(ColKey col) const noexcept {
  41. auto const& props = objectSchema->persisted_properties;
  42. for (size_t i = 0; i < props.size(); ++i) {
  43. if (props[i].column_key == col) {
  44. return rlmObjectSchema.properties[i];
  45. }
  46. }
  47. return nil;
  48. }
  49. RLMProperty *RLMClassInfo::propertyForPrimaryKey() const noexcept {
  50. return rlmObjectSchema.primaryKeyProperty;
  51. }
  52. realm::ColKey RLMClassInfo::tableColumn(NSString *propertyName) const {
  53. return tableColumn(RLMValidatedProperty(rlmObjectSchema, propertyName));
  54. }
  55. realm::ColKey RLMClassInfo::tableColumn(RLMProperty *property) const {
  56. return objectSchema->persisted_properties[property.index].column_key;
  57. }
  58. RLMClassInfo &RLMClassInfo::linkTargetType(size_t propertyIndex) {
  59. return realm->_info[rlmObjectSchema.properties[propertyIndex].objectClassName];
  60. }
  61. RLMClassInfo &RLMClassInfo::linkTargetType(realm::Property const& property) {
  62. REALM_ASSERT(property.type == PropertyType::Object);
  63. return linkTargetType(&property - &objectSchema->persisted_properties[0]);
  64. }
  65. RLMClassInfo &RLMClassInfo::resolve(__unsafe_unretained RLMRealm *const realm) {
  66. return realm->_info[rlmObjectSchema.className];
  67. }
  68. RLMSchemaInfo::impl::iterator RLMSchemaInfo::begin() noexcept { return m_objects.begin(); }
  69. RLMSchemaInfo::impl::iterator RLMSchemaInfo::end() noexcept { return m_objects.end(); }
  70. RLMSchemaInfo::impl::const_iterator RLMSchemaInfo::begin() const noexcept { return m_objects.begin(); }
  71. RLMSchemaInfo::impl::const_iterator RLMSchemaInfo::end() const noexcept { return m_objects.end(); }
  72. RLMClassInfo& RLMSchemaInfo::operator[](NSString *name) {
  73. auto it = m_objects.find(name);
  74. if (it == m_objects.end()) {
  75. @throw RLMException(@"Object type '%@' is not managed by the Realm. "
  76. @"If using a custom `objectClasses` / `objectTypes` array in your configuration, "
  77. @"add `%@` to the list of `objectClasses` / `objectTypes`.",
  78. name, name);
  79. }
  80. return *&it->second;
  81. }
  82. RLMSchemaInfo::RLMSchemaInfo(RLMRealm *realm) {
  83. RLMSchema *rlmSchema = realm.schema;
  84. realm::Schema const& schema = realm->_realm->schema();
  85. // rlmSchema can be larger due to multiple classes backed by one table
  86. REALM_ASSERT(rlmSchema.objectSchema.count >= schema.size());
  87. m_objects.reserve(schema.size());
  88. for (RLMObjectSchema *rlmObjectSchema in rlmSchema.objectSchema) {
  89. m_objects.emplace(std::piecewise_construct,
  90. std::forward_as_tuple(rlmObjectSchema.className),
  91. std::forward_as_tuple(realm, rlmObjectSchema,
  92. &*schema.find(rlmObjectSchema.objectName.UTF8String)));
  93. }
  94. }
  95. RLMSchemaInfo RLMSchemaInfo::clone(realm::Schema const& source_schema,
  96. __unsafe_unretained RLMRealm *const target_realm) {
  97. RLMSchemaInfo info;
  98. info.m_objects.reserve(m_objects.size());
  99. auto& schema = target_realm->_realm->schema();
  100. for (auto& pair : m_objects) {
  101. size_t idx = pair.second.objectSchema - &*source_schema.begin();
  102. info.m_objects.emplace(std::piecewise_construct,
  103. std::forward_as_tuple(pair.first),
  104. std::forward_as_tuple(target_realm, pair.second.rlmObjectSchema,
  105. &*schema.begin() + idx));
  106. }
  107. return info;
  108. }