Skip to content

Commit 90198d8

Browse files
committed
added find string methods and reset() to PropertyQuery
1 parent e4e76d8 commit 90198d8

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ public class PropertyQuery {
4444
this.property = property;
4545
}
4646

47+
/** Clears all values (e.g. distinct and null value). */
48+
public PropertyQuery reset() {
49+
distinct = false;
50+
noCaseIfDistinct = true;
51+
enableNull = false;
52+
nullValueDouble = 0;
53+
nullValueFloat = 0;
54+
nullValueString = null;
55+
nullValueLong = 0;
56+
return this;
57+
}
58+
4759
/**
4860
* Only distinct values should be returned (e.g. 1,2,3 instead of 1,1,2,3,3,3).
4961
* <p>
@@ -251,6 +263,24 @@ public double[] call() {
251263
});
252264
}
253265

266+
private String findString(final boolean unique) {
267+
return (String) query.callInReadTx(new Callable<String>() {
268+
@Override
269+
public String call() {
270+
return query.nativeFindString(query.handle, query.cursorHandle(), property.id, unique,
271+
enableNull, nullValueString);
272+
}
273+
});
274+
}
275+
276+
public String findFirstString() {
277+
return findString(false);
278+
}
279+
280+
public String findUniqueString() {
281+
return findString(true);
282+
}
283+
254284
private Object findNumber(final boolean unique) {
255285
return query.callInReadTx(new Callable<Object>() {
256286
@Override
@@ -269,7 +299,6 @@ public Long findUniqueLong() {
269299
return (Long) findNumber(true);
270300
}
271301

272-
273302
public Integer findFirstInt() {
274303
return (Integer) findNumber(false);
275304
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ native double[] nativeFindDoubles(long handle, long cursorHandle, int propertyId
8686
native Object nativeFindNumber(long handle, long cursorHandle, int propertyId, boolean unique, boolean enableNull,
8787
long nullValue, float nullValueFloat, double nullValueDouble);
8888

89+
native String nativeFindString(long handle, long cursorHandle, int propertyId, boolean unique, boolean enableNull,
90+
String nullValue);
91+
8992
native long nativeCount(long handle, long cursorHandle);
9093

9194
native long nativeSum(long handle, long cursorHandle, int propertyId);

tests/objectbox-java-test/src/main/java/io/objectbox/query/PropertyQueryTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,32 @@ public void testFindStrings_wrongPropertyType() {
131131
box.query().build().property(simpleInt).findStrings();
132132
}
133133

134+
@Test
135+
public void testFindString() {
136+
Query<TestEntity> query = box.query().greater(simpleLong, 1002).build();
137+
PropertyQuery propertyQuery = query.property(simpleString);
138+
assertNull(propertyQuery.findFirstString());
139+
assertNull(propertyQuery.reset().findUniqueString());
140+
putTestEntities(5);
141+
assertEquals("foo3", propertyQuery.reset().findFirstString());
142+
143+
query = box.query().greater(simpleLong, 1004).build();
144+
propertyQuery = query.property(simpleString);
145+
assertEquals("foo5", propertyQuery.reset().findUniqueString());
146+
147+
putTestEntity(null, 6);
148+
// TODO XXX enable me after fixing combination of unique and distinct: putTestEntity(null, 7);
149+
query.setParameter(simpleLong, 1005);
150+
assertEquals("nope", propertyQuery.reset().distinct().nullValue("nope").findUniqueString());
151+
}
152+
153+
@Test(expected = DbException.class)
154+
public void testFindString_uniqueFails() {
155+
putTestEntity("foo", 1);
156+
putTestEntity("foo", 2);
157+
box.query().build().property(simpleString).findUniqueString();
158+
}
159+
134160
@Test
135161
public void testFindLongs() {
136162
putTestEntities(5);

0 commit comments

Comments
 (0)