Hook up the sort/distinct orderings from parsed queries (#1653)

* Hook up the sort/distinct orderings from parsed queries
* Update changelog, better tests, fix syntax
This commit is contained in:
James Stone 2018-02-07 07:11:43 -08:00 committed by Kenneth Geisshirt
parent 5dd8fdc08d
commit b78a8a60d0
5 changed files with 41 additions and 4 deletions

View File

@ -8,7 +8,12 @@
### Enhancements
* Reduced initial download times in Realms with long transaction histories.
* Wait for pending notifications to complete when removing a sync listener (1648).
* Further enhancements of the query parser.
* Enabled sort and distinct in the query string. If sort or distinct are also applied outside of the query string, the conditions are stacked.
- Example syntax: `age > 20 SORT(name ASC, age DESC) DISTINCT(name)`
- The ordering for sorting can be one of the following case insensitive literals: `ASC`, `ASCENDING`, `DESC`, `DESCENDING`.
- Any number of properties can appear inside the brackets in a comma separated list.
- Any number of sort/distinct conditions can be indicated, they will be applied in the specified order.
- Sort or distinct cannot operate independently, these conditions must be attached to at least one query filter.
### Bug fixes
* None.

View File

@ -152,13 +152,15 @@ typename T::Object ResultsClass<T>::create_filtered(ContextType ctx, const U &co
auto query = collection.get_query();
auto const &realm = collection.get_realm();
auto const &object_schema = collection.get_object_schema();
DescriptorOrdering ordering;
parser::ParserResult result = parser::parse(query_string);
NativeAccessor<T> accessor(ctx, realm, object_schema);
query_builder::ArgumentConverter<ValueType, NativeAccessor<T>> converter(accessor, &args.value[1], args.count - 1);
query_builder::apply_predicate(query, result.predicate, converter);
query_builder::apply_ordering(ordering, query.get_table(), result.ordering);
return create_instance(ctx, collection.filter(std::move(query)));
return create_instance(ctx, collection.filter(std::move(query)).apply_ordering(std::move(ordering)));
}
template<typename T>

@ -1 +1 @@
Subproject commit 436c1112337ee68fc65655c2a253997c5dcf458f
Subproject commit dbbd3d9212e608359fc8297fb9c29433e9a13724

View File

@ -154,5 +154,8 @@ module.exports = {
},
testOptionalQueries: function() {
runQuerySuite(testCases.optionalTests);
},
testOrderingQueries: function() {
runQuerySuite(testCases.orderingTests);
}
};

View File

@ -377,6 +377,33 @@
["ObjectSet", [1], "LinkTypesObject", "basicLink.dateCol == null"],
["ObjectSet", [0], "LinkTypesObject", "basicLink.dateCol != null"]
]
}
},
"orderingTests" : {
"schema" : [
{ "name": "Person",
"properties": [
{ "name": "id", "type": "int"},
{ "name": "name", "type": "string" },
{ "name": "age", "type": "int"}
],
"primaryKey" : "id" }
],
"objects": [
{ "type": "Person", "value": [0, "John", 28] },
{ "type": "Person", "value": [1, "John", 37] },
{ "type": "Person", "value": [2, "Jake", 27] },
{ "type": "Person", "value": [3, "Jake", 32] },
{ "type": "Person", "value": [4, "Jake", 32] },
{ "type": "Person", "value": [5, "Johnny", 19] }
],
"tests": [
["ObjectSet", [1, 3], "Person", "age > 20 SORT(age DESC) DISTINCT(name)"],
["ObjectSet", [2, 0], "Person", "age > 20 SORT(age ASC) DISTINCT(name)"],
["ObjectSet", [2, 0], "Person", "age > 20 SORT(age ASC, name DESC) DISTINCT(name)"],
["ObjectSet", [2, 0], "Person", "age > 20 SORT(name DESC) SORT(age ASC) DISTINCT(name)"],
["ObjectSet", [2, 0, 3, 1], "Person", "age > 20 SORT(age ASC, name DESC) DISTINCT(name, age)"],
["ObjectSet", [0, 2], "Person", "age > 20 SORT(age ASC) DISTINCT(age) SORT(name DESC) DISTINCT(name)"]
]
}
}