FIRQuery.mm 27 KB

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