Skip to content

Commit 2a726c8

Browse files
Add containsKeyValue query condition and setParameters overload.
1 parent bccfa0f commit 2a726c8

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

objectbox-java/src/main/java/io/objectbox/Property.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.objectbox.query.PropertyQueryConditionImpl.StringArrayCondition;
3232
import io.objectbox.query.PropertyQueryConditionImpl.StringCondition;
3333
import io.objectbox.query.PropertyQueryConditionImpl.StringCondition.Operation;
34+
import io.objectbox.query.PropertyQueryConditionImpl.StringStringCondition;
3435
import io.objectbox.query.QueryBuilder.StringOrder;
3536

3637
import javax.annotation.Nullable;
@@ -463,6 +464,25 @@ private void checkIsStringArray() {
463464
}
464465
}
465466

467+
/**
468+
* For a String-key map property, matches if at least one key and value combination equals the given values
469+
* using {@link StringOrder#CASE_SENSITIVE StringOrder#CASE_SENSITIVE}.
470+
*
471+
* @see #containsKeyValue(String, String, StringOrder)
472+
*/
473+
public PropertyQueryCondition<ENTITY> containsKeyValue(String key, String value) {
474+
return new StringStringCondition<>(this, StringStringCondition.Operation.CONTAINS_KEY_VALUE,
475+
key, value, StringOrder.CASE_SENSITIVE);
476+
}
477+
478+
/**
479+
* @see #containsKeyValue(String, String)
480+
*/
481+
public PropertyQueryCondition<ENTITY> containsKeyValue(String key, String value, StringOrder order) {
482+
return new StringStringCondition<>(this, StringStringCondition.Operation.CONTAINS_KEY_VALUE,
483+
key, value, order);
484+
}
485+
466486
/**
467487
* Creates a starts with condition using {@link StringOrder#CASE_SENSITIVE StringOrder#CASE_SENSITIVE}.
468488
*

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,34 @@ void applyCondition(QueryBuilder<T> builder) {
343343
}
344344
}
345345

346+
public static class StringStringCondition<T> extends PropertyQueryConditionImpl<T> {
347+
private final Operation op;
348+
private final String leftValue;
349+
private final String rightValue;
350+
private final StringOrder order;
351+
352+
public enum Operation {
353+
CONTAINS_KEY_VALUE
354+
}
355+
356+
public StringStringCondition(Property<T> property, Operation op, String leftValue, String rightValue, StringOrder order) {
357+
super(property);
358+
this.op = op;
359+
this.leftValue = leftValue;
360+
this.rightValue = rightValue;
361+
this.order = order;
362+
}
363+
364+
@Override
365+
void applyCondition(QueryBuilder<T> builder) {
366+
if (op == Operation.CONTAINS_KEY_VALUE) {
367+
builder.containsKeyValue(property, leftValue, rightValue, order);
368+
} else {
369+
throw new UnsupportedOperationException(op + " is not supported with two String values");
370+
}
371+
}
372+
}
373+
346374
public static class StringArrayCondition<T> extends PropertyQueryConditionImpl<T> {
347375
private final Operation op;
348376
private final String[] value;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public class Query<T> implements Closeable {
7474
native void nativeSetParameter(long handle, int entityId, int propertyId, @Nullable String parameterAlias,
7575
String value);
7676

77+
private native void nativeSetParameters(long handle, int entityId, int propertyId, @Nullable String parameterAlias,
78+
String value, String value2);
79+
7780
native void nativeSetParameter(long handle, int entityId, int propertyId, @Nullable String parameterAlias,
7881
long value);
7982

@@ -564,6 +567,24 @@ public Query<T> setParameters(String alias, String[] values) {
564567
return this;
565568
}
566569

570+
/**
571+
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
572+
*/
573+
public Query<T> setParameters(Property<?> property, String key, String value) {
574+
nativeSetParameters(handle, property.getEntityId(), property.getId(), null, key, value);
575+
return this;
576+
}
577+
578+
/**
579+
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
580+
*
581+
* @param alias as defined using {@link QueryBuilder#parameterAlias(String)}.
582+
*/
583+
public Query<T> setParameters(String alias, String key, String value) {
584+
nativeSetParameters(handle, 0, 0, alias, key, value);
585+
return this;
586+
}
587+
567588
/**
568589
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
569590
*/

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ private native long nativeLink(long handle, long storeHandle, int relationOwnerE
184184

185185
private native long nativeContainsElement(long handle, int propertyId, String value, boolean caseSensitive);
186186

187+
private native long nativeContainsKeyValue(long handle, int propertyId, String key, String value, boolean caseSensitive);
188+
187189
private native long nativeStartsWith(long handle, int propertyId, String value, boolean caseSensitive);
188190

189191
private native long nativeEndsWith(long handle, int propertyId, String value, boolean caseSensitive);
@@ -780,6 +782,15 @@ public QueryBuilder<T> containsElement(Property<T> property, String value, Strin
780782
return this;
781783
}
782784

785+
/**
786+
* For a String-key map property, matches if at least one key and value combination equals the given values.
787+
*/
788+
public QueryBuilder<T> containsKeyValue(Property<T> property, String key, String value, StringOrder order) {
789+
verifyHandle();
790+
checkCombineCondition(nativeContainsKeyValue(handle, property.getId(), key, value, order == StringOrder.CASE_SENSITIVE));
791+
return this;
792+
}
793+
783794
public QueryBuilder<T> startsWith(Property<T> property, String value, StringOrder order) {
784795
verifyHandle();
785796
checkCombineCondition(nativeStartsWith(handle, property.getId(), value, order == StringOrder.CASE_SENSITIVE));

0 commit comments

Comments
 (0)