Skip to content

Commit 06b42aa

Browse files
author
Ivan Dlugos
committed
ByteVector property queries
1 parent 1ea37a7 commit 06b42aa

File tree

5 files changed

+82
-6
lines changed

5 files changed

+82
-6
lines changed

lib/src/query/query.dart

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
library query;
22

33
import 'dart:ffi';
4+
import 'dart:typed_data';
5+
46
import 'package:ffi/ffi.dart' show allocate, free, Utf8;
57

68
import '../store.dart';
@@ -136,7 +138,29 @@ class QueryByteVectorProperty extends QueryProperty {
136138
/*required*/ int obxType})
137139
: super(entityId, propertyId, obxType);
138140

139-
// TODO conditions
141+
Condition _op(List<int> val, ConditionOp cop) {
142+
return ByteVectorCondition(cop, this, Uint8List.fromList(val));
143+
}
144+
145+
Condition equals(List<int> val) {
146+
return _op(val, ConditionOp.eq);
147+
}
148+
149+
Condition greaterThan(List<int> val) {
150+
return _op(val, ConditionOp.gt);
151+
}
152+
153+
Condition greaterOrEqual(List<int> val) {
154+
return _op(val, ConditionOp.greaterOrEq);
155+
}
156+
157+
Condition lessThan(List<int> val) {
158+
return _op(val, ConditionOp.lt);
159+
}
160+
161+
Condition lessOrEqual(List<int> val) {
162+
return _op(val, ConditionOp.lessOrEq);
163+
}
140164
}
141165

142166
class QueryIntegerProperty extends QueryProperty {
@@ -541,6 +565,40 @@ class DoubleCondition extends PropertyCondition<double> {
541565
}
542566
}
543567

568+
class ByteVectorCondition extends PropertyCondition<Uint8List> {
569+
ByteVectorCondition(ConditionOp op, QueryProperty prop, Uint8List value)
570+
: super(op, prop, value);
571+
572+
int _op1(QueryBuilder builder,
573+
int Function(Pointer<OBX_query_builder>, int, Pointer<Void>, int) func) {
574+
final cBytes = OBX_bytes_wrapper.managedCopyOf(_value, align: false);
575+
try {
576+
return func(
577+
builder._cBuilder, _property._propertyId, cBytes.ptr, cBytes.size);
578+
} finally {
579+
cBytes.freeManaged();
580+
}
581+
}
582+
583+
@override
584+
int apply(QueryBuilder builder, bool isRoot) {
585+
switch (_op) {
586+
case ConditionOp.eq:
587+
return _op1(builder, bindings.obx_qb_equals_bytes);
588+
case ConditionOp.lt:
589+
return _op1(builder, bindings.obx_qb_less_than_bytes);
590+
case ConditionOp.lessOrEq:
591+
return _op1(builder, bindings.obx_qb_less_or_equal_bytes);
592+
case ConditionOp.gt:
593+
return _op1(builder, bindings.obx_qb_greater_than_bytes);
594+
case ConditionOp.greaterOrEq:
595+
return _op1(builder, bindings.obx_qb_greater_or_equal_bytes);
596+
default:
597+
throw Exception('Unsupported operation ${_op.toString()}');
598+
}
599+
}
600+
}
601+
544602
class ConditionGroup extends Condition {
545603
final List<Condition> _conditions;
546604
final int Function(Pointer<OBX_query_builder>, Pointer<Int32>, int) _func;

test/box_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ void main() {
3939
final int putId = box.put(TestEntity(
4040
tString: 'Hello',
4141
tStrings: ['foo', 'bar'],
42-
tListInt: [1, 99, -54],
42+
tByteList: [1, 99, -54],
4343
tUint8List: Uint8List.fromList([2, 50, 78]),
4444
tInt8List: Int8List.fromList([-16, 20, 43])));
4545
final TestEntity item = box.get(putId) /*!*/;
4646
expect(item.id, equals(putId));
4747
expect(item.tString, equals('Hello'));
4848
expect(item.tStrings, equals(['foo', 'bar']));
49-
expect(item.tListInt, equals([1, 99, -54]));
49+
expect(item.tByteList, equals([1, 99, -54]));
5050
expect(item.tUint8List, equals([2, 50, 78]));
5151
expect(item.tInt8List, equals([-16, 20, 43]));
5252
});

test/entity.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class TestEntity {
5454

5555
// OBXPropertyType.ByteVector
5656
@Property(type: PropertyType.byteVector)
57-
List<int> /*?*/ tListInt;
57+
List<int> /*?*/ tByteList;
5858

5959
// OBXPropertyType.ByteVector
6060
Int8List /*?*/ tInt8List;
@@ -74,7 +74,7 @@ class TestEntity {
7474
this.tInt,
7575
this.tFloat,
7676
this.tStrings,
77-
this.tListInt,
77+
this.tByteList,
7878
this.tInt8List,
7979
this.tUint8List,
8080
this.ignore});

test/objectbox-model.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
},
152152
{
153153
"id": "28:7286551998282235109",
154-
"name": "tListInt",
154+
"name": "tByteList",
155155
"type": 23
156156
},
157157
{

test/query_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,24 @@ void main() {
513513
queryReverseOrder.close();
514514
});
515515

516+
test('.describeParameters BytesVector', () {
517+
final q = box
518+
.query(TestEntity_.tUint8List.equals([1, 2]) &
519+
TestEntity_.tInt8List.greaterThan([3, 4]) &
520+
TestEntity_.tByteList.greaterOrEqual([5, 6, 7]) &
521+
TestEntity_.tUint8List.lessThan([8]) &
522+
TestEntity_.tUint8List.lessOrEqual([9, 10, 11, 12]))
523+
.build();
524+
expect(
525+
q.describeParameters(),
526+
equals('(tUint8List == byte[2]{0x0102}\n'
527+
' AND tInt8List > byte[2]{0x0304}\n'
528+
' AND tByteList >= byte[3]{0x050607}\n'
529+
' AND tUint8List < byte[1]{0x08}\n'
530+
' AND tUint8List <= byte[4]{0x090A0B0C})'));
531+
q.close();
532+
});
533+
516534
tearDown(() {
517535
env.close();
518536
});

0 commit comments

Comments
 (0)