object.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2017 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. #include "object.hpp"
  19. #include "impl/object_notifier.hpp"
  20. #include "impl/realm_coordinator.hpp"
  21. #include "object_schema.hpp"
  22. #include "object_store.hpp"
  23. #include <realm/table.hpp>
  24. using namespace realm;
  25. Object Object::freeze(std::shared_ptr<Realm> frozen_realm) const
  26. {
  27. return Object(frozen_realm, frozen_realm->import_copy_of(m_obj));
  28. }
  29. bool Object::is_frozen() const noexcept
  30. {
  31. return m_realm->is_frozen();
  32. }
  33. InvalidatedObjectException::InvalidatedObjectException(const std::string& object_type)
  34. : std::logic_error("Accessing object of type " + object_type + " which has been invalidated or deleted")
  35. , object_type(object_type)
  36. {}
  37. InvalidPropertyException::InvalidPropertyException(const std::string& object_type, const std::string& property_name)
  38. : std::logic_error(util::format("Property '%1.%2' does not exist", object_type, property_name))
  39. , object_type(object_type), property_name(property_name)
  40. {}
  41. MissingPropertyValueException::MissingPropertyValueException(const std::string& object_type, const std::string& property_name)
  42. : std::logic_error(util::format("Missing value for property '%1.%2'", object_type, property_name))
  43. , object_type(object_type), property_name(property_name)
  44. {}
  45. MissingPrimaryKeyException::MissingPrimaryKeyException(const std::string& object_type)
  46. : std::logic_error(util::format("'%1' does not have a primary key defined", object_type))
  47. , object_type(object_type)
  48. {}
  49. ReadOnlyPropertyException::ReadOnlyPropertyException(const std::string& object_type, const std::string& property_name)
  50. : std::logic_error(util::format("Cannot modify read-only property '%1.%2'", object_type, property_name))
  51. , object_type(object_type), property_name(property_name) {}
  52. ModifyPrimaryKeyException::ModifyPrimaryKeyException(const std::string& object_type, const std::string& property_name)
  53. : std::logic_error(util::format("Cannot modify primary key after creation: '%1.%2'", object_type, property_name))
  54. , object_type(object_type), property_name(property_name) {}
  55. Object::Object(SharedRealm r, ObjectSchema const& s, Obj const& o)
  56. : m_realm(std::move(r)), m_object_schema(&s), m_obj(o) { }
  57. Object::Object(SharedRealm r, Obj const& o)
  58. : m_realm(std::move(r))
  59. , m_object_schema(&*m_realm->schema().find(ObjectStore::object_type_for_table_name(o.get_table()->get_name())))
  60. , m_obj(o)
  61. {
  62. }
  63. Object::Object(SharedRealm r, StringData object_type, ObjKey key)
  64. : m_realm(std::move(r))
  65. , m_object_schema(&*m_realm->schema().find(object_type))
  66. , m_obj(ObjectStore::table_for_object_type(m_realm->read_group(), object_type)->get_object(key))
  67. {
  68. }
  69. Object::Object(SharedRealm r, StringData object_type, size_t index)
  70. : m_realm(std::move(r))
  71. , m_object_schema(&*m_realm->schema().find(object_type))
  72. , m_obj(ObjectStore::table_for_object_type(m_realm->read_group(), object_type)->get_object(index))
  73. {
  74. }
  75. Object::Object() = default;
  76. Object::~Object() = default;
  77. Object::Object(Object const&) = default;
  78. Object::Object(Object&&) = default;
  79. Object& Object::operator=(Object const&) = default;
  80. Object& Object::operator=(Object&&) = default;
  81. NotificationToken Object::add_notification_callback(CollectionChangeCallback callback) &
  82. {
  83. verify_attached();
  84. m_realm->verify_notifications_available();
  85. if (!m_notifier) {
  86. m_notifier = std::make_shared<_impl::ObjectNotifier>(m_realm, m_obj.get_table()->get_key(), m_obj.get_key());
  87. _impl::RealmCoordinator::register_notifier(m_notifier);
  88. }
  89. return {m_notifier, m_notifier->add_callback(std::move(callback))};
  90. }
  91. void Object::verify_attached() const
  92. {
  93. m_realm->verify_thread();
  94. if (!m_obj.is_valid()) {
  95. throw InvalidatedObjectException(m_object_schema->name);
  96. }
  97. }
  98. Property const& Object::property_for_name(StringData prop_name) const
  99. {
  100. auto prop = m_object_schema->property_for_name(prop_name);
  101. if (!prop) {
  102. throw InvalidPropertyException(m_object_schema->name, prop_name);
  103. }
  104. return *prop;
  105. }
  106. void Object::validate_property_for_setter(Property const& property) const
  107. {
  108. verify_attached();
  109. m_realm->verify_in_write();
  110. // Modifying primary keys is allowed in migrations to make it possible to
  111. // add a new primary key to a type (or change the property type), but it
  112. // is otherwise considered the immutable identity of the row
  113. if (property.is_primary) {
  114. if (!m_realm->is_in_migration())
  115. throw ModifyPrimaryKeyException(m_object_schema->name, property.name);
  116. // Modifying the PK property while it's the PK will corrupt the table,
  117. // so remove it and then restore it at the end of the migration (which will rebuild the table)
  118. m_obj.get_table()->set_primary_key_column({});
  119. }
  120. }
  121. #if REALM_ENABLE_SYNC
  122. void Object::ensure_user_in_everyone_role()
  123. {
  124. if (auto role_table = m_realm->read_group().get_table("class___Role")) {
  125. if (ObjKey ndx = role_table->find_first_string(role_table->get_column_key("name"), "everyone")) {
  126. auto role = role_table->get_object(ndx);
  127. auto users = role.get_linklist(role_table->get_column_key("members"));
  128. if (users.find_first(m_obj.get_key()) == realm::npos) {
  129. users.add(m_obj.get_key());
  130. }
  131. }
  132. }
  133. }
  134. void Object::ensure_private_role_exists_for_user()
  135. {
  136. auto user_id = m_obj.get<StringData>("id");
  137. ObjectStore::ensure_private_role_exists_for_user(static_cast<Transaction&>(m_realm->read_group()), user_id);
  138. }
  139. #endif