FIRQuery.mm 24 KB

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