Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for conditionless queries #132

Merged
merged 3 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions example/flutter/objectbox_demo/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,8 @@ class ViewModel {
_box = Box<Note>(_store);

final dateProp = Note_.date;
final dummyCondition = dateProp.greaterThan(0);

_query = _box
.query(dummyCondition)
.order(dateProp, flags: Order.descending)
.build();
_query = _box.emptyQuery.order(dateProp, flags: Order.descending).build();
}

void addNote(Note note) => _box.put(note);
Expand Down
7 changes: 7 additions & 0 deletions lib/src/bindings/signatures.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,10 @@ typedef obx_bytes_array_set_t<Ret, SizeT> = Ret Function(
SizeT index,
Pointer<Uint8> data,
SizeT size);

/* // TODO
obx_qb_bytes_eq_dart_t obx_qb_bytes_equal;
obx_qb_bytes_lt_gt_dart_t obx_qb_bytes_greater, obx_qb_bytes_less;

obx_qb_param_alias_dart_t obx_qb_param_alias;
*/
4 changes: 4 additions & 0 deletions lib/src/box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ class Box<T> {
}
}

/// When no conditions are required on properties
QueryBuilder get emptyQuery =>
QueryBuilder<T>(_store, _fbManager, _modelEntity.id.id);

vaind marked this conversation as resolved.
Show resolved Hide resolved
/// Returns true if no objects are in this box.
bool isEmpty() {
final isEmpty = allocate<Uint8>();
Expand Down
40 changes: 3 additions & 37 deletions lib/src/query/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class QueryBuilder<T> {
Pointer<Void> _cBuilder;
final OBXFlatbuffersManager _fbManager;

QueryBuilder(
this._store, this._fbManager, this._entityId, this._queryCondition);
QueryBuilder(this._store, this._fbManager, this._entityId,
[this._queryCondition]);

void _throwExceptionIfNecessary() {
if (bindings.obx_qb_error_code(_cBuilder) != OBXError.OBX_SUCCESS) {
Expand All @@ -24,7 +24,7 @@ class QueryBuilder<T> {
Query build() {
_createBuilder();

if (0 == _queryCondition.apply(this, true)) {
if (_queryCondition != null && 0 == _queryCondition.apply(this, true)) {
_throwExceptionIfNecessary();
}

Expand All @@ -45,37 +45,3 @@ class QueryBuilder<T> {
return this;
}
}

/* // Not done yet
obx_qb_bytes_eq_dart_t obx_qb_bytes_equal;
obx_qb_bytes_lt_gt_dart_t obx_qb_bytes_greater, obx_qb_bytes_less;

obx_qb_param_alias_dart_t obx_qb_param_alias;
*/

//////
//////

/** Inspiration
Modifier and Type Method Description
<TARGET> QueryBuilder<TARGET> backlink​(RelationInfo<TARGET,?> relationInfo)
Creates a backlink (reversed link) to another entity, for which you also can describe conditions using the returned builder.
void close()
** QueryBuilder<T> eager​(int limit, RelationInfo relationInfo, RelationInfo... more)
Like eager(RelationInfo, RelationInfo[]), but limits eager loading to the given count.
** QueryBuilder<T> eager​(RelationInfo relationInfo, RelationInfo... more)
Specifies relations that should be resolved eagerly.
** QueryBuilder<T> filter​(QueryFilter<T> filter) // dart has built-in higher order functions
Sets a filter that executes on primary query results (returned from the db core) on a Java level.
<TARGET> QueryBuilder<TARGET> link​(RelationInfo<?,TARGET> relationInfo)
Creates a link to another entity, for which you also can describe conditions using the returned builder.
** QueryBuilder<T> order​(Property<T> property)
Specifies given property to be used for sorting.
** QueryBuilder<T> order​(Property<T> property, int flags)
Defines the order with which the results are ordered (default: none).
** QueryBuilder<T> orderDesc​(Property<T> property)
Specifies given property in descending order to be used for sorting.
** QueryBuilder<T> parameterAlias​(java.lang.String alias)
Assigns the given alias to the previous condition.
** QueryBuilder<T> sort​(java.util.Comparator<T> comparator)
*/
17 changes: 17 additions & 0 deletions test/query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ void main() {
box = env.box;
});

test('Query with no conditions, and order as desc ints', () {
box.putMany([
TestEntity(tInt: 0),
TestEntity(tInt: 10),
TestEntity(tInt: 100),
TestEntity(tInt: 10),
TestEntity(tInt: 0),
]);

var query =
box.emptyQuery.order(TestEntity_.tInt, flags: Order.descending).build();
final listDesc = query.find();
query.close();

expect(listDesc.map((t) => t.tInt).toList(), [100, 10, 10, 0, 0]);
});

test('ignore transient field', () {
box.put(TestEntity(tDouble: 0.1, ignore: 1337));

Expand Down