diff --git a/object_store.cpp b/object_store.cpp index 1e8f0025..18160e08 100644 --- a/object_store.cpp +++ b/object_store.cpp @@ -293,7 +293,7 @@ bool ObjectStore::create_tables(realm::Group *group, ObjectStore::Schema &target bool ObjectStore::is_migration_required(realm::Group *group, uint64_t new_version) { uint64_t old_version = get_schema_version(group); if (old_version > new_version && old_version != NotVersioned) { - throw ObjectStoreException(ObjectStoreException::RealmVersionGreaterThanSchemaVersion); + throw ObjectStoreException(ObjectStoreException::Kind::RealmVersionGreaterThanSchemaVersion); } return old_version != new_version; } @@ -387,7 +387,7 @@ bool ObjectStore::update_indexes(Group *group, Schema &schema) { table->add_search_index(property.table_column); } catch (realm::LogicError const&) { - throw ObjectStoreException(ObjectStoreException::RealmPropertyTypeNotIndexable, { + throw ObjectStoreException(ObjectStoreException::Kind::RealmPropertyTypeNotIndexable, { {"object_type", object_schema.name}, {"property_name", property.name}, {"property_type", string_for_property_type(property.type)} @@ -411,7 +411,8 @@ void ObjectStore::validate_primary_column_uniqueness(Group *group, Schema &schem TableRef table = table_for_object_type(group, object_schema.name); if (table->get_distinct_view(primary_prop->table_column).size() != table->size()) { - throw ObjectStoreException(ObjectStoreException::RealmDuplicatePrimaryKeyValue, {{"object_type", object_schema.name}, {"property_name", primary_prop->name}}); + throw ObjectStoreException(ObjectStoreException::Kind::RealmDuplicatePrimaryKeyValue, + {{"object_type", object_schema.name}, {"property_name", primary_prop->name}}); } } } diff --git a/object_store.hpp b/object_store.hpp index f60d1f55..278ed39c 100644 --- a/object_store.hpp +++ b/object_store.hpp @@ -24,6 +24,7 @@ #include #include "object_schema.hpp" +#include "object_store_exceptions.hpp" namespace realm { class Group; @@ -105,38 +106,6 @@ namespace realm { friend ObjectSchema; }; - - class ObjectStoreException : public std::exception { - public: - enum Kind { - // thrown when calling update_realm_to_schema and the realm version is greater than the given version - RealmVersionGreaterThanSchemaVersion, - RealmPropertyTypeNotIndexable, // object_type, property_name, property_type - RealmDuplicatePrimaryKeyValue, // object_type, property_name - }; - typedef std::map Dict; - - ObjectStoreException(Kind kind, Dict dict = Dict()) : m_kind(kind), m_dict(dict) {} - - ObjectStoreException::Kind kind() const { return m_kind; } - const ObjectStoreException::Dict &dict() const { return m_dict; } - - private: - Kind m_kind; - Dict m_dict; - }; - - class ObjectStoreValidationException : public std::exception { - public: - ObjectStoreValidationException(std::vector validation_errors, std::string object_type) : - m_validation_errors(validation_errors), m_object_type(object_type) {} - const std::vector &validation_errors() const { return m_validation_errors; } - std::string object_type() const { return m_object_type; } - - private: - std::vector m_validation_errors; - std::string m_object_type; - }; } #endif /* defined(REALM_OBJECT_STORE_HPP) */ diff --git a/object_store_exceptions.cpp b/object_store_exceptions.cpp new file mode 100644 index 00000000..67c4b37c --- /dev/null +++ b/object_store_exceptions.cpp @@ -0,0 +1,45 @@ +//////////////////////////////////////////////////////////////////////////// +// +// Copyright 2014 Realm Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////// + +#include "object_store_exceptions.hpp" + +using namespace realm; +using namespace std; + +ObjectStoreException::ObjectStoreException(Kind kind, Dict dict) : m_kind(kind), m_dict(dict) { + switch (m_kind) { + case Kind::RealmVersionGreaterThanSchemaVersion: + m_what = "Schema version less than last set version in Realm"; + break; + case Kind::RealmPropertyTypeNotIndexable: + m_what = "Can't index property '" + m_dict.at("object_type") + "." + m_dict.at("property_name") + "': " + + "indexing properties of type '" + m_dict.at("property_type") + "' is currently not supported"; + break; + case Kind::RealmDuplicatePrimaryKeyValue: + m_what = "Primary key property '" + m_dict["property_name"] + "' has duplicate values after migration."; + break; + } +} + +ObjectStoreValidationException::ObjectStoreValidationException(std::vector validation_errors, std::string object_type) : + m_validation_errors(validation_errors), m_object_type(object_type) { + m_what = "Migration is required for object type '" + m_object_type + "' due to the following errors:"; + for (auto error : m_validation_errors) { + m_what += "\n- " + error; + } +} diff --git a/object_store_exceptions.hpp b/object_store_exceptions.hpp new file mode 100644 index 00000000..7f6cc2e8 --- /dev/null +++ b/object_store_exceptions.hpp @@ -0,0 +1,66 @@ +//////////////////////////////////////////////////////////////////////////// +// +// Copyright 2014 Realm Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////// + +#ifndef REALM_OBJECT_STORE_EXCEPTIONS_HPP +#define REALM_OBJECT_STORE_EXCEPTIONS_HPP + +#include +#include +#include + +namespace realm { + + class ObjectStoreException : public std::exception { + public: + enum class Kind { + // thrown when calling update_realm_to_schema and the realm version is greater than the given version + RealmVersionGreaterThanSchemaVersion, + RealmPropertyTypeNotIndexable, // object_type, property_name, property_type + RealmDuplicatePrimaryKeyValue, // object_type, property_name + }; + typedef std::map Dict; + + ObjectStoreException(Kind kind, Dict dict = Dict()); + + ObjectStoreException::Kind kind() const { return m_kind; } + const ObjectStoreException::Dict &dict() const { return m_dict; } + + const char *what() const noexcept override { return m_what.c_str(); } + + private: + Kind m_kind; + Dict m_dict; + std::string m_what; + }; + + class ObjectStoreValidationException : public std::exception { + public: + ObjectStoreValidationException(std::vector validation_errors, std::string object_type); + + const std::vector &validation_errors() const { return m_validation_errors; } + std::string object_type() const { return m_object_type; } + const char *what() const noexcept override { return m_what.c_str(); } + + private: + std::vector m_validation_errors; + std::string m_object_type; + std::string m_what; + }; +} + +#endif /* defined(REALM_OBJECT_STORE_EXCEPTIONS_HPP) */ diff --git a/property.hpp b/property.hpp index 0a368684..9679034a 100644 --- a/property.hpp +++ b/property.hpp @@ -16,8 +16,8 @@ // //////////////////////////////////////////////////////////////////////////// -#ifndef __realm__property__ -#define __realm__property__ +#ifndef REALM_PROPERTY_HPP +#define REALM_PROPERTY_HPP #include @@ -83,4 +83,4 @@ namespace realm { } } -#endif /* defined__realm__property__ */ +#endif /* REALM_PROPERTY_HPP */