Search in sources :

Example 6 with Builder

use of com.google.cloud.firestore.Query.QueryOptions.Builder in project java-firestore by googleapis.

the class Query method select.

/**
 * Creates and returns a new Query instance that applies a field mask to the result and returns
 * the specified subset of fields. You can specify a list of field paths to return, or use an
 * empty list to only return the references of matching documents.
 *
 * @param fieldPaths The field paths to include.
 * @return The created Query.
 */
@Nonnull
public Query select(FieldPath... fieldPaths) {
    ImmutableList.Builder<FieldReference> fieldProjections = ImmutableList.builder();
    if (fieldPaths.length == 0) {
        fieldPaths = new FieldPath[] { FieldPath.DOCUMENT_ID };
    }
    for (FieldPath path : fieldPaths) {
        FieldReference fieldReference = FieldReference.newBuilder().setFieldPath(path.getEncodedPath()).build();
        fieldProjections.add(fieldReference);
    }
    Builder newOptions = options.toBuilder().setFieldProjections(fieldProjections.build());
    return new Query(rpcContext, newOptions.build());
}
Also used : FieldReference(com.google.firestore.v1.StructuredQuery.FieldReference) StructuredQuery(com.google.firestore.v1.StructuredQuery) BundledQuery(com.google.firestore.bundle.BundledQuery) ImmutableList(com.google.common.collect.ImmutableList) Builder(com.google.cloud.firestore.Query.QueryOptions.Builder) Nonnull(javax.annotation.Nonnull)

Example 7 with Builder

use of com.google.cloud.firestore.Query.QueryOptions.Builder in project java-firestore by googleapis.

the class Query method endBefore.

/**
 * Creates and returns a new Query that ends before the provided fields relative to the order of
 * the query. The order of the field values must match the order of the order by clauses of the
 * query.
 *
 * @param fieldValues The field values to end this query before, in order of the query's order by.
 * @return The created Query.
 */
@Nonnull
public Query endBefore(Object... fieldValues) {
    ImmutableList<FieldOrder> fieldOrders = fieldValues.length == 1 && fieldValues[0] instanceof DocumentReference ? createImplicitOrderBy() : options.getFieldOrders();
    Cursor cursor = createCursor(fieldOrders, fieldValues, true);
    Builder newOptions = options.toBuilder();
    newOptions.setFieldOrders(fieldOrders);
    newOptions.setEndCursor(cursor);
    return new Query(rpcContext, newOptions.build());
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) BundledQuery(com.google.firestore.bundle.BundledQuery) Builder(com.google.cloud.firestore.Query.QueryOptions.Builder) Cursor(com.google.firestore.v1.Cursor) Nonnull(javax.annotation.Nonnull)

Example 8 with Builder

use of com.google.cloud.firestore.Query.QueryOptions.Builder in project java-firestore by googleapis.

the class Query method whereEqualTo.

/**
 * Creates and returns a new Query with the additional filter that documents must contain the
 * specified field and the value should be equal to the specified value.
 *
 * @param fieldPath The path of the field to compare.
 * @param value The value for comparison.
 * @return The created Query.
 */
@Nonnull
public Query whereEqualTo(@Nonnull FieldPath fieldPath, @Nullable Object value) {
    Preconditions.checkState(options.getStartCursor() == null && options.getEndCursor() == null, "Cannot call whereEqualTo() after defining a boundary with startAt(), " + "startAfter(), endBefore() or endAt().");
    if (isUnaryComparison(value)) {
        Builder newOptions = options.toBuilder();
        StructuredQuery.UnaryFilter.Operator op = value == null ? StructuredQuery.UnaryFilter.Operator.IS_NULL : StructuredQuery.UnaryFilter.Operator.IS_NAN;
        UnaryFilter newFieldFilter = new UnaryFilter(fieldPath.toProto(), op);
        newOptions.setFieldFilters(append(options.getFieldFilters(), newFieldFilter));
        return new Query(rpcContext, newOptions.build());
    } else {
        return whereHelper(fieldPath, EQUAL, value);
    }
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) BundledQuery(com.google.firestore.bundle.BundledQuery) Builder(com.google.cloud.firestore.Query.QueryOptions.Builder) Nonnull(javax.annotation.Nonnull)

Example 9 with Builder

use of com.google.cloud.firestore.Query.QueryOptions.Builder in project java-firestore by googleapis.

the class Query method startAt.

/**
 * Creates and returns a new Query that starts at the provided fields relative to the order of the
 * query. The order of the field values must match the order of the order by clauses of the query.
 *
 * @param fieldValues The field values to start this query at, in order of the query's order by.
 * @return The created Query.
 */
@Nonnull
public Query startAt(Object... fieldValues) {
    ImmutableList<FieldOrder> fieldOrders = fieldValues.length == 1 && fieldValues[0] instanceof DocumentReference ? createImplicitOrderBy() : options.getFieldOrders();
    Cursor cursor = createCursor(fieldOrders, fieldValues, true);
    Builder newOptions = options.toBuilder();
    newOptions.setFieldOrders(fieldOrders);
    newOptions.setStartCursor(cursor);
    return new Query(rpcContext, newOptions.build());
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) BundledQuery(com.google.firestore.bundle.BundledQuery) Builder(com.google.cloud.firestore.Query.QueryOptions.Builder) Cursor(com.google.firestore.v1.Cursor) Nonnull(javax.annotation.Nonnull)

Example 10 with Builder

use of com.google.cloud.firestore.Query.QueryOptions.Builder in project java-firestore by googleapis.

the class Query method whereNotEqualTo.

/**
 * Creates and returns a new Query with the additional filter that documents must contain the
 * specified field and the value does not equal the specified value.
 *
 * @param fieldPath The path of the field to compare.
 * @param value The value for comparison.
 * @return The created Query.
 */
@Nonnull
public Query whereNotEqualTo(@Nonnull FieldPath fieldPath, @Nullable Object value) {
    Preconditions.checkState(options.getStartCursor() == null && options.getEndCursor() == null, "Cannot call whereNotEqualTo() after defining a boundary with startAt(), " + "startAfter(), endBefore() or endAt().");
    if (isUnaryComparison(value)) {
        Builder newOptions = options.toBuilder();
        StructuredQuery.UnaryFilter.Operator op = value == null ? StructuredQuery.UnaryFilter.Operator.IS_NOT_NULL : StructuredQuery.UnaryFilter.Operator.IS_NOT_NAN;
        UnaryFilter newFieldFilter = new UnaryFilter(fieldPath.toProto(), op);
        newOptions.setFieldFilters(append(options.getFieldFilters(), newFieldFilter));
        return new Query(rpcContext, newOptions.build());
    } else {
        return whereHelper(fieldPath, NOT_EQUAL, value);
    }
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) BundledQuery(com.google.firestore.bundle.BundledQuery) Builder(com.google.cloud.firestore.Query.QueryOptions.Builder) Nonnull(javax.annotation.Nonnull)

Aggregations

Builder (com.google.cloud.firestore.Query.QueryOptions.Builder)13 BundledQuery (com.google.firestore.bundle.BundledQuery)13 StructuredQuery (com.google.firestore.v1.StructuredQuery)13 Nonnull (javax.annotation.Nonnull)11 Cursor (com.google.firestore.v1.Cursor)8 ImmutableList (com.google.common.collect.ImmutableList)2 FieldReference (com.google.firestore.v1.StructuredQuery.FieldReference)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1