Skip to content

WIP: OR Queries #3185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
27 changes: 27 additions & 0 deletions firebase-firestore/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,32 @@ package com.google.firebase.firestore {
method @NonNull public static com.google.firebase.firestore.FieldValue serverTimestamp();
}

public abstract class Filter {
ctor public Filter();
method @NonNull public static com.google.firebase.firestore.Filter and(com.google.firebase.firestore.Filter...);
method @NonNull public static com.google.firebase.firestore.Filter arrayContains(@NonNull String, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter arrayContains(@NonNull com.google.firebase.firestore.FieldPath, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter arrayContainsAny(@NonNull String, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter arrayContainsAny(@NonNull com.google.firebase.firestore.FieldPath, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter equalTo(@NonNull String, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter equalTo(@NonNull com.google.firebase.firestore.FieldPath, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter greaterThan(@NonNull String, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter greaterThan(@NonNull com.google.firebase.firestore.FieldPath, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter greaterThanOrEqualTo(@NonNull String, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter greaterThanOrEqualTo(@NonNull com.google.firebase.firestore.FieldPath, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter inList(@NonNull String, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter inList(@NonNull com.google.firebase.firestore.FieldPath, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter lessThan(@NonNull String, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter lessThan(@NonNull com.google.firebase.firestore.FieldPath, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter lessThanOrEqualTo(@NonNull String, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter lessThanOrEqualTo(@NonNull com.google.firebase.firestore.FieldPath, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter notEqualTo(@NonNull String, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter notEqualTo(@NonNull com.google.firebase.firestore.FieldPath, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter notInList(@NonNull String, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter notInList(@NonNull com.google.firebase.firestore.FieldPath, @Nullable Object);
method @NonNull public static com.google.firebase.firestore.Filter or(com.google.firebase.firestore.Filter...);
}

public class FirebaseFirestore {
method @NonNull public com.google.firebase.firestore.ListenerRegistration addSnapshotsInSyncListener(@NonNull Runnable);
method @NonNull public com.google.firebase.firestore.ListenerRegistration addSnapshotsInSyncListener(@NonNull android.app.Activity, @NonNull Runnable);
Expand Down Expand Up @@ -305,6 +331,7 @@ package com.google.firebase.firestore {
method @NonNull public com.google.firebase.firestore.Query startAfter(java.lang.Object...);
method @NonNull public com.google.firebase.firestore.Query startAt(@NonNull com.google.firebase.firestore.DocumentSnapshot);
method @NonNull public com.google.firebase.firestore.Query startAt(java.lang.Object...);
method @NonNull public com.google.firebase.firestore.Query where(@NonNull com.google.firebase.firestore.Filter...);
method @NonNull public com.google.firebase.firestore.Query whereArrayContains(@NonNull String, @NonNull Object);
method @NonNull public com.google.firebase.firestore.Query whereArrayContains(@NonNull com.google.firebase.firestore.FieldPath, @NonNull Object);
method @NonNull public com.google.firebase.firestore.Query whereArrayContainsAny(@NonNull String, @NonNull java.util.List<?>);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.firestore;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import com.google.firebase.firestore.core.CompositeFilter;
import com.google.firebase.firestore.core.FieldFilter;
import com.google.firebase.firestore.model.Document;
import com.google.firestore.v1.StructuredQuery;
import java.util.Arrays;

public abstract class Filter {
@NonNull
public static Filter equalTo(@NonNull String field, @Nullable Object value) {
return equalTo(FieldPath.fromDotSeparatedPath(field), value);
}

@NonNull
public static Filter equalTo(@NonNull FieldPath fieldPath, @Nullable Object value) {
return FieldFilter.create(fieldPath.getInternalPath(), FieldFilter.Operator.EQUAL, value);
}

@NonNull
public static Filter notEqualTo(@NonNull String field, @Nullable Object value) {
return notEqualTo(FieldPath.fromDotSeparatedPath(field), value);
}

@NonNull
public static Filter notEqualTo(@NonNull FieldPath fieldPath, @Nullable Object value) {
return FieldFilter.create(fieldPath.getInternalPath(), FieldFilter.Operator.NOT_EQUAL, value);
}

@NonNull
public static Filter greaterThan(@NonNull String field, @Nullable Object value) {
return greaterThan(FieldPath.fromDotSeparatedPath(field), value);
}

@NonNull
public static Filter greaterThan(@NonNull FieldPath fieldPath, @Nullable Object value) {
return FieldFilter.create(
fieldPath.getInternalPath(), FieldFilter.Operator.GREATER_THAN, value);
}

@NonNull
public static Filter greaterThanOrEqualTo(@NonNull String field, @Nullable Object value) {
return greaterThanOrEqualTo(FieldPath.fromDotSeparatedPath(field), value);
}

@NonNull
public static Filter greaterThanOrEqualTo(@NonNull FieldPath fieldPath, @Nullable Object value) {
return FieldFilter.create(
fieldPath.getInternalPath(), FieldFilter.Operator.GREATER_THAN_OR_EQUAL, value);
}

@NonNull
public static Filter lessThan(@NonNull String field, @Nullable Object value) {
return lessThan(FieldPath.fromDotSeparatedPath(field), value);
}

@NonNull
public static Filter lessThan(@NonNull FieldPath fieldPath, @Nullable Object value) {
return FieldFilter.create(fieldPath.getInternalPath(), FieldFilter.Operator.LESS_THAN, value);
}

@NonNull
public static Filter lessThanOrEqualTo(@NonNull String field, @Nullable Object value) {
return lessThanOrEqualTo(FieldPath.fromDotSeparatedPath(field), value);
}

@NonNull
public static Filter lessThanOrEqualTo(@NonNull FieldPath fieldPath, @Nullable Object value) {
return FieldFilter.create(
fieldPath.getInternalPath(), FieldFilter.Operator.LESS_THAN_OR_EQUAL, value);
}

@NonNull
public static Filter arrayContains(@NonNull String field, @Nullable Object value) {
return arrayContains(FieldPath.fromDotSeparatedPath(field), value);
}

@NonNull
public static Filter arrayContains(@NonNull FieldPath fieldPath, @Nullable Object value) {
return FieldFilter.create(
fieldPath.getInternalPath(), FieldFilter.Operator.ARRAY_CONTAINS, value);
}

@NonNull
public static Filter arrayContainsAny(@NonNull String field, @Nullable Object value) {
return arrayContainsAny(FieldPath.fromDotSeparatedPath(field), value);
}

@NonNull
public static Filter arrayContainsAny(@NonNull FieldPath fieldPath, @Nullable Object value) {
return FieldFilter.create(
fieldPath.getInternalPath(), FieldFilter.Operator.ARRAY_CONTAINS_ANY, value);
}

@NonNull
public static Filter inList(@NonNull String field, @Nullable Object value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a comment regarding the naming of this method in the API proposal. This should probably be inArray.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged

return inList(FieldPath.fromDotSeparatedPath(field), value);
}

@NonNull
public static Filter inList(@NonNull FieldPath fieldPath, @Nullable Object value) {
return FieldFilter.create(fieldPath.getInternalPath(), FieldFilter.Operator.IN, value);
}

@NonNull
public static Filter notInList(@NonNull String field, @Nullable Object value) {
return notInList(FieldPath.fromDotSeparatedPath(field), value);
}

@NonNull
public static Filter notInList(@NonNull FieldPath fieldPath, @Nullable Object value) {
return FieldFilter.create(fieldPath.getInternalPath(), FieldFilter.Operator.NOT_IN, value);
}

@NonNull
public static Filter or(Filter... filters) {
// TODO(ehsann): Change this to Operator.OR once it is available.
return new CompositeFilter(
Arrays.asList(filters), StructuredQuery.CompositeFilter.Operator.OPERATOR_UNSPECIFIED);
}

@NonNull
public static Filter and(Filter... filters) {
return new CompositeFilter(
Arrays.asList(filters), StructuredQuery.CompositeFilter.Operator.AND);
}

/** @hide */
@RestrictTo(RestrictTo.Scope.LIBRARY)
public abstract boolean matches(@NonNull Document doc);

/** @hide */
@NonNull
@RestrictTo(RestrictTo.Scope.LIBRARY)
public abstract String getCanonicalId();

/** @hide */
@NonNull
@RestrictTo(RestrictTo.Scope.LIBRARY)
public abstract Filter applyAssociativity();

/** @hide */
@NonNull
@RestrictTo(RestrictTo.Scope.LIBRARY)
public abstract Filter applyDistribution(@Nullable Filter other);

/** @hide */
@NonNull
@RestrictTo(RestrictTo.Scope.LIBRARY)
public abstract Filter computeDnf();

/** @hide */
@NonNull
@RestrictTo(RestrictTo.Scope.LIBRARY)
public abstract boolean equals(Object o);

/** @hide */
@NonNull
@RestrictTo(RestrictTo.Scope.LIBRARY)
public abstract int hashCode();
}
Loading