T
- Entity class for which the Query is built.public class QueryBuilder<T>
extends java.lang.Object
implements java.io.Closeable
Query
using conditions which can then be used to return a list of matching Objects.
A simple example:
userBox.query() .equal(User_.firstName, "Joe") .order(User_.lastName) .build() .find()
To add a condition use the appropriate method, for example equal(Property, String)
or
isNull(Property)
. To order results use order(Property)
and its related methods.
Use build()
to create a Query
object, which is used to actually get the results.
Note: by default Query returns full Objects. To return only values or an aggregate value for a single Property,
use Query.property(Property)
.
See the Queries documentation for details.
Modifier and Type | Class and Description |
---|---|
static class |
QueryBuilder.StringOrder |
Modifier and Type | Field and Description |
---|---|
static int |
CASE_SENSITIVE
Makes upper case letters (e.g.
|
static int |
DESCENDING
Reverts the order from ascending (default) to descending.
|
static int |
NULLS_LAST
null values will be put last.
|
static int |
NULLS_ZERO
null values should be treated equal to zero (scalars only).
|
static int |
UNSIGNED
For scalars only: changes the comparison to unsigned (default is signed).
|
Constructor and Description |
---|
QueryBuilder(Box<T> box,
long storeHandle,
java.lang.String entityName) |
Modifier and Type | Method and Description |
---|---|
QueryBuilder<T> |
and()
And AND changes how conditions are combined using a following OR.
|
<TARGET> QueryBuilder<TARGET> |
backlink(RelationInfo<TARGET,?> relationInfo)
Creates a backlink (reversed link) to another entity,
for which you also can describe conditions using the returned builder.
|
QueryBuilder<T> |
between(Property<T> property,
java.util.Date value1,
java.util.Date value2)
Finds objects with property value between and including the first and second value.
|
QueryBuilder<T> |
between(Property<T> property,
double value1,
double value2)
Finds objects with property value between and including the first and second value.
|
QueryBuilder<T> |
between(Property<T> property,
long value1,
long value2)
Finds objects with property value between and including the first and second value.
|
Query<T> |
build()
Builds the query and closes this QueryBuilder.
|
void |
close() |
QueryBuilder<T> |
contains(Property<T> property,
java.lang.String value)
Ignores case when matching results.
|
QueryBuilder<T> |
contains(Property<T> property,
java.lang.String value,
QueryBuilder.StringOrder order) |
QueryBuilder<T> |
eager(int limit,
RelationInfo relationInfo,
RelationInfo... more)
Like
eager(RelationInfo, RelationInfo[]) , but limits eager loading to the given count. |
QueryBuilder<T> |
eager(RelationInfo relationInfo,
RelationInfo... more)
Specifies relations that should be resolved eagerly.
|
QueryBuilder<T> |
endsWith(Property<T> property,
java.lang.String value)
Ignores case when matching results.
|
QueryBuilder<T> |
endsWith(Property<T> property,
java.lang.String value,
QueryBuilder.StringOrder order) |
QueryBuilder<T> |
equal(Property<T> property,
boolean value) |
QueryBuilder<T> |
equal(Property<T> property,
byte[] value) |
QueryBuilder<T> |
equal(Property<T> property,
java.util.Date value) |
QueryBuilder<T> |
equal(Property<T> property,
double value,
double tolerance)
Floating point equality is non-trivial; this is just a convenience for
between(Property, double, double) with parameters(property, value - tolerance, value + tolerance). |
QueryBuilder<T> |
equal(Property<T> property,
long value) |
QueryBuilder<T> |
equal(Property<T> property,
java.lang.String value)
Creates an "equal ('=')" condition for this property.
|
QueryBuilder<T> |
equal(Property<T> property,
java.lang.String value,
QueryBuilder.StringOrder order)
Creates an "equal ('=')" condition for this property.
|
QueryBuilder<T> |
filter(QueryFilter<T> filter)
Sets a filter that executes on primary query results (returned from the db core) on a Java level.
|
protected void |
finalize()
Explicitly call
close() instead to avoid expensive finalization. |
QueryBuilder<T> |
greater(Property<T> property,
byte[] value) |
QueryBuilder<T> |
greater(Property<T> property,
java.util.Date value) |
QueryBuilder<T> |
greater(Property<T> property,
double value) |
QueryBuilder<T> |
greater(Property<T> property,
long value) |
QueryBuilder<T> |
greater(Property<T> property,
java.lang.String value)
Ignores case when matching results.
|
QueryBuilder<T> |
greater(Property<T> property,
java.lang.String value,
QueryBuilder.StringOrder order) |
QueryBuilder<T> |
greaterOrEqual(Property<T> property,
byte[] value) |
QueryBuilder<T> |
greaterOrEqual(Property<T> property,
java.util.Date value) |
QueryBuilder<T> |
greaterOrEqual(Property<T> property,
double value) |
QueryBuilder<T> |
greaterOrEqual(Property<T> property,
long value) |
QueryBuilder<T> |
greaterOrEqual(Property<T> property,
java.lang.String value,
QueryBuilder.StringOrder order) |
QueryBuilder<T> |
in(Property<T> property,
int[] values) |
QueryBuilder<T> |
in(Property<T> property,
long[] values) |
QueryBuilder<T> |
in(Property<T> property,
java.lang.String[] values)
Ignores case when matching results.
|
QueryBuilder<T> |
in(Property<T> property,
java.lang.String[] values,
QueryBuilder.StringOrder order) |
QueryBuilder<T> |
isNull(Property<T> property) |
QueryBuilder<T> |
less(Property<T> property,
byte[] value) |
QueryBuilder<T> |
less(Property<T> property,
java.util.Date value) |
QueryBuilder<T> |
less(Property<T> property,
double value) |
QueryBuilder<T> |
less(Property<T> property,
long value) |
QueryBuilder<T> |
less(Property<T> property,
java.lang.String value)
Ignores case when matching results.
|
QueryBuilder<T> |
less(Property<T> property,
java.lang.String value,
QueryBuilder.StringOrder order) |
QueryBuilder<T> |
lessOrEqual(Property<T> property,
byte[] value) |
QueryBuilder<T> |
lessOrEqual(Property<T> property,
java.util.Date value) |
QueryBuilder<T> |
lessOrEqual(Property<T> property,
double value) |
QueryBuilder<T> |
lessOrEqual(Property<T> property,
long value) |
QueryBuilder<T> |
lessOrEqual(Property<T> property,
java.lang.String value,
QueryBuilder.StringOrder order) |
<TARGET> QueryBuilder<TARGET> |
link(RelationInfo<?,TARGET> relationInfo)
Creates a link to another entity, for which you also can describe conditions using the returned builder.
|
QueryBuilder<T> |
notEqual(Property<T> property,
boolean value) |
QueryBuilder<T> |
notEqual(Property<T> property,
java.util.Date value) |
QueryBuilder<T> |
notEqual(Property<T> property,
long value) |
QueryBuilder<T> |
notEqual(Property<T> property,
java.lang.String value)
Creates a "not equal ('<>')" condition for this property.
|
QueryBuilder<T> |
notEqual(Property<T> property,
java.lang.String value,
QueryBuilder.StringOrder order)
Creates a "not equal ('<>')" condition for this property.
|
QueryBuilder<T> |
notIn(Property<T> property,
int[] values) |
QueryBuilder<T> |
notIn(Property<T> property,
long[] values) |
QueryBuilder<T> |
notNull(Property<T> property) |
QueryBuilder<T> |
or()
Combines the previous condition with the following condition with a logical OR.
|
QueryBuilder<T> |
order(Property<T> property)
Specifies given property to be used for sorting.
|
QueryBuilder<T> |
order(Property<T> property,
int flags)
Defines the order with which the results are ordered (default: none).
|
QueryBuilder<T> |
orderDesc(Property<T> property)
Specifies given property in descending order to be used for sorting.
|
QueryBuilder<T> |
parameterAlias(java.lang.String alias)
Asigns the given alias to the previous condition.
|
QueryBuilder<T> |
sort(java.util.Comparator<T> comparator) |
QueryBuilder<T> |
startsWith(Property<T> property,
java.lang.String value)
Ignores case when matching results.
|
QueryBuilder<T> |
startsWith(Property<T> property,
java.lang.String value,
QueryBuilder.StringOrder order) |
public static final int DESCENDING
public static final int CASE_SENSITIVE
public static final int NULLS_LAST
public static final int NULLS_ZERO
public static final int UNSIGNED
protected void finalize() throws java.lang.Throwable
close()
instead to avoid expensive finalization.finalize
in class java.lang.Object
java.lang.Throwable
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
public QueryBuilder<T> order(Property<T> property)
order(Property, int)
with flags equal to 0.order(Property, int)
,
orderDesc(Property)
public QueryBuilder<T> orderDesc(Property<T> property)
order(Property, int)
with flags equal to DESCENDING
.order(Property, int)
,
order(Property)
public QueryBuilder<T> order(Property<T> property, int flags)
Example:
queryBuilder.order(Name).orderDesc(YearOfBirth);
Here, "Name" defines the primary sort order. The secondary sort order "YearOfBirth" is only used to compare entries with the same "Name" values.
property
- the property defining the orderflags
- Bit flags that can be combined using the binary OR operator (|). Available flags are
DESCENDING
, CASE_SENSITIVE
, NULLS_LAST
, NULLS_ZERO
,
and UNSIGNED
.order(Property)
,
orderDesc(Property)
public QueryBuilder<T> sort(java.util.Comparator<T> comparator)
public QueryBuilder<T> parameterAlias(java.lang.String alias)
alias
- The string alias for use with setParameter(s) methods.public <TARGET> QueryBuilder<TARGET> link(RelationInfo<?,TARGET> relationInfo)
Note: in relational databases you would use a "join" for this.
TARGET
- The target entity. For parent/tree like relations, it can be the same type.relationInfo
- Relation meta info (generated)public <TARGET> QueryBuilder<TARGET> backlink(RelationInfo<TARGET,?> relationInfo)
Note: only use this method over link(RelationInfo)
,
if you did not define @Backlink
in the entity already.
Note: in relational databases you would use a "join" for this.
TARGET
- The target entity. For parent/tree like relations, it can be the same type.relationInfo
- Relation meta info (generated) of the original relation (reverse direction)public QueryBuilder<T> eager(RelationInfo relationInfo, RelationInfo... more)
relationInfo
- The relation as found in the generated meta info class ("EntityName_") of class T.more
- Supply further relations to be eagerly loaded.public QueryBuilder<T> eager(int limit, RelationInfo relationInfo, @Nullable RelationInfo... more)
eager(RelationInfo, RelationInfo[])
, but limits eager loading to the given count.limit
- Count of entities to be eager loaded.relationInfo
- The relation as found in the generated meta info class ("EntityName_") of class T.more
- Supply further relations to be eagerly loaded.public QueryBuilder<T> filter(QueryFilter<T> filter)
equal(Property, String)
if
possible.
A filter requires to instantiate full Java objects beforehand, which is less efficient.
The upside of filters is that they allow any complex operation including traversing object graphs, and that filtering is executed along with the query (preferably in a background thread). Use filtering wisely ;-).
Also note, that a filter may only be used along with Query.find()
and
Query.forEach(QueryConsumer)
at this point.
Other find methods will throw a exception and aggregate functions will silently ignore the filter.
public QueryBuilder<T> or()
Example (querying t-shirts):
queryBuilder.equal(color, "blue").or().less(price, 30).build() // color is blue OR price < 30
public QueryBuilder<T> and()
However, this method change the precedence with other combinations such as or()
.
This is best explained by example.
Example (querying t-shirts):
// Case (1): OR has precedence
queryBuilder.equal(color, "blue").equal(size, "XL").or().less(price, 30).build()
// Case (2): AND has precedence
queryBuilder.equal(color, "blue").and().equal(size, "XL").or().less(price, 30).build()
Rule: Explicit AND / OR combination have precedence.
That's why (1) is evaluated like "must be blue and is either of size XL or costs less than 30", or more formally: blue AND (size XL OR price less than 30).
Rule: Conditions are applied from left to right (in the order they are called).
That's why in (2) the AND is evaluated before the OR. Thus, (2) evaluates to "either must be blue and of size XL, or costs less than 30", or, more formally: (blue AND size XL) OR price less than 30.
public QueryBuilder<T> isNull(Property<T> property)
public QueryBuilder<T> notNull(Property<T> property)
public QueryBuilder<T> equal(Property<T> property, long value)
public QueryBuilder<T> notEqual(Property<T> property, long value)
public QueryBuilder<T> less(Property<T> property, long value)
public QueryBuilder<T> lessOrEqual(Property<T> property, long value)
public QueryBuilder<T> greater(Property<T> property, long value)
public QueryBuilder<T> greaterOrEqual(Property<T> property, long value)
public QueryBuilder<T> between(Property<T> property, long value1, long value2)
public QueryBuilder<T> in(Property<T> property, long[] values)
public QueryBuilder<T> notIn(Property<T> property, long[] values)
public QueryBuilder<T> in(Property<T> property, int[] values)
public QueryBuilder<T> notIn(Property<T> property, int[] values)
public QueryBuilder<T> equal(Property<T> property, boolean value)
public QueryBuilder<T> notEqual(Property<T> property, boolean value)
public QueryBuilder<T> equal(Property<T> property, java.util.Date value)
java.lang.NullPointerException
- if given value is null. Use isNull(Property)
instead.public QueryBuilder<T> notEqual(Property<T> property, java.util.Date value)
java.lang.NullPointerException
- if given value is null. Use isNull(Property)
instead.public QueryBuilder<T> less(Property<T> property, java.util.Date value)
java.lang.NullPointerException
- if given value is null. Use isNull(Property)
instead.public QueryBuilder<T> lessOrEqual(Property<T> property, java.util.Date value)
java.lang.NullPointerException
- if given value is null. Use isNull(Property)
instead.public QueryBuilder<T> greater(Property<T> property, java.util.Date value)
java.lang.NullPointerException
- if given value is null. Use isNull(Property)
instead.public QueryBuilder<T> greaterOrEqual(Property<T> property, java.util.Date value)
java.lang.NullPointerException
- if given value is null. Use isNull(Property)
instead.public QueryBuilder<T> between(Property<T> property, java.util.Date value1, java.util.Date value2)
java.lang.NullPointerException
- if one of the given values is null.public QueryBuilder<T> equal(Property<T> property, java.lang.String value)
Ignores case when matching results, e.g. equal(prop, "example")
matches both "Example" and "example".
Use equal(prop, value, StringOrder.CASE_SENSITIVE)
to only match
if case is equal.
Note: Use a case sensitive condition to utilize an @Index
on property
, dramatically speeding up look-up of results.
public QueryBuilder<T> equal(Property<T> property, java.lang.String value, QueryBuilder.StringOrder order)
Set order
to StringOrder.CASE_SENSITIVE
to only match
if case is equal. E.g. equal(prop, "example", StringOrder.CASE_SENSITIVE)
only matches "example",
but not "Example".
Note: Use a case sensitive condition to utilize an @Index
on property
, dramatically speeding up look-up of results.
public QueryBuilder<T> notEqual(Property<T> property, java.lang.String value)
Ignores case when matching results, e.g. notEqual(prop, "example")
excludes both "Example" and "example".
Use notEqual(prop, value, StringOrder.CASE_SENSITIVE)
to only exclude
if case is equal.
Note: Use a case sensitive condition to utilize an @Index
on property
, dramatically speeding up look-up of results.
public QueryBuilder<T> notEqual(Property<T> property, java.lang.String value, QueryBuilder.StringOrder order)
Set order
to StringOrder.CASE_SENSITIVE
to only exclude
if case is equal. E.g. notEqual(prop, "example", StringOrder.CASE_SENSITIVE)
only excludes "example",
but not "Example".
Note: Use a case sensitive condition to utilize an @Index
on property
, dramatically speeding up look-up of results.
public QueryBuilder<T> contains(Property<T> property, java.lang.String value)
StringOrder.CASE_SENSITIVE
to specify that case should not be ignored.public QueryBuilder<T> startsWith(Property<T> property, java.lang.String value)
StringOrder.CASE_SENSITIVE
to specify that case should not be ignored.public QueryBuilder<T> endsWith(Property<T> property, java.lang.String value)
StringOrder.CASE_SENSITIVE
to specify that case should not be ignored.public QueryBuilder<T> contains(Property<T> property, java.lang.String value, QueryBuilder.StringOrder order)
public QueryBuilder<T> startsWith(Property<T> property, java.lang.String value, QueryBuilder.StringOrder order)
public QueryBuilder<T> endsWith(Property<T> property, java.lang.String value, QueryBuilder.StringOrder order)
public QueryBuilder<T> less(Property<T> property, java.lang.String value)
StringOrder.CASE_SENSITIVE
to specify that case should not be ignored.public QueryBuilder<T> less(Property<T> property, java.lang.String value, QueryBuilder.StringOrder order)
public QueryBuilder<T> lessOrEqual(Property<T> property, java.lang.String value, QueryBuilder.StringOrder order)
public QueryBuilder<T> greater(Property<T> property, java.lang.String value)
StringOrder.CASE_SENSITIVE
to specify that case should not be ignored.public QueryBuilder<T> greater(Property<T> property, java.lang.String value, QueryBuilder.StringOrder order)
public QueryBuilder<T> greaterOrEqual(Property<T> property, java.lang.String value, QueryBuilder.StringOrder order)
public QueryBuilder<T> in(Property<T> property, java.lang.String[] values)
StringOrder.CASE_SENSITIVE
to specify that case should not be ignored.public QueryBuilder<T> in(Property<T> property, java.lang.String[] values, QueryBuilder.StringOrder order)
public QueryBuilder<T> equal(Property<T> property, double value, double tolerance)
between(Property, double, double)
with parameters(property, value - tolerance, value + tolerance).
When using Query.setParameters(Property, double, double)
,
consider that the params are the lower and upper bounds.public QueryBuilder<T> less(Property<T> property, double value)
public QueryBuilder<T> lessOrEqual(Property<T> property, double value)
public QueryBuilder<T> greater(Property<T> property, double value)
public QueryBuilder<T> greaterOrEqual(Property<T> property, double value)
public QueryBuilder<T> between(Property<T> property, double value1, double value2)
public QueryBuilder<T> equal(Property<T> property, byte[] value)
public QueryBuilder<T> less(Property<T> property, byte[] value)
public QueryBuilder<T> lessOrEqual(Property<T> property, byte[] value)
public QueryBuilder<T> greater(Property<T> property, byte[] value)
public QueryBuilder<T> greaterOrEqual(Property<T> property, byte[] value)