Skip to content

Commit 902e313

Browse files
committed
query_condition: added and/or operator overloads #39
1 parent be2f16d commit 902e313

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

objectbox/condition.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
class QueryCondition:
1313
def and_(self, other: QueryCondition) -> QueryCondition:
1414
return LogicQueryCondition(self, other, LogicQueryConditionOp.AND)
15+
__and__ = and_
1516

1617
def or_(self, other: QueryCondition) -> QueryCondition:
1718
return LogicQueryCondition(self, other, LogicQueryConditionOp.OR)
19+
__or__ = or_
1820

1921
def apply(self, qb: QueryBuilder) -> obx_qb_cond:
2022
""" Applies the QueryCondition to the supplied QueryBuilder.

tests/test_query.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,13 @@ def test_set_parameter_alias():
354354
query.set_parameter_alias_int("int32 condition", 40)
355355
assert query.find()[0].str == "FooBar"
356356

357+
# Test with &
358+
query = box.query(
359+
str_prop.equals("Foo").alias("str condition")
360+
& int32_prop.greater_than(700).alias("int32 condition")
361+
).build()
362+
assert query.count() == 1
363+
357364
# Test set parameter alias on vector
358365
vector_prop: Property = VectorEntity.get_property("vector_euclidean")
359366

@@ -396,6 +403,17 @@ def test_set_parameter_alias_advanced():
396403
)
397404
).build()
398405
assert len(query.find_ids()) == 0
406+
407+
# Test using & and | ops
408+
query = box.query(
409+
str_prop.equals("Dummy", case_sensitive=False).alias("str_filter")
410+
& bool_prop.equals(False).alias("bool_filter")
411+
& (
412+
int64_prop.greater_than(0).alias("int64_filter")
413+
| int32_prop.less_than(100).alias("int32_filter")
414+
)
415+
).build()
416+
assert len(query.find_ids()) == 0
399417

400418
# TODO currently we don't support set_parameter_* for int32/bool/other types...
401419

0 commit comments

Comments
 (0)