2016-02-18 11:59:34 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 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-08-13 09:12:48 -07:00
|
|
|
|
2015-12-01 14:52:38 -08:00
|
|
|
#pragma once
|
|
|
|
|
2016-04-12 14:42:05 -07:00
|
|
|
#include "js_collection.hpp"
|
2016-04-18 01:14:48 -07:00
|
|
|
#include "js_realm_object.hpp"
|
2016-03-30 14:11:57 -07:00
|
|
|
|
2016-04-12 14:42:05 -07:00
|
|
|
#include "results.hpp"
|
|
|
|
#include "list.hpp"
|
2016-08-15 11:27:59 -07:00
|
|
|
#include "object_store.hpp"
|
2016-04-12 14:42:05 -07:00
|
|
|
#include "parser.hpp"
|
|
|
|
#include "query_builder.hpp"
|
2015-08-13 09:12:48 -07:00
|
|
|
|
|
|
|
namespace realm {
|
2016-04-12 14:42:05 -07:00
|
|
|
namespace js {
|
|
|
|
|
|
|
|
template<typename T>
|
2016-10-04 15:02:51 -07:00
|
|
|
class Results : public realm::Results {
|
|
|
|
public:
|
|
|
|
Results(Results const& r) : realm::Results(r) {};
|
|
|
|
Results(realm::Results const& r) : realm::Results(r) {};
|
|
|
|
Results(Results&&) = default;
|
|
|
|
Results& operator=(Results&&) = default;
|
|
|
|
Results& operator=(Results const&) = default;
|
|
|
|
|
|
|
|
using realm::Results::Results;
|
|
|
|
|
|
|
|
std::vector<std::pair<Protected<typename T::Function>, NotificationToken>> m_notification_tokens;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct ResultsClass : ClassDefinition<T, realm::js::Results<T>, CollectionClass<T>> {
|
2016-04-18 12:15:00 -07:00
|
|
|
using ContextType = typename T::Context;
|
|
|
|
using ObjectType = typename T::Object;
|
|
|
|
using ValueType = typename T::Value;
|
2016-10-04 15:02:51 -07:00
|
|
|
using FunctionType = typename T::Function;
|
2016-04-18 18:30:55 -07:00
|
|
|
using Object = js::Object<T>;
|
|
|
|
using Value = js::Value<T>;
|
|
|
|
using ReturnValue = js::ReturnValue<T>;
|
2016-04-12 14:42:05 -07:00
|
|
|
|
2016-07-18 14:01:03 -07:00
|
|
|
static ObjectType create_instance(ContextType, realm::Results);
|
2017-04-18 13:41:44 +03:00
|
|
|
static ObjectType create_instance(ContextType, SharedRealm, const std::string &object_type);
|
2016-04-12 14:42:05 -07:00
|
|
|
|
|
|
|
template<typename U>
|
2016-04-18 12:15:00 -07:00
|
|
|
static ObjectType create_filtered(ContextType, const U &, size_t, const ValueType[]);
|
2016-04-12 14:42:05 -07:00
|
|
|
|
|
|
|
template<typename U>
|
2016-04-18 12:15:00 -07:00
|
|
|
static ObjectType create_sorted(ContextType, const U &, size_t, const ValueType[]);
|
2016-04-12 14:42:05 -07:00
|
|
|
|
2016-04-18 12:15:00 -07:00
|
|
|
static void get_length(ContextType, ObjectType, ReturnValue &);
|
|
|
|
static void get_index(ContextType, ObjectType, uint32_t, ReturnValue &);
|
2016-04-12 14:42:05 -07:00
|
|
|
|
2017-02-07 11:01:26 +01:00
|
|
|
static void snapshot(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
|
|
|
static void filtered(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
|
|
|
static void sorted(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
|
|
|
static void is_valid(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
2016-04-12 14:42:05 -07:00
|
|
|
|
2016-10-04 15:02:51 -07:00
|
|
|
// observable
|
2017-02-07 11:01:26 +01:00
|
|
|
static void add_listener(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
|
|
|
static void remove_listener(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
|
|
|
static void remove_all_listeners(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
2016-10-04 15:02:51 -07:00
|
|
|
|
2016-04-12 14:42:05 -07:00
|
|
|
std::string const name = "Results";
|
|
|
|
|
|
|
|
MethodMap<T> const methods = {
|
2016-05-13 17:12:06 -07:00
|
|
|
{"snapshot", wrap<snapshot>},
|
|
|
|
{"filtered", wrap<filtered>},
|
|
|
|
{"sorted", wrap<sorted>},
|
2016-05-16 16:01:14 -07:00
|
|
|
{"isValid", wrap<is_valid>},
|
2016-10-04 15:02:51 -07:00
|
|
|
{"addListener", wrap<add_listener>},
|
|
|
|
{"removeListener", wrap<remove_listener>},
|
|
|
|
{"removeAllListeners", wrap<remove_all_listeners>},
|
2016-04-12 14:42:05 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
PropertyMap<T> const properties = {
|
2016-05-13 17:12:06 -07:00
|
|
|
{"length", {wrap<get_length>, nullptr}},
|
2016-04-12 14:42:05 -07:00
|
|
|
};
|
|
|
|
|
2016-05-13 17:12:06 -07:00
|
|
|
IndexPropertyType<T> const index_accessor = {wrap<get_index>, nullptr};
|
2016-04-12 14:42:05 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2016-07-18 14:01:03 -07:00
|
|
|
typename T::Object ResultsClass<T>::create_instance(ContextType ctx, realm::Results results) {
|
2016-10-04 15:02:51 -07:00
|
|
|
return create_object<T, ResultsClass<T>>(ctx, new realm::js::Results<T>(std::move(results)));
|
2016-04-12 14:42:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2017-04-18 13:41:44 +03:00
|
|
|
typename T::Object ResultsClass<T>::create_instance(ContextType ctx, SharedRealm realm, const std::string &object_type) {
|
|
|
|
auto table = ObjectStore::table_for_object_type(realm->read_group(), object_type);
|
|
|
|
if (!table) {
|
|
|
|
throw std::runtime_error("Table does not exist. Object type: " + object_type);
|
|
|
|
}
|
2016-10-04 15:02:51 -07:00
|
|
|
return create_object<T, ResultsClass<T>>(ctx, new realm::js::Results<T>(realm, *table));
|
2016-04-12 14:42:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
template<typename U>
|
2016-05-13 17:12:06 -07:00
|
|
|
typename T::Object ResultsClass<T>::create_filtered(ContextType ctx, const U &collection, size_t argc, const ValueType arguments[]) {
|
2016-04-12 14:42:05 -07:00
|
|
|
auto query_string = Value::validated_to_string(ctx, arguments[0], "predicate");
|
|
|
|
auto query = collection.get_query();
|
|
|
|
auto const &realm = collection.get_realm();
|
|
|
|
auto const &object_schema = collection.get_object_schema();
|
|
|
|
|
2016-07-18 14:01:03 -07:00
|
|
|
std::vector<ValueType> args(&arguments[1], &arguments[argc]);
|
2016-04-12 14:42:05 -07:00
|
|
|
|
|
|
|
parser::Predicate predicate = parser::parse(query_string);
|
2016-06-09 13:59:49 -07:00
|
|
|
query_builder::ArgumentConverter<ValueType, ContextType> converter(ctx, realm, args);
|
2016-08-02 09:26:31 -07:00
|
|
|
query_builder::apply_predicate(query, predicate, converter, realm->schema(), object_schema.name);
|
2016-04-12 14:42:05 -07:00
|
|
|
|
2016-07-18 14:01:03 -07:00
|
|
|
return create_instance(ctx, realm::Results(realm, std::move(query)));
|
2016-04-12 14:42:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
template<typename U>
|
2016-05-13 17:12:06 -07:00
|
|
|
typename T::Object ResultsClass<T>::create_sorted(ContextType ctx, const U &collection, size_t argc, const ValueType arguments[]) {
|
2016-04-12 14:42:05 -07:00
|
|
|
auto const &realm = collection.get_realm();
|
|
|
|
auto const &object_schema = collection.get_object_schema();
|
|
|
|
std::vector<std::string> prop_names;
|
|
|
|
std::vector<bool> ascending;
|
|
|
|
size_t prop_count;
|
|
|
|
|
|
|
|
if (Value::is_array(ctx, arguments[0])) {
|
|
|
|
validate_argument_count(argc, 1, "Second argument is not allowed if passed an array of sort descriptors");
|
|
|
|
|
2016-04-18 12:15:00 -07:00
|
|
|
ObjectType js_prop_names = Value::validated_to_object(ctx, arguments[0]);
|
2016-04-12 14:42:05 -07:00
|
|
|
prop_count = Object::validated_get_length(ctx, js_prop_names);
|
|
|
|
if (!prop_count) {
|
|
|
|
throw std::invalid_argument("Sort descriptor array must not be empty");
|
|
|
|
}
|
|
|
|
|
|
|
|
prop_names.resize(prop_count);
|
|
|
|
ascending.resize(prop_count);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < prop_count; i++) {
|
2016-04-18 12:15:00 -07:00
|
|
|
ValueType value = Object::validated_get_property(ctx, js_prop_names, i);
|
2016-04-12 14:42:05 -07:00
|
|
|
|
|
|
|
if (Value::is_array(ctx, value)) {
|
2016-04-18 12:15:00 -07:00
|
|
|
ObjectType array = Value::to_array(ctx, value);
|
2016-04-12 14:42:05 -07:00
|
|
|
prop_names[i] = Object::validated_get_string(ctx, array, 0);
|
|
|
|
ascending[i] = !Object::validated_get_boolean(ctx, array, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
prop_names[i] = Value::validated_to_string(ctx, value);
|
|
|
|
ascending[i] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
validate_argument_count(argc, 1, 2);
|
|
|
|
|
|
|
|
prop_count = 1;
|
|
|
|
prop_names.push_back(Value::validated_to_string(ctx, arguments[0]));
|
|
|
|
ascending.push_back(argc == 1 ? true : !Value::to_boolean(ctx, arguments[1]));
|
|
|
|
}
|
|
|
|
|
2016-08-15 11:27:59 -07:00
|
|
|
std::vector<std::vector<size_t>> columns;
|
2016-04-12 14:42:05 -07:00
|
|
|
columns.reserve(prop_count);
|
|
|
|
|
|
|
|
for (std::string &prop_name : prop_names) {
|
|
|
|
const Property *prop = object_schema.property_for_name(prop_name);
|
|
|
|
if (!prop) {
|
|
|
|
throw std::runtime_error("Property '" + prop_name + "' does not exist on object type '" + object_schema.name + "'");
|
|
|
|
}
|
2016-08-15 11:27:59 -07:00
|
|
|
columns.push_back({prop->table_column});
|
2016-04-12 14:42:05 -07:00
|
|
|
}
|
|
|
|
|
2016-08-15 11:27:59 -07:00
|
|
|
auto table = realm::ObjectStore::table_for_object_type(realm->read_group(), object_schema.name);
|
2016-10-04 15:02:51 -07:00
|
|
|
auto results = new realm::js::Results<T>(realm, collection.get_query(),
|
|
|
|
{*table, std::move(columns), std::move(ascending)});
|
2016-04-15 13:47:01 -07:00
|
|
|
return create_object<T, ResultsClass<T>>(ctx, results);
|
2016-04-12 14:42:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-05-13 17:12:06 -07:00
|
|
|
void ResultsClass<T>::get_length(ContextType ctx, ObjectType object, ReturnValue &return_value) {
|
2016-04-15 13:47:01 -07:00
|
|
|
auto results = get_internal<T, ResultsClass<T>>(object);
|
2016-04-12 14:42:05 -07:00
|
|
|
return_value.set((uint32_t)results->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-05-13 17:12:06 -07:00
|
|
|
void ResultsClass<T>::get_index(ContextType ctx, ObjectType object, uint32_t index, ReturnValue &return_value) {
|
2016-04-15 13:47:01 -07:00
|
|
|
auto results = get_internal<T, ResultsClass<T>>(object);
|
2016-04-12 14:42:05 -07:00
|
|
|
auto row = results->get(index);
|
|
|
|
|
|
|
|
// Return null for deleted objects in a snapshot.
|
|
|
|
if (!row.is_attached()) {
|
|
|
|
return_value.set_null();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto realm_object = realm::Object(results->get_realm(), results->get_object_schema(), results->get(index));
|
2016-05-31 15:54:43 -07:00
|
|
|
return_value.set(RealmObjectClass<T>::create_instance(ctx, std::move(realm_object)));
|
2016-04-12 14:42:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2017-02-07 11:01:26 +01:00
|
|
|
void ResultsClass<T>::snapshot(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
2016-04-12 14:42:05 -07:00
|
|
|
validate_argument_count(argc, 0);
|
|
|
|
|
2016-04-15 13:47:01 -07:00
|
|
|
auto results = get_internal<T, ResultsClass<T>>(this_object);
|
2016-07-18 14:01:03 -07:00
|
|
|
return_value.set(ResultsClass<T>::create_instance(ctx, results->snapshot()));
|
2016-04-12 14:42:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2017-02-07 11:01:26 +01:00
|
|
|
void ResultsClass<T>::filtered(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
2016-04-12 14:42:05 -07:00
|
|
|
validate_argument_count_at_least(argc, 1);
|
|
|
|
|
2016-04-15 13:47:01 -07:00
|
|
|
auto results = get_internal<T, ResultsClass<T>>(this_object);
|
2016-04-12 14:42:05 -07:00
|
|
|
return_value.set(create_filtered(ctx, *results, argc, arguments));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2017-02-07 11:01:26 +01:00
|
|
|
void ResultsClass<T>::sorted(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
2016-04-12 14:42:05 -07:00
|
|
|
validate_argument_count(argc, 1, 2);
|
|
|
|
|
2016-04-15 13:47:01 -07:00
|
|
|
auto results = get_internal<T, ResultsClass<T>>(this_object);
|
2016-04-12 14:42:05 -07:00
|
|
|
return_value.set(create_sorted(ctx, *results, argc, arguments));
|
|
|
|
}
|
|
|
|
|
2016-05-16 16:01:14 -07:00
|
|
|
template<typename T>
|
2017-02-07 11:01:26 +01:00
|
|
|
void ResultsClass<T>::is_valid(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
2016-05-16 16:01:14 -07:00
|
|
|
return_value.set(get_internal<T, ResultsClass<T>>(this_object)->is_valid());
|
|
|
|
}
|
2016-10-04 15:02:51 -07:00
|
|
|
|
|
|
|
template<typename T>
|
2017-02-07 11:01:26 +01:00
|
|
|
void ResultsClass<T>::add_listener(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
2016-10-04 15:02:51 -07:00
|
|
|
validate_argument_count(argc, 1);
|
|
|
|
|
|
|
|
auto results = get_internal<T, ResultsClass<T>>(this_object);
|
|
|
|
auto callback = Value::validated_to_function(ctx, arguments[0]);
|
|
|
|
Protected<FunctionType> protected_callback(ctx, callback);
|
|
|
|
Protected<ObjectType> protected_this(ctx, this_object);
|
|
|
|
Protected<typename T::GlobalContext> protected_ctx(Context<T>::get_global_context(ctx));
|
|
|
|
|
|
|
|
auto token = results->add_notification_callback([=](CollectionChangeSet change_set, std::exception_ptr exception) {
|
2016-11-11 11:41:27 -08:00
|
|
|
HANDLESCOPE
|
2016-10-04 15:02:51 -07:00
|
|
|
|
|
|
|
ValueType arguments[2];
|
|
|
|
arguments[0] = static_cast<ObjectType>(protected_this);
|
|
|
|
arguments[1] = CollectionClass<T>::create_collection_change_set(protected_ctx, change_set);
|
|
|
|
Function<T>::call(protected_ctx, protected_callback, protected_this, 2, arguments);
|
|
|
|
});
|
|
|
|
results->m_notification_tokens.emplace_back(protected_callback, std::move(token));
|
|
|
|
}
|
2016-05-16 16:01:14 -07:00
|
|
|
|
2016-10-04 15:02:51 -07:00
|
|
|
template<typename T>
|
2017-02-07 11:01:26 +01:00
|
|
|
void ResultsClass<T>::remove_listener(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
2016-10-04 15:02:51 -07:00
|
|
|
validate_argument_count(argc, 1);
|
|
|
|
|
|
|
|
auto results = get_internal<T, ResultsClass<T>>(this_object);
|
|
|
|
auto callback = Value::validated_to_function(ctx, arguments[0]);
|
|
|
|
auto protected_function = Protected<FunctionType>(ctx, callback);
|
|
|
|
|
|
|
|
auto iter = results->m_notification_tokens.begin();
|
|
|
|
typename Protected<FunctionType>::Comparator compare;
|
|
|
|
while (iter != results->m_notification_tokens.end()) {
|
|
|
|
if(compare(iter->first, protected_function)) {
|
|
|
|
iter = results->m_notification_tokens.erase(iter);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2017-02-07 11:01:26 +01:00
|
|
|
void ResultsClass<T>::remove_all_listeners(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
2016-10-04 15:02:51 -07:00
|
|
|
validate_argument_count(argc, 0);
|
|
|
|
|
|
|
|
auto results = get_internal<T, ResultsClass<T>>(this_object);
|
|
|
|
results->m_notification_tokens.clear();
|
|
|
|
}
|
|
|
|
|
2016-04-12 14:42:05 -07:00
|
|
|
} // js
|
|
|
|
} // realm
|