Search in sources :

Example 11 with Builder

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

the class Query method whereHelper.

private Query whereHelper(FieldPath fieldPath, StructuredQuery.FieldFilter.Operator operator, Object value) {
    Preconditions.checkArgument(!isUnaryComparison(value), "Cannot use '%s' in field comparison. Use an equality filter instead.", value);
    if (fieldPath.equals(FieldPath.DOCUMENT_ID)) {
        if (operator == ARRAY_CONTAINS || operator == ARRAY_CONTAINS_ANY) {
            throw new IllegalArgumentException(String.format("Invalid query. You cannot perform '%s' queries on FieldPath.documentId().", operator.toString()));
        } else if (operator == IN | operator == NOT_IN) {
            if (!(value instanceof List) || ((List<?>) value).isEmpty()) {
                throw new IllegalArgumentException(String.format("Invalid Query. A non-empty array is required for '%s' filters.", operator.toString()));
            }
            List<Object> referenceList = new ArrayList<>();
            for (Object arrayValue : (List<Object>) value) {
                Object convertedValue = this.convertReference(arrayValue);
                referenceList.add(convertedValue);
            }
            value = referenceList;
        } else {
            value = this.convertReference(value);
        }
    }
    Builder newOptions = options.toBuilder();
    ComparisonFilter newFieldFilter = new ComparisonFilter(fieldPath.toProto(), operator, encodeValue(fieldPath, value));
    newOptions.setFieldFilters(append(options.getFieldFilters(), newFieldFilter));
    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) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList)

Example 12 with Builder

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

the class Query method orderBy.

/**
 * Creates and returns a new Query that's additionally sorted by the specified field, optionally
 * in descending order instead of ascending.
 *
 * @param fieldPath The field to sort by.
 * @param direction The direction to sort.
 * @return The created Query.
 */
@Nonnull
public Query orderBy(@Nonnull FieldPath fieldPath, @Nonnull Direction direction) {
    Preconditions.checkState(options.getStartCursor() == null && options.getEndCursor() == null, "Cannot specify an orderBy() constraint after calling startAt(), " + "startAfter(), endBefore() or endAt().");
    Builder newOptions = options.toBuilder();
    FieldOrder newFieldOrder = new FieldOrder(fieldPath.toProto(), direction);
    newOptions.setFieldOrders(append(options.getFieldOrders(), newFieldOrder));
    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) Nonnull(javax.annotation.Nonnull)

Example 13 with Builder

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

the class Query method startAfter.

/**
 * Creates and returns a new Query that starts after 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 after, in order of the query's order
 *     by.
 * @return The created Query.
 */
public Query startAfter(Object... fieldValues) {
    ImmutableList<FieldOrder> fieldOrders = fieldValues.length == 1 && fieldValues[0] instanceof DocumentReference ? createImplicitOrderBy() : options.getFieldOrders();
    Cursor cursor = createCursor(fieldOrders, fieldValues, false);
    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)

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