Search in sources :

Example 1 with CompositeFilter

use of com.google.firestore.v1.StructuredQuery.CompositeFilter in project spring-cloud-gcp by spring-cloud.

the class PartTreeFirestoreQuery method createBuilderWithFilter.

private StructuredQuery.Builder createBuilderWithFilter(Object[] parameters) {
    StructuredQuery.Builder builder = StructuredQuery.newBuilder();
    Iterator it = Arrays.asList(parameters).iterator();
    StructuredQuery.CompositeFilter.Builder compositeFilter = StructuredQuery.CompositeFilter.newBuilder();
    compositeFilter.setOp(StructuredQuery.CompositeFilter.Operator.AND);
    this.tree.getParts().forEach(part -> {
        StructuredQuery.FieldReference fieldReference = StructuredQuery.FieldReference.newBuilder().setFieldPath(buildName(part)).build();
        StructuredQuery.Filter.Builder filter = StructuredQuery.Filter.newBuilder();
        if (part.getType() == Part.Type.IS_NULL) {
            filter.getUnaryFilterBuilder().setField(fieldReference).setOp(StructuredQuery.UnaryFilter.Operator.IS_NULL);
        } else {
            if (!it.hasNext()) {
                throw new FirestoreDataException("Too few parameters are provided for query method: " + getQueryMethod().getName());
            }
            Object value = it.next();
            filter.getFieldFilterBuilder().setField(fieldReference).setOp(getOperator(part, value)).setValue(this.classMapper.toFirestoreValue(value));
        }
        compositeFilter.addFilters(filter.build());
    });
    builder.setWhere(StructuredQuery.Filter.newBuilder().setCompositeFilter(compositeFilter.build()));
    return builder;
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) Iterator(java.util.Iterator) FirestoreDataException(org.springframework.cloud.gcp.data.firestore.FirestoreDataException) FieldReference(com.google.firestore.v1.StructuredQuery.FieldReference)

Example 2 with CompositeFilter

use of com.google.firestore.v1.StructuredQuery.CompositeFilter in project firebase-android-sdk by firebase.

the class RemoteSerializer method encodeCompositeFilter.

@VisibleForTesting
StructuredQuery.Filter encodeCompositeFilter(com.google.firebase.firestore.core.CompositeFilter compositeFilter) {
    List<StructuredQuery.Filter> protos = new ArrayList<>(compositeFilter.getFilters().size());
    for (Filter filter : compositeFilter.getFilters()) {
        protos.add(encodeFilter(filter));
    }
    // If there's only one filter in the composite filter, use it directly.
    if (protos.size() == 1) {
        return protos.get(0);
    }
    CompositeFilter.Builder composite = CompositeFilter.newBuilder();
    composite.setOp(compositeFilter.getOperator());
    composite.addAllFilters(protos);
    return StructuredQuery.Filter.newBuilder().setCompositeFilter(composite).build();
}
Also used : CompositeFilter(com.google.firestore.v1.StructuredQuery.CompositeFilter) FieldFilter(com.google.firebase.firestore.core.FieldFilter) CompositeFilter(com.google.firestore.v1.StructuredQuery.CompositeFilter) UnaryFilter(com.google.firestore.v1.StructuredQuery.UnaryFilter) Filter(com.google.firebase.firestore.core.Filter) ArrayList(java.util.ArrayList) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 3 with CompositeFilter

use of com.google.firestore.v1.StructuredQuery.CompositeFilter in project spring-cloud-gcp by GoogleCloudPlatform.

the class PartTreeFirestoreQuery method createBuilderWithFilter.

private StructuredQuery.Builder createBuilderWithFilter(Object[] parameters) {
    StructuredQuery.Builder builder = StructuredQuery.newBuilder();
    Iterator it = Arrays.asList(parameters).iterator();
    StructuredQuery.CompositeFilter.Builder compositeFilter = StructuredQuery.CompositeFilter.newBuilder();
    compositeFilter.setOp(StructuredQuery.CompositeFilter.Operator.AND);
    this.tree.getParts().forEach(part -> {
        StructuredQuery.FieldReference fieldReference = StructuredQuery.FieldReference.newBuilder().setFieldPath(buildName(part)).build();
        StructuredQuery.Filter.Builder filter = StructuredQuery.Filter.newBuilder();
        if (part.getType() == Part.Type.IS_NULL) {
            filter.getUnaryFilterBuilder().setField(fieldReference).setOp(StructuredQuery.UnaryFilter.Operator.IS_NULL);
        } else {
            if (!it.hasNext()) {
                throw new FirestoreDataException("Too few parameters are provided for query method: " + getQueryMethod().getName());
            }
            Object value = it.next();
            StructuredQuery.FieldFilter.Builder fieldFilterBuilder = filter.getFieldFilterBuilder().setField(fieldReference).setOp(getOperator(part, value));
            if (fieldReference.getFieldPath().equals(NAME_FIELD)) {
                // In Firestore, a field that is used as a document ID can only be of String type
                fieldFilterBuilder.setValue(Value.newBuilder().setReferenceValue(this.reactiveOperations.buildResourceName(persistentEntity, (String) value)));
            } else {
                fieldFilterBuilder.setValue(this.classMapper.toFirestoreValue(value));
            }
        }
        compositeFilter.addFilters(filter.build());
    });
    builder.setWhere(StructuredQuery.Filter.newBuilder().setCompositeFilter(compositeFilter.build()));
    return builder;
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) Iterator(java.util.Iterator) FirestoreDataException(com.google.cloud.spring.data.firestore.FirestoreDataException) FieldReference(com.google.firestore.v1.StructuredQuery.FieldReference)

Example 4 with CompositeFilter

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

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

the class Query method fromProto.

private static Query fromProto(FirestoreRpcContext<?> rpcContext, RunQueryRequest proto) {
    QueryOptions.Builder queryOptions = QueryOptions.builder();
    StructuredQuery structuredQuery = proto.getStructuredQuery();
    ResourcePath parentPath = ResourcePath.create(proto.getParent());
    if (!rpcContext.getDatabaseName().equals(parentPath.getDatabaseName().toString())) {
        throw new IllegalArgumentException(String.format("Cannot deserialize query from different Firestore project (\"%s\" vs \"%s\")", rpcContext.getDatabaseName(), parentPath.getDatabaseName()));
    }
    queryOptions.setParentPath(parentPath);
    Preconditions.checkArgument(structuredQuery.getFromCount() == 1, "Can only deserialize query with exactly one collection selector.");
    queryOptions.setCollectionId(structuredQuery.getFrom(0).getCollectionId());
    queryOptions.setAllDescendants(structuredQuery.getFrom(0).getAllDescendants());
    if (structuredQuery.hasWhere()) {
        Filter where = structuredQuery.getWhere();
        if (where.hasCompositeFilter()) {
            CompositeFilter compositeFilter = where.getCompositeFilter();
            ImmutableList.Builder<FieldFilter> fieldFilters = ImmutableList.builder();
            for (Filter filter : compositeFilter.getFiltersList()) {
                fieldFilters.add(FieldFilter.fromProto(filter));
            }
            queryOptions.setFieldFilters(fieldFilters.build());
        } else {
            queryOptions.setFieldFilters(ImmutableList.of(FieldFilter.fromProto(where)));
        }
    }
    ImmutableList.Builder<FieldOrder> fieldOrders = ImmutableList.builder();
    for (Order order : structuredQuery.getOrderByList()) {
        fieldOrders.add(new FieldOrder(order.getField(), Direction.valueOf(order.getDirection().name())));
    }
    queryOptions.setFieldOrders(fieldOrders.build());
    if (structuredQuery.hasLimit()) {
        queryOptions.setLimit(structuredQuery.getLimit().getValue());
    }
    if (structuredQuery.getOffset() != 0) {
        queryOptions.setOffset(structuredQuery.getOffset());
    }
    if (structuredQuery.hasSelect()) {
        queryOptions.setFieldProjections(ImmutableList.copyOf(structuredQuery.getSelect().getFieldsList()));
    }
    if (structuredQuery.hasStartAt()) {
        queryOptions.setStartCursor(structuredQuery.getStartAt());
    }
    if (structuredQuery.hasEndAt()) {
        queryOptions.setEndCursor(structuredQuery.getEndAt());
    }
    return new Query(rpcContext, queryOptions.build());
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) Order(com.google.firestore.v1.StructuredQuery.Order) CompositeFilter(com.google.firestore.v1.StructuredQuery.CompositeFilter) StructuredQuery(com.google.firestore.v1.StructuredQuery) BundledQuery(com.google.firestore.bundle.BundledQuery) ImmutableList(com.google.common.collect.ImmutableList) CompositeFilter(com.google.firestore.v1.StructuredQuery.CompositeFilter) Filter(com.google.firestore.v1.StructuredQuery.Filter) Builder(com.google.cloud.firestore.Query.QueryOptions.Builder)

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