Skip to content

Commit 04eee60

Browse files
greenrobot-teamgreenrobot
authored andcommitted
Query: add describe and describeParams, test.
1 parent 4214b56 commit 04eee60

File tree

2 files changed

+45
-0
lines changed
  • objectbox-java/src/main/java/io/objectbox/query
  • tests/objectbox-java-test/src/test/java/io/objectbox/query

2 files changed

+45
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public class Query<T> implements Closeable {
6262

6363
native long nativeRemove(long handle, long cursorHandle);
6464

65+
native String nativeToString(long handle);
66+
67+
native String nativeDescribeParameters(long handle);
68+
6569
native void nativeSetParameter(long handle, int entityId, int propertyId, @Nullable String parameterAlias,
6670
String value);
6771

@@ -654,4 +658,24 @@ public void publish() {
654658
publisher.publish();
655659
}
656660

661+
/**
662+
* For logging and testing, returns a string describing this query
663+
* like "Query for entity Example with 4 conditions with properties prop1, prop2".
664+
* <p>
665+
* Note: the format of the returned string may change without notice.
666+
*/
667+
public String describe() {
668+
return nativeToString(handle);
669+
}
670+
671+
/**
672+
* For logging and testing, returns a string describing the conditions of this query
673+
* like "(prop1 == A AND prop2 is null)".
674+
* <p>
675+
* Note: the format of the returned string may change without notice.
676+
*/
677+
public String describeParameters() {
678+
return nativeDescribeParameters(handle);
679+
}
680+
657681
}

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
@@ -699,4 +699,25 @@ public void onDbException(Exception e) {
699699
}
700700
}
701701

702+
@Test
703+
public void testDescribe() {
704+
// Note: description string correctness is fully asserted in core library.
705+
706+
// No conditions.
707+
Query<TestEntity> queryNoConditions = box.query().build();
708+
assertEquals("Query for entity TestEntity with 1 conditions",queryNoConditions.describe());
709+
assertEquals("TRUE", queryNoConditions.describeParameters());
710+
711+
// Some conditions.
712+
Query<TestEntity> query = box.query()
713+
.equal(TestEntity_.simpleString, "Hello")
714+
.or().greater(TestEntity_.simpleInt, 42)
715+
.build();
716+
String describeActual = query.describe();
717+
assertTrue(describeActual.startsWith("Query for entity TestEntity with 3 conditions with properties "));
718+
// Note: the order properties are listed in is not fixed.
719+
assertTrue(describeActual.contains(TestEntity_.simpleString.name));
720+
assertTrue(describeActual.contains(TestEntity_.simpleInt.name));
721+
assertEquals("(simpleString ==(i) \"Hello\"\n OR simpleInt > 42)", query.describeParameters());
722+
}
702723
}

0 commit comments

Comments
 (0)