FSTQuery.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  18. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  19. @class FSTDocument;
  20. @class FSTFieldValue;
  21. NS_ASSUME_NONNULL_BEGIN
  22. /**
  23. * FSTRelationFilterOperator is a value relation operator that can be used to filter documents.
  24. * It is similar to NSPredicateOperatorType, but only has operators supported by Firestore.
  25. */
  26. typedef NS_ENUM(NSInteger, FSTRelationFilterOperator) {
  27. FSTRelationFilterOperatorLessThan = 0,
  28. FSTRelationFilterOperatorLessThanOrEqual,
  29. FSTRelationFilterOperatorEqual,
  30. FSTRelationFilterOperatorGreaterThanOrEqual,
  31. FSTRelationFilterOperatorGreaterThan,
  32. FSTRelationFilterOperatorArrayContains,
  33. };
  34. /** Interface used for all query filters. */
  35. @protocol FSTFilter <NSObject>
  36. /** Returns the field the Filter operates over. */
  37. - (const firebase::firestore::model::FieldPath &)field;
  38. /** Returns true if a document matches the filter. */
  39. - (BOOL)matchesDocument:(FSTDocument *)document;
  40. /** A unique ID identifying the filter; used when serializing queries. */
  41. - (NSString *)canonicalID;
  42. @end
  43. /**
  44. * FSTRelationFilter is a document filter constraint on a query with a single relation operator.
  45. * It is similar to NSComparisonPredicate, except customized for Firestore semantics.
  46. */
  47. @interface FSTRelationFilter : NSObject <FSTFilter>
  48. /**
  49. * Creates a new constraint for filtering documents.
  50. *
  51. * @param field A path to a field in the document to filter on. The LHS of the expression.
  52. * @param filterOperator The binary operator to apply.
  53. * @param value A constant value to compare @a field to. The RHS of the expression.
  54. * @return A new instance of FSTRelationFilter.
  55. */
  56. + (instancetype)filterWithField:(firebase::firestore::model::FieldPath)field
  57. filterOperator:(FSTRelationFilterOperator)filterOperator
  58. value:(FSTFieldValue *)value;
  59. - (instancetype)init NS_UNAVAILABLE;
  60. /** Returns YES if the receiver is not an equality relation. */
  61. - (BOOL)isInequality;
  62. /** The left hand side of the relation. A path into a document field. */
  63. - (const firebase::firestore::model::FieldPath &)field;
  64. /** The type of equality/inequality operator to use in the relation. */
  65. @property(nonatomic, assign, readonly) FSTRelationFilterOperator filterOperator;
  66. /** The right hand side of the relation. A constant value to compare to. */
  67. @property(nonatomic, strong, readonly) FSTFieldValue *value;
  68. @end
  69. /** Filter that matches NULL values. */
  70. @interface FSTNullFilter : NSObject <FSTFilter>
  71. - (instancetype)init NS_UNAVAILABLE;
  72. - (instancetype)initWithField:(firebase::firestore::model::FieldPath)field
  73. NS_DESIGNATED_INITIALIZER;
  74. @end
  75. /** Filter that matches NAN values. */
  76. @interface FSTNanFilter : NSObject <FSTFilter>
  77. - (instancetype)init NS_UNAVAILABLE;
  78. - (instancetype)initWithField:(firebase::firestore::model::FieldPath)field
  79. NS_DESIGNATED_INITIALIZER;
  80. @end
  81. /** FSTSortOrder is a field and direction to order query results by. */
  82. @interface FSTSortOrder : NSObject <NSCopying>
  83. /** Creates a new sort order with the given field and direction. */
  84. + (instancetype)sortOrderWithFieldPath:(firebase::firestore::model::FieldPath)fieldPath
  85. ascending:(BOOL)ascending;
  86. - (instancetype)init NS_UNAVAILABLE;
  87. /** Compares two documents based on the field and direction of this sort order. */
  88. - (NSComparisonResult)compareDocument:(FSTDocument *)document1 toDocument:(FSTDocument *)document2;
  89. /** The field to sort by. */
  90. - (const firebase::firestore::model::FieldPath &)field;
  91. /** The direction of the sort. */
  92. @property(nonatomic, assign, readonly, getter=isAscending) BOOL ascending;
  93. @end
  94. /**
  95. * FSTBound represents a bound of a query.
  96. *
  97. * The bound is specified with the given components representing a position and whether it's just
  98. * before or just after the position (relative to whatever the query order is).
  99. *
  100. * The position represents a logical index position for a query. It's a prefix of values for
  101. * the (potentially implicit) order by clauses of a query.
  102. *
  103. * FSTBound provides a function to determine whether a document comes before or after a bound.
  104. * This is influenced by whether the position is just before or just after the provided values.
  105. */
  106. @interface FSTBound : NSObject <NSCopying>
  107. /**
  108. * Creates a new bound.
  109. *
  110. * @param position The position relative to the sort order.
  111. * @param isBefore Whether this bound is just before or just after the position.
  112. */
  113. + (instancetype)boundWithPosition:(NSArray<FSTFieldValue *> *)position isBefore:(BOOL)isBefore;
  114. /** Whether this bound is just before or just after the provided position */
  115. @property(nonatomic, assign, readonly, getter=isBefore) BOOL before;
  116. /** The index position of this bound represented as an array of field values. */
  117. @property(nonatomic, strong, readonly) NSArray<FSTFieldValue *> *position;
  118. /** Returns YES if a document comes before a bound using the provided sort order. */
  119. - (BOOL)sortsBeforeDocument:(FSTDocument *)document
  120. usingSortOrder:(NSArray<FSTSortOrder *> *)sortOrder;
  121. @end
  122. /** FSTQuery represents the internal structure of a Firestore query. */
  123. @interface FSTQuery : NSObject <NSCopying>
  124. - (id)init NS_UNAVAILABLE;
  125. /**
  126. * Initializes a query with all of its components directly.
  127. */
  128. - (instancetype)initWithPath:(firebase::firestore::model::ResourcePath)path
  129. filterBy:(NSArray<id<FSTFilter>> *)filters
  130. orderBy:(NSArray<FSTSortOrder *> *)sortOrders
  131. limit:(NSInteger)limit
  132. startAt:(nullable FSTBound *)startAtBound
  133. endAt:(nullable FSTBound *)endAtBound NS_DESIGNATED_INITIALIZER;
  134. /**
  135. * Creates and returns a new FSTQuery.
  136. *
  137. * @param path The path to the collection to be queried over.
  138. * @return A new instance of FSTQuery.
  139. */
  140. + (instancetype)queryWithPath:(firebase::firestore::model::ResourcePath)path;
  141. /**
  142. * Returns the list of ordering constraints that were explicitly requested on the query by the
  143. * user.
  144. *
  145. * Note that the actual query performed might add additional sort orders to match the behavior
  146. * of the backend.
  147. */
  148. - (NSArray<FSTSortOrder *> *)explicitSortOrders;
  149. /**
  150. * Returns the full list of ordering constraints on the query.
  151. *
  152. * This might include additional sort orders added implicitly to match the backend behavior.
  153. */
  154. - (NSArray<FSTSortOrder *> *)sortOrders;
  155. /**
  156. * Creates a new FSTQuery with an additional filter.
  157. *
  158. * @param filter The predicate to filter by.
  159. * @return the new FSTQuery.
  160. */
  161. - (instancetype)queryByAddingFilter:(id<FSTFilter>)filter;
  162. /**
  163. * Creates a new FSTQuery with an additional ordering constraint.
  164. *
  165. * @param sortOrder The key and direction to order by.
  166. * @return the new FSTQuery.
  167. */
  168. - (instancetype)queryByAddingSortOrder:(FSTSortOrder *)sortOrder;
  169. /**
  170. * Returns a new FSTQuery with the given limit on how many results can be returned.
  171. *
  172. * @param limit The maximum number of results to return. If @a limit <= 0, behavior is unspecified.
  173. * If @a limit == NSNotFound, then no limit is applied.
  174. */
  175. - (instancetype)queryBySettingLimit:(NSInteger)limit;
  176. /**
  177. * Creates a new FSTQuery starting at the provided bound.
  178. *
  179. * @param bound The bound to start this query at.
  180. * @return the new FSTQuery.
  181. */
  182. - (instancetype)queryByAddingStartAt:(FSTBound *)bound;
  183. /**
  184. * Creates a new FSTQuery ending at the provided bound.
  185. *
  186. * @param bound The bound to end this query at.
  187. * @return the new FSTQuery.
  188. */
  189. - (instancetype)queryByAddingEndAt:(FSTBound *)bound;
  190. /** Returns YES if the receiver is query for a specific document. */
  191. - (BOOL)isDocumentQuery;
  192. /** Returns YES if the @a document matches the constraints of the receiver. */
  193. - (BOOL)matchesDocument:(FSTDocument *)document;
  194. /** Returns a comparator that will sort documents according to the receiver's sort order. */
  195. - (NSComparator)comparator;
  196. /** Returns the field of the first filter on the receiver that's an inequality, or nullptr if none.
  197. */
  198. - (const firebase::firestore::model::FieldPath *)inequalityFilterField;
  199. /** Returns YES if the query has an arrayContains filter already. */
  200. - (BOOL)hasArrayContainsFilter;
  201. /** Returns the first field in an order-by constraint, or nullptr if none. */
  202. - (const firebase::firestore::model::FieldPath *)firstSortOrderField;
  203. /** The base path of the query. */
  204. - (const firebase::firestore::model::ResourcePath &)path;
  205. /** The filters on the documents returned by the query. */
  206. @property(nonatomic, strong, readonly) NSArray<id<FSTFilter>> *filters;
  207. /** The maximum number of results to return, or NSNotFound if no limit. */
  208. @property(nonatomic, assign, readonly) NSInteger limit;
  209. /**
  210. * A canonical string identifying the query. Two different instances of equivalent queries will
  211. * return the same canonicalID.
  212. */
  213. @property(nonatomic, strong, readonly) NSString *canonicalID;
  214. /** An optional bound to start the query at. */
  215. @property(nonatomic, nullable, strong, readonly) FSTBound *startAt;
  216. /** An optional bound to end the query at. */
  217. @property(nonatomic, nullable, strong, readonly) FSTBound *endAt;
  218. @end
  219. NS_ASSUME_NONNULL_END