Skip to content

Commit 9aa9f59

Browse files
authored
Merge pull request #46 from Buggaboo/order-dev
implemented (QueryBuilder) order
2 parents 63037e4 + 6b8e36f commit 9aa9f59

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,23 @@ final overloaded = ((text == "meh") | (text == "bleh")) | text.contains("Hello")
144144
box.query(overloaded as Condition).build(); // the cast is necessary due to the type analyzer
145145
```
146146

147+
Ordering
148+
--------
149+
150+
The results from a query can be ordered using the `order` method, e.g.
151+
152+
```dart
153+
final q = box.query(Entity_.number > 0)
154+
.order(Type_.number)
155+
.build();
156+
157+
// ...
158+
159+
final qt = box.query(Entity_.text.notNull())
160+
.order(Entity_.text, flags: OBXOrderFlag.DESCENDING | OBXOrderFlag.CASE_SENSITIVE)
161+
.build();
162+
```
163+
147164
Basic technical approach
148165
------------------------
149166
ObjectBox offers a [C API](https://github.com/objectbox/objectbox-c) which can be called by [Dart FFI](https://dart.dev/server/c-interop).

lib/src/query/builder.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ class QueryBuilder<T> {
1717
}
1818
}
1919

20+
_createBuilder() => _cBuilder ??= bindings.obx_qb_create(_store.ptr, _entityId);
21+
2022
Query build() {
21-
_cBuilder = bindings.obx_qb_create(_store.ptr, _entityId);
23+
_createBuilder();
2224

2325
if (0 == _queryCondition.apply(this, true)) {
2426
_throwExceptionIfNecessary();
@@ -30,15 +32,19 @@ class QueryBuilder<T> {
3032
checkObx(bindings.obx_qb_close(_cBuilder));
3133
}
3234
}
35+
36+
QueryBuilder<T> order(QueryProperty p, {int flags=0}) {
37+
_createBuilder();
38+
checkObx(bindings.obx_qb_order(_cBuilder, p._propertyId, flags));
39+
return this;
40+
}
3341
}
3442

3543
/* // Not done yet
3644
obx_qb_bytes_eq_dart_t obx_qb_bytes_equal;
3745
obx_qb_bytes_lt_gt_dart_t obx_qb_bytes_greater, obx_qb_bytes_less;
3846
3947
obx_qb_param_alias_dart_t obx_qb_param_alias;
40-
41-
obx_qb_order_dart_t obx_qb_order;
4248
*/
4349

4450
//////

test/query_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import "package:test/test.dart";
22
import "package:objectbox/objectbox.dart";
33
import "entity.dart";
44
import 'test_env.dart';
5+
import "package:objectbox/src/bindings/constants.dart" show OBXOrderFlag;
56

67
void main() {
78
TestEnv env;
@@ -356,6 +357,41 @@ void main() {
356357
}
357358
});
358359

360+
test(".order queryBuilder", () {
361+
box.put(TestEntity.initText("World"));
362+
box.put(TestEntity.initText("Hello"));
363+
box.put(TestEntity.initText("HELLO"));
364+
box.put(TestEntity.initText("World"));
365+
box.put(TestEntity.initText("Goodbye"));
366+
box.put(TestEntity.initText("Cruel"));
367+
box.put(TestEntity.initInteger(1337));
368+
369+
final text = TestEntity_.text;
370+
371+
final condition = text.notNull();
372+
373+
final query = box.query(condition)
374+
.order(text)
375+
.build();
376+
377+
final queryWithFlags = box.query(condition)
378+
.order(text, flags: OBXOrderFlag.DESCENDING | OBXOrderFlag.CASE_SENSITIVE)
379+
.build();
380+
381+
final result1 = query.find().map((e) => e.text).toList();
382+
final result2 = queryWithFlags.find().map((e) => e.text).toList();
383+
384+
expect("Cruel", result1[0]);
385+
expect("World", result2[0]);
386+
expect("Hello", result1[2]);
387+
expect("Hello", result2[2]);
388+
expect("HELLO", result1[3]);
389+
expect("HELLO", result2[3]);
390+
391+
query.close();
392+
queryWithFlags.close();
393+
});
394+
359395
tearDown(() {
360396
env.close();
361397
});

0 commit comments

Comments
 (0)