Overview of Dari queryingΒΆ

The Query API provides several methods for specifying the types of objects to be retrieved, for filtering results, and for executing actions on retrieved objects. A typical Dari query is expressed as a chain of methods, as shown in this syntax diagram. The API methods correspond to SQL clauses as described in the table that follows.

Query.from(<class>)
     .where(<predicate>)
     .and | .or | .not (<predicate>)
     .sortAscending | .sortDescending
     .selectAll | .first()
     /* OR */
     .select(<range>)
     /* OR */
     .groupBy<fields>
Query Clause Description
from<class>

Specifies the class type on which to search for instances. The following query creates a Query object that retrieves all Article objects:

Query<Article> query = Query.from(Article.class);

There are several from-related methods that specify the object type to search for. See FROM.

where<predicate>

Specifies predicates to filter search results. You can express conditions with comparison, compound, and other operators. The following query retrieves an Article object with the specified headline:

Query<Article> query = Query.from(Article.class).where(headline = "Marshmallow Dreams");

Alternatively, you can express conditions using predicate objects instead of string operators. To express predicates with object operands, you can use bind variables and object references to simplify your query constructs.

and<predicate>

Represents logical conjunction (AND) operation. The following query retrieves articles for which the headline is I flossed today and the subheadline is Was it necessary:

Query<Article> query = Query.from(Article.class).where(headline = "I flossed today").and(subheadline = "Was it necessary");

or<predicate>

Represents logical disjunction (OR) operation. The following query retrieves articles for which the headline is either I flossed today or I flossed yesterday:

Query<Article> query = Query.from(Article.class).where(headline = "I flossed today").or(headline = "I flossed yesterday");

not<predicate>

Represents logical negation (NOT) operation. The following query retrieves articles for which the headline is I flossed today and the subheadline is not But not yesterday:

Query<Article> query = Query.from(Article.class).where(headline = "I flossed today").not(subheadline = "But not yesterday");

sortAscending<field>

Sorts results by field in ascending order. The following query sorts Author objects in ascending alphabetical order by last name:

List<Author> authors = Query.from(Author.class).sortAscending("lastName");

sortDescending<field>

Sorts results by field in descending order. The following query sorts Author objects in descending alphabetical order by last name:

List<Author> authors = Query.from(Author.class).sortDescending("lastName");

selectAll()

Selects all objects. See FROM. The following query selects all articles:

List<Article> articles = Query.from(Article.class).selectAll();

first()

Selects the first object. See FROM. The following query selects the first article:

Article article = Query.from(Article.class).first();

select<range>

Specifies the number of instances to return in a paginated result. The following query limits the number of articles returned at any one time to 100, referenced in a PaginatedResult object.

PaginatedResult<Article> results = Query.from(Article.class).select(0, 99);

Subsequent calls can be made on PaginatedResult to return additional batches of articles.

groupBy<fields>

Groups retrieved objects by field, with objects with the same field value placed into the same grouping. The following query groups the articles returned by author field, with articles by the same author grouped together:

List<Grouping<Article>> groupings = Query.from(Article.class).groupBy("author");

The Query API includes methods to retrieve objects as reference-only. For more information, see Advanced.