RLMClassInfo.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "object_schema.hpp"
  26. #import "object_store.hpp"
  27. #import "schema.hpp"
  28. #import "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::Table *RLMClassInfo::table() const {
  35. if (!m_table) {
  36. m_table = ObjectStore::table_for_object_type(realm.group, objectSchema->name).get();
  37. }
  38. return m_table;
  39. }
  40. RLMProperty *RLMClassInfo::propertyForTableColumn(NSUInteger col) const noexcept {
  41. auto const& props = objectSchema->persisted_properties;
  42. for (size_t i = 0; i < props.size(); ++i) {
  43. if (props[i].table_column == col) {
  44. return rlmObjectSchema.properties[i];
  45. }
  46. }
  47. return nil;
  48. }
  49. RLMProperty *RLMClassInfo::propertyForPrimaryKey() const noexcept {
  50. return rlmObjectSchema.primaryKeyProperty;
  51. }
  52. NSUInteger RLMClassInfo::tableColumn(NSString *propertyName) const {
  53. return tableColumn(RLMValidatedProperty(rlmObjectSchema, propertyName));
  54. }
  55. NSUInteger RLMClassInfo::tableColumn(RLMProperty *property) const {
  56. return objectSchema->persisted_properties[property.index].table_column;
  57. }
  58. RLMClassInfo &RLMClassInfo::linkTargetType(size_t propertyIndex) {
  59. if (propertyIndex < m_linkTargets.size() && m_linkTargets[propertyIndex]) {
  60. return *m_linkTargets[propertyIndex];
  61. }
  62. if (m_linkTargets.size() <= propertyIndex) {
  63. m_linkTargets.resize(propertyIndex + 1);
  64. }
  65. m_linkTargets[propertyIndex] = &realm->_info[rlmObjectSchema.properties[propertyIndex].objectClassName];
  66. return *m_linkTargets[propertyIndex];
  67. }
  68. RLMClassInfo &RLMClassInfo::linkTargetType(realm::Property const& property) {
  69. REALM_ASSERT(property.type == PropertyType::Object);
  70. return linkTargetType(&property - &objectSchema->persisted_properties[0]);
  71. }
  72. RLMSchemaInfo::impl::iterator RLMSchemaInfo::begin() noexcept { return m_objects.begin(); }
  73. RLMSchemaInfo::impl::iterator RLMSchemaInfo::end() noexcept { return m_objects.end(); }
  74. RLMSchemaInfo::impl::const_iterator RLMSchemaInfo::begin() const noexcept { return m_objects.begin(); }
  75. RLMSchemaInfo::impl::const_iterator RLMSchemaInfo::end() const noexcept { return m_objects.end(); }
  76. RLMClassInfo& RLMSchemaInfo::operator[](NSString *name) {
  77. auto it = m_objects.find(name);
  78. if (it == m_objects.end()) {
  79. @throw RLMException(@"Object type '%@' is not managed by the Realm. "
  80. @"If using a custom `objectClasses` / `objectTypes` array in your configuration, "
  81. @"add `%@` to the list of `objectClasses` / `objectTypes`.",
  82. name, name);
  83. }
  84. return *&it->second;
  85. }
  86. RLMSchemaInfo::RLMSchemaInfo(RLMRealm *realm) {
  87. RLMSchema *rlmSchema = realm.schema;
  88. realm::Schema const& schema = realm->_realm->schema();
  89. // rlmSchema can be larger due to multiple classes backed by one table
  90. REALM_ASSERT(rlmSchema.objectSchema.count >= schema.size());
  91. m_objects.reserve(schema.size());
  92. for (RLMObjectSchema *rlmObjectSchema in rlmSchema.objectSchema) {
  93. m_objects.emplace(std::piecewise_construct,
  94. std::forward_as_tuple(rlmObjectSchema.className),
  95. std::forward_as_tuple(realm, rlmObjectSchema,
  96. &*schema.find(rlmObjectSchema.objectName.UTF8String)));
  97. }
  98. }
  99. RLMSchemaInfo RLMSchemaInfo::clone(realm::Schema const& source_schema,
  100. __unsafe_unretained RLMRealm *const target_realm) {
  101. RLMSchemaInfo info;
  102. info.m_objects.reserve(m_objects.size());
  103. auto& schema = target_realm->_realm->schema();
  104. for (auto& pair : m_objects) {
  105. size_t idx = pair.second.objectSchema - &*source_schema.begin();
  106. info.m_objects.emplace(std::piecewise_construct,
  107. std::forward_as_tuple(pair.first),
  108. std::forward_as_tuple(target_realm, pair.second.rlmObjectSchema,
  109. &*schema.begin() + idx));
  110. }
  111. return info;
  112. }