-
Notifications
You must be signed in to change notification settings - Fork 626
[WIP] Expose query constrains on the public Query class #178
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
Changes from all commits
b34151a
ee2f364
449a701
e4c50c9
bcde738
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.google.firebase.firestore; | ||
|
||
import com.google.firebase.annotations.PublicApi; | ||
import com.google.firebase.firestore.core.NaNFilter; | ||
import com.google.firebase.firestore.core.NullFilter; | ||
import com.google.firebase.firestore.core.RelationFilter; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
@PublicApi | ||
public final class Filter { | ||
|
||
@PublicApi | ||
public enum Operator { | ||
LESS_THAN("<"), | ||
LESS_THAN_OR_EQUAL("<="), | ||
EQUAL("=="), | ||
GREATER_THAN(">"), | ||
GREATER_THAN_OR_EQUAL(">="), | ||
ARRAY_CONTAINS("array_contains"); | ||
|
||
private final String text; | ||
|
||
Operator(String text) { | ||
this.text = text; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return text; | ||
} | ||
} | ||
|
||
private final com.google.firebase.firestore.core.Filter filter; | ||
|
||
/** Initializes a filter */ | ||
public Filter(com.google.firebase.firestore.core.Filter filter) { | ||
this.filter = filter; | ||
} | ||
|
||
@PublicApi | ||
public boolean isRelationFilter() { | ||
return filter instanceof RelationFilter; | ||
} | ||
|
||
@PublicApi | ||
public String getFieldPath() { | ||
return filter.getField().canonicalString(); | ||
} | ||
|
||
@Nullable | ||
@PublicApi | ||
public Operator getOperator() { | ||
if (isRelationFilter()) { | ||
return ((RelationFilter) filter).getOperator(); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Nullable | ||
@PublicApi | ||
public Object getFieldValue() { | ||
if (filter instanceof NaNFilter) { | ||
return Double.NaN; | ||
} else { | ||
if (isRelationFilter()) { | ||
return ((RelationFilter) filter).getValue().value(); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,16 +12,21 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.firestore.core; | ||
package com.google.firebase.firestore; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import com.google.firebase.annotations.PublicApi; | ||
import com.google.firebase.firestore.model.Document; | ||
import com.google.firebase.firestore.model.FieldPath; | ||
import com.google.firebase.firestore.model.value.FieldValue; | ||
import com.google.firebase.firestore.util.Assert; | ||
|
||
/** Represents a sort order for a Firestore Query */ | ||
@PublicApi | ||
public class OrderBy { | ||
/** The direction of the ordering */ | ||
/** @hide **/ | ||
public enum Direction { | ||
ASCENDING(1), | ||
DESCENDING(-1); | ||
|
@@ -45,19 +50,28 @@ public Direction getDirection() { | |
return direction; | ||
} | ||
|
||
@NonNull | ||
@PublicApi | ||
public Query.Direction getSortDirection() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd drop "Sort" and just make this |
||
return direction == Direction.ASCENDING ? | ||
Query.Direction.ASCENDING : Query.Direction.DESCENDING; | ||
} | ||
|
||
@PublicApi | ||
public FieldPath getField() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: As mentioned above, we probably need to add methods to |
||
return field; | ||
} | ||
|
||
private final Direction direction; | ||
final FieldPath field; | ||
private final FieldPath field; | ||
|
||
private OrderBy(Direction direction, FieldPath field) { | ||
this.direction = direction; | ||
this.field = field; | ||
} | ||
|
||
int compare(Document d1, Document d2) { | ||
/** @hide **/ | ||
public int compare(Document d1, Document d2) { | ||
if (field.equals(FieldPath.KEY_PATH)) { | ||
return direction.getComparisonModifier() * d1.getKey().compareTo(d2.getKey()); | ||
} else { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,7 @@ | |
import com.google.firebase.firestore.core.Bound; | ||
import com.google.firebase.firestore.core.EventManager.ListenOptions; | ||
import com.google.firebase.firestore.core.Filter; | ||
import com.google.firebase.firestore.core.Filter.Operator; | ||
import com.google.firebase.firestore.core.OrderBy; | ||
import com.google.firebase.firestore.Filter.Operator; | ||
import com.google.firebase.firestore.core.QueryListener; | ||
import com.google.firebase.firestore.core.RelationFilter; | ||
import com.google.firebase.firestore.core.ViewSnapshot; | ||
|
@@ -900,6 +899,47 @@ private ListenerRegistration addSnapshotListenerInternal( | |
firestore.getClient(), queryListener, activity, wrappedListener); | ||
} | ||
|
||
@PublicApi | ||
public String getPath() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self: It's tempting to use a CollectionReference here but that only works for collection queries, not the soon-to-be-added "collection group" queries which are rooted at a document path. Still, it would be nice to avoid exposing just a raw string path. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self: We need to figure out how we want to expose Collection Group queries (#233) in general. |
||
return query.getPath().canonicalString(); | ||
} | ||
|
||
@PublicApi | ||
public List<com.google.firebase.firestore.Filter> getFilters() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self: Since we're exposing filters as |
||
com.google.firebase.firestore.Filter newFilter; | ||
List<com.google.firebase.firestore.Filter> filters = new ArrayList<>(); | ||
for (Filter filter : query.getFilters()) { | ||
newFilter = new com.google.firebase.firestore.Filter(filter); | ||
filters.add(newFilter); | ||
} | ||
return filters; | ||
} | ||
|
||
@PublicApi | ||
public List<OrderBy> getOrderBy() { | ||
return query.getExplicitOrderBy(); | ||
} | ||
|
||
@PublicApi | ||
public boolean hasLimit() { | ||
return query.hasLimit(); | ||
} | ||
|
||
@PublicApi | ||
public long getLimit() { | ||
return query.getLimit(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to myself: It's tempting to drop |
||
} | ||
|
||
@PublicApi | ||
public @Nullable String getStartAt() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think getStartAt() / getEndAt() need to return |
||
return query.getStartAt().getFieldValue(); | ||
} | ||
|
||
@PublicApi | ||
public @Nullable String getEndAt() { | ||
return query.getEndAt().getFieldValue(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning the field path as a
String
here using.canonicalString()
exposes the underlying backend representation of field paths which we don't currently expose in our API, specifically:FieldPath.of("emails", "[email protected]")
then the string returned here will be something like"emails.`test@example\.com`"
(since we have to escape.
when it's contained within a field).FieldPath.documentId()
will show up as"__name__"
here.I need to discuss this with the team, but I think my suggestion would be to:
FieldPath
here instead ofString
getSegments()
method toFieldPath
that returns a String[] containing the path segments.isDocumentId()
method that returns a bool indicating whether the FieldPath is equal toFieldPath.documentId()
.