mirror of
https://github.com/status-im/realm-js.git
synced 2025-01-11 14:54:33 +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);
|
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) {
|
template<> size_t RJSAccessor::to_existing_object_index(JSContextRef ctx, JSValueRef &val) {
|
||||||
JSObjectRef object = RJSValidatedValueToObject(ctx, 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
|
// copy to dictionary
|
||||||
if (object_schema.properties.size() != RJSValidatedListLength(ctx, array)) {
|
if (object_schema.properties.size() != RJSValidatedListLength(ctx, array)) {
|
||||||
throw std::runtime_error("Array must contain values for all object properties");
|
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) {
|
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);
|
TableRef table = ObjectStore::table_for_object_type(realm->read_group(), className);
|
||||||
Query query = table->where();
|
Query query = table->where();
|
||||||
Schema &schema = *realm->config().schema;
|
const Schema &schema = *realm->config().schema;
|
||||||
auto object_schema = schema.find(className);
|
auto object_schema = schema.find(className);
|
||||||
if (object_schema == schema.end()) {
|
if (object_schema == schema.end()) {
|
||||||
throw std::runtime_error("Object type '" + className + "' not present in Realm.");
|
throw std::runtime_error("Object type '" + className + "' not present in Realm.");
|
||||||
|
@ -27,7 +27,7 @@ namespace realm {
|
|||||||
public:
|
public:
|
||||||
List(SharedRealm &r, const ObjectSchema &s, LinkViewRef l) : m_realm(r), object_schema(s), m_link_view(l) {}
|
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; }
|
SharedRealm realm() { return m_realm; }
|
||||||
|
|
||||||
size_t size();
|
size_t size();
|
||||||
|
@ -25,9 +25,9 @@ namespace realm {
|
|||||||
|
|
||||||
// create an Object from a native representation
|
// create an Object from a native representation
|
||||||
template<typename ValueType, typename ContextType>
|
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; }
|
SharedRealm realm() { return m_realm; }
|
||||||
Row row() { return m_row; }
|
Row row() { return m_row; }
|
||||||
|
|
||||||
@ -243,7 +243,7 @@ namespace realm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename ValueType, typename ContextType>
|
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>;
|
using Accessor = NativeAccessor<ValueType, ContextType>;
|
||||||
|
|
||||||
@ -283,7 +283,7 @@ namespace realm {
|
|||||||
|
|
||||||
// populate
|
// populate
|
||||||
Object object(realm, object_schema, table->get(row_index));
|
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 (created || !prop.is_primary) {
|
||||||
if (Accessor::dict_has_value_for_key(ctx, value, prop.name)) {
|
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);
|
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
|
struct PropertyExpression
|
||||||
{
|
{
|
||||||
Property *prop = nullptr;
|
const Property *prop = nullptr;
|
||||||
std::vector<size_t> indexes;
|
std::vector<size_t> indexes;
|
||||||
std::function<Table *()> table_getter;
|
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);
|
KeyPath key_path = key_path_from_string(key_path_string);
|
||||||
for (size_t index = 0; index < key_path.size(); index++) {
|
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>
|
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)
|
PropertyExpression &expr, A &lhs, B &rhs, Arguments &args)
|
||||||
{
|
{
|
||||||
auto type = expr.prop->type;
|
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;
|
Predicate::Comparison &cmpr = pred.cmpr;
|
||||||
auto t0 = cmpr.expr[0].type, t1 = cmpr.expr[1].type;
|
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) {
|
if (pred.negate) {
|
||||||
query.Not();
|
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);
|
update_query_with_predicate(query, predicate, arguments, schema, objectType);
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ namespace realm {
|
|||||||
namespace query_builder {
|
namespace query_builder {
|
||||||
class Arguments;
|
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
|
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
|
void Results::validate_read() const
|
||||||
{
|
{
|
||||||
if (m_realm)
|
if (m_realm)
|
||||||
|
@ -38,14 +38,14 @@ public:
|
|||||||
// Results is copyable and moveable
|
// Results is copyable and moveable
|
||||||
Results(Results const&) = default;
|
Results(Results const&) = default;
|
||||||
Results(Results&&) = default;
|
Results(Results&&) = default;
|
||||||
Results& operator=(Results const&) = default;
|
|
||||||
Results& operator=(Results&&) = default;
|
Results& operator=(Results&&) = default;
|
||||||
|
Results& operator=(Results const&);
|
||||||
|
|
||||||
// Get the Realm
|
// Get the Realm
|
||||||
SharedRealm get_realm() const { return m_realm; }
|
SharedRealm get_realm() const { return m_realm; }
|
||||||
|
|
||||||
// Object schema describing the vendored object type
|
// 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
|
// 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
|
// 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");
|
throw UnitializedRealmException("Can't open an un-initialized Realm without a Schema");
|
||||||
}
|
}
|
||||||
target_schema->validate();
|
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);
|
realm->m_config.schema = std::move(target_schema);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -180,13 +180,13 @@ SharedRealm Realm::get_shared_realm(Config config)
|
|||||||
return realm;
|
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();
|
schema->validate();
|
||||||
|
|
||||||
bool needs_update = !m_config.read_only && (m_config.schema_version != version || ObjectStore::needs_update(*m_config.schema, *schema));
|
bool needs_update = !m_config.read_only && (m_config.schema_version != version || ObjectStore::needs_update(*m_config.schema, *schema));
|
||||||
if (!needs_update) {
|
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 = std::move(schema);
|
||||||
m_config.schema_version = version;
|
m_config.schema_version = version;
|
||||||
return false;
|
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.read_only = true;
|
||||||
old_config.schema = std::move(old_schema);
|
old_config.schema = std::move(old_schema);
|
||||||
|
|
||||||
m_config.schema = std::move(schema);
|
|
||||||
m_config.schema_version = version;
|
|
||||||
|
|
||||||
auto migration_function = [&](Group*, Schema&) {
|
auto migration_function = [&](Group*, Schema&) {
|
||||||
SharedRealm old_realm(new Realm(old_config));
|
SharedRealm old_realm(new Realm(old_config));
|
||||||
auto updated_realm = shared_from_this();
|
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
|
// update and migrate
|
||||||
begin_transaction();
|
begin_transaction();
|
||||||
bool changed = ObjectStore::update_realm_with_schema(read_group(), *old_config.schema,
|
bool changed = ObjectStore::update_realm_with_schema(read_group(), *old_config.schema,
|
||||||
version, *m_config.schema,
|
version, const_cast<Schema &>(*schema), migration_function);
|
||||||
migration_function);
|
m_config.schema = std::move(schema);
|
||||||
|
m_config.schema_version = version;
|
||||||
commit_transaction();
|
commit_transaction();
|
||||||
return changed;
|
return changed;
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ namespace realm {
|
|||||||
bool cache = true;
|
bool cache = true;
|
||||||
std::vector<char> encryption_key;
|
std::vector<char> encryption_key;
|
||||||
|
|
||||||
std::unique_ptr<Schema> schema;
|
std::unique_ptr<const Schema> schema;
|
||||||
uint64_t schema_version = ObjectStore::NotVersioned;
|
uint64_t schema_version = ObjectStore::NotVersioned;
|
||||||
|
|
||||||
MigrationFunction migration_function;
|
MigrationFunction migration_function;
|
||||||
@ -78,7 +78,7 @@ namespace realm {
|
|||||||
// on the Config, and the resulting Schema and version with updated
|
// on the Config, and the resulting Schema and version with updated
|
||||||
// column mappings are set on the realms config upon success.
|
// column mappings are set on the realms config upon success.
|
||||||
// returns if any changes were made
|
// 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);
|
static uint64_t get_schema_version(Config const& config);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user