2015-05-28 23:53:35 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-05-29 00:11:54 +00:00
|
|
|
#ifndef __realm__object_store__
|
|
|
|
#define __realm__object_store__
|
2015-05-28 23:53:35 +00:00
|
|
|
|
|
|
|
#include <realm/group.hpp>
|
2015-06-04 00:27:21 +00:00
|
|
|
#include "object_schema.hpp"
|
2015-05-28 23:53:35 +00:00
|
|
|
|
|
|
|
namespace realm {
|
|
|
|
class ObjectStore {
|
|
|
|
public:
|
|
|
|
// Schema version used for uninitialized Realms
|
|
|
|
static const uint64_t NotVersioned;
|
|
|
|
|
|
|
|
// get the last set schema version
|
2015-06-04 00:27:21 +00:00
|
|
|
static uint64_t get_schema_version(Group *group);
|
2015-05-28 23:53:35 +00:00
|
|
|
|
|
|
|
// set a new schema version
|
2015-06-04 01:09:14 +00:00
|
|
|
// FIXME - should be private (set through update_realm_with_schema)
|
2015-06-04 00:27:21 +00:00
|
|
|
static void set_schema_version(Group *group, uint64_t version);
|
2015-05-28 23:53:35 +00:00
|
|
|
|
2015-06-04 01:04:21 +00:00
|
|
|
// checks if a migration is required for a given schema version
|
|
|
|
static bool is_migration_required(realm::Group *group, uint64_t new_version);
|
2015-06-04 00:27:21 +00:00
|
|
|
|
|
|
|
// verify a target schema against its table, setting the table_column property on each schema object
|
|
|
|
// returns array of validation errors
|
2015-06-04 01:04:21 +00:00
|
|
|
static std::vector<std::string> validate_schema_and_update_column_mapping(Group *group, ObjectSchema &target_schema);
|
2015-06-04 00:27:21 +00:00
|
|
|
|
|
|
|
// updates a Realm to a given target schema/version creating tables as necessary
|
|
|
|
// returns if any changes were made
|
|
|
|
// passed in schema ar updated with the correct column mapping
|
|
|
|
// optionally runs migration function/lambda if schema is out of date
|
|
|
|
// NOTE: must be performed within a write transaction
|
|
|
|
typedef std::function<void()> MigrationFunction;
|
|
|
|
typedef std::vector<ObjectSchemaRef> Schema;
|
|
|
|
static bool update_realm_with_schema(Group *group,
|
|
|
|
uint64_t version,
|
|
|
|
Schema schema,
|
|
|
|
MigrationFunction migration);
|
2015-06-04 01:04:21 +00:00
|
|
|
|
|
|
|
// 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;
|
2015-06-04 00:27:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
ObjectStoreException(Kind kind) : m_kind(kind) {}
|
|
|
|
ObjectStoreException::Kind kind() { return m_kind; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Kind m_kind;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ObjectStoreValidationException : public std::exception {
|
|
|
|
public:
|
|
|
|
ObjectStoreValidationException(std::vector<std::string> validation_errors, std::string object_type) :
|
|
|
|
m_validation_errors(validation_errors), m_object_type(object_type) {}
|
|
|
|
std::vector<std::string> validation_errors() { return m_validation_errors; }
|
|
|
|
std::string object_type() { return m_object_type; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::string> m_validation_errors;
|
|
|
|
std::string m_object_type;
|
2015-05-28 23:53:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-05-29 00:11:54 +00:00
|
|
|
#endif /* defined(__realm__object_store__) */
|
2015-05-28 23:53:35 +00:00
|
|
|
|