realm-js/src/js_results.hpp

238 lines
9.1 KiB
C++
Raw Normal View History

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"
#include "js_realm_object.hpp"
2016-04-12 14:42:05 -07:00
#include "results.hpp"
#include "list.hpp"
#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>
struct ResultsClass : ClassDefinition<T, realm::Results, 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;
using Object = js::Object<T>;
using Value = js::Value<T>;
using ReturnValue = js::ReturnValue<T>;
2016-04-12 14:42:05 -07:00
2016-04-18 12:15:00 -07:00
static ObjectType create_instance(ContextType, const realm::Results &, bool live = true);
static ObjectType create_instance(ContextType, const realm::List &, bool live = true);
static ObjectType create_instance(ContextType, SharedRealm, const std::string &type, bool live = true);
static ObjectType create_instance(ContextType, SharedRealm, const ObjectSchema &, Query, bool live = true);
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
2016-04-18 12:15:00 -07:00
static void snapshot(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void filtered(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void sorted(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
2016-04-12 14:42:05 -07:00
std::string const name = "Results";
MethodMap<T> const methods = {
{"snapshot", wrap<snapshot>},
{"filtered", wrap<filtered>},
{"sorted", wrap<sorted>},
2016-04-12 14:42:05 -07:00
};
PropertyMap<T> const properties = {
{"length", {wrap<get_length>, nullptr}},
2016-04-12 14:42:05 -07:00
};
IndexPropertyType<T> const index_accessor = {wrap<get_index>, nullptr};
2016-04-12 14:42:05 -07:00
};
template<typename T>
typename T::Object ResultsClass<T>::create_instance(ContextType ctx, const realm::Results &results, bool live) {
2016-04-12 14:42:05 -07:00
auto new_results = new realm::Results(results);
new_results->set_live(live);
return create_object<T, ResultsClass<T>>(ctx, new_results);
2016-04-12 14:42:05 -07:00
}
template<typename T>
typename T::Object ResultsClass<T>::create_instance(ContextType ctx, const realm::List &list, bool live) {
2016-04-14 11:19:01 -07:00
return create_instance(ctx, list.get_realm(), list.get_object_schema(), list.get_query(), live);
2016-04-12 14:42:05 -07:00
}
template<typename T>
typename T::Object ResultsClass<T>::create_instance(ContextType ctx, SharedRealm realm, const std::string &type, bool live) {
2016-04-12 14:42:05 -07:00
auto table = ObjectStore::table_for_object_type(realm->read_group(), type);
auto &schema = realm->config().schema;
auto object_schema = schema->find(type);
if (object_schema == schema->end()) {
throw std::runtime_error("Object type '" + type + "' not present in Realm.");
}
auto results = new realm::Results(realm, *object_schema, *table);
results->set_live(live);
return create_object<T, ResultsClass<T>>(ctx, results);
2016-04-12 14:42:05 -07:00
}
template<typename T>
typename T::Object ResultsClass<T>::create_instance(ContextType ctx, SharedRealm realm, const ObjectSchema &object_schema, Query query, bool live) {
2016-04-12 14:42:05 -07:00
auto results = new realm::Results(realm, object_schema, std::move(query));
results->set_live(live);
return create_object<T, ResultsClass<T>>(ctx, results);
2016-04-12 14:42:05 -07:00
}
template<typename T>
template<typename U>
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-04-18 12:15:00 -07:00
std::vector<ValueType> args;
2016-04-12 14:42:05 -07:00
args.reserve(argc - 1);
for (size_t i = 1; i < argc; i++) {
args.push_back(arguments[i]);
}
parser::Predicate predicate = parser::parse(query_string);
2016-04-18 12:15:00 -07:00
query_builder::ArgumentConverter<ValueType, ContextType> converter(ctx, args);
2016-04-12 14:42:05 -07:00
query_builder::apply_predicate(query, predicate, converter, *realm->config().schema, object_schema.name);
2016-04-14 11:19:01 -07:00
return create_instance(ctx, realm, object_schema, std::move(query));
2016-04-12 14:42:05 -07:00
}
template<typename T>
template<typename U>
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]));
}
std::vector<size_t> columns;
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 + "'");
}
columns.push_back(prop->table_column);
}
auto results = new realm::Results(realm, object_schema, collection.get_query(), {std::move(columns), std::move(ascending)});
return create_object<T, ResultsClass<T>>(ctx, results);
2016-04-12 14:42:05 -07:00
}
template<typename T>
void ResultsClass<T>::get_length(ContextType ctx, ObjectType object, ReturnValue &return_value) {
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>
void ResultsClass<T>::get_index(ContextType ctx, ObjectType object, uint32_t index, ReturnValue &return_value) {
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-13 17:21:11 -07:00
return_value.set(RealmObjectClass<T>::create_instance(ctx, realm_object));
2016-04-12 14:42:05 -07:00
}
template<typename T>
void ResultsClass<T>::snapshot(ContextType ctx, 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);
auto results = get_internal<T, ResultsClass<T>>(this_object);
return_value.set(ResultsClass<T>::create_instance(ctx, *results, false));
2016-04-12 14:42:05 -07:00
}
template<typename T>
void ResultsClass<T>::filtered(ContextType ctx, 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);
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>
void ResultsClass<T>::sorted(ContextType ctx, 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);
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));
}
} // js
} // realm