FSTQuery.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. };
  33. /** Interface used for all query filters. */
  34. @protocol FSTFilter <NSObject>
  35. /** Returns the field the Filter operates over. */
  36. - (const firebase::firestore::model::FieldPath &)field;
  37. /** Returns true if a document matches the filter. */
  38. - (BOOL)matchesDocument:(FSTDocument *)document;
  39. /** A unique ID identifying the filter; used when serializing queries. */
  40. - (NSString *)canonicalID;
  41. @end
  42. /**
  43. * FSTRelationFilter is a document filter constraint on a query with a single relation operator.
  44. * It is similar to NSComparisonPredicate, except customized for Firestore semantics.
  45. */
  46. @interface FSTRelationFilter : NSObject <FSTFilter>
  47. /**
  48. * Creates a new constraint for filtering documents.
  49. *
  50. * @param field A path to a field in the document to filter on. The LHS of the expression.
  51. * @param filterOperator The binary operator to apply.
  52. * @param value A constant value to compare @a field to. The RHS of the expression.
  53. * @return A new instance of FSTRelationFilter.
  54. */
  55. + (instancetype)filterWithField:(firebase::firestore::model::FieldPath)field
  56. filterOperator:(FSTRelationFilterOperator)filterOperator
  57. value:(FSTFieldValue *)value;
  58. - (instancetype)init NS_UNAVAILABLE;
  59. /** Returns YES if the receiver is not an equality relation. */
  60. - (BOOL)isInequality;
  61. /** The left hand side of the relation. A path into a document field. */
  62. - (const firebase::firestore::model::FieldPath &)field;
  63. /** The type of equality/inequality operator to use in the relation. */
  64. @property(nonatomic, assign, readonly) FSTRelationFilterOperator filterOperator;
  65. /** The right hand side of the relation. A constant value to compare to. */
  66. @property(nonatomic, strong, readonly) FSTFieldValue *value;
  67. @end
  68. /** Filter that matches NULL values. */
  69. @interface FSTNullFilter : NSObject <FSTFilter>
  70. - (instancetype)init NS_UNAVAILABLE;
  71. - (instancetype)initWithField:(firebase::firestore::model::FieldPath)field
  72. NS_DESIGNATED_INITIALIZER;
  73. @end
  74. /** Filter that matches NAN values. */
  75. @interface FSTNanFilter : NSObject <FSTFilter>
  76. - (instancetype)init NS_UNAVAILABLE;
  77. - (instancetype)initWithField:(firebase::firestore::model::FieldPath)field
  78. NS_DESIGNATED_INITIALIZER;
  79. @end
  80. /** FSTSortOrder is a field and direction to order query results by. */
  81. @interface FSTSortOrder : NSObject <NSCopying>
  82. /** Creates a new sort order with the given field and direction. */
  83. + (instancetype)sortOrderWithFieldPath:(firebase::firestore::model::FieldPath)fieldPath
  84. ascending:(BOOL)ascending;
  85. - (instancetype)init NS_UNAVAILABLE;
  86. /** Compares two documents based on the field and direction of this sort order. */
  87. - (NSComparisonResult)compareDocument:(FSTDocument *)document1 toDocument:(FSTDocument *)document2;
  88. /** The field to sort by. */
  89. - (const firebase::firestore::model::FieldPath &)field;
  90. /** The direction of the sort. */
  91. @property(nonatomic, assign, readonly, getter=isAscending) BOOL ascending;
  92. @end
  93. /**
  94. * FSTBound represents a bound of a query.
  95. *
  96. * The bound is specified with the given components representing a position and whether it's just
  97. * before or just after the position (relative to whatever the query order is).
  98. *
  99. * The position represents a logical index position for a query. It's a prefix of values for
  100. * the (potentially implicit) order by clauses of a query.
  101. *
  102. * FSTBound provides a function to determine whether a document comes before or after a bound.
  103. * This is influenced by whether the position is just before or just after the provided values.
  104. */
  105. @interface FSTBound : NSObject <NSCopying>
  106. /**
  107. * Creates a new bound.
  108. *
  109. * @param position The position relative to the sort order.
  110. * @param isBefore Whether this bound is just before or just after the position.
  111. */
  112. + (instancetype)boundWithPosition:(NSArray<FSTFieldValue *> *)position isBefore:(BOOL)isBefore;
  113. /** Whether this bound is just before or just after the provided position */
  114. @property(nonatomic, assign, readonly, getter=isBefore) BOOL before;
  115. /** The index position of this bound represented as an array of field values. */
  116. @property(nonatomic, strong, readonly) NSArray<FSTFieldValue *> *position;
  117. /** Returns YES if a document comes before a bound using the provided sort order. */
  118. - (BOOL)sortsBeforeDocument:(FSTDocument *)document
  119. usingSortOrder:(NSArray<FSTSortOrder *> *)sortOrder;
  120. @end
  121. /** FSTQuery represents the internal structure of a Firestore query. */
  122. @interface FSTQuery : NSObject <NSCopying>
  123. - (id)init NS_UNAVAILABLE;
  124. /**
  125. * Initializes a query with all of its components directly.
  126. */
  127. - (instancetype)initWithPath:(firebase::firestore::model::ResourcePath)path
  128. filterBy:(NSArray<id<FSTFilter>> *)filters
  129. orderBy:(NSArray<FSTSortOrder *> *)sortOrders
  130. limit:(NSInteger)limit
  131. startAt:(nullable FSTBound *)startAtBound
  132. endAt:(nullable FSTBound *)endAtBound NS_DESIGNATED_INITIALIZER;
  133. /**
  134. * Creates and returns a new FSTQuery.
  135. *
  136. * @param path The path to the collection to be queried over.
  137. * @return A new instance of FSTQuery.
  138. */
  139. + (instancetype)queryWithPath:(firebase::firestore::model::ResourcePath)path;
  140. /**
  141. * Returns the list of ordering constraints that were explicitly requested on the query by the
  142. * user.
  143. *
  144. * Note that the actual query performed might add additional sort orders to match the behavior
  145. * of the backend.
  146. */
  147. - (NSArray<FSTSortOrder *> *)explicitSortOrders;
  148. /**
  149. * Returns the full list of ordering constraints on the query.
  150. *
  151. * This might include additional sort orders added implicitly to match the backend behavior.
  152. */
  153. - (NSArray<FSTSortOrder *> *)sortOrders;
  154. /**
  155. * Creates a new FSTQuery with an additional filter.
  156. *
  157. * @param filter The predicate to filter by.
  158. * @return the new FSTQuery.
  159. */
  160. - (instancetype)queryByAddingFilter:(id<FSTFilter>)filter;
  161. /**
  162. * Creates a new FSTQuery with an additional ordering constraint.
  163. *
  164. * @param sortOrder The key and direction to order by.
  165. * @return the new FSTQuery.
  166. */
  167. - (instancetype)queryByAddingSortOrder:(FSTSortOrder *)sortOrder;
  168. /**
  169. * Returns a new FSTQuery with the given limit on how many results can be returned.
  170. *
  171. * @param limit The maximum number of results to return. If @a limit <= 0, behavior is unspecified.
  172. * If @a limit == NSNotFound, then no limit is applied.
  173. */
  174. - (instancetype)queryBySettingLimit:(NSInteger)limit;
  175. /**
  176. * Creates a new FSTQuery starting at the provided bound.
  177. *
  178. * @param bound The bound to start this query at.
  179. * @return the new FSTQuery.
  180. */
  181. - (instancetype)queryByAddingStartAt:(FSTBound *)bound;
  182. /**
  183. * Creates a new FSTQuery ending at the provided bound.
  184. *
  185. * @param bound The bound to end this query at.
  186. * @return the new FSTQuery.
  187. */
  188. - (instancetype)queryByAddingEndAt:(FSTBound *)bound;
  189. /** Returns YES if the receiver is query for a specific document. */
  190. - (BOOL)isDocumentQuery;
  191. /** Returns YES if the @a document matches the constraints of the receiver. */
  192. - (BOOL)matchesDocument:(FSTDocument *)document;
  193. /** Returns a comparator that will sort documents according to the receiver's sort order. */
  194. - (NSComparator)comparator;
  195. /** Returns the field of the first filter on the receiver that's an inequality, or nullptr if none.
  196. */
  197. - (const firebase::firestore::model::FieldPath *)inequalityFilterField;
  198. /** Returns the first field in an order-by constraint, or nullptr if none. */
  199. - (const firebase::firestore::model::FieldPath *)firstSortOrderField;
  200. /** The base path of the query. */
  201. - (const firebase::firestore::model::ResourcePath &)path;
  202. /** The filters on the documents returned by the query. */
  203. @property(nonatomic, strong, readonly) NSArray<id<FSTFilter>> *filters;
  204. /** The maximum number of results to return, or NSNotFound if no limit. */
  205. @property(nonatomic, assign, readonly) NSInteger limit;
  206. /**
  207. * A canonical string identifying the query. Two different instances of equivalent queries will
  208. * return the same canonicalID.
  209. */
  210. @property(nonatomic, strong, readonly) NSString *canonicalID;
  211. /** An optional bound to start the query at. */
  212. @property(nonatomic, nullable, strong, readonly) FSTBound *startAt;
  213. /** An optional bound to end the query at. */
  214. @property(nonatomic, nullable, strong, readonly) FSTBound *endAt;
  215. @end
  216. NS_ASSUME_NONNULL_END