FIRQuery.mm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 *)queryFilteredUsingComparisonPredicate:(NSPredicate *)predicate {
  212. NSComparisonPredicate *comparison = (NSComparisonPredicate *)predicate;
  213. if (comparison.comparisonPredicateModifier != NSDirectPredicateModifier) {
  214. ThrowInvalidArgument("Invalid query. Predicate cannot have an aggregate modifier.");
  215. }
  216. NSString *path;
  217. id value = nil;
  218. if ([comparison.leftExpression expressionType] == NSKeyPathExpressionType &&
  219. [comparison.rightExpression expressionType] == NSConstantValueExpressionType) {
  220. path = comparison.leftExpression.keyPath;
  221. value = comparison.rightExpression.constantValue;
  222. switch (comparison.predicateOperatorType) {
  223. case NSEqualToPredicateOperatorType:
  224. return [self queryWhereField:path isEqualTo:value];
  225. case NSLessThanPredicateOperatorType:
  226. return [self queryWhereField:path isLessThan:value];
  227. case NSLessThanOrEqualToPredicateOperatorType:
  228. return [self queryWhereField:path isLessThanOrEqualTo:value];
  229. case NSGreaterThanPredicateOperatorType:
  230. return [self queryWhereField:path isGreaterThan:value];
  231. case NSGreaterThanOrEqualToPredicateOperatorType:
  232. return [self queryWhereField:path isGreaterThanOrEqualTo:value];
  233. default:; // Fallback below to throw assertion.
  234. }
  235. } else if ([comparison.leftExpression expressionType] == NSConstantValueExpressionType &&
  236. [comparison.rightExpression expressionType] == NSKeyPathExpressionType) {
  237. path = comparison.rightExpression.keyPath;
  238. value = comparison.leftExpression.constantValue;
  239. switch (comparison.predicateOperatorType) {
  240. case NSEqualToPredicateOperatorType:
  241. return [self queryWhereField:path isEqualTo:value];
  242. case NSLessThanPredicateOperatorType:
  243. return [self queryWhereField:path isGreaterThan:value];
  244. case NSLessThanOrEqualToPredicateOperatorType:
  245. return [self queryWhereField:path isGreaterThanOrEqualTo:value];
  246. case NSGreaterThanPredicateOperatorType:
  247. return [self queryWhereField:path isLessThan:value];
  248. case NSGreaterThanOrEqualToPredicateOperatorType:
  249. return [self queryWhereField:path isLessThanOrEqualTo:value];
  250. default:; // Fallback below to throw assertion.
  251. }
  252. } else {
  253. ThrowInvalidArgument(
  254. "Invalid query. Predicate comparisons must include a key path and a constant.");
  255. }
  256. // Fallback cases of unsupported comparison operator.
  257. switch (comparison.predicateOperatorType) {
  258. case NSCustomSelectorPredicateOperatorType:
  259. ThrowInvalidArgument("Invalid query. Custom predicate filters are not supported.");
  260. break;
  261. default:
  262. ThrowInvalidArgument("Invalid query. Operator type %s is not supported.",
  263. comparison.predicateOperatorType);
  264. }
  265. }
  266. - (FIRQuery *)queryFilteredUsingCompoundPredicate:(NSPredicate *)predicate {
  267. NSCompoundPredicate *compound = (NSCompoundPredicate *)predicate;
  268. if (compound.compoundPredicateType != NSAndPredicateType || compound.subpredicates.count == 0) {
  269. ThrowInvalidArgument("Invalid query. Only compound queries using AND are supported.");
  270. }
  271. FIRQuery *query = self;
  272. for (NSPredicate *pred in compound.subpredicates) {
  273. query = [query queryFilteredUsingPredicate:pred];
  274. }
  275. return query;
  276. }
  277. - (FIRQuery *)queryFilteredUsingPredicate:(NSPredicate *)predicate {
  278. if ([predicate isKindOfClass:[NSComparisonPredicate class]]) {
  279. return [self queryFilteredUsingComparisonPredicate:predicate];
  280. } else if ([predicate isKindOfClass:[NSCompoundPredicate class]]) {
  281. return [self queryFilteredUsingCompoundPredicate:predicate];
  282. } else if ([predicate isKindOfClass:[[NSPredicate
  283. predicateWithBlock:^BOOL(id obj, NSDictionary *bindings) {
  284. return true;
  285. }] class]]) {
  286. ThrowInvalidArgument("Invalid query. Block-based predicates are not supported. Please use "
  287. "predicateWithFormat to create predicates instead.");
  288. } else {
  289. ThrowInvalidArgument("Invalid query. Expect comparison or compound of comparison predicate. "
  290. "Please use predicateWithFormat to create predicates.");
  291. }
  292. }
  293. - (FIRQuery *)queryOrderedByField:(NSString *)field {
  294. return [self queryOrderedByFieldPath:[FIRFieldPath pathWithDotSeparatedString:field]
  295. descending:NO];
  296. }
  297. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)fieldPath {
  298. return [self queryOrderedByFieldPath:fieldPath descending:NO];
  299. }
  300. - (FIRQuery *)queryOrderedByField:(NSString *)field descending:(BOOL)descending {
  301. return [self queryOrderedByFieldPath:[FIRFieldPath pathWithDotSeparatedString:field]
  302. descending:descending];
  303. }
  304. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)fieldPath descending:(BOOL)descending {
  305. return Wrap(_query.OrderBy(fieldPath.internalValue, Direction::FromDescending(descending)));
  306. }
  307. - (FIRQuery *)queryLimitedTo:(NSInteger)limit {
  308. int32_t internalLimit;
  309. if (limit == NSNotFound || limit >= core::Query::kNoLimit) {
  310. internalLimit = core::Query::kNoLimit;
  311. } else {
  312. internalLimit = static_cast<int32_t>(limit);
  313. }
  314. return Wrap(_query.Limit(internalLimit));
  315. }
  316. - (FIRQuery *)queryStartingAtDocument:(FIRDocumentSnapshot *)snapshot {
  317. Bound bound = [self boundFromSnapshot:snapshot isBefore:YES];
  318. return Wrap(_query.StartAt(std::move(bound)));
  319. }
  320. - (FIRQuery *)queryStartingAtValues:(NSArray *)fieldValues {
  321. Bound bound = [self boundFromFieldValues:fieldValues isBefore:YES];
  322. return Wrap(_query.StartAt(std::move(bound)));
  323. }
  324. - (FIRQuery *)queryStartingAfterDocument:(FIRDocumentSnapshot *)snapshot {
  325. Bound bound = [self boundFromSnapshot:snapshot isBefore:NO];
  326. return Wrap(_query.StartAt(std::move(bound)));
  327. }
  328. - (FIRQuery *)queryStartingAfterValues:(NSArray *)fieldValues {
  329. Bound bound = [self boundFromFieldValues:fieldValues isBefore:NO];
  330. return Wrap(_query.StartAt(std::move(bound)));
  331. }
  332. - (FIRQuery *)queryEndingBeforeDocument:(FIRDocumentSnapshot *)snapshot {
  333. Bound bound = [self boundFromSnapshot:snapshot isBefore:YES];
  334. return Wrap(_query.EndAt(std::move(bound)));
  335. }
  336. - (FIRQuery *)queryEndingBeforeValues:(NSArray *)fieldValues {
  337. Bound bound = [self boundFromFieldValues:fieldValues isBefore:YES];
  338. return Wrap(_query.EndAt(std::move(bound)));
  339. }
  340. - (FIRQuery *)queryEndingAtDocument:(FIRDocumentSnapshot *)snapshot {
  341. Bound bound = [self boundFromSnapshot:snapshot isBefore:NO];
  342. return Wrap(_query.EndAt(std::move(bound)));
  343. }
  344. - (FIRQuery *)queryEndingAtValues:(NSArray *)fieldValues {
  345. Bound bound = [self boundFromFieldValues:fieldValues isBefore:NO];
  346. return Wrap(_query.EndAt(std::move(bound)));
  347. }
  348. #pragma mark - Private Methods
  349. - (FieldValue)parsedQueryValue:(id)value {
  350. return [self.firestore.dataConverter parsedQueryValue:value];
  351. }
  352. - (QuerySnapshot::Listener)wrapQuerySnapshotBlock:(FIRQuerySnapshotBlock)block {
  353. class Converter : public EventListener<QuerySnapshot> {
  354. public:
  355. explicit Converter(FIRQuerySnapshotBlock block) : block_(block) {
  356. }
  357. void OnEvent(StatusOr<QuerySnapshot> maybe_snapshot) override {
  358. if (maybe_snapshot.ok()) {
  359. FIRQuerySnapshot *result =
  360. [[FIRQuerySnapshot alloc] initWithSnapshot:std::move(maybe_snapshot).ValueOrDie()];
  361. block_(result, nil);
  362. } else {
  363. block_(nil, util::MakeNSError(maybe_snapshot.status()));
  364. }
  365. }
  366. private:
  367. FIRQuerySnapshotBlock block_;
  368. };
  369. return absl::make_unique<Converter>(block);
  370. }
  371. /** Private helper for all of the queryWhereField: methods. */
  372. - (FIRQuery *)queryWithFilterOperator:(Filter::Operator)filterOperator
  373. field:(NSString *)field
  374. value:(id)value {
  375. return [self queryWithFilterOperator:filterOperator path:MakeFieldPath(field) value:value];
  376. }
  377. - (FIRQuery *)queryWithFilterOperator:(Filter::Operator)filterOperator
  378. path:(const FieldPath &)fieldPath
  379. value:(id)value {
  380. FieldValue fieldValue = [self parsedQueryValue:value];
  381. auto describer = [value] { return util::MakeString(NSStringFromClass([value class])); };
  382. return Wrap(_query.Filter(fieldPath, filterOperator, std::move(fieldValue), describer));
  383. }
  384. /**
  385. * Create a Bound from a query given the document.
  386. *
  387. * Note that the Bound will always include the key of the document and the position will be
  388. * unambiguous.
  389. *
  390. * Will throw if the document does not contain all fields of the order by of
  391. * the query or if any of the fields in the order by are an uncommitted server
  392. * timestamp.
  393. */
  394. - (Bound)boundFromSnapshot:(FIRDocumentSnapshot *)snapshot isBefore:(BOOL)isBefore {
  395. if (![snapshot exists]) {
  396. ThrowInvalidArgument("Invalid query. You are trying to start or end a query using a document "
  397. "that doesn't exist.");
  398. }
  399. const Document &document = *snapshot.internalDocument;
  400. const DatabaseId &databaseID = self.firestore.databaseID;
  401. std::vector<FieldValue> components;
  402. // Because people expect to continue/end a query at the exact document provided, we need to
  403. // use the implicit sort order rather than the explicit sort order, because it's guaranteed to
  404. // contain the document key. That way the position becomes unambiguous and the query
  405. // continues/ends exactly at the provided document. Without the key (by using the explicit sort
  406. // orders), multiple documents could match the position, yielding duplicate results.
  407. for (const OrderBy &orderBy : self.query.order_bys()) {
  408. if (orderBy.field() == FieldPath::KeyFieldPath()) {
  409. components.push_back(FieldValue::FromReference(databaseID, document.key()));
  410. } else {
  411. absl::optional<FieldValue> value = document.field(orderBy.field());
  412. if (value) {
  413. if (value->type() == FieldValue::Type::ServerTimestamp) {
  414. ThrowInvalidArgument(
  415. "Invalid query. You are trying to start or end a query using a document for which "
  416. "the field '%s' is an uncommitted server timestamp. (Since the value of this field "
  417. "is unknown, you cannot start/end a query with it.)",
  418. orderBy.field().CanonicalString());
  419. } else {
  420. components.push_back(*value);
  421. }
  422. } else {
  423. ThrowInvalidArgument(
  424. "Invalid query. You are trying to start or end a query using a document for which the "
  425. "field '%s' (used as the order by) does not exist.",
  426. orderBy.field().CanonicalString());
  427. }
  428. }
  429. }
  430. return Bound(std::move(components), isBefore);
  431. }
  432. /** Converts a list of field values to an Bound. */
  433. - (Bound)boundFromFieldValues:(NSArray<id> *)fieldValues isBefore:(BOOL)isBefore {
  434. // Use explicit sort order because it has to match the query the user made
  435. const OrderByList &explicitSortOrders = self.query.explicit_order_bys();
  436. if (fieldValues.count > explicitSortOrders.size()) {
  437. ThrowInvalidArgument("Invalid query. You are trying to start or end a query using more values "
  438. "than were specified in the order by.");
  439. }
  440. std::vector<FieldValue> components;
  441. for (NSUInteger idx = 0, max = fieldValues.count; idx < max; ++idx) {
  442. id rawValue = fieldValues[idx];
  443. const OrderBy &sortOrder = explicitSortOrders[idx];
  444. FieldValue fieldValue = [self parsedQueryValue:rawValue];
  445. if (sortOrder.field().IsKeyFieldPath()) {
  446. if (fieldValue.type() != FieldValue::Type::String) {
  447. ThrowInvalidArgument("Invalid query. Expected a string for the document ID.");
  448. }
  449. const std::string &documentID = fieldValue.string_value();
  450. if (!self.query.IsCollectionGroupQuery() && documentID.find('/') != std::string::npos) {
  451. ThrowInvalidArgument("Invalid query. When querying a collection and ordering by document "
  452. "ID, you must pass a plain document ID, but '%s' contains a slash.",
  453. documentID);
  454. }
  455. ResourcePath path = self.query.path().Append(ResourcePath::FromString(documentID));
  456. if (!DocumentKey::IsDocumentKey(path)) {
  457. ThrowInvalidArgument("Invalid query. When querying a collection group and ordering by "
  458. "document ID, you must pass a value that results in a valid document "
  459. "path, but '%s' is not because it contains an odd number of segments.",
  460. path.CanonicalString());
  461. }
  462. DocumentKey key{path};
  463. fieldValue = FieldValue::FromReference(self.firestore.databaseID, key);
  464. }
  465. components.push_back(fieldValue);
  466. }
  467. return Bound(std::move(components), isBefore);
  468. }
  469. @end
  470. @implementation FIRQuery (Internal)
  471. - (FIRQuery *)queryWhereField:(NSString *)field arrayContainsAny:(NSArray<id> *)value {
  472. return [self queryWithFilterOperator:Filter::Operator::ArrayContainsAny field:field value:value];
  473. }
  474. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path arrayContainsAny:(NSArray<id> *)value {
  475. return [self queryWithFilterOperator:Filter::Operator::ArrayContainsAny
  476. path:path.internalValue
  477. value:value];
  478. }
  479. - (FIRQuery *)queryWhereField:(NSString *)field in:(NSArray<id> *)value {
  480. return [self queryWithFilterOperator:Filter::Operator::In field:field value:value];
  481. }
  482. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path in:(NSArray<id> *)value {
  483. return [self queryWithFilterOperator:Filter::Operator::In path:path.internalValue value:value];
  484. }
  485. - (const core::Query &)query {
  486. return _query.query();
  487. }
  488. - (const api::Query &)apiQuery {
  489. return _query;
  490. }
  491. @end
  492. NS_ASSUME_NONNULL_END