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_object.hpp"
|
2016-03-30 14:11:57 -07:00
|
|
|
|
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>
|
2016-04-17 22:49:07 -07:00
|
|
|
class Results {
|
2016-04-14 10:54:43 -07:00
|
|
|
using TContext = typename T::Context;
|
|
|
|
using TObject = typename T::Object;
|
|
|
|
using TValue = typename T::Value;
|
2016-04-12 14:42:05 -07:00
|
|
|
using Object = Object<T>;
|
|
|
|
using Value = Value<T>;
|
|
|
|
using ReturnValue = ReturnValue<T>;
|
|
|
|
|
2016-04-17 22:49:07 -07:00
|
|
|
public:
|
2016-04-14 11:19:01 -07:00
|
|
|
static TObject create_instance(TContext, const realm::Results &, bool live = true);
|
|
|
|
static TObject create_instance(TContext, const realm::List &, bool live = true);
|
|
|
|
static TObject create_instance(TContext, SharedRealm, const std::string &type, bool live = true);
|
|
|
|
static TObject create_instance(TContext, SharedRealm, const ObjectSchema &, Query, bool live = true);
|
2016-04-12 14:42:05 -07:00
|
|
|
|
|
|
|
template<typename U>
|
2016-04-14 10:54:43 -07:00
|
|
|
static TObject create_filtered(TContext, const U &, size_t, const TValue[]);
|
2016-04-12 14:42:05 -07:00
|
|
|
|
|
|
|
template<typename U>
|
2016-04-14 10:54:43 -07:00
|
|
|
static TObject create_sorted(TContext, const U &, size_t, const TValue[]);
|
2016-04-12 14:42:05 -07:00
|
|
|
|
2016-04-17 22:49:07 -07:00
|
|
|
static void get_length(TContext, TObject, ReturnValue &);
|
|
|
|
static void get_index(TContext, TObject, uint32_t, ReturnValue &);
|
2016-04-12 14:42:05 -07:00
|
|
|
|
2016-04-17 22:49:07 -07:00
|
|
|
static void snapshot(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
|
|
|
static void filtered(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
|
|
|
static void sorted(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
2016-04-12 14:42:05 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2016-04-16 16:46:16 -07:00
|
|
|
struct ResultsClass : ClassDefinition<T, realm::Results, CollectionClass<T>> {
|
2016-04-12 14:42:05 -07:00
|
|
|
using Results = Results<T>;
|
|
|
|
|
|
|
|
std::string const name = "Results";
|
|
|
|
|
|
|
|
MethodMap<T> const methods = {
|
2016-04-17 22:49:07 -07:00
|
|
|
{"snapshot", wrap<Results::snapshot>},
|
|
|
|
{"filtered", wrap<Results::filtered>},
|
|
|
|
{"sorted", wrap<Results::sorted>},
|
2016-04-12 14:42:05 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
PropertyMap<T> const properties = {
|
2016-04-17 22:49:07 -07:00
|
|
|
{"length", {wrap<Results::get_length>, nullptr}},
|
2016-04-12 14:42:05 -07:00
|
|
|
};
|
|
|
|
|
2016-04-17 22:49:07 -07:00
|
|
|
IndexPropertyType<T> const index_accessor = {wrap<Results::get_index>, nullptr};
|
2016-04-12 14:42:05 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2016-04-14 11:19:01 -07:00
|
|
|
typename T::Object Results<T>::create_instance(TContext 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);
|
|
|
|
|
2016-04-15 13:47:01 -07:00
|
|
|
return create_object<T, ResultsClass<T>>(ctx, new_results);
|
2016-04-12 14:42:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-04-14 11:19:01 -07:00
|
|
|
typename T::Object Results<T>::create_instance(TContext ctx, const realm::List &list, bool live) {
|
|
|
|
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>
|
2016-04-14 11:19:01 -07:00
|
|
|
typename T::Object Results<T>::create_instance(TContext 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);
|
|
|
|
|
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-04-14 11:19:01 -07:00
|
|
|
typename T::Object Results<T>::create_instance(TContext 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);
|
|
|
|
|
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>
|
|
|
|
template<typename U>
|
2016-04-14 10:54:43 -07:00
|
|
|
typename T::Object Results<T>::create_filtered(TContext ctx, const U &collection, size_t argc, const TValue 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-14 10:54:43 -07:00
|
|
|
std::vector<TValue> 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-14 10:54:43 -07:00
|
|
|
query_builder::ArgumentConverter<TValue, TContext> 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>
|
2016-04-14 10:54:43 -07:00
|
|
|
typename T::Object Results<T>::create_sorted(TContext ctx, const U &collection, size_t argc, const TValue 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-14 10:54:43 -07:00
|
|
|
TObject 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-14 10:54:43 -07:00
|
|
|
TValue 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-14 10:54:43 -07:00
|
|
|
TObject 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)});
|
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-04-17 22:49:07 -07:00
|
|
|
void Results<T>::get_length(TContext ctx, TObject 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-04-17 22:49:07 -07:00
|
|
|
void Results<T>::get_index(TContext ctx, TObject 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-04-14 11:19:01 -07:00
|
|
|
return_value.set(RealmObject<T>::create_instance(ctx, realm_object));
|
2016-04-12 14:42:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-04-17 22:49:07 -07:00
|
|
|
void Results<T>::snapshot(TContext ctx, TObject this_object, size_t argc, const TValue 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-04-14 11:19:01 -07:00
|
|
|
return_value.set(Results<T>::create_instance(ctx, *results, false));
|
2016-04-12 14:42:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-04-17 22:49:07 -07:00
|
|
|
void Results<T>::filtered(TContext ctx, TObject this_object, size_t argc, const TValue 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>
|
2016-04-17 22:49:07 -07:00
|
|
|
void Results<T>::sorted(TContext ctx, TObject this_object, size_t argc, const TValue 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // js
|
|
|
|
} // realm
|