FIRQuery.mm 29 KB

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