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;
}
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();
}
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;
}
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;
}
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());
}
Aggregations