Queries
You can query the ObjectBox C / C++ database for objects by specifying criteria with the Query builder. It is easy, learn how to do it here.
ObjectBox queries return persisted objects that match user-defined criteria. You use QueryBuilder to specify criteria and create a Query which actually executes the query and returns matching objects.
Building queries
The QueryBuilder<T>/OBX_query_builder
class lets you build custom queries for your entities. Create an instance via box.query()
(C++) or obx_query_builder()
(C).
The generated code contains entity and property meta-information in an enum
(C) or a struct
(C++). These provide a way to define query conditions safely, without literal entity and property IDs spread throughout the code. Let's have a look at a fragment of the generated code for a User
entity (its unique Entity ID is 6
and it has three properties: id
, name
surname
) and the examples below.
Here's how we could query for all users with the first name “Joe” (case insensitive) and their surname starting with a capital "O". First, we initialize the QueryBuilder, next we add one or more conditions (combined with AND operator by default), then we build
the query and finally execute it using find
.
Reusing queries and parameters
If you frequently run a Query
you should cache the Query
object and re-use it. To make a Query
more reusable you can change the values, or query parameters, of each condition you added even after the Query
is built. Let's see how.
Assume we want to find a list of User
with specific name
values. First, we build a regular Query
with an equal
condition for name
. Because we have to pass an initial parameter value to equal()
but plan to override it before running the Query
later, we just pass an empty string:
Now at some later point, we want to actually run the Query
. To set a value for the name
parameter on the Query
and pass the name
property and the new parameter value:
Aliases
So you might already be wondering, what happens if you have more than one condition using the same property? For this purpose, you can assign each condition an alias by calling Alias()
right after specifying the condition:
Then you'll pass the alias when setting a new parameter value:
Limit, Offset, and Pagination
Sometimes you only need a subset of a query, for example, the first 10 elements. This is especially helpful (and frugal) when you have a high number of entities and you cannot limit the result using query conditions only. The built query has offset
and limit
methods to help you do that.
Reading a single property
If you only want to return the values of certain property and not a list of full objects you can use a PropertyQuery:
Note: the returned array of property values is not in any particular order, even if you did specify an order when building the query.
Handling null values
By default, null values are not returned (they're skipped). However, you can specify a replacement value to return if a property is null as the second argument to obx_query_prop_*_find()
.
Other query features
There are many more features, such as property aggregate functions, distinct, removal of all data matching a query. Be sure to check out the objectbox.h
and objectbox.hpp
or API docs to discover more.
Last updated