realm-js/object_store.hpp

239 lines
10 KiB
C++
Raw Normal View History

////////////////////////////////////////////////////////////////////////////
//
2015-06-11 17:33:46 +00:00
// Copyright 2015 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-06-10 21:21:43 +00:00
#ifndef REALM_OBJECT_STORE_HPP
#define REALM_OBJECT_STORE_HPP
#include "object_schema.hpp"
#include "property.hpp"
2015-09-04 16:53:51 +00:00
#include <functional>
#include <realm/group.hpp>
#include <realm/link_view.hpp>
namespace realm {
2015-07-27 19:42:55 +00:00
class ObjectSchemaValidationException;
2015-11-23 19:26:50 +00:00
class Schema;
2015-06-10 21:21:43 +00:00
class ObjectStore {
2015-07-28 18:45:19 +00:00
public:
// Schema version used for uninitialized Realms
static const uint64_t NotVersioned;
// get the last set schema version
2015-09-04 16:40:29 +00:00
static uint64_t get_schema_version(const Group *group);
2015-06-11 17:33:46 +00:00
// checks if the schema in the group is at the given version
2015-09-04 16:40:29 +00:00
static bool is_schema_at_version(const Group *group, uint64_t version);
// verify that schema from a group and a target schema are compatible
// updates the column mapping on all ObjectSchema properties of the target schema
// throws if the schema is invalid or does not match
2015-09-04 16:40:29 +00:00
static void verify_schema(Schema const& actual_schema, Schema& target_schema, bool allow_missing_tables = false);
// determines if a realm with the given old schema needs non-migration
// changes to make it compatible with the given target schema
2015-09-04 16:40:29 +00:00
static bool needs_update(Schema const& old_schema, Schema const& schema);
2015-07-27 19:42:55 +00:00
// updates a Realm from old_schema to the given target schema, creating and updating tables as needed
// returns if any changes were made
// passed in target schema is updated with the correct column mapping
// optionally runs migration function if schema is out of date
// NOTE: must be performed within a write transaction
typedef std::function<void(Group *, Schema &)> MigrationFunction;
static bool update_realm_with_schema(Group *group, Schema const& old_schema, 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);
2015-09-04 16:40:29 +00:00
static realm::ConstTableRef table_for_object_type(const Group *group, StringData object_type);
// get existing Schema from a group
2015-09-04 16:40:29 +00:00
static Schema schema_from_group(const Group *group);
// deletes the table for the given type
2015-11-23 19:26:50 +00:00
static void delete_data_for_object(Group *group, StringData object_type);
2015-09-13 13:11:54 +00:00
// indicates if this group contains any objects
static bool is_empty(const Group *group);
2015-11-23 19:26:50 +00:00
static std::string table_name_for_object_type(StringData class_name);
static StringData object_type_for_table_name(StringData table_name);
2015-09-13 13:11:54 +00:00
private:
2015-06-04 01:34:50 +00:00
// set a new schema version
static void set_schema_version(Group *group, uint64_t version);
// check if the realm already has all metadata tables
2015-09-04 16:40:29 +00:00
static bool has_metadata_tables(const 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 only adds and initializes new tables
2015-07-27 19:42:55 +00:00
static bool create_tables(realm::Group *group, Schema &target_schema, bool update_existing);
// verify a target schema against an expected schema, setting the table_column property on each schema object
2015-07-28 18:25:04 +00:00
// updates the column mapping on the target_schema
// returns array of validation errors
2015-09-04 16:40:29 +00:00
static std::vector<ObjectSchemaValidationException> verify_object_schema(ObjectSchema const& expected,
ObjectSchema &target_schema);
2015-07-28 18:25:04 +00:00
// get primary key property name for object type
2015-09-04 16:40:29 +00:00
static StringData get_primary_key_for_object(const 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);
2015-11-23 19:26:50 +00:00
static TableRef table_for_object_type_create_if_needed(Group *group, StringData object_type, bool &created);
// returns if any indexes were changed
static bool update_indexes(Group *group, Schema &schema);
2015-06-05 01:14:53 +00:00
// validates that all primary key properties have unique values
2015-09-04 16:40:29 +00:00
static void validate_primary_column_uniqueness(const Group *group, Schema const& schema);
2015-06-05 01:14:53 +00:00
friend ObjectSchema;
};
2015-07-27 19:42:55 +00:00
// Base exception
class ObjectStoreException : public std::exception {
2015-07-28 18:45:19 +00:00
public:
2015-07-27 19:42:55 +00:00
ObjectStoreException() = default;
ObjectStoreException(const std::string &what) : m_what(what) {}
const char* what() const noexcept override { return m_what.c_str(); }
2015-07-28 18:45:19 +00:00
protected:
2015-07-27 19:42:55 +00:00
std::string m_what;
};
// Migration exceptions
class MigrationException : public ObjectStoreException {};
class InvalidSchemaVersionException : public MigrationException {
2015-07-28 18:45:19 +00:00
public:
2015-07-27 19:42:55 +00:00
InvalidSchemaVersionException(uint64_t old_version, uint64_t new_version);
2015-09-04 16:40:29 +00:00
uint64_t old_version() const { return m_old_version; }
uint64_t new_version() const { return m_new_version; }
2015-07-28 18:45:19 +00:00
private:
2015-07-27 19:42:55 +00:00
uint64_t m_old_version, m_new_version;
};
class DuplicatePrimaryKeyValueException : public MigrationException {
2015-07-28 18:45:19 +00:00
public:
2015-09-04 16:53:51 +00:00
DuplicatePrimaryKeyValueException(std::string const& object_type, Property const& property);
2015-10-27 16:12:39 +00:00
DuplicatePrimaryKeyValueException(std::string const& object_type, Property const& property, const std::string message);
2015-09-04 16:40:29 +00:00
std::string object_type() const { return m_object_type; }
Property const& property() const { return m_property; }
2015-07-28 18:45:19 +00:00
private:
2015-07-27 19:42:55 +00:00
std::string m_object_type;
Property m_property;
};
// Schema validation exceptions
2015-07-28 18:25:04 +00:00
class SchemaValidationException : public ObjectStoreException {
2015-07-28 18:45:19 +00:00
public:
2015-09-04 16:53:51 +00:00
SchemaValidationException(std::vector<ObjectSchemaValidationException> const& errors);
std::vector<ObjectSchemaValidationException> const& validation_errors() const { return m_validation_errors; }
2015-07-28 18:45:19 +00:00
private:
2015-07-27 19:42:55 +00:00
std::vector<ObjectSchemaValidationException> m_validation_errors;
2015-07-28 18:25:04 +00:00
};
class ObjectSchemaValidationException : public ObjectStoreException {
2015-07-28 18:45:19 +00:00
public:
2015-09-04 16:53:51 +00:00
ObjectSchemaValidationException(std::string const& object_type) : m_object_type(object_type) {}
ObjectSchemaValidationException(std::string const& object_type, std::string const& message) :
2015-07-28 18:25:04 +00:00
m_object_type(object_type) { m_what = message; }
2015-09-04 16:40:29 +00:00
std::string object_type() const { return m_object_type; }
2015-07-28 18:45:19 +00:00
protected:
2015-07-27 19:42:55 +00:00
std::string m_object_type;
};
2015-07-28 18:45:19 +00:00
class ObjectSchemaPropertyException : public ObjectSchemaValidationException {
public:
2015-09-04 16:53:51 +00:00
ObjectSchemaPropertyException(std::string const& object_type, Property const& property) :
2015-07-28 18:45:19 +00:00
ObjectSchemaValidationException(object_type), m_property(property) {}
2015-09-04 16:40:29 +00:00
Property const& property() const { return m_property; }
2015-07-28 18:45:19 +00:00
private:
2015-07-27 19:42:55 +00:00
Property m_property;
};
2015-07-28 18:45:19 +00:00
class PropertyTypeNotIndexableException : public ObjectSchemaPropertyException {
public:
2015-09-04 16:53:51 +00:00
PropertyTypeNotIndexableException(std::string const& object_type, Property const& property);
2015-07-28 18:45:19 +00:00
};
class ExtraPropertyException : public ObjectSchemaPropertyException {
public:
2015-09-04 16:53:51 +00:00
ExtraPropertyException(std::string const& object_type, Property const& property);
2015-07-27 19:42:55 +00:00
};
2015-07-28 18:45:19 +00:00
class MissingPropertyException : public ObjectSchemaPropertyException {
public:
2015-09-04 16:53:51 +00:00
MissingPropertyException(std::string const& object_type, Property const& property);
2015-07-28 18:45:19 +00:00
};
class InvalidNullabilityException : public ObjectSchemaPropertyException {
2015-07-28 18:45:19 +00:00
public:
2015-09-04 16:53:51 +00:00
InvalidNullabilityException(std::string const& object_type, Property const& property);
};
class MissingObjectTypeException : public ObjectSchemaPropertyException {
public:
2015-09-04 16:53:51 +00:00
MissingObjectTypeException(std::string const& object_type, Property const& property);
};
class DuplicatePrimaryKeysException : public ObjectSchemaValidationException {
public:
2015-09-04 16:53:51 +00:00
DuplicatePrimaryKeysException(std::string const& object_type);
2015-07-27 19:42:55 +00:00
};
class MismatchedPropertiesException : public ObjectSchemaValidationException {
2015-07-28 18:45:19 +00:00
public:
2015-09-04 16:53:51 +00:00
MismatchedPropertiesException(std::string const& object_type, Property const& old_property, Property const& new_property);
2015-09-04 16:40:29 +00:00
Property const& old_property() const { return m_old_property; }
Property const& new_property() const { return m_new_property; }
2015-07-28 18:45:19 +00:00
private:
2015-07-27 19:42:55 +00:00
Property m_old_property, m_new_property;
};
class ChangedPrimaryKeyException : public ObjectSchemaValidationException {
2015-07-28 18:45:19 +00:00
public:
2015-09-04 16:53:51 +00:00
ChangedPrimaryKeyException(std::string const& object_type, std::string const& old_primary, std::string const& new_primary);
2015-09-04 16:40:29 +00:00
std::string old_primary() const { return m_old_primary; }
std::string new_primary() const { return m_new_primary; }
2015-07-28 18:45:19 +00:00
private:
2015-07-27 19:42:55 +00:00
std::string m_old_primary, m_new_primary;
};
class InvalidPrimaryKeyException : public ObjectSchemaValidationException {
2015-07-28 18:45:19 +00:00
public:
2015-09-04 16:53:51 +00:00
InvalidPrimaryKeyException(std::string const& object_type, std::string const& primary_key);
2015-09-04 16:40:29 +00:00
std::string primary_key() const { return m_primary_key; }
2015-07-28 18:45:19 +00:00
private:
std::string m_primary_key;
2015-07-28 18:25:04 +00:00
};
}
2015-06-10 21:21:43 +00:00
#endif /* defined(REALM_OBJECT_STORE_HPP) */