Skip to content

Commit 9f6b237

Browse files
Merge branch '42-alias-combined-condition-crash' into release-3.0.0-alpha2
2 parents eac40d0 + 7ae6490 commit 9f6b237

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

objectbox-java/src/main/java/io/objectbox/query/QueryBuilder.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,14 @@ enum Operator {
9494

9595
private long handle;
9696

97+
/**
98+
* Holds on to last condition. May be a property condition or a combined condition.
99+
*/
97100
private long lastCondition;
101+
/**
102+
* Holds on to last property condition to use with {@link #parameterAlias(String)}
103+
*/
104+
private long lastPropertyCondition;
98105
private Operator combineNextWith = Operator.NONE;
99106

100107
@Nullable
@@ -307,10 +314,10 @@ public QueryBuilder<T> sort(Comparator<T> comparator) {
307314
*/
308315
public QueryBuilder<T> parameterAlias(String alias) {
309316
verifyHandle();
310-
if (lastCondition == 0) {
317+
if (lastPropertyCondition == 0) {
311318
throw new IllegalStateException("No previous condition. Before you can assign an alias, you must first have a condition.");
312319
}
313-
nativeSetParameterAlias(lastCondition, alias);
320+
nativeSetParameterAlias(lastPropertyCondition, alias);
314321
return this;
315322
}
316323

@@ -478,6 +485,7 @@ private void checkCombineCondition(long currentCondition) {
478485
} else {
479486
lastCondition = currentCondition;
480487
}
488+
lastPropertyCondition = currentCondition;
481489
}
482490

483491
public QueryBuilder<T> isNull(Property<T> property) {

tests/objectbox-java-test/src/test/java/io/objectbox/query/QueryTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,27 @@ public void testSetParameterString() {
618618
assertEquals(2, query.findUnique().getId());
619619
}
620620

621+
/**
622+
* https://github.com/objectbox/objectbox-java/issues/834
623+
*/
624+
@Test
625+
public void parameterAlias_combinedConditions() {
626+
putTestEntitiesScalars();
627+
628+
Query<TestEntity> query = box.query()
629+
.greater(simpleInt, 0).parameterAlias("greater")
630+
.or()
631+
.less(simpleInt, 0).parameterAlias("less")
632+
.build();
633+
List<TestEntity> results = query
634+
.setParameter("greater", 2008)
635+
.setParameter("less", 2001)
636+
.find();
637+
assertEquals(2, results.size());
638+
assertEquals(2000, results.get(0).getSimpleInt());
639+
assertEquals(2009, results.get(1).getSimpleInt());
640+
}
641+
621642
@Test
622643
public void testForEach() {
623644
List<TestEntity> testEntities = putTestEntitiesStrings();

0 commit comments

Comments
 (0)