Skip to content

Commit 4ad93fd

Browse files
authored
Merge branch 'dev' into devquid
2 parents 991ea6c + 907af5a commit 4ad93fd

File tree

7 files changed

+90
-30
lines changed

7 files changed

+90
-30
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).

analysis_options.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
include: package:pedantic/analysis_options.yaml
1+
include: package:pedantic/analysis_options.yaml
2+
3+
linter:
4+
rules:
5+
unrelated_type_equality_checks: false # query condition operator overrides

lib/src/bindings/constants.dart

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,4 @@ class OBXError {
8282

8383
/// A requested schema object (e.g. entity or property) was not found in the schema
8484
static const int OBX_ERROR_SCHEMA_OBJECT_NOT_FOUND = 10503;
85-
}
86-
87-
// query builder
88-
89-
class OBXOrderFlag {
90-
/// Reverts the order from ascending (default) to descending.
91-
static final DESCENDING = 1;
92-
93-
/// Makes upper case letters (e.g. "Z") be sorted before lower case letters (e.g. "a").
94-
/// If not specified, the default is case insensitive for ASCII characters.
95-
static final CASE_SENSITIVE = 2;
96-
97-
/// For scalars only: changes the comparison to unsigned (default is signed).
98-
static final UNSIGNED = 4;
99-
100-
/// null values will be put last.
101-
/// If not specified, by default null values will be put first.
102-
static final NULLS_LAST = 8;
103-
104-
/// null values should be treated equal to zero (scalars only).
105-
static final NULLS_ZERO = 16;
106-
}
85+
}

lib/src/box.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,12 @@ class Box<T> {
233233
}
234234

235235
int removeAll() {
236-
Pointer<Uint64> removedIds = Pointer<Uint64>.allocate();
236+
Pointer<Uint64> removedItems = Pointer<Uint64>.allocate();
237237
try {
238-
checkObx(bindings.obx_box_remove_all(_cBox, removedIds));
239-
return removedIds.load<int>();
238+
checkObx(bindings.obx_box_remove_all(_cBox, removedItems));
239+
return removedItems.load<int>();
240240
} finally {
241-
removedIds.free();
241+
removedItems.free();
242242
}
243243
}
244244

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
//////

lib/src/query/index.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
11
export "query.dart";
2+
3+
class Order {
4+
/// Reverts the order from ascending (default) to descending.
5+
static final descending = 1;
6+
7+
/// Makes upper case letters (e.g. "Z") be sorted before lower case letters (e.g. "a").
8+
/// If not specified, the default is case insensitive for ASCII characters.
9+
static final caseSensitive = 2;
10+
11+
/// For scalars only: changes the comparison to unsigned (default is signed).
12+
static final unsigned = 4;
13+
14+
/// null values will be put last.
15+
/// If not specified, by default null values will be put first.
16+
static final nullsLast = 8;
17+
18+
/// null values should be treated equal to zero (scalars only).
19+
static final nullsAsZero = 16;
20+
}

test/query_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,41 @@ void main() {
356356
}
357357
});
358358

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

0 commit comments

Comments
 (0)