FSTQuery.h 11 KB

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