mirror of
https://github.com/status-im/realm-js.git
synced 2025-01-11 06:46:03 +00:00
Merge pull request #189 from realm/al-const
Schema stored in Realm::Config should be const
This commit is contained in:
commit
8f8d8d46b4
@ -215,7 +215,7 @@ template<> JSValueRef RJSAccessor::from_datetime(JSContextRef ctx, DateTime dt)
|
||||
return JSObjectMakeDate(ctx, 1, &time, NULL);
|
||||
}
|
||||
|
||||
extern JSObjectRef RJSDictForPropertyArray(JSContextRef ctx, ObjectSchema &object_schema, JSObjectRef array);
|
||||
extern JSObjectRef RJSDictForPropertyArray(JSContextRef ctx, const ObjectSchema &object_schema, JSObjectRef array);
|
||||
|
||||
template<> size_t RJSAccessor::to_existing_object_index(JSContextRef ctx, JSValueRef &val) {
|
||||
JSObjectRef object = RJSValidatedValueToObject(ctx, val);
|
||||
|
@ -245,7 +245,7 @@ JSValueRef RealmObjects(JSContextRef ctx, JSObjectRef function, JSObjectRef this
|
||||
}
|
||||
}
|
||||
|
||||
JSObjectRef RJSDictForPropertyArray(JSContextRef ctx, ObjectSchema &object_schema, JSObjectRef array) {
|
||||
JSObjectRef RJSDictForPropertyArray(JSContextRef ctx, const ObjectSchema &object_schema, JSObjectRef array) {
|
||||
// copy to dictionary
|
||||
if (object_schema.properties.size() != RJSValidatedListLength(ctx, array)) {
|
||||
throw std::runtime_error("Array must contain values for all object properties");
|
||||
|
@ -109,7 +109,7 @@ JSObjectRef RJSResultsCreate(JSContextRef ctx, SharedRealm realm, std::string cl
|
||||
JSObjectRef RJSResultsCreate(JSContextRef ctx, SharedRealm realm, std::string className, std::string queryString, std::vector<JSValueRef> args) {
|
||||
TableRef table = ObjectStore::table_for_object_type(realm->read_group(), className);
|
||||
Query query = table->where();
|
||||
Schema &schema = *realm->config().schema;
|
||||
const Schema &schema = *realm->config().schema;
|
||||
auto object_schema = schema.find(className);
|
||||
if (object_schema == schema.end()) {
|
||||
throw std::runtime_error("Object type '" + className + "' not present in Realm.");
|
||||
|
@ -27,7 +27,7 @@ namespace realm {
|
||||
public:
|
||||
List(SharedRealm &r, const ObjectSchema &s, LinkViewRef l) : m_realm(r), object_schema(s), m_link_view(l) {}
|
||||
|
||||
const ObjectSchema object_schema;
|
||||
const ObjectSchema &object_schema;
|
||||
SharedRealm realm() { return m_realm; }
|
||||
|
||||
size_t size();
|
||||
|
@ -25,9 +25,9 @@ namespace realm {
|
||||
|
||||
// create an Object from a native representation
|
||||
template<typename ValueType, typename ContextType>
|
||||
static inline Object create(ContextType ctx, SharedRealm realm, ObjectSchema &object_schema, ValueType value, bool try_update);
|
||||
static inline Object create(ContextType ctx, SharedRealm realm, const ObjectSchema &object_schema, ValueType value, bool try_update);
|
||||
|
||||
const ObjectSchema object_schema;
|
||||
const ObjectSchema &object_schema;
|
||||
SharedRealm realm() { return m_realm; }
|
||||
Row row() { return m_row; }
|
||||
|
||||
@ -243,7 +243,7 @@ namespace realm {
|
||||
}
|
||||
|
||||
template<typename ValueType, typename ContextType>
|
||||
inline Object Object::create(ContextType ctx, SharedRealm realm, ObjectSchema &object_schema, ValueType value, bool try_update)
|
||||
inline Object Object::create(ContextType ctx, SharedRealm realm, const ObjectSchema &object_schema, ValueType value, bool try_update)
|
||||
{
|
||||
using Accessor = NativeAccessor<ValueType, ContextType>;
|
||||
|
||||
@ -283,7 +283,7 @@ namespace realm {
|
||||
|
||||
// populate
|
||||
Object object(realm, object_schema, table->get(row_index));
|
||||
for (Property &prop : object_schema.properties) {
|
||||
for (const Property &prop : object_schema.properties) {
|
||||
if (created || !prop.is_primary) {
|
||||
if (Accessor::dict_has_value_for_key(ctx, value, prop.name)) {
|
||||
object.set_property_value_impl(ctx, prop, Accessor::dict_value_for_key(ctx, value, prop.name), try_update);
|
||||
|
@ -66,11 +66,11 @@ KeyPath key_path_from_string(const std::string &s) {
|
||||
|
||||
struct PropertyExpression
|
||||
{
|
||||
Property *prop = nullptr;
|
||||
const Property *prop = nullptr;
|
||||
std::vector<size_t> indexes;
|
||||
std::function<Table *()> table_getter;
|
||||
|
||||
PropertyExpression(Query &query, Schema &schema, Schema::iterator desc, const std::string &key_path_string)
|
||||
PropertyExpression(Query &query, const Schema &schema, Schema::const_iterator desc, const std::string &key_path_string)
|
||||
{
|
||||
KeyPath key_path = key_path_from_string(key_path_string);
|
||||
for (size_t index = 0; index < key_path.size(); index++) {
|
||||
@ -375,7 +375,7 @@ auto value_of_type_for_query(TableGetter&& tables, Value&& value, Arguments &arg
|
||||
}
|
||||
|
||||
template <typename A, typename B>
|
||||
void do_add_comparison_to_query(Query &query, Schema &schema, ObjectSchema &object_schema, Predicate::Operator op,
|
||||
void do_add_comparison_to_query(Query &query, const Schema &schema, const ObjectSchema &object_schema, Predicate::Operator op,
|
||||
PropertyExpression &expr, A &lhs, B &rhs, Arguments &args)
|
||||
{
|
||||
auto type = expr.prop->type;
|
||||
@ -418,7 +418,7 @@ void do_add_comparison_to_query(Query &query, Schema &schema, ObjectSchema &obje
|
||||
}
|
||||
}
|
||||
|
||||
void add_comparison_to_query(Query &query, Predicate &pred, Arguments &args, Schema &schema, const std::string &type)
|
||||
void add_comparison_to_query(Query &query, Predicate &pred, Arguments &args, const Schema &schema, const std::string &type)
|
||||
{
|
||||
Predicate::Comparison &cmpr = pred.cmpr;
|
||||
auto t0 = cmpr.expr[0].type, t1 = cmpr.expr[1].type;
|
||||
@ -436,7 +436,7 @@ void add_comparison_to_query(Query &query, Predicate &pred, Arguments &args, Sch
|
||||
}
|
||||
}
|
||||
|
||||
void update_query_with_predicate(Query &query, Predicate &pred, Arguments &arguments, Schema &schema, const std::string &type)
|
||||
void update_query_with_predicate(Query &query, Predicate &pred, Arguments &arguments, const Schema &schema, const std::string &type)
|
||||
{
|
||||
if (pred.negate) {
|
||||
query.Not();
|
||||
@ -484,7 +484,7 @@ void update_query_with_predicate(Query &query, Predicate &pred, Arguments &argum
|
||||
}
|
||||
}
|
||||
|
||||
void apply_predicate(Query &query, Predicate &predicate, Arguments &arguments, Schema &schema, std::string objectType)
|
||||
void apply_predicate(Query &query, Predicate &predicate, Arguments &arguments, const Schema &schema, std::string objectType)
|
||||
{
|
||||
update_query_with_predicate(query, predicate, arguments, schema, objectType);
|
||||
|
||||
|
@ -30,7 +30,7 @@ namespace realm {
|
||||
namespace query_builder {
|
||||
class Arguments;
|
||||
|
||||
void apply_predicate(Query &query, parser::Predicate &predicate, Arguments &arguments, Schema &schema, std::string objectType);
|
||||
void apply_predicate(Query &query, parser::Predicate &predicate, Arguments &arguments, const Schema &schema, std::string objectType);
|
||||
|
||||
class Arguments
|
||||
{
|
||||
|
@ -38,6 +38,17 @@ Results::Results(SharedRealm r, const ObjectSchema &o, Table& table)
|
||||
{
|
||||
}
|
||||
|
||||
Results& Results::operator=(Results const& r)
|
||||
{
|
||||
m_realm = r.m_realm;
|
||||
m_table = r.m_table;
|
||||
m_sort = r.m_sort;
|
||||
m_query = r.get_query();
|
||||
m_mode = Mode::Query;
|
||||
const_cast<ObjectSchema &>(object_schema) = r.object_schema;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Results::validate_read() const
|
||||
{
|
||||
if (m_realm)
|
||||
|
@ -38,14 +38,14 @@ public:
|
||||
// Results is copyable and moveable
|
||||
Results(Results const&) = default;
|
||||
Results(Results&&) = default;
|
||||
Results& operator=(Results const&) = default;
|
||||
Results& operator=(Results&&) = default;
|
||||
Results& operator=(Results const&);
|
||||
|
||||
// Get the Realm
|
||||
SharedRealm get_realm() const { return m_realm; }
|
||||
|
||||
// Object schema describing the vendored object type
|
||||
ObjectSchema object_schema;
|
||||
const ObjectSchema &object_schema;
|
||||
|
||||
// Get a query which will match the same rows as is contained in this Results
|
||||
// Returned query will not be valid if the current mode is Empty
|
||||
|
@ -165,7 +165,7 @@ SharedRealm Realm::get_shared_realm(Config config)
|
||||
throw UnitializedRealmException("Can't open an un-initialized Realm without a Schema");
|
||||
}
|
||||
target_schema->validate();
|
||||
ObjectStore::verify_schema(*realm->m_config.schema, *target_schema, true);
|
||||
ObjectStore::verify_schema(*realm->m_config.schema, const_cast<Schema &>(*target_schema), true);
|
||||
realm->m_config.schema = std::move(target_schema);
|
||||
}
|
||||
else {
|
||||
@ -180,13 +180,13 @@ SharedRealm Realm::get_shared_realm(Config config)
|
||||
return realm;
|
||||
}
|
||||
|
||||
bool Realm::update_schema(std::unique_ptr<Schema> schema, uint64_t version)
|
||||
bool Realm::update_schema(std::unique_ptr<const Schema> schema, uint64_t version)
|
||||
{
|
||||
schema->validate();
|
||||
|
||||
bool needs_update = !m_config.read_only && (m_config.schema_version != version || ObjectStore::needs_update(*m_config.schema, *schema));
|
||||
if (!needs_update) {
|
||||
ObjectStore::verify_schema(*m_config.schema, *schema, m_config.read_only);
|
||||
ObjectStore::verify_schema(*m_config.schema, const_cast<Schema &>(*schema), m_config.read_only);
|
||||
m_config.schema = std::move(schema);
|
||||
m_config.schema_version = version;
|
||||
return false;
|
||||
@ -199,9 +199,6 @@ bool Realm::update_schema(std::unique_ptr<Schema> schema, uint64_t version)
|
||||
old_config.read_only = true;
|
||||
old_config.schema = std::move(old_schema);
|
||||
|
||||
m_config.schema = std::move(schema);
|
||||
m_config.schema_version = version;
|
||||
|
||||
auto migration_function = [&](Group*, Schema&) {
|
||||
SharedRealm old_realm(new Realm(old_config));
|
||||
auto updated_realm = shared_from_this();
|
||||
@ -214,8 +211,9 @@ bool Realm::update_schema(std::unique_ptr<Schema> schema, uint64_t version)
|
||||
// update and migrate
|
||||
begin_transaction();
|
||||
bool changed = ObjectStore::update_realm_with_schema(read_group(), *old_config.schema,
|
||||
version, *m_config.schema,
|
||||
migration_function);
|
||||
version, const_cast<Schema &>(*schema), migration_function);
|
||||
m_config.schema = std::move(schema);
|
||||
m_config.schema_version = version;
|
||||
commit_transaction();
|
||||
return changed;
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ namespace realm {
|
||||
bool cache = true;
|
||||
std::vector<char> encryption_key;
|
||||
|
||||
std::unique_ptr<Schema> schema;
|
||||
std::unique_ptr<const Schema> schema;
|
||||
uint64_t schema_version = ObjectStore::NotVersioned;
|
||||
|
||||
MigrationFunction migration_function;
|
||||
@ -78,7 +78,7 @@ namespace realm {
|
||||
// on the Config, and the resulting Schema and version with updated
|
||||
// column mappings are set on the realms config upon success.
|
||||
// returns if any changes were made
|
||||
bool update_schema(std::unique_ptr<Schema> schema, uint64_t version);
|
||||
bool update_schema(std::unique_ptr<const Schema> schema, uint64_t version);
|
||||
|
||||
static uint64_t get_schema_version(Config const& config);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user