FSTQuery.h 11 KB

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