FIRQuery.mm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 "FIRQuery.h"
  17. #include <memory>
  18. #include <utility>
  19. #import "FIRDocumentReference.h"
  20. #import "FIRFirestoreErrors.h"
  21. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  22. #import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
  23. #import "Firestore/Source/API/FIRFieldPath+Internal.h"
  24. #import "Firestore/Source/API/FIRFieldValue+Internal.h"
  25. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  26. #import "Firestore/Source/API/FIRFirestoreSource+Internal.h"
  27. #import "Firestore/Source/API/FIRListenerRegistration+Internal.h"
  28. #import "Firestore/Source/API/FIRQuery+Internal.h"
  29. #import "Firestore/Source/API/FIRQuerySnapshot+Internal.h"
  30. #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
  31. #import "Firestore/Source/API/FSTUserDataConverter.h"
  32. #import "Firestore/Source/Core/FSTEventManager.h"
  33. #import "Firestore/Source/Core/FSTFirestoreClient.h"
  34. #import "Firestore/Source/Core/FSTQuery.h"
  35. #import "Firestore/Source/Model/FSTDocument.h"
  36. #import "Firestore/Source/Model/FSTFieldValue.h"
  37. #include "Firestore/core/src/firebase/firestore/api/input_validation.h"
  38. #include "Firestore/core/src/firebase/firestore/api/query_core.h"
  39. #include "Firestore/core/src/firebase/firestore/core/direction.h"
  40. #include "Firestore/core/src/firebase/firestore/core/filter.h"
  41. #include "Firestore/core/src/firebase/firestore/core/query.h"
  42. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  43. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  44. #include "Firestore/core/src/firebase/firestore/model/field_value.h"
  45. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  46. #include "Firestore/core/src/firebase/firestore/util/error_apple.h"
  47. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  48. #include "Firestore/core/src/firebase/firestore/util/statusor.h"
  49. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  50. #include "absl/memory/memory.h"
  51. namespace util = firebase::firestore::util;
  52. using firebase::firestore::api::Firestore;
  53. using firebase::firestore::api::ListenerRegistration;
  54. using firebase::firestore::api::Query;
  55. using firebase::firestore::api::QuerySnapshot;
  56. using firebase::firestore::api::SnapshotMetadata;
  57. using firebase::firestore::api::Source;
  58. using firebase::firestore::api::ThrowInvalidArgument;
  59. using firebase::firestore::core::AsyncEventListener;
  60. using firebase::firestore::core::Direction;
  61. using firebase::firestore::core::EventListener;
  62. using firebase::firestore::core::Filter;
  63. using firebase::firestore::core::ListenOptions;
  64. using firebase::firestore::core::QueryListener;
  65. using firebase::firestore::core::ViewSnapshot;
  66. using firebase::firestore::model::DocumentKey;
  67. using firebase::firestore::model::FieldPath;
  68. using firebase::firestore::model::FieldValue;
  69. using firebase::firestore::model::ResourcePath;
  70. using firebase::firestore::util::MakeNSError;
  71. using firebase::firestore::util::StatusOr;
  72. NS_ASSUME_NONNULL_BEGIN
  73. namespace {
  74. FieldPath MakeFieldPath(NSString *field) {
  75. return FieldPath::FromDotSeparatedString(util::MakeString(field));
  76. }
  77. FIRQuery *Wrap(Query &&query) {
  78. return [[FIRQuery alloc] initWithQuery:std::move(query)];
  79. }
  80. } // namespace
  81. @implementation FIRQuery {
  82. Query _query;
  83. }
  84. #pragma mark - Constructor Methods
  85. - (instancetype)initWithQuery:(Query &&)query {
  86. if (self = [super init]) {
  87. _query = std::move(query);
  88. }
  89. return self;
  90. }
  91. - (instancetype)initWithQuery:(FSTQuery *)query firestore:(std::shared_ptr<Firestore>)firestore {
  92. return [self initWithQuery:Query{query, std::move(firestore)}];
  93. }
  94. #pragma mark - NSObject Methods
  95. - (BOOL)isEqual:(nullable id)other {
  96. if (other == self) return YES;
  97. if (![[other class] isEqual:[self class]]) return NO;
  98. auto otherQuery = static_cast<FIRQuery *>(other);
  99. return _query == otherQuery->_query;
  100. }
  101. - (NSUInteger)hash {
  102. return _query.Hash();
  103. }
  104. #pragma mark - Public Methods
  105. - (FIRFirestore *)firestore {
  106. return [FIRFirestore recoverFromFirestore:_query.firestore()];
  107. }
  108. - (void)getDocumentsWithCompletion:(void (^)(FIRQuerySnapshot *_Nullable snapshot,
  109. NSError *_Nullable error))completion {
  110. _query.GetDocuments(Source::Default, [self wrapQuerySnapshotBlock:completion]);
  111. }
  112. - (void)getDocumentsWithSource:(FIRFirestoreSource)publicSource
  113. completion:(void (^)(FIRQuerySnapshot *_Nullable snapshot,
  114. NSError *_Nullable error))completion {
  115. Source source = api::MakeSource(publicSource);
  116. _query.GetDocuments(source, [self wrapQuerySnapshotBlock:completion]);
  117. }
  118. - (id<FIRListenerRegistration>)addSnapshotListener:(FIRQuerySnapshotBlock)listener {
  119. return [self addSnapshotListenerWithIncludeMetadataChanges:NO listener:listener];
  120. }
  121. - (id<FIRListenerRegistration>)
  122. addSnapshotListenerWithIncludeMetadataChanges:(BOOL)includeMetadataChanges
  123. listener:(FIRQuerySnapshotBlock)listener {
  124. auto options = ListenOptions::FromIncludeMetadataChanges(includeMetadataChanges);
  125. return [self addSnapshotListenerInternalWithOptions:options listener:listener];
  126. }
  127. - (id<FIRListenerRegistration>)addSnapshotListenerInternalWithOptions:(ListenOptions)internalOptions
  128. listener:
  129. (FIRQuerySnapshotBlock)listener {
  130. std::shared_ptr<Firestore> firestore = self.firestore.wrapped;
  131. FSTQuery *query = self.query;
  132. // Convert from ViewSnapshots to QuerySnapshots.
  133. auto view_listener = EventListener<ViewSnapshot>::Create(
  134. [listener, firestore, query](StatusOr<ViewSnapshot> maybe_snapshot) {
  135. if (!maybe_snapshot.status().ok()) {
  136. listener(nil, MakeNSError(maybe_snapshot.status()));
  137. return;
  138. }
  139. ViewSnapshot snapshot = std::move(maybe_snapshot).ValueOrDie();
  140. SnapshotMetadata metadata(snapshot.has_pending_writes(), snapshot.from_cache());
  141. listener([[FIRQuerySnapshot alloc] initWithFirestore:firestore
  142. originalQuery:query
  143. snapshot:std::move(snapshot)
  144. metadata:std::move(metadata)],
  145. nil);
  146. });
  147. // Call the view_listener on the user Executor.
  148. auto async_listener = AsyncEventListener<ViewSnapshot>::Create(firestore->client().userExecutor,
  149. std::move(view_listener));
  150. std::shared_ptr<QueryListener> query_listener =
  151. [firestore->client() listenToQuery:query options:internalOptions listener:async_listener];
  152. return [[FSTListenerRegistration alloc]
  153. initWithRegistration:ListenerRegistration(firestore->client(), std::move(async_listener),
  154. std::move(query_listener))];
  155. }
  156. - (FIRQuery *)queryWhereField:(NSString *)field isEqualTo:(id)value {
  157. return [self queryWithFilterOperator:Filter::Operator::Equal field:field value:value];
  158. }
  159. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path isEqualTo:(id)value {
  160. return [self queryWithFilterOperator:Filter::Operator::Equal path:path.internalValue value:value];
  161. }
  162. - (FIRQuery *)queryWhereField:(NSString *)field isLessThan:(id)value {
  163. return [self queryWithFilterOperator:Filter::Operator::LessThan field:field value:value];
  164. }
  165. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path isLessThan:(id)value {
  166. return [self queryWithFilterOperator:Filter::Operator::LessThan
  167. path:path.internalValue
  168. value:value];
  169. }
  170. - (FIRQuery *)queryWhereField:(NSString *)field isLessThanOrEqualTo:(id)value {
  171. return [self queryWithFilterOperator:Filter::Operator::LessThanOrEqual field:field value:value];
  172. }
  173. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path isLessThanOrEqualTo:(id)value {
  174. return [self queryWithFilterOperator:Filter::Operator::LessThanOrEqual
  175. path:path.internalValue
  176. value:value];
  177. }
  178. - (FIRQuery *)queryWhereField:(NSString *)field isGreaterThan:(id)value {
  179. return [self queryWithFilterOperator:Filter::Operator::GreaterThan field:field value:value];
  180. }
  181. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path isGreaterThan:(id)value {
  182. return [self queryWithFilterOperator:Filter::Operator::GreaterThan
  183. path:path.internalValue
  184. value:value];
  185. }
  186. - (FIRQuery *)queryWhereField:(NSString *)field arrayContains:(id)value {
  187. return [self queryWithFilterOperator:Filter::Operator::ArrayContains field:field value:value];
  188. }
  189. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path arrayContains:(id)value {
  190. return [self queryWithFilterOperator:Filter::Operator::ArrayContains
  191. path:path.internalValue
  192. value:value];
  193. }
  194. - (FIRQuery *)queryWhereField:(NSString *)field isGreaterThanOrEqualTo:(id)value {
  195. return [self queryWithFilterOperator:Filter::Operator::GreaterThanOrEqual
  196. field:field
  197. value:value];
  198. }
  199. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path isGreaterThanOrEqualTo:(id)value {
  200. return [self queryWithFilterOperator:Filter::Operator::GreaterThanOrEqual
  201. path:path.internalValue
  202. value:value];
  203. }
  204. - (FIRQuery *)queryFilteredUsingComparisonPredicate:(NSPredicate *)predicate {
  205. NSComparisonPredicate *comparison = (NSComparisonPredicate *)predicate;
  206. if (comparison.comparisonPredicateModifier != NSDirectPredicateModifier) {
  207. ThrowInvalidArgument("Invalid query. Predicate cannot have an aggregate modifier.");
  208. }
  209. NSString *path;
  210. id value = nil;
  211. if ([comparison.leftExpression expressionType] == NSKeyPathExpressionType &&
  212. [comparison.rightExpression expressionType] == NSConstantValueExpressionType) {
  213. path = comparison.leftExpression.keyPath;
  214. value = comparison.rightExpression.constantValue;
  215. switch (comparison.predicateOperatorType) {
  216. case NSEqualToPredicateOperatorType:
  217. return [self queryWhereField:path isEqualTo:value];
  218. case NSLessThanPredicateOperatorType:
  219. return [self queryWhereField:path isLessThan:value];
  220. case NSLessThanOrEqualToPredicateOperatorType:
  221. return [self queryWhereField:path isLessThanOrEqualTo:value];
  222. case NSGreaterThanPredicateOperatorType:
  223. return [self queryWhereField:path isGreaterThan:value];
  224. case NSGreaterThanOrEqualToPredicateOperatorType:
  225. return [self queryWhereField:path isGreaterThanOrEqualTo:value];
  226. default:; // Fallback below to throw assertion.
  227. }
  228. } else if ([comparison.leftExpression expressionType] == NSConstantValueExpressionType &&
  229. [comparison.rightExpression expressionType] == NSKeyPathExpressionType) {
  230. path = comparison.rightExpression.keyPath;
  231. value = comparison.leftExpression.constantValue;
  232. switch (comparison.predicateOperatorType) {
  233. case NSEqualToPredicateOperatorType:
  234. return [self queryWhereField:path isEqualTo:value];
  235. case NSLessThanPredicateOperatorType:
  236. return [self queryWhereField:path isGreaterThan:value];
  237. case NSLessThanOrEqualToPredicateOperatorType:
  238. return [self queryWhereField:path isGreaterThanOrEqualTo:value];
  239. case NSGreaterThanPredicateOperatorType:
  240. return [self queryWhereField:path isLessThan:value];
  241. case NSGreaterThanOrEqualToPredicateOperatorType:
  242. return [self queryWhereField:path isLessThanOrEqualTo:value];
  243. default:; // Fallback below to throw assertion.
  244. }
  245. } else {
  246. ThrowInvalidArgument(
  247. "Invalid query. Predicate comparisons must include a key path and a constant.");
  248. }
  249. // Fallback cases of unsupported comparison operator.
  250. switch (comparison.predicateOperatorType) {
  251. case NSCustomSelectorPredicateOperatorType:
  252. ThrowInvalidArgument("Invalid query. Custom predicate filters are not supported.");
  253. break;
  254. default:
  255. ThrowInvalidArgument("Invalid query. Operator type %s is not supported.",
  256. comparison.predicateOperatorType);
  257. }
  258. }
  259. - (FIRQuery *)queryFilteredUsingCompoundPredicate:(NSPredicate *)predicate {
  260. NSCompoundPredicate *compound = (NSCompoundPredicate *)predicate;
  261. if (compound.compoundPredicateType != NSAndPredicateType || compound.subpredicates.count == 0) {
  262. ThrowInvalidArgument("Invalid query. Only compound queries using AND are supported.");
  263. }
  264. FIRQuery *query = self;
  265. for (NSPredicate *pred in compound.subpredicates) {
  266. query = [query queryFilteredUsingPredicate:pred];
  267. }
  268. return query;
  269. }
  270. - (FIRQuery *)queryFilteredUsingPredicate:(NSPredicate *)predicate {
  271. if ([predicate isKindOfClass:[NSComparisonPredicate class]]) {
  272. return [self queryFilteredUsingComparisonPredicate:predicate];
  273. } else if ([predicate isKindOfClass:[NSCompoundPredicate class]]) {
  274. return [self queryFilteredUsingCompoundPredicate:predicate];
  275. } else if ([predicate isKindOfClass:[[NSPredicate
  276. predicateWithBlock:^BOOL(id obj, NSDictionary *bindings) {
  277. return true;
  278. }] class]]) {
  279. ThrowInvalidArgument("Invalid query. Block-based predicates are not supported. Please use "
  280. "predicateWithFormat to create predicates instead.");
  281. } else {
  282. ThrowInvalidArgument("Invalid query. Expect comparison or compound of comparison predicate. "
  283. "Please use predicateWithFormat to create predicates.");
  284. }
  285. }
  286. - (FIRQuery *)queryOrderedByField:(NSString *)field {
  287. return [self queryOrderedByFieldPath:[FIRFieldPath pathWithDotSeparatedString:field]
  288. descending:NO];
  289. }
  290. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)fieldPath {
  291. return [self queryOrderedByFieldPath:fieldPath descending:NO];
  292. }
  293. - (FIRQuery *)queryOrderedByField:(NSString *)field descending:(BOOL)descending {
  294. return [self queryOrderedByFieldPath:[FIRFieldPath pathWithDotSeparatedString:field]
  295. descending:descending];
  296. }
  297. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)fieldPath descending:(BOOL)descending {
  298. return Wrap(_query.OrderBy(fieldPath.internalValue, Direction::FromDescending(descending)));
  299. }
  300. - (FIRQuery *)queryLimitedTo:(NSInteger)limit {
  301. int32_t internalLimit;
  302. if (limit == NSNotFound || limit >= core::Query::kNoLimit) {
  303. internalLimit = core::Query::kNoLimit;
  304. } else {
  305. internalLimit = static_cast<int32_t>(limit);
  306. }
  307. return Wrap(_query.Limit(internalLimit));
  308. }
  309. - (FIRQuery *)queryStartingAtDocument:(FIRDocumentSnapshot *)snapshot {
  310. FSTBound *bound = [self boundFromSnapshot:snapshot isBefore:YES];
  311. return Wrap(_query.StartAt(bound));
  312. }
  313. - (FIRQuery *)queryStartingAtValues:(NSArray *)fieldValues {
  314. FSTBound *bound = [self boundFromFieldValues:fieldValues isBefore:YES];
  315. return Wrap(_query.StartAt(bound));
  316. }
  317. - (FIRQuery *)queryStartingAfterDocument:(FIRDocumentSnapshot *)snapshot {
  318. FSTBound *bound = [self boundFromSnapshot:snapshot isBefore:NO];
  319. return Wrap(_query.StartAt(bound));
  320. }
  321. - (FIRQuery *)queryStartingAfterValues:(NSArray *)fieldValues {
  322. FSTBound *bound = [self boundFromFieldValues:fieldValues isBefore:NO];
  323. return Wrap(_query.StartAt(bound));
  324. }
  325. - (FIRQuery *)queryEndingBeforeDocument:(FIRDocumentSnapshot *)snapshot {
  326. FSTBound *bound = [self boundFromSnapshot:snapshot isBefore:YES];
  327. return Wrap(_query.EndAt(bound));
  328. }
  329. - (FIRQuery *)queryEndingBeforeValues:(NSArray *)fieldValues {
  330. FSTBound *bound = [self boundFromFieldValues:fieldValues isBefore:YES];
  331. return Wrap(_query.EndAt(bound));
  332. }
  333. - (FIRQuery *)queryEndingAtDocument:(FIRDocumentSnapshot *)snapshot {
  334. FSTBound *bound = [self boundFromSnapshot:snapshot isBefore:NO];
  335. return Wrap(_query.EndAt(bound));
  336. }
  337. - (FIRQuery *)queryEndingAtValues:(NSArray *)fieldValues {
  338. FSTBound *bound = [self boundFromFieldValues:fieldValues isBefore:NO];
  339. return Wrap(_query.EndAt(bound));
  340. }
  341. #pragma mark - Private Methods
  342. - (FSTFieldValue *)parsedQueryValue:(id)value {
  343. return [self.firestore.dataConverter parsedQueryValue:value];
  344. }
  345. - (QuerySnapshot::Listener)wrapQuerySnapshotBlock:(FIRQuerySnapshotBlock)block {
  346. class Converter : public EventListener<QuerySnapshot> {
  347. public:
  348. explicit Converter(FIRQuerySnapshotBlock block) : block_(block) {
  349. }
  350. void OnEvent(StatusOr<QuerySnapshot> maybe_snapshot) override {
  351. if (maybe_snapshot.ok()) {
  352. FIRQuerySnapshot *result =
  353. [[FIRQuerySnapshot alloc] initWithSnapshot:std::move(maybe_snapshot).ValueOrDie()];
  354. block_(result, nil);
  355. } else {
  356. block_(nil, util::MakeNSError(maybe_snapshot.status()));
  357. }
  358. }
  359. private:
  360. FIRQuerySnapshotBlock block_;
  361. };
  362. return absl::make_unique<Converter>(block);
  363. }
  364. /** Private helper for all of the queryWhereField: methods. */
  365. - (FIRQuery *)queryWithFilterOperator:(Filter::Operator)filterOperator
  366. field:(NSString *)field
  367. value:(id)value {
  368. return [self queryWithFilterOperator:filterOperator path:MakeFieldPath(field) value:value];
  369. }
  370. - (FIRQuery *)queryWithFilterOperator:(Filter::Operator)filterOperator
  371. path:(const FieldPath &)fieldPath
  372. value:(id)value {
  373. FSTFieldValue *fieldValue = [self parsedQueryValue:value];
  374. auto describer = [value] { return util::MakeString(NSStringFromClass([value class])); };
  375. return Wrap(_query.Filter(fieldPath, filterOperator, fieldValue, describer));
  376. }
  377. /**
  378. * Create a FSTBound from a query given the document.
  379. *
  380. * Note that the FSTBound will always include the key of the document and the position will be
  381. * unambiguous.
  382. *
  383. * Will throw if the document does not contain all fields of the order by of
  384. * the query or if any of the fields in the order by are an uncommitted server
  385. * timestamp.
  386. */
  387. - (FSTBound *)boundFromSnapshot:(FIRDocumentSnapshot *)snapshot isBefore:(BOOL)isBefore {
  388. if (![snapshot exists]) {
  389. ThrowInvalidArgument("Invalid query. You are trying to start or end a query using a document "
  390. "that doesn't exist.");
  391. }
  392. FSTDocument *document = snapshot.internalDocument;
  393. NSMutableArray<FSTFieldValue *> *components = [NSMutableArray array];
  394. // Because people expect to continue/end a query at the exact document provided, we need to
  395. // use the implicit sort order rather than the explicit sort order, because it's guaranteed to
  396. // contain the document key. That way the position becomes unambiguous and the query
  397. // continues/ends exactly at the provided document. Without the key (by using the explicit sort
  398. // orders), multiple documents could match the position, yielding duplicate results.
  399. for (FSTSortOrder *sortOrder in self.query.sortOrders) {
  400. if (sortOrder.field == FieldPath::KeyFieldPath()) {
  401. [components addObject:[FSTReferenceValue
  402. referenceValue:[FSTDocumentKey keyWithDocumentKey:document.key]
  403. databaseID:self.firestore.databaseID]];
  404. } else {
  405. FSTFieldValue *value = [document fieldForPath:sortOrder.field];
  406. if (value.type == FieldValue::Type::ServerTimestamp) {
  407. ThrowInvalidArgument(
  408. "Invalid query. You are trying to start or end a query using a document for which the "
  409. "field '%s' is an uncommitted server timestamp. (Since the value of this field is "
  410. "unknown, you cannot start/end a query with it.)",
  411. sortOrder.field.CanonicalString());
  412. } else if (value != nil) {
  413. [components addObject:value];
  414. } else {
  415. ThrowInvalidArgument(
  416. "Invalid query. You are trying to start or end a query using a document for which the "
  417. "field '%s' (used as the order by) does not exist.",
  418. sortOrder.field.CanonicalString());
  419. }
  420. }
  421. }
  422. return [FSTBound boundWithPosition:components isBefore:isBefore];
  423. }
  424. /** Converts a list of field values to an FSTBound. */
  425. - (FSTBound *)boundFromFieldValues:(NSArray<id> *)fieldValues isBefore:(BOOL)isBefore {
  426. // Use explicit sort order because it has to match the query the user made
  427. NSArray<FSTSortOrder *> *explicitSortOrders = self.query.explicitSortOrders;
  428. if (fieldValues.count > explicitSortOrders.count) {
  429. ThrowInvalidArgument("Invalid query. You are trying to start or end a query using more values "
  430. "than were specified in the order by.");
  431. }
  432. NSMutableArray<FSTFieldValue *> *components = [NSMutableArray array];
  433. [fieldValues enumerateObjectsUsingBlock:^(id rawValue, NSUInteger idx, BOOL *stop) {
  434. FSTSortOrder *sortOrder = explicitSortOrders[idx];
  435. FSTFieldValue *fieldValue = [self parsedQueryValue:rawValue];
  436. if (sortOrder.field.IsKeyFieldPath()) {
  437. if (fieldValue.type != FieldValue::Type::String) {
  438. ThrowInvalidArgument("Invalid query. Expected a string for the document ID.");
  439. }
  440. const std::string &documentID =
  441. static_cast<FSTDelegateValue *>(fieldValue).internalValue.string_value();
  442. if (![self.query isCollectionGroupQuery] && documentID.find('/') != std::string::npos) {
  443. ThrowInvalidArgument("Invalid query. When querying a collection and ordering by document "
  444. "ID, you must pass a plain document ID, but '%s' contains a slash.",
  445. documentID);
  446. }
  447. ResourcePath path = self.query.path.Append(ResourcePath::FromString(documentID));
  448. if (!DocumentKey::IsDocumentKey(path)) {
  449. ThrowInvalidArgument("Invalid query. When querying a collection group and ordering by "
  450. "document ID, you must pass a value that results in a valid document "
  451. "path, but '%s' is not because it contains an odd number of segments.",
  452. path.CanonicalString());
  453. }
  454. DocumentKey key{path};
  455. fieldValue = [FSTReferenceValue referenceValue:[FSTDocumentKey keyWithDocumentKey:key]
  456. databaseID:self.firestore.databaseID];
  457. }
  458. [components addObject:fieldValue];
  459. }];
  460. return [FSTBound boundWithPosition:components isBefore:isBefore];
  461. }
  462. @end
  463. @implementation FIRQuery (Internal)
  464. - (FSTQuery *)query {
  465. return _query.query();
  466. }
  467. - (const api::Query &)apiQuery {
  468. return _query;
  469. }
  470. @end
  471. NS_ASSUME_NONNULL_END