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