FIRQuery.mm 27 KB

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