Search in sources :

Example 1 with UnaryFilter

use of com.google.firestore.v1.StructuredQuery.UnaryFilter in project java-firestore by googleapis.

the class Query method buildWithoutClientTranslation.

private StructuredQuery.Builder buildWithoutClientTranslation() {
    StructuredQuery.Builder structuredQuery = StructuredQuery.newBuilder();
    CollectionSelector.Builder collectionSelector = CollectionSelector.newBuilder();
    // Kindless queries select all descendant documents, so we don't add the collectionId field.
    if (!options.isKindless()) {
        collectionSelector.setCollectionId(options.getCollectionId());
    }
    collectionSelector.setAllDescendants(options.getAllDescendants());
    structuredQuery.addFrom(collectionSelector);
    if (options.getFieldFilters().size() == 1) {
        Filter filter = options.getFieldFilters().get(0).toProto();
        if (filter.hasFieldFilter()) {
            structuredQuery.getWhereBuilder().setFieldFilter(filter.getFieldFilter());
        } else {
            Preconditions.checkState(filter.hasUnaryFilter(), "Expected a UnaryFilter or a FieldFilter.");
            structuredQuery.getWhereBuilder().setUnaryFilter(filter.getUnaryFilter());
        }
    } else if (options.getFieldFilters().size() > 1) {
        Filter.Builder filter = Filter.newBuilder();
        StructuredQuery.CompositeFilter.Builder compositeFilter = StructuredQuery.CompositeFilter.newBuilder();
        compositeFilter.setOp(CompositeFilter.Operator.AND);
        for (FieldFilter fieldFilter : options.getFieldFilters()) {
            compositeFilter.addFilters(fieldFilter.toProto());
        }
        filter.setCompositeFilter(compositeFilter.build());
        structuredQuery.setWhere(filter.build());
    }
    if (!options.getFieldOrders().isEmpty()) {
        for (FieldOrder order : options.getFieldOrders()) {
            structuredQuery.addOrderBy(order.toProto());
        }
    } else if (LimitType.Last.equals(options.getLimitType())) {
        throw new IllegalStateException("limitToLast() queries require specifying at least one orderBy() clause.");
    }
    if (!options.getFieldProjections().isEmpty()) {
        structuredQuery.getSelectBuilder().addAllFields(options.getFieldProjections());
    }
    if (options.getLimit() != null) {
        structuredQuery.setLimit(Int32Value.newBuilder().setValue(options.getLimit()));
    }
    if (options.getOffset() != null) {
        structuredQuery.setOffset(options.getOffset());
    }
    if (options.getStartCursor() != null) {
        structuredQuery.setStartAt(options.getStartCursor());
    }
    if (options.getEndCursor() != null) {
        structuredQuery.setEndAt(options.getEndCursor());
    }
    return structuredQuery;
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) CollectionSelector(com.google.firestore.v1.StructuredQuery.CollectionSelector) CompositeFilter(com.google.firestore.v1.StructuredQuery.CompositeFilter) Filter(com.google.firestore.v1.StructuredQuery.Filter) Builder(com.google.cloud.firestore.Query.QueryOptions.Builder)

Example 2 with UnaryFilter

use of com.google.firestore.v1.StructuredQuery.UnaryFilter in project java-firestore by googleapis.

the class QueryTest method withFilter.

@Test
public void withFilter() throws Exception {
    doAnswer(queryResponse()).when(firestoreMock).streamRequest(runQuery.capture(), streamObserverCapture.capture(), Matchers.<ServerStreamingCallable>any());
    query.whereEqualTo("foo", "bar").get().get();
    query.whereEqualTo("foo", null).get().get();
    query.whereEqualTo("foo", Double.NaN).get().get();
    query.whereEqualTo("foo", Float.NaN).get().get();
    query.whereNotEqualTo("foo", "bar").get().get();
    query.whereNotEqualTo("foo", null).get().get();
    query.whereNotEqualTo("foo", Double.NaN).get().get();
    query.whereNotEqualTo("foo", Float.NaN).get().get();
    query.whereGreaterThan("foo", "bar").get().get();
    query.whereGreaterThanOrEqualTo("foo", "bar").get().get();
    query.whereLessThan("foo", "bar").get().get();
    query.whereLessThanOrEqualTo("foo", "bar").get().get();
    query.whereArrayContains("foo", "bar").get().get();
    query.whereIn("foo", Collections.<Object>singletonList("bar"));
    query.whereArrayContainsAny("foo", Collections.<Object>singletonList("bar"));
    query.whereNotIn("foo", Collections.<Object>singletonList("bar"));
    Iterator<RunQueryRequest> expected = Arrays.asList(query(filter(StructuredQuery.FieldFilter.Operator.EQUAL)), query(unaryFilter(StructuredQuery.UnaryFilter.Operator.IS_NULL)), query(unaryFilter(StructuredQuery.UnaryFilter.Operator.IS_NAN)), query(unaryFilter(StructuredQuery.UnaryFilter.Operator.IS_NAN)), query(filter(StructuredQuery.FieldFilter.Operator.NOT_EQUAL)), query(unaryFilter(StructuredQuery.UnaryFilter.Operator.IS_NOT_NULL)), query(unaryFilter(StructuredQuery.UnaryFilter.Operator.IS_NOT_NAN)), query(unaryFilter(StructuredQuery.UnaryFilter.Operator.IS_NOT_NAN)), query(filter(StructuredQuery.FieldFilter.Operator.GREATER_THAN)), query(filter(StructuredQuery.FieldFilter.Operator.GREATER_THAN_OR_EQUAL)), query(filter(StructuredQuery.FieldFilter.Operator.LESS_THAN)), query(filter(StructuredQuery.FieldFilter.Operator.LESS_THAN_OR_EQUAL)), query(filter(StructuredQuery.FieldFilter.Operator.ARRAY_CONTAINS)), query(filter(StructuredQuery.FieldFilter.Operator.IN)), query(filter(StructuredQuery.FieldFilter.Operator.ARRAY_CONTAINS_ANY)), query(filter(StructuredQuery.FieldFilter.Operator.NOT_IN))).iterator();
    for (RunQueryRequest actual : runQuery.getAllValues()) {
        assertEquals(expected.next(), actual);
    }
}
Also used : RunQueryRequest(com.google.firestore.v1.RunQueryRequest) Test(org.junit.Test)

Example 3 with UnaryFilter

use of com.google.firestore.v1.StructuredQuery.UnaryFilter 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 4 with UnaryFilter

use of com.google.firestore.v1.StructuredQuery.UnaryFilter 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)

Example 5 with UnaryFilter

use of com.google.firestore.v1.StructuredQuery.UnaryFilter in project java-firestore by googleapis.

the class LocalFirestoreHelper method query.

public static RunQueryRequest query(@Nullable String transactionId, String parent, boolean allDescendants, boolean kindless, StructuredQuery... query) {
    RunQueryRequest.Builder request = RunQueryRequest.newBuilder();
    if (!parent.equals("")) {
        parent = '/' + parent;
    }
    request.setParent(LocalFirestoreHelper.DATABASE_NAME + "/documents" + parent);
    StructuredQuery.Builder structuredQuery = request.getStructuredQueryBuilder();
    CollectionSelector.Builder builder = CollectionSelector.newBuilder().setAllDescendants(allDescendants);
    if (!kindless) {
        builder.setCollectionId("coll");
    }
    structuredQuery.addFrom(builder);
    for (StructuredQuery option : query) {
        structuredQuery.mergeFrom(option);
    }
    CompositeFilter compositeFilter = structuredQuery.getWhere().getCompositeFilter();
    if (compositeFilter.getFiltersCount() == 1) {
        if (compositeFilter.getFilters(0).hasFieldFilter()) {
            FieldFilter fieldFilter = compositeFilter.getFilters(0).getFieldFilter();
            structuredQuery.getWhereBuilder().setFieldFilter(fieldFilter);
        } else {
            UnaryFilter unaryFilter = compositeFilter.getFilters(0).getUnaryFilter();
            structuredQuery.getWhereBuilder().setUnaryFilter(unaryFilter);
        }
    }
    if (transactionId != null) {
        request.setTransaction(ByteString.copyFromUtf8(transactionId));
    }
    return request.build();
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) FieldFilter(com.google.firestore.v1.StructuredQuery.FieldFilter) CompositeFilter(com.google.firestore.v1.StructuredQuery.CompositeFilter) CollectionSelector(com.google.firestore.v1.StructuredQuery.CollectionSelector) UnaryFilter(com.google.firestore.v1.StructuredQuery.UnaryFilter) RunQueryRequest(com.google.firestore.v1.RunQueryRequest)

Aggregations

StructuredQuery (com.google.firestore.v1.StructuredQuery)4 Builder (com.google.cloud.firestore.Query.QueryOptions.Builder)3 BundledQuery (com.google.firestore.bundle.BundledQuery)2 RunQueryRequest (com.google.firestore.v1.RunQueryRequest)2 CollectionSelector (com.google.firestore.v1.StructuredQuery.CollectionSelector)2 CompositeFilter (com.google.firestore.v1.StructuredQuery.CompositeFilter)2 Nonnull (javax.annotation.Nonnull)2 FieldFilter (com.google.firestore.v1.StructuredQuery.FieldFilter)1 Filter (com.google.firestore.v1.StructuredQuery.Filter)1 UnaryFilter (com.google.firestore.v1.StructuredQuery.UnaryFilter)1 Test (org.junit.Test)1