FIRQuery.mm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 "FIRFirestoreSource.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/FIRListenerRegistration+Internal.h"
  28. #import "Firestore/Source/API/FIRQuery+Internal.h"
  29. #import "Firestore/Source/API/FIRQuerySnapshot+Internal.h"
  30. #import "Firestore/Source/API/FIRQuery_Init.h"
  31. #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
  32. #import "Firestore/Source/API/FSTUserDataConverter.h"
  33. #import "Firestore/Source/Core/FSTEventManager.h"
  34. #import "Firestore/Source/Core/FSTFirestoreClient.h"
  35. #import "Firestore/Source/Core/FSTQuery.h"
  36. #import "Firestore/Source/Model/FSTDocument.h"
  37. #import "Firestore/Source/Model/FSTFieldValue.h"
  38. #import "Firestore/Source/Util/FSTUsageValidation.h"
  39. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  40. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  41. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  42. #include "Firestore/core/src/firebase/firestore/util/error_apple.h"
  43. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  44. #include "Firestore/core/src/firebase/firestore/util/statusor.h"
  45. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  46. namespace util = firebase::firestore::util;
  47. using firebase::firestore::core::AsyncEventListener;
  48. using firebase::firestore::core::EventListener;
  49. using firebase::firestore::core::ViewSnapshot;
  50. using firebase::firestore::model::DocumentKey;
  51. using firebase::firestore::model::FieldPath;
  52. using firebase::firestore::model::ResourcePath;
  53. using firebase::firestore::util::MakeNSError;
  54. using firebase::firestore::util::StatusOr;
  55. NS_ASSUME_NONNULL_BEGIN
  56. @interface FIRQuery ()
  57. @property(nonatomic, strong, readonly) FSTQuery *query;
  58. @end
  59. @implementation FIRQuery (Internal)
  60. + (instancetype)referenceWithQuery:(FSTQuery *)query firestore:(FIRFirestore *)firestore {
  61. return [[FIRQuery alloc] initWithQuery:query firestore:firestore];
  62. }
  63. @end
  64. @implementation FIRQuery
  65. #pragma mark - Constructor Methods
  66. - (instancetype)initWithQuery:(FSTQuery *)query firestore:(FIRFirestore *)firestore {
  67. if (self = [super init]) {
  68. _query = query;
  69. _firestore = firestore;
  70. }
  71. return self;
  72. }
  73. #pragma mark - NSObject Methods
  74. - (BOOL)isEqual:(nullable id)other {
  75. if (other == self) return YES;
  76. if (![[other class] isEqual:[self class]]) return NO;
  77. return [self isEqualToQuery:other];
  78. }
  79. - (BOOL)isEqualToQuery:(nullable FIRQuery *)query {
  80. if (self == query) return YES;
  81. if (query == nil) return NO;
  82. return [self.firestore isEqual:query.firestore] && [self.query isEqual:query.query];
  83. }
  84. - (NSUInteger)hash {
  85. NSUInteger hash = [self.firestore hash];
  86. hash = hash * 31u + [self.query hash];
  87. return hash;
  88. }
  89. #pragma mark - Public Methods
  90. - (void)getDocumentsWithCompletion:(void (^)(FIRQuerySnapshot *_Nullable snapshot,
  91. NSError *_Nullable error))completion {
  92. [self getDocumentsWithSource:FIRFirestoreSourceDefault completion:completion];
  93. }
  94. - (void)getDocumentsWithSource:(FIRFirestoreSource)source
  95. completion:(void (^)(FIRQuerySnapshot *_Nullable snapshot,
  96. NSError *_Nullable error))completion {
  97. if (source == FIRFirestoreSourceCache) {
  98. [self.firestore.client getDocumentsFromLocalCache:self completion:completion];
  99. return;
  100. }
  101. ListenOptions listenOptions(
  102. /*include_query_metadata_changes=*/true,
  103. /*include_document_metadata_changes=*/true,
  104. /*wait_for_sync_when_online=*/true);
  105. dispatch_semaphore_t registered = dispatch_semaphore_create(0);
  106. __block id<FIRListenerRegistration> listenerRegistration;
  107. FIRQuerySnapshotBlock listener = ^(FIRQuerySnapshot *snapshot, NSError *error) {
  108. if (error) {
  109. completion(nil, error);
  110. return;
  111. }
  112. // Remove query first before passing event to user to avoid user actions affecting the
  113. // now stale query.
  114. dispatch_semaphore_wait(registered, DISPATCH_TIME_FOREVER);
  115. [listenerRegistration remove];
  116. if (snapshot.metadata.fromCache && source == FIRFirestoreSourceServer) {
  117. completion(nil,
  118. [NSError errorWithDomain:FIRFirestoreErrorDomain
  119. code:FIRFirestoreErrorCodeUnavailable
  120. userInfo:@{
  121. NSLocalizedDescriptionKey :
  122. @"Failed to get documents from server. (However, these "
  123. @"documents may exist in the local cache. Run again "
  124. @"without setting source to FIRFirestoreSourceServer to "
  125. @"retrieve the cached documents.)"
  126. }]);
  127. } else {
  128. completion(snapshot, nil);
  129. }
  130. };
  131. listenerRegistration = [self addSnapshotListenerInternalWithOptions:listenOptions
  132. listener:listener];
  133. dispatch_semaphore_signal(registered);
  134. }
  135. - (id<FIRListenerRegistration>)addSnapshotListener:(FIRQuerySnapshotBlock)listener {
  136. return [self addSnapshotListenerWithIncludeMetadataChanges:NO listener:listener];
  137. }
  138. - (id<FIRListenerRegistration>)
  139. addSnapshotListenerWithIncludeMetadataChanges:(BOOL)includeMetadataChanges
  140. listener:(FIRQuerySnapshotBlock)listener {
  141. auto options = ListenOptions::FromIncludeMetadataChanges(includeMetadataChanges);
  142. return [self addSnapshotListenerInternalWithOptions:options listener:listener];
  143. }
  144. - (id<FIRListenerRegistration>)addSnapshotListenerInternalWithOptions:(ListenOptions)internalOptions
  145. listener:
  146. (FIRQuerySnapshotBlock)listener {
  147. Firestore *firestore = self.firestore.wrapped;
  148. FSTQuery *query = self.query;
  149. // Convert from ViewSnapshots to QuerySnapshots.
  150. auto view_listener = EventListener<ViewSnapshot>::Create(
  151. [listener, firestore, query](StatusOr<ViewSnapshot> maybe_snapshot) {
  152. if (!maybe_snapshot.status().ok()) {
  153. listener(nil, MakeNSError(maybe_snapshot.status()));
  154. return;
  155. }
  156. ViewSnapshot snapshot = std::move(maybe_snapshot).ValueOrDie();
  157. SnapshotMetadata metadata(snapshot.has_pending_writes(), snapshot.from_cache());
  158. listener([[FIRQuerySnapshot alloc] initWithFirestore:firestore
  159. originalQuery:query
  160. snapshot:std::move(snapshot)
  161. metadata:std::move(metadata)],
  162. nil);
  163. });
  164. // Call the view_listener on the user Executor.
  165. auto async_listener = AsyncEventListener<ViewSnapshot>::Create(firestore->client().userExecutor,
  166. std::move(view_listener));
  167. std::shared_ptr<QueryListener> query_listener =
  168. [firestore->client() listenToQuery:query options:internalOptions listener:async_listener];
  169. return [[FSTListenerRegistration alloc]
  170. initWithRegistration:ListenerRegistration(firestore->client(), std::move(async_listener),
  171. std::move(query_listener))];
  172. }
  173. - (FIRQuery *)queryWhereField:(NSString *)field isEqualTo:(id)value {
  174. return [self queryWithFilterOperator:FSTRelationFilterOperatorEqual field:field value:value];
  175. }
  176. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path isEqualTo:(id)value {
  177. return [self queryWithFilterOperator:FSTRelationFilterOperatorEqual
  178. path:path.internalValue
  179. value:value];
  180. }
  181. - (FIRQuery *)queryWhereField:(NSString *)field isLessThan:(id)value {
  182. return [self queryWithFilterOperator:FSTRelationFilterOperatorLessThan field:field value:value];
  183. }
  184. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path isLessThan:(id)value {
  185. return [self queryWithFilterOperator:FSTRelationFilterOperatorLessThan
  186. path:path.internalValue
  187. value:value];
  188. }
  189. - (FIRQuery *)queryWhereField:(NSString *)field isLessThanOrEqualTo:(id)value {
  190. return [self queryWithFilterOperator:FSTRelationFilterOperatorLessThanOrEqual
  191. field:field
  192. value:value];
  193. }
  194. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path isLessThanOrEqualTo:(id)value {
  195. return [self queryWithFilterOperator:FSTRelationFilterOperatorLessThanOrEqual
  196. path:path.internalValue
  197. value:value];
  198. }
  199. - (FIRQuery *)queryWhereField:(NSString *)field isGreaterThan:(id)value {
  200. return [self queryWithFilterOperator:FSTRelationFilterOperatorGreaterThan
  201. field:field
  202. value:value];
  203. }
  204. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path isGreaterThan:(id)value {
  205. return [self queryWithFilterOperator:FSTRelationFilterOperatorGreaterThan
  206. path:path.internalValue
  207. value:value];
  208. }
  209. - (FIRQuery *)queryWhereField:(NSString *)field arrayContains:(id)value {
  210. return [self queryWithFilterOperator:FSTRelationFilterOperatorArrayContains
  211. field:field
  212. value:value];
  213. }
  214. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path arrayContains:(id)value {
  215. return [self queryWithFilterOperator:FSTRelationFilterOperatorArrayContains
  216. path:path.internalValue
  217. value:value];
  218. }
  219. - (FIRQuery *)queryWhereField:(NSString *)field isGreaterThanOrEqualTo:(id)value {
  220. return [self queryWithFilterOperator:FSTRelationFilterOperatorGreaterThanOrEqual
  221. field:field
  222. value:value];
  223. }
  224. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path isGreaterThanOrEqualTo:(id)value {
  225. return [self queryWithFilterOperator:FSTRelationFilterOperatorGreaterThanOrEqual
  226. path:path.internalValue
  227. value:value];
  228. }
  229. - (FIRQuery *)queryFilteredUsingComparisonPredicate:(NSPredicate *)predicate {
  230. NSComparisonPredicate *comparison = (NSComparisonPredicate *)predicate;
  231. if (comparison.comparisonPredicateModifier != NSDirectPredicateModifier) {
  232. FSTThrowInvalidArgument(@"Invalid query. Predicate cannot have an aggregate modifier.");
  233. }
  234. NSString *path;
  235. id value = nil;
  236. if ([comparison.leftExpression expressionType] == NSKeyPathExpressionType &&
  237. [comparison.rightExpression expressionType] == NSConstantValueExpressionType) {
  238. path = comparison.leftExpression.keyPath;
  239. value = comparison.rightExpression.constantValue;
  240. switch (comparison.predicateOperatorType) {
  241. case NSEqualToPredicateOperatorType:
  242. return [self queryWhereField:path isEqualTo:value];
  243. case NSLessThanPredicateOperatorType:
  244. return [self queryWhereField:path isLessThan:value];
  245. case NSLessThanOrEqualToPredicateOperatorType:
  246. return [self queryWhereField:path isLessThanOrEqualTo:value];
  247. case NSGreaterThanPredicateOperatorType:
  248. return [self queryWhereField:path isGreaterThan:value];
  249. case NSGreaterThanOrEqualToPredicateOperatorType:
  250. return [self queryWhereField:path isGreaterThanOrEqualTo:value];
  251. default:; // Fallback below to throw assertion.
  252. }
  253. } else if ([comparison.leftExpression expressionType] == NSConstantValueExpressionType &&
  254. [comparison.rightExpression expressionType] == NSKeyPathExpressionType) {
  255. path = comparison.rightExpression.keyPath;
  256. value = comparison.leftExpression.constantValue;
  257. switch (comparison.predicateOperatorType) {
  258. case NSEqualToPredicateOperatorType:
  259. return [self queryWhereField:path isEqualTo:value];
  260. case NSLessThanPredicateOperatorType:
  261. return [self queryWhereField:path isGreaterThan:value];
  262. case NSLessThanOrEqualToPredicateOperatorType:
  263. return [self queryWhereField:path isGreaterThanOrEqualTo:value];
  264. case NSGreaterThanPredicateOperatorType:
  265. return [self queryWhereField:path isLessThan:value];
  266. case NSGreaterThanOrEqualToPredicateOperatorType:
  267. return [self queryWhereField:path isLessThanOrEqualTo:value];
  268. default:; // Fallback below to throw assertion.
  269. }
  270. } else {
  271. FSTThrowInvalidArgument(
  272. @"Invalid query. Predicate comparisons must include a key path and a constant.");
  273. }
  274. // Fallback cases of unsupported comparison operator.
  275. switch (comparison.predicateOperatorType) {
  276. case NSCustomSelectorPredicateOperatorType:
  277. FSTThrowInvalidArgument(@"Invalid query. Custom predicate filters are not supported.");
  278. break;
  279. default:
  280. FSTThrowInvalidArgument(@"Invalid query. Operator type %lu is not supported.",
  281. (unsigned long)comparison.predicateOperatorType);
  282. }
  283. }
  284. - (FIRQuery *)queryFilteredUsingCompoundPredicate:(NSPredicate *)predicate {
  285. NSCompoundPredicate *compound = (NSCompoundPredicate *)predicate;
  286. if (compound.compoundPredicateType != NSAndPredicateType || compound.subpredicates.count == 0) {
  287. FSTThrowInvalidArgument(@"Invalid query. Only compound queries using AND are supported.");
  288. }
  289. FIRQuery *query = self;
  290. for (NSPredicate *pred in compound.subpredicates) {
  291. query = [query queryFilteredUsingPredicate:pred];
  292. }
  293. return query;
  294. }
  295. - (FIRQuery *)queryFilteredUsingPredicate:(NSPredicate *)predicate {
  296. if ([predicate isKindOfClass:[NSComparisonPredicate class]]) {
  297. return [self queryFilteredUsingComparisonPredicate:predicate];
  298. } else if ([predicate isKindOfClass:[NSCompoundPredicate class]]) {
  299. return [self queryFilteredUsingCompoundPredicate:predicate];
  300. } else if ([predicate isKindOfClass:[[NSPredicate
  301. predicateWithBlock:^BOOL(id obj, NSDictionary *bindings) {
  302. return true;
  303. }] class]]) {
  304. FSTThrowInvalidArgument(@"Invalid query. Block-based predicates are not "
  305. "supported. Please use predicateWithFormat to "
  306. "create predicates instead.");
  307. } else {
  308. FSTThrowInvalidArgument(@"Invalid query. Expect comparison or compound of "
  309. "comparison predicate. Please use "
  310. "predicateWithFormat to create predicates.");
  311. }
  312. }
  313. - (FIRQuery *)queryOrderedByField:(NSString *)field {
  314. return [self queryOrderedByFieldPath:[FIRFieldPath pathWithDotSeparatedString:field]
  315. descending:NO];
  316. }
  317. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)fieldPath {
  318. return [self queryOrderedByFieldPath:fieldPath descending:NO];
  319. }
  320. - (FIRQuery *)queryOrderedByField:(NSString *)field descending:(BOOL)descending {
  321. return [self queryOrderedByFieldPath:[FIRFieldPath pathWithDotSeparatedString:field]
  322. descending:descending];
  323. }
  324. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)fieldPath descending:(BOOL)descending {
  325. [self validateNewOrderByPath:fieldPath.internalValue];
  326. if (self.query.startAt) {
  327. FSTThrowInvalidUsage(
  328. @"InvalidQueryException",
  329. @"Invalid query. You must not specify a starting point before specifying the order by.");
  330. }
  331. if (self.query.endAt) {
  332. FSTThrowInvalidUsage(
  333. @"InvalidQueryException",
  334. @"Invalid query. You must not specify an ending point before specifying the order by.");
  335. }
  336. FSTSortOrder *sortOrder = [FSTSortOrder sortOrderWithFieldPath:fieldPath.internalValue
  337. ascending:!descending];
  338. return [FIRQuery referenceWithQuery:[self.query queryByAddingSortOrder:sortOrder]
  339. firestore:self.firestore];
  340. }
  341. - (FIRQuery *)queryLimitedTo:(NSInteger)limit {
  342. if (limit <= 0) {
  343. FSTThrowInvalidArgument(@"Invalid Query. Query limit (%ld) is invalid. Limit must be positive.",
  344. (long)limit);
  345. }
  346. return [FIRQuery referenceWithQuery:[self.query queryBySettingLimit:limit] firestore:_firestore];
  347. }
  348. - (FIRQuery *)queryStartingAtDocument:(FIRDocumentSnapshot *)snapshot {
  349. FSTBound *bound = [self boundFromSnapshot:snapshot isBefore:YES];
  350. return [FIRQuery referenceWithQuery:[self.query queryByAddingStartAt:bound]
  351. firestore:self.firestore];
  352. }
  353. - (FIRQuery *)queryStartingAtValues:(NSArray *)fieldValues {
  354. FSTBound *bound = [self boundFromFieldValues:fieldValues isBefore:YES];
  355. return [FIRQuery referenceWithQuery:[self.query queryByAddingStartAt:bound]
  356. firestore:self.firestore];
  357. }
  358. - (FIRQuery *)queryStartingAfterDocument:(FIRDocumentSnapshot *)snapshot {
  359. FSTBound *bound = [self boundFromSnapshot:snapshot isBefore:NO];
  360. return [FIRQuery referenceWithQuery:[self.query queryByAddingStartAt:bound]
  361. firestore:self.firestore];
  362. }
  363. - (FIRQuery *)queryStartingAfterValues:(NSArray *)fieldValues {
  364. FSTBound *bound = [self boundFromFieldValues:fieldValues isBefore:NO];
  365. return [FIRQuery referenceWithQuery:[self.query queryByAddingStartAt:bound]
  366. firestore:self.firestore];
  367. }
  368. - (FIRQuery *)queryEndingBeforeDocument:(FIRDocumentSnapshot *)snapshot {
  369. FSTBound *bound = [self boundFromSnapshot:snapshot isBefore:YES];
  370. return [FIRQuery referenceWithQuery:[self.query queryByAddingEndAt:bound]
  371. firestore:self.firestore];
  372. }
  373. - (FIRQuery *)queryEndingBeforeValues:(NSArray *)fieldValues {
  374. FSTBound *bound = [self boundFromFieldValues:fieldValues isBefore:YES];
  375. return [FIRQuery referenceWithQuery:[self.query queryByAddingEndAt:bound]
  376. firestore:self.firestore];
  377. }
  378. - (FIRQuery *)queryEndingAtDocument:(FIRDocumentSnapshot *)snapshot {
  379. FSTBound *bound = [self boundFromSnapshot:snapshot isBefore:NO];
  380. return [FIRQuery referenceWithQuery:[self.query queryByAddingEndAt:bound]
  381. firestore:self.firestore];
  382. }
  383. - (FIRQuery *)queryEndingAtValues:(NSArray *)fieldValues {
  384. FSTBound *bound = [self boundFromFieldValues:fieldValues isBefore:NO];
  385. return [FIRQuery referenceWithQuery:[self.query queryByAddingEndAt:bound]
  386. firestore:self.firestore];
  387. }
  388. #pragma mark - Private Methods
  389. /** Private helper for all of the queryWhereField: methods. */
  390. - (FIRQuery *)queryWithFilterOperator:(FSTRelationFilterOperator)filterOperator
  391. field:(NSString *)field
  392. value:(id)value {
  393. return [self queryWithFilterOperator:filterOperator
  394. path:[FIRFieldPath pathWithDotSeparatedString:field].internalValue
  395. value:value];
  396. }
  397. - (FIRQuery *)queryWithFilterOperator:(FSTRelationFilterOperator)filterOperator
  398. path:(const FieldPath &)fieldPath
  399. value:(id)value {
  400. FSTFieldValue *fieldValue;
  401. if (fieldPath.IsKeyFieldPath()) {
  402. if (filterOperator == FSTRelationFilterOperatorArrayContains) {
  403. FSTThrowInvalidArgument(
  404. @"Invalid query. You can't perform arrayContains queries on document ID since document "
  405. "IDs are not arrays.");
  406. }
  407. if ([value isKindOfClass:[NSString class]]) {
  408. NSString *documentKey = (NSString *)value;
  409. if (documentKey.length == 0) {
  410. FSTThrowInvalidArgument(@"Invalid query. When querying by document ID you must provide "
  411. "a valid document ID, but it was an empty string.");
  412. }
  413. if (![self.query isCollectionGroupQuery] && [documentKey containsString:@"/"]) {
  414. FSTThrowInvalidArgument(
  415. @"Invalid query. When querying a collection by document ID you must provide "
  416. "a plain document ID, but '%@' contains a '/' character.",
  417. documentKey);
  418. }
  419. ResourcePath path =
  420. self.query.path.Append(ResourcePath::FromString([documentKey UTF8String]));
  421. if (!DocumentKey::IsDocumentKey(path)) {
  422. FSTThrowInvalidArgument(
  423. @"Invalid query. When querying a collection group by document ID, "
  424. "the value provided must result in a valid document path, but '%s' is not because it "
  425. "has an odd number of segments.",
  426. path.CanonicalString().c_str());
  427. }
  428. fieldValue =
  429. [FSTReferenceValue referenceValue:[FSTDocumentKey keyWithDocumentKey:DocumentKey{path}]
  430. databaseID:self.firestore.databaseID];
  431. } else if ([value isKindOfClass:[FIRDocumentReference class]]) {
  432. FIRDocumentReference *ref = (FIRDocumentReference *)value;
  433. fieldValue = [FSTReferenceValue referenceValue:[FSTDocumentKey keyWithDocumentKey:ref.key]
  434. databaseID:self.firestore.databaseID];
  435. } else {
  436. FSTThrowInvalidArgument(@"Invalid query. When querying by document ID you must provide a "
  437. "valid string or DocumentReference, but it was of type: %@",
  438. NSStringFromClass([value class]));
  439. }
  440. } else {
  441. fieldValue = [self.firestore.dataConverter parsedQueryValue:value];
  442. }
  443. FSTFilter *filter = [FSTFilter filterWithField:fieldPath
  444. filterOperator:filterOperator
  445. value:fieldValue];
  446. if ([filter isKindOfClass:[FSTRelationFilter class]]) {
  447. [self validateNewRelationFilter:(FSTRelationFilter *)filter];
  448. }
  449. return [FIRQuery referenceWithQuery:[self.query queryByAddingFilter:filter]
  450. firestore:self.firestore];
  451. }
  452. - (void)validateNewRelationFilter:(FSTRelationFilter *)filter {
  453. if ([filter isInequality]) {
  454. const FieldPath *existingField = [self.query inequalityFilterField];
  455. if (existingField && *existingField != filter.field) {
  456. FSTThrowInvalidUsage(
  457. @"InvalidQueryException",
  458. @"Invalid Query. All where filters with an inequality "
  459. "(lessThan, lessThanOrEqual, greaterThan, or greaterThanOrEqual) must be on the same "
  460. "field. But you have inequality filters on '%s' and '%s'",
  461. existingField->CanonicalString().c_str(), filter.field.CanonicalString().c_str());
  462. }
  463. const FieldPath *firstOrderByField = [self.query firstSortOrderField];
  464. if (firstOrderByField) {
  465. [self validateOrderByField:*firstOrderByField matchesInequalityField:filter.field];
  466. }
  467. } else if (filter.filterOperator == FSTRelationFilterOperatorArrayContains) {
  468. if ([self.query hasArrayContainsFilter]) {
  469. FSTThrowInvalidUsage(@"InvalidQueryException",
  470. @"Invalid Query. Queries only support a single arrayContains filter.");
  471. }
  472. }
  473. }
  474. - (void)validateNewOrderByPath:(const FieldPath &)fieldPath {
  475. if (![self.query firstSortOrderField]) {
  476. // This is the first order by. It must match any inequality.
  477. const FieldPath *inequalityField = [self.query inequalityFilterField];
  478. if (inequalityField) {
  479. [self validateOrderByField:fieldPath matchesInequalityField:*inequalityField];
  480. }
  481. }
  482. }
  483. - (void)validateOrderByField:(const FieldPath &)orderByField
  484. matchesInequalityField:(const FieldPath &)inequalityField {
  485. if (orderByField != inequalityField) {
  486. FSTThrowInvalidUsage(
  487. @"InvalidQueryException",
  488. @"Invalid query. You have a where filter with an "
  489. "inequality (lessThan, lessThanOrEqual, greaterThan, or greaterThanOrEqual) on field '%s' "
  490. "and so you must also use '%s' as your first queryOrderedBy field, but your first "
  491. "queryOrderedBy is currently on field '%s' instead.",
  492. inequalityField.CanonicalString().c_str(), inequalityField.CanonicalString().c_str(),
  493. orderByField.CanonicalString().c_str());
  494. }
  495. }
  496. /**
  497. * Create a FSTBound from a query given the document.
  498. *
  499. * Note that the FSTBound will always include the key of the document and the position will be
  500. * unambiguous.
  501. *
  502. * Will throw if the document does not contain all fields of the order by of
  503. * the query or if any of the fields in the order by are an uncommitted server
  504. * timestamp.
  505. */
  506. - (FSTBound *)boundFromSnapshot:(FIRDocumentSnapshot *)snapshot isBefore:(BOOL)isBefore {
  507. if (![snapshot exists]) {
  508. FSTThrowInvalidUsage(@"InvalidQueryException",
  509. @"Invalid query. You are trying to start or end a query using a document "
  510. @"that doesn't exist.");
  511. }
  512. FSTDocument *document = snapshot.internalDocument;
  513. NSMutableArray<FSTFieldValue *> *components = [NSMutableArray array];
  514. // Because people expect to continue/end a query at the exact document provided, we need to
  515. // use the implicit sort order rather than the explicit sort order, because it's guaranteed to
  516. // contain the document key. That way the position becomes unambiguous and the query
  517. // continues/ends exactly at the provided document. Without the key (by using the explicit sort
  518. // orders), multiple documents could match the position, yielding duplicate results.
  519. for (FSTSortOrder *sortOrder in self.query.sortOrders) {
  520. if (sortOrder.field == FieldPath::KeyFieldPath()) {
  521. [components addObject:[FSTReferenceValue
  522. referenceValue:[FSTDocumentKey keyWithDocumentKey:document.key]
  523. databaseID:self.firestore.databaseID]];
  524. } else {
  525. FSTFieldValue *value = [document fieldForPath:sortOrder.field];
  526. if ([value isKindOfClass:[FSTServerTimestampValue class]]) {
  527. FSTThrowInvalidUsage(@"InvalidQueryException",
  528. @"Invalid query. You are trying to start or end a query using a "
  529. "document for which the field '%s' is an uncommitted server "
  530. "timestamp. (Since the value of this field is unknown, you cannot "
  531. "start/end a query with it.)",
  532. sortOrder.field.CanonicalString().c_str());
  533. } else if (value != nil) {
  534. [components addObject:value];
  535. } else {
  536. FSTThrowInvalidUsage(@"InvalidQueryException",
  537. @"Invalid query. You are trying to start or end a query using a "
  538. "document for which the field '%s' (used as the order by) "
  539. "does not exist.",
  540. sortOrder.field.CanonicalString().c_str());
  541. }
  542. }
  543. }
  544. return [FSTBound boundWithPosition:components isBefore:isBefore];
  545. }
  546. /** Converts a list of field values to an FSTBound. */
  547. - (FSTBound *)boundFromFieldValues:(NSArray<id> *)fieldValues isBefore:(BOOL)isBefore {
  548. // Use explicit sort order because it has to match the query the user made
  549. NSArray<FSTSortOrder *> *explicitSortOrders = self.query.explicitSortOrders;
  550. if (fieldValues.count > explicitSortOrders.count) {
  551. FSTThrowInvalidUsage(@"InvalidQueryException",
  552. @"Invalid query. You are trying to start or end a query using more values "
  553. @"than were specified in the order by.");
  554. }
  555. NSMutableArray<FSTFieldValue *> *components = [NSMutableArray array];
  556. [fieldValues enumerateObjectsUsingBlock:^(id rawValue, NSUInteger idx, BOOL *stop) {
  557. FSTSortOrder *sortOrder = explicitSortOrders[idx];
  558. if (sortOrder.field == FieldPath::KeyFieldPath()) {
  559. if (![rawValue isKindOfClass:[NSString class]]) {
  560. FSTThrowInvalidUsage(@"InvalidQueryException",
  561. @"Invalid query. Expected a string for the document ID.");
  562. }
  563. NSString *documentID = (NSString *)rawValue;
  564. if (![self.query isCollectionGroupQuery] && [documentID containsString:@"/"]) {
  565. FSTThrowInvalidUsage(
  566. @"InvalidQueryException",
  567. @"Invalid query. When querying a collection and ordering by document ID, "
  568. "you must pass a plain document ID, but '%@' contains a slash.",
  569. documentID);
  570. }
  571. ResourcePath path = self.query.path.Append(ResourcePath::FromString([documentID UTF8String]));
  572. if (!DocumentKey::IsDocumentKey(path)) {
  573. FSTThrowInvalidUsage(
  574. @"InvalidQueryException",
  575. @"Invalid query. When querying a collection group and ordering by document ID, "
  576. "you must pass a value that results in a valid document path, but '%s' "
  577. "is not because it contains an odd number of segments.",
  578. path.CanonicalString().c_str());
  579. }
  580. DocumentKey key{path};
  581. [components
  582. addObject:[FSTReferenceValue referenceValue:[FSTDocumentKey keyWithDocumentKey:key]
  583. databaseID:self.firestore.databaseID]];
  584. } else {
  585. FSTFieldValue *fieldValue = [self.firestore.dataConverter parsedQueryValue:rawValue];
  586. [components addObject:fieldValue];
  587. }
  588. }];
  589. return [FSTBound boundWithPosition:components isBefore:isBefore];
  590. }
  591. @end
  592. NS_ASSUME_NONNULL_END