Merge pull request #36 from realm/tg/schema-literal
Make Schema constructable from initializer lists
This commit is contained in:
commit
4607e75f90
|
@ -28,6 +28,14 @@ using namespace realm;
|
||||||
|
|
||||||
ObjectSchema::~ObjectSchema() = default;
|
ObjectSchema::~ObjectSchema() = default;
|
||||||
|
|
||||||
|
ObjectSchema::ObjectSchema(std::string name, std::string primary_key, std::initializer_list<Property> 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) {
|
ObjectSchema::ObjectSchema(const Group *group, const std::string &name) : name(name) {
|
||||||
ConstTableRef table = ObjectStore::table_for_object_type(group, 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);
|
primary_key = realm::ObjectStore::get_primary_key_for_object(group, name);
|
||||||
if (primary_key.length()) {
|
set_primary_key_property();
|
||||||
auto primary_key_prop = primary_key_property();
|
|
||||||
if (!primary_key_prop) {
|
|
||||||
throw InvalidPrimaryKeyException(name, primary_key);
|
|
||||||
}
|
|
||||||
primary_key_prop->is_primary = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Property *ObjectSchema::property_for_name(StringData name) {
|
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 {
|
const Property *ObjectSchema::property_for_name(StringData name) const {
|
||||||
return const_cast<ObjectSchema *>(this)->property_for_name(name);
|
return const_cast<ObjectSchema *>(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@ namespace realm {
|
||||||
class ObjectSchema {
|
class ObjectSchema {
|
||||||
public:
|
public:
|
||||||
ObjectSchema() = default;
|
ObjectSchema() = default;
|
||||||
|
ObjectSchema(std::string name, std::string primary_key, std::initializer_list<Property> properties);
|
||||||
~ObjectSchema();
|
~ObjectSchema();
|
||||||
|
|
||||||
// create object schema from existing table
|
// create object schema from existing table
|
||||||
|
@ -49,6 +50,9 @@ namespace realm {
|
||||||
const Property *primary_key_property() const {
|
const Property *primary_key_property() const {
|
||||||
return property_for_name(primary_key);
|
return property_for_name(primary_key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void set_primary_key_property();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,7 @@ namespace realm {
|
||||||
bool is_indexed = false;
|
bool is_indexed = false;
|
||||||
bool is_nullable = false;
|
bool is_nullable = false;
|
||||||
|
|
||||||
size_t table_column;
|
size_t table_column = -1;
|
||||||
bool requires_index() const { return is_primary || is_indexed; }
|
bool requires_index() const { return is_primary || is_indexed; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,7 @@ private:
|
||||||
public:
|
public:
|
||||||
// Create a schema from a vector of ObjectSchema
|
// Create a schema from a vector of ObjectSchema
|
||||||
Schema(base types);
|
Schema(base types);
|
||||||
|
Schema(std::initializer_list<ObjectSchema> types) : Schema(base(types)) { }
|
||||||
|
|
||||||
// find an ObjectSchema by name
|
// find an ObjectSchema by name
|
||||||
iterator find(std::string const& name);
|
iterator find(std::string const& name);
|
||||||
|
|
Loading…
Reference in New Issue