diff --git a/src/object_schema.cpp b/src/object_schema.cpp index 33cd2497..3be3953f 100644 --- a/src/object_schema.cpp +++ b/src/object_schema.cpp @@ -28,6 +28,14 @@ using namespace realm; ObjectSchema::~ObjectSchema() = default; +ObjectSchema::ObjectSchema(std::string name, std::string primary_key, std::initializer_list properties) +: name(std::move(name)) +, properties(properties) +, primary_key(std::move(primary_key)) +{ + set_primary_key_property(); +} + ObjectSchema::ObjectSchema(const Group *group, const std::string &name) : name(name) { ConstTableRef table = ObjectStore::table_for_object_type(group, name); @@ -50,13 +58,7 @@ ObjectSchema::ObjectSchema(const Group *group, const std::string &name) : name(n } primary_key = realm::ObjectStore::get_primary_key_for_object(group, name); - if (primary_key.length()) { - auto primary_key_prop = primary_key_property(); - if (!primary_key_prop) { - throw InvalidPrimaryKeyException(name, primary_key); - } - primary_key_prop->is_primary = true; - } + set_primary_key_property(); } Property *ObjectSchema::property_for_name(StringData name) { @@ -71,3 +73,14 @@ Property *ObjectSchema::property_for_name(StringData name) { const Property *ObjectSchema::property_for_name(StringData name) const { return const_cast(this)->property_for_name(name); } + +void ObjectSchema::set_primary_key_property() +{ + if (primary_key.length()) { + auto primary_key_prop = primary_key_property(); + if (!primary_key_prop) { + throw InvalidPrimaryKeyException(name, primary_key); + } + primary_key_prop->is_primary = true; + } +} diff --git a/src/object_schema.hpp b/src/object_schema.hpp index 4ede6ce5..10a2e555 100644 --- a/src/object_schema.hpp +++ b/src/object_schema.hpp @@ -31,6 +31,7 @@ namespace realm { class ObjectSchema { public: ObjectSchema() = default; + ObjectSchema(std::string name, std::string primary_key, std::initializer_list properties); ~ObjectSchema(); // create object schema from existing table @@ -49,6 +50,9 @@ namespace realm { const Property *primary_key_property() const { return property_for_name(primary_key); } + + private: + void set_primary_key_property(); }; } diff --git a/src/property.hpp b/src/property.hpp index 3883e805..05993ae1 100644 --- a/src/property.hpp +++ b/src/property.hpp @@ -53,7 +53,7 @@ namespace realm { bool is_indexed = false; bool is_nullable = false; - size_t table_column; + size_t table_column = -1; bool requires_index() const { return is_primary || is_indexed; } }; diff --git a/src/schema.hpp b/src/schema.hpp index b8d50777..2df1ec9e 100644 --- a/src/schema.hpp +++ b/src/schema.hpp @@ -31,6 +31,7 @@ private: public: // Create a schema from a vector of ObjectSchema Schema(base types); + Schema(std::initializer_list types) : Schema(base(types)) { } // find an ObjectSchema by name iterator find(std::string const& name);