File tree Expand file tree Collapse file tree 2 files changed +31
-2
lines changed
objectbox-java/src/main/java/io/objectbox/query
tests/objectbox-java-test/src/test/java/io/objectbox/query Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -94,7 +94,14 @@ enum Operator {
94
94
95
95
private long handle ;
96
96
97
+ /**
98
+ * Holds on to last condition. May be a property condition or a combined condition.
99
+ */
97
100
private long lastCondition ;
101
+ /**
102
+ * Holds on to last property condition to use with {@link #parameterAlias(String)}
103
+ */
104
+ private long lastPropertyCondition ;
98
105
private Operator combineNextWith = Operator .NONE ;
99
106
100
107
@ Nullable
@@ -307,10 +314,10 @@ public QueryBuilder<T> sort(Comparator<T> comparator) {
307
314
*/
308
315
public QueryBuilder <T > parameterAlias (String alias ) {
309
316
verifyHandle ();
310
- if (lastCondition == 0 ) {
317
+ if (lastPropertyCondition == 0 ) {
311
318
throw new IllegalStateException ("No previous condition. Before you can assign an alias, you must first have a condition." );
312
319
}
313
- nativeSetParameterAlias (lastCondition , alias );
320
+ nativeSetParameterAlias (lastPropertyCondition , alias );
314
321
return this ;
315
322
}
316
323
@@ -478,6 +485,7 @@ private void checkCombineCondition(long currentCondition) {
478
485
} else {
479
486
lastCondition = currentCondition ;
480
487
}
488
+ lastPropertyCondition = currentCondition ;
481
489
}
482
490
483
491
public QueryBuilder <T > isNull (Property <T > property ) {
Original file line number Diff line number Diff line change @@ -607,6 +607,27 @@ public void testSetParameterString() {
607
607
assertEquals (2 , query .findUnique ().getId ());
608
608
}
609
609
610
+ /**
611
+ * https://github.com/objectbox/objectbox-java/issues/834
612
+ */
613
+ @ Test
614
+ public void parameterAlias_combinedConditions () {
615
+ putTestEntitiesScalars ();
616
+
617
+ Query <TestEntity > query = box .query ()
618
+ .greater (simpleInt , 0 ).parameterAlias ("greater" )
619
+ .or ()
620
+ .less (simpleInt , 0 ).parameterAlias ("less" )
621
+ .build ();
622
+ List <TestEntity > results = query
623
+ .setParameter ("greater" , 2008 )
624
+ .setParameter ("less" , 2001 )
625
+ .find ();
626
+ assertEquals (2 , results .size ());
627
+ assertEquals (2000 , results .get (0 ).getSimpleInt ());
628
+ assertEquals (2009 , results .get (1 ).getSimpleInt ());
629
+ }
630
+
610
631
@ Test
611
632
public void testForEach () {
612
633
List <TestEntity > testEntities = putTestEntitiesStrings ();
You can’t perform that action at this time.
0 commit comments