Search in sources :

Example 6 with CompositeFilter

use of com.google.firestore.v1.StructuredQuery.CompositeFilter 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)5 CompositeFilter (com.google.firestore.v1.StructuredQuery.CompositeFilter)4 Builder (com.google.cloud.firestore.Query.QueryOptions.Builder)2 CollectionSelector (com.google.firestore.v1.StructuredQuery.CollectionSelector)2 FieldReference (com.google.firestore.v1.StructuredQuery.FieldReference)2 Filter (com.google.firestore.v1.StructuredQuery.Filter)2 UnaryFilter (com.google.firestore.v1.StructuredQuery.UnaryFilter)2 Iterator (java.util.Iterator)2 VisibleForTesting (androidx.annotation.VisibleForTesting)1 FirestoreDataException (com.google.cloud.spring.data.firestore.FirestoreDataException)1 ImmutableList (com.google.common.collect.ImmutableList)1 FieldFilter (com.google.firebase.firestore.core.FieldFilter)1 Filter (com.google.firebase.firestore.core.Filter)1 BundledQuery (com.google.firestore.bundle.BundledQuery)1 RunQueryRequest (com.google.firestore.v1.RunQueryRequest)1 FieldFilter (com.google.firestore.v1.StructuredQuery.FieldFilter)1 Order (com.google.firestore.v1.StructuredQuery.Order)1 ArrayList (java.util.ArrayList)1 FirestoreDataException (org.springframework.cloud.gcp.data.firestore.FirestoreDataException)1