Make Results derived from List use its LinkViewRef

There are some advantages for the `Results` being built from a `LinkViewRef`, and the object store very much encourages that with its `filter` and `sort` methods on List.
This commit is contained in:
Scott Kyle 2016-06-30 03:13:11 -07:00
parent c026c198ea
commit 14ba37ad39
5 changed files with 29 additions and 24 deletions

View File

@ -38,10 +38,9 @@ struct ResultsClass : ClassDefinition<T, realm::Results, CollectionClass<T>> {
using Value = js::Value<T>; using Value = js::Value<T>;
using ReturnValue = js::ReturnValue<T>; using ReturnValue = js::ReturnValue<T>;
static ObjectType create_instance(ContextType, const realm::Results &, bool live = true); static ObjectType create_instance(ContextType, realm::Results, bool live = true);
static ObjectType create_instance(ContextType, const realm::List &, bool live = true); static ObjectType create_instance(ContextType, const realm::List &, bool live = true);
static ObjectType create_instance(ContextType, SharedRealm, const ObjectSchema &, bool live = true); static ObjectType create_instance(ContextType, SharedRealm, const ObjectSchema &, bool live = true);
static ObjectType create_instance(ContextType, SharedRealm, const ObjectSchema &, Query, bool live = true);
template<typename U> template<typename U>
static ObjectType create_filtered(ContextType, const U &, size_t, const ValueType[]); static ObjectType create_filtered(ContextType, const U &, size_t, const ValueType[]);
@ -74,8 +73,8 @@ struct ResultsClass : ClassDefinition<T, realm::Results, CollectionClass<T>> {
}; };
template<typename T> template<typename T>
typename T::Object ResultsClass<T>::create_instance(ContextType ctx, const realm::Results &results, bool live) { typename T::Object ResultsClass<T>::create_instance(ContextType ctx, realm::Results results, bool live) {
auto new_results = new realm::Results(results); auto new_results = new realm::Results(std::move(results));
new_results->set_live(live); new_results->set_live(live);
return create_object<T, ResultsClass<T>>(ctx, new_results); return create_object<T, ResultsClass<T>>(ctx, new_results);
@ -83,7 +82,7 @@ typename T::Object ResultsClass<T>::create_instance(ContextType ctx, const realm
template<typename T> template<typename T>
typename T::Object ResultsClass<T>::create_instance(ContextType ctx, const realm::List &list, bool live) { typename T::Object ResultsClass<T>::create_instance(ContextType ctx, const realm::List &list, bool live) {
return create_instance(ctx, list.get_realm(), list.get_object_schema(), list.get_query(), live); return create_instance(ctx, list.sort({}), live);
} }
template<typename T> template<typename T>
@ -95,21 +94,14 @@ typename T::Object ResultsClass<T>::create_instance(ContextType ctx, SharedRealm
return create_object<T, ResultsClass<T>>(ctx, results); return create_object<T, ResultsClass<T>>(ctx, results);
} }
template<typename T>
typename T::Object ResultsClass<T>::create_instance(ContextType ctx, SharedRealm realm, const ObjectSchema &object_schema, Query query, bool live) {
auto results = new realm::Results(realm, object_schema, std::move(query));
results->set_live(live);
return create_object<T, ResultsClass<T>>(ctx, results);
}
template<typename T> template<typename T>
template<typename U> template<typename U>
typename T::Object ResultsClass<T>::create_filtered(ContextType ctx, const U &collection, size_t argc, const ValueType arguments[]) { typename T::Object ResultsClass<T>::create_filtered(ContextType ctx, const U &collection, size_t argc, const ValueType arguments[]) {
auto query_string = Value::validated_to_string(ctx, arguments[0], "predicate");
auto query = collection.get_query();
auto const &realm = collection.get_realm(); auto const &realm = collection.get_realm();
auto const &object_schema = collection.get_object_schema(); auto const &object_schema = collection.get_object_schema();
auto query = collection.get_table()->where();
auto query_string = Value::validated_to_string(ctx, arguments[0], "predicate");
auto predicate = parser::parse(query_string);
std::vector<ValueType> args; std::vector<ValueType> args;
args.reserve(argc - 1); args.reserve(argc - 1);
@ -118,17 +110,15 @@ typename T::Object ResultsClass<T>::create_filtered(ContextType ctx, const U &co
args.push_back(arguments[i]); args.push_back(arguments[i]);
} }
parser::Predicate predicate = parser::parse(query_string);
query_builder::ArgumentConverter<ValueType, ContextType> converter(ctx, realm, args); query_builder::ArgumentConverter<ValueType, ContextType> converter(ctx, realm, args);
query_builder::apply_predicate(query, predicate, converter, *realm->config().schema, object_schema.name); query_builder::apply_predicate(query, predicate, converter, *realm->config().schema, object_schema.name);
return create_instance(ctx, realm, object_schema, std::move(query)); return create_instance(ctx, collection.filter(std::move(query)));
} }
template<typename T> template<typename T>
template<typename U> template<typename U>
typename T::Object ResultsClass<T>::create_sorted(ContextType ctx, const U &collection, size_t argc, const ValueType arguments[]) { typename T::Object ResultsClass<T>::create_sorted(ContextType ctx, const U &collection, size_t argc, const ValueType arguments[]) {
auto const &realm = collection.get_realm();
auto const &object_schema = collection.get_object_schema(); auto const &object_schema = collection.get_object_schema();
std::vector<std::string> prop_names; std::vector<std::string> prop_names;
std::vector<bool> ascending; std::vector<bool> ascending;
@ -179,8 +169,7 @@ typename T::Object ResultsClass<T>::create_sorted(ContextType ctx, const U &coll
columns.push_back(prop->table_column); 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_instance(ctx, collection.sort({std::move(columns), std::move(ascending)}));
return create_object<T, ResultsClass<T>>(ctx, results);
} }
template<typename T> template<typename T>

View File

@ -50,6 +50,12 @@ Query List::get_query() const
return m_link_view->get_target_table().where(m_link_view); return m_link_view->get_target_table().where(m_link_view);
} }
const Table* List::get_table() const
{
verify_attached();
return &m_link_view->get_target_table();
}
size_t List::get_origin_row_index() const size_t List::get_origin_row_index() const
{ {
verify_attached(); verify_attached();
@ -169,13 +175,13 @@ void List::delete_all()
m_link_view->remove_all_target_rows(); m_link_view->remove_all_target_rows();
} }
Results List::sort(SortOrder order) Results List::sort(SortOrder order) const
{ {
verify_attached(); verify_attached();
return Results(m_realm, *m_object_schema, m_link_view, util::none, std::move(order)); return Results(m_realm, *m_object_schema, m_link_view, util::none, std::move(order));
} }
Results List::filter(Query q) Results List::filter(Query q) const
{ {
verify_attached(); verify_attached();
return Results(m_realm, *m_object_schema, m_link_view, get_query().and_query(std::move(q))); return Results(m_realm, *m_object_schema, m_link_view, get_query().and_query(std::move(q)));

View File

@ -51,6 +51,7 @@ public:
const std::shared_ptr<Realm>& get_realm() const { return m_realm; } const std::shared_ptr<Realm>& get_realm() const { return m_realm; }
Query get_query() const; Query get_query() const;
const ObjectSchema& get_object_schema() const { return *m_object_schema; } const ObjectSchema& get_object_schema() const { return *m_object_schema; }
const Table* get_table() const;
size_t get_origin_row_index() const; size_t get_origin_row_index() const;
bool is_valid() const; bool is_valid() const;
@ -72,8 +73,8 @@ public:
void delete_all(); void delete_all();
Results sort(SortOrder order); Results sort(SortOrder order) const;
Results filter(Query q); Results filter(Query q) const;
bool operator==(List const& rgt) const noexcept; bool operator==(List const& rgt) const noexcept;

View File

@ -475,6 +475,12 @@ Query Results::get_query() const
REALM_UNREACHABLE(); REALM_UNREACHABLE();
} }
const Table* Results::get_table() const
{
validate_read();
return m_table;
}
TableView Results::get_tableview() TableView Results::get_tableview()
{ {
validate_read(); validate_read();

View File

@ -74,6 +74,9 @@ public:
// Get the currently applied sort order for this Results // Get the currently applied sort order for this Results
SortOrder const& get_sort() const noexcept { return m_sort; } SortOrder const& get_sort() const noexcept { return m_sort; }
// Get the table targeted by this Results
const Table* get_table() const;
// Get a tableview containing the same rows as this Results // Get a tableview containing the same rows as this Results
TableView get_tableview(); TableView get_tableview();