FSTQuery.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <memory>
  18. #include <string>
  19. #include <vector>
  20. #include "Firestore/core/src/firebase/firestore/core/filter.h"
  21. #include "Firestore/core/src/firebase/firestore/core/query.h"
  22. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  23. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  24. #include "Firestore/core/src/firebase/firestore/model/field_value.h"
  25. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  26. @class FSTDocument;
  27. namespace core = firebase::firestore::core;
  28. namespace model = firebase::firestore::model;
  29. namespace util = firebase::firestore::util;
  30. NS_ASSUME_NONNULL_BEGIN
  31. /** FSTSortOrder is a field and direction to order query results by. */
  32. @interface FSTSortOrder : NSObject <NSCopying>
  33. /** Creates a new sort order with the given field and direction. */
  34. + (instancetype)sortOrderWithFieldPath:(model::FieldPath)fieldPath ascending:(BOOL)ascending;
  35. - (instancetype)init NS_UNAVAILABLE;
  36. /** Compares two documents based on the field and direction of this sort order. */
  37. - (util::ComparisonResult)compareDocument:(FSTDocument *)document1
  38. toDocument:(FSTDocument *)document2;
  39. /** The field to sort by. */
  40. - (const model::FieldPath &)field;
  41. /** The direction of the sort. */
  42. @property(nonatomic, assign, readonly, getter=isAscending) BOOL ascending;
  43. @end
  44. /**
  45. * FSTBound represents a bound of a query.
  46. *
  47. * The bound is specified with the given components representing a position and whether it's just
  48. * before or just after the position (relative to whatever the query order is).
  49. *
  50. * The position represents a logical index position for a query. It's a prefix of values for
  51. * the (potentially implicit) order by clauses of a query.
  52. *
  53. * FSTBound provides a function to determine whether a document comes before or after a bound.
  54. * This is influenced by whether the position is just before or just after the provided values.
  55. */
  56. @interface FSTBound : NSObject <NSCopying>
  57. /**
  58. * Creates a new bound.
  59. *
  60. * @param position The position relative to the sort order.
  61. * @param isBefore Whether this bound is just before or just after the position.
  62. */
  63. + (instancetype)boundWithPosition:(std::vector<model::FieldValue>)position isBefore:(bool)isBefore;
  64. /** Whether this bound is just before or just after the provided position */
  65. @property(nonatomic, assign, readonly, getter=isBefore) bool before;
  66. /** The index position of this bound represented as an array of field values. */
  67. @property(nonatomic, assign, readonly) const std::vector<model::FieldValue> &position;
  68. /** Returns true if a document comes before a bound using the provided sort order. */
  69. - (bool)sortsBeforeDocument:(FSTDocument *)document
  70. usingSortOrder:(NSArray<FSTSortOrder *> *)sortOrder;
  71. @end
  72. /** FSTQuery represents the internal structure of a Firestore query. */
  73. @interface FSTQuery : NSObject <NSCopying>
  74. - (id)init NS_UNAVAILABLE;
  75. /**
  76. * Initializes a query with all of its components directly.
  77. */
  78. - (instancetype)initWithQuery:(core::Query)query
  79. orderBy:(NSArray<FSTSortOrder *> *)sortOrders
  80. limit:(int32_t)limit
  81. startAt:(nullable FSTBound *)startAtBound
  82. endAt:(nullable FSTBound *)endAtBound NS_DESIGNATED_INITIALIZER;
  83. /**
  84. * Creates and returns a new FSTQuery.
  85. *
  86. * @param path The path to the collection to be queried over.
  87. * @return A new instance of FSTQuery.
  88. */
  89. + (instancetype)queryWithPath:(model::ResourcePath)path;
  90. /**
  91. * Creates and returns a new FSTQuery.
  92. *
  93. * @param path The path to the location to be queried over. Must currently be
  94. * empty in the case of a collection group query.
  95. * @param collectionGroup The collection group to be queried over. nullptr if this
  96. * is not a collection group query.
  97. * @return A new instance of FSTQuery.
  98. */
  99. + (instancetype)queryWithPath:(model::ResourcePath)path
  100. collectionGroup:(std::shared_ptr<const std::string>)collectionGroup;
  101. /**
  102. * Returns the list of ordering constraints that were explicitly requested on the query by the
  103. * user.
  104. *
  105. * Note that the actual query performed might add additional sort orders to match the behavior
  106. * of the backend.
  107. */
  108. - (NSArray<FSTSortOrder *> *)explicitSortOrders;
  109. /**
  110. * Returns the full list of ordering constraints on the query.
  111. *
  112. * This might include additional sort orders added implicitly to match the backend behavior.
  113. */
  114. - (NSArray<FSTSortOrder *> *)sortOrders;
  115. /**
  116. * Creates a new FSTQuery with an additional filter.
  117. *
  118. * @param filter The predicate to filter by.
  119. * @return the new FSTQuery.
  120. */
  121. - (instancetype)queryByAddingFilter:(std::shared_ptr<core::Filter>)filter;
  122. /**
  123. * Creates a new FSTQuery with an additional ordering constraint.
  124. *
  125. * @param sortOrder The key and direction to order by.
  126. * @return the new FSTQuery.
  127. */
  128. - (instancetype)queryByAddingSortOrder:(FSTSortOrder *)sortOrder;
  129. /**
  130. * Returns a new FSTQuery with the given limit on how many results can be returned.
  131. *
  132. * @param limit The maximum number of results to return. If @a limit <= 0, behavior is unspecified.
  133. * If @a limit == NSNotFound, then no limit is applied.
  134. */
  135. - (instancetype)queryBySettingLimit:(int32_t)limit;
  136. /**
  137. * Creates a new FSTQuery starting at the provided bound.
  138. *
  139. * @param bound The bound to start this query at.
  140. * @return the new FSTQuery.
  141. */
  142. - (instancetype)queryByAddingStartAt:(FSTBound *)bound;
  143. /**
  144. * Creates a new FSTQuery ending at the provided bound.
  145. *
  146. * @param bound The bound to end this query at.
  147. * @return the new FSTQuery.
  148. */
  149. - (instancetype)queryByAddingEndAt:(FSTBound *)bound;
  150. /**
  151. * Helper to convert a collection group query into a collection query at a specific path. This is
  152. * used when executing collection group queries, since we have to split the query into a set of
  153. * collection queries at multiple paths.
  154. */
  155. - (instancetype)collectionQueryAtPath:(model::ResourcePath)path;
  156. /** Returns YES if the receiver is query for a specific document. */
  157. - (BOOL)isDocumentQuery;
  158. /** Returns YES if the receiver is a collection group query. */
  159. - (BOOL)isCollectionGroupQuery;
  160. /** Returns YES if the @a document matches the constraints of the receiver. */
  161. - (BOOL)matchesDocument:(FSTDocument *)document;
  162. /** Returns a comparator that will sort documents according to the receiver's sort order. */
  163. - (model::DocumentComparator)comparator;
  164. /** Returns the field of the first filter on the receiver that's an inequality, or nullptr if none.
  165. */
  166. - (nullable const model::FieldPath *)inequalityFilterField;
  167. /** Returns YES if the query has an arrayContains filter already. */
  168. - (BOOL)hasArrayContainsFilter;
  169. /** Returns the first field in an order-by constraint, or nullptr if none. */
  170. - (nullable const model::FieldPath *)firstSortOrderField;
  171. /** The base path of the query. */
  172. - (const model::ResourcePath &)path;
  173. /** The collection group of the query. */
  174. - (const std::shared_ptr<const std::string> &)collectionGroup;
  175. /** The filters on the documents returned by the query. */
  176. - (const core::Query::FilterList &)filters;
  177. /** The maximum number of results to return, or NSNotFound if no limit. */
  178. @property(nonatomic, assign, readonly) int32_t limit;
  179. /**
  180. * A canonical string identifying the query. Two different instances of equivalent queries will
  181. * return the same canonicalID.
  182. */
  183. @property(nonatomic, strong, readonly) NSString *canonicalID;
  184. /** An optional bound to start the query at. */
  185. @property(nonatomic, nullable, strong, readonly) FSTBound *startAt;
  186. /** An optional bound to end the query at. */
  187. @property(nonatomic, nullable, strong, readonly) FSTBound *endAt;
  188. @end
  189. NS_ASSUME_NONNULL_END