remove duplicte code and privitize methods

This commit is contained in:
Ari Lazier 2015-06-03 18:04:21 -07:00
parent a54d2216f9
commit 6e9d9bb793
3 changed files with 44 additions and 36 deletions

View File

@ -35,7 +35,7 @@ ObjectSchema::ObjectSchema(realm::Group *group, std::string name) : name(name) {
if (property.type == PropertyTypeObject || property.type == PropertyTypeArray) {
// set link type for objects and arrays
realm::TableRef linkTable = table->get_link_target(col);
property.object_type = ObjectStore::class_for_table_name(linkTable->get_name().data());
property.object_type = ObjectStore::object_type_for_table_name(linkTable->get_name().data());
}
else {
property.object_type = "";
@ -72,7 +72,7 @@ std::vector<ObjectSchema> ObjectSchema::object_schema_from_group(realm::Group *g
vector<ObjectSchema> object_schema;
for (unsigned long i = 0; i < numTables; i++) {
std::string name = ObjectStore::class_for_table_name(group->get_table_name(i).data());
std::string name = ObjectStore::object_type_for_table_name(group->get_table_name(i).data());
if (name.length()) {
object_schema.push_back(ObjectSchema(group, name));
}

View File

@ -105,26 +105,26 @@ void ObjectStore::set_primary_key_for_object(realm::Group *group, StringData obj
}
}
string ObjectStore::class_for_table_name(string table_name) {
string ObjectStore::object_type_for_table_name(string table_name) {
if (table_name.compare(0, 6, c_object_table_name_prefix) == 0) {
return table_name.substr(6, table_name.length()-6);
}
return string();
}
string ObjectStore::table_name_for_class(string class_name) {
return c_object_table_name_prefix + class_name;
string ObjectStore::table_name_for_object_type(string object_type) {
return c_object_table_name_prefix + object_type;
}
realm::TableRef ObjectStore::table_for_object_type(realm::Group *group, StringData object_type) {
return group->get_table(table_name_for_class(object_type));
return group->get_table(table_name_for_object_type(object_type));
}
realm::TableRef ObjectStore::table_for_object_type_create_if_needed(realm::Group *group, StringData object_type, bool &created) {
return group->get_or_add_table(table_name_for_class(object_type), &created);
return group->get_or_add_table(table_name_for_object_type(object_type), &created);
}
std::vector<std::string> ObjectStore::validate_and_update_column_mapping(realm::Group *group, ObjectSchema &target_schema) {
std::vector<std::string> ObjectStore::validate_schema_and_update_column_mapping(realm::Group *group, ObjectSchema &target_schema) {
vector<string> validation_errors;
ObjectSchema table_schema(group, target_schema.name);
@ -180,9 +180,8 @@ static inline bool property_has_changed(Property &p1, Property &p2) {
// set references to tables on targetSchema and create/update any missing or out-of-date tables
// if update existing is true, updates existing tables, otherwise validates existing tables
// NOTE: must be called from within write transaction
static inline bool create_tables(realm::Group *group, ObjectStore::Schema target_schema, bool update_existing) {
// create metadata tables if neded
bool changed = realm::ObjectStore::create_metadata_tables(group);
bool ObjectStore::create_tables(realm::Group *group, ObjectStore::Schema target_schema, bool update_existing) {
bool changed = false;
// first pass to create missing tables
vector<ObjectSchema *> to_update;
@ -276,14 +275,14 @@ bool ObjectStore::update_realm_with_schema(realm::Group *group,
bool migrating = is_migration_required(group, version);
// create tables
bool changed = create_tables(group, schema, migrating);
bool changed = create_metadata_tables(group) | create_tables(group, schema, migrating);
for (size_t i = 0; i < schema.size(); i++) {
ObjectSchema *target_schema = schema[i].get();
TableRef table = table_for_object_type(group, target_schema->name);
// read-only realms may be missing tables entirely
if (table) {
auto errors = validate_and_update_column_mapping(group, *target_schema);
auto errors = validate_schema_and_update_column_mapping(group, *target_schema);
if (errors.size()) {
throw ObjectStoreValidationException(errors, target_schema->name);
}

View File

@ -28,38 +28,18 @@ namespace realm {
// Schema version used for uninitialized Realms
static const uint64_t NotVersioned;
// check if the realm already has all metadata tables
static bool has_metadata_tables(Group *group);
// create any metadata tables that don't already exist
// must be in write transaction to set
// returns true if it actually did anything
static bool create_metadata_tables(Group *group);
// get the last set schema version
static uint64_t get_schema_version(Group *group);
// set a new schema version
static void set_schema_version(Group *group, uint64_t version);
// get primary key property name for object type
static StringData get_primary_key_for_object(Group *group, StringData object_type);
// sets primary key property for object type
// must be in write transaction to set
static void set_primary_key_for_object(Group *group, StringData object_type, StringData primary_key);
// checks if a migration is required for a given schema version
static bool is_migration_required(realm::Group *group, uint64_t new_version);
// verify a target schema against its table, setting the table_column property on each schema object
// returns array of validation errors
static std::vector<std::string> validate_and_update_column_mapping(Group *group, ObjectSchema &target_schema);
// get the table used to store object of objectClass
static std::string table_name_for_class(std::string class_name);
static std::string class_for_table_name(std::string table_name);
static realm::TableRef table_for_object_type(Group *group, StringData object_type);
static realm::TableRef table_for_object_type_create_if_needed(Group *group, StringData object_type, bool &created);
static bool is_migration_required(realm::Group *group, uint64_t new_version);
static std::vector<std::string> validate_schema_and_update_column_mapping(Group *group, ObjectSchema &target_schema);
// updates a Realm to a given target schema/version creating tables as necessary
// returns if any changes were made
@ -72,6 +52,35 @@ namespace realm {
uint64_t version,
Schema schema,
MigrationFunction migration);
// get a table for an object type
static realm::TableRef table_for_object_type(Group *group, StringData object_type);
private:
// check if the realm already has all metadata tables
static bool has_metadata_tables(Group *group);
// create any metadata tables that don't already exist
// must be in write transaction to set
// returns true if it actually did anything
static bool create_metadata_tables(Group *group);
// set references to tables on targetSchema and create/update any missing or out-of-date tables
// if update existing is true, updates existing tables, otherwise validates existing tables
static bool create_tables(realm::Group *group, ObjectStore::Schema target_schema, bool update_existing);
// get primary key property name for object type
static StringData get_primary_key_for_object(Group *group, StringData object_type);
// sets primary key property for object type
// must be in write transaction to set
static void set_primary_key_for_object(Group *group, StringData object_type, StringData primary_key);
static realm::TableRef table_for_object_type_create_if_needed(Group *group, StringData object_type, bool &created);
static std::string table_name_for_object_type(std::string class_name);
static std::string object_type_for_table_name(std::string table_name);
friend ObjectSchema;
};
class ObjectStoreException : public std::exception {