1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2015 Realm Inc.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 ////////////////////////////////////////////////////////////////////////////
19 #ifndef REALM_SCHEMA_HPP
20 #define REALM_SCHEMA_HPP
25 #include <realm/util/features.h>
33 class Schema : private std::vector<ObjectSchema> {
35 using base = std::vector<ObjectSchema>;
39 // Create a schema from a vector of ObjectSchema
41 Schema(std::initializer_list<ObjectSchema> types);
43 Schema(Schema const&);
45 Schema& operator=(Schema const&);
46 Schema& operator=(Schema&&);
48 // find an ObjectSchema by name
49 iterator find(StringData name);
50 const_iterator find(StringData name) const;
52 // find an ObjectSchema with the same name as the passed in one
53 iterator find(ObjectSchema const& object) noexcept;
54 const_iterator find(ObjectSchema const& object) const noexcept;
56 // Verify that this schema is internally consistent (i.e. all properties are
57 // valid, links link to types that actually exist, etc.)
58 void validate() const;
60 // Get the changes which must be applied to this schema to produce the passed-in schema
61 std::vector<SchemaChange> compare(Schema const&) const;
63 void copy_table_columns_from(Schema const&);
65 friend bool operator==(Schema const&, Schema const&);
66 friend bool operator!=(Schema const& a, Schema const& b) { return !(a == b); }
69 using base::const_iterator;
76 template<typename T, typename U, typename Func>
77 static void zip_matching(T&& a, U&& b, Func&& func);
80 namespace schema_change {
82 const ObjectSchema* object;
85 struct AddInitialProperties {
86 const ObjectSchema* object;
90 const ObjectSchema* object;
91 const Property* property;
94 struct RemoveProperty {
95 const ObjectSchema* object;
96 const Property* property;
99 struct ChangePropertyType {
100 const ObjectSchema* object;
101 const Property* old_property;
102 const Property* new_property;
105 struct MakePropertyNullable {
106 const ObjectSchema* object;
107 const Property* property;
110 struct MakePropertyRequired {
111 const ObjectSchema* object;
112 const Property* property;
116 const ObjectSchema* object;
117 const Property* property;
121 const ObjectSchema* object;
122 const Property* property;
125 struct ChangePrimaryKey {
126 const ObjectSchema* object;
127 const Property* property;
131 #define REALM_FOR_EACH_SCHEMA_CHANGE_TYPE(macro) \
133 macro(AddInitialProperties) \
135 macro(RemoveProperty) \
136 macro(ChangePropertyType) \
137 macro(MakePropertyNullable) \
138 macro(MakePropertyRequired) \
141 macro(ChangePrimaryKey) \
145 #define REALM_SCHEMA_CHANGE_CONSTRUCTOR(name) \
146 SchemaChange(schema_change::name value) : m_kind(Kind::name) { name = value; }
147 REALM_FOR_EACH_SCHEMA_CHANGE_TYPE(REALM_SCHEMA_CHANGE_CONSTRUCTOR)
148 #undef REALM_SCHEMA_CHANGE_CONSTRUCTOR
150 template<typename Visitor>
151 auto visit(Visitor&& visitor) const {
153 #define REALM_SWITCH_CASE(name) case Kind::name: return visitor(name);
154 REALM_FOR_EACH_SCHEMA_CHANGE_TYPE(REALM_SWITCH_CASE)
155 #undef REALM_SWITCH_CASE
157 REALM_COMPILER_HINT_UNREACHABLE();
160 friend bool operator==(SchemaChange const& lft, SchemaChange const& rgt);
163 #define REALM_SCHEMA_CHANGE_TYPE(name) name,
164 REALM_FOR_EACH_SCHEMA_CHANGE_TYPE(REALM_SCHEMA_CHANGE_TYPE)
165 #undef REALM_SCHEMA_CHANGE_TYPE
169 #define REALM_DEFINE_FIELD(name) schema_change::name name;
170 REALM_FOR_EACH_SCHEMA_CHANGE_TYPE(REALM_DEFINE_FIELD)
171 #undef REALM_DEFINE_FIELD
175 #undef REALM_FOR_EACH_SCHEMA_CHANGE_TYPE
178 #endif /* defined(REALM_SCHEMA_HPP) */