FIRAggregateQuery.mm 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2022 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 "FIRAggregateQuery+Internal.h"
  17. #import "FIRAggregateQuerySnapshot+Internal.h"
  18. #import "FIRQuery+Internal.h"
  19. #include "Firestore/core/src/api/aggregate_query.h"
  20. #include "Firestore/core/src/api/query_core.h"
  21. #include "Firestore/core/src/util/error_apple.h"
  22. #include "Firestore/core/src/util/statusor.h"
  23. NS_ASSUME_NONNULL_BEGIN
  24. #pragma mark - FIRAggregateQuery
  25. @implementation FIRAggregateQuery {
  26. FIRQuery *_query;
  27. std::unique_ptr<api::AggregateQuery> _aggregation;
  28. }
  29. - (instancetype)initWithQuery:(FIRQuery *)query {
  30. if (self = [super init]) {
  31. _query = query;
  32. _aggregation = absl::make_unique<api::AggregateQuery>(query.apiQuery.Count());
  33. }
  34. return self;
  35. }
  36. #pragma mark - NSObject Methods
  37. - (BOOL)isEqual:(nullable id)other {
  38. if (other == self) return YES;
  39. if (![[other class] isEqual:[self class]]) return NO;
  40. auto otherQuery = static_cast<FIRAggregateQuery *>(other);
  41. return [_query isEqual:otherQuery->_query];
  42. }
  43. - (NSUInteger)hash {
  44. return [_query hash];
  45. }
  46. #pragma mark - Public Methods
  47. - (FIRQuery *)query {
  48. return _query;
  49. }
  50. - (void)aggregationWithSource:(FIRAggregateSource)source
  51. completion:(void (^)(FIRAggregateQuerySnapshot *_Nullable snapshot,
  52. NSError *_Nullable error))completion {
  53. _aggregation->Get([self, completion](const firebase::firestore::util::StatusOr<int64_t> &result) {
  54. if (result.ok()) {
  55. completion([[FIRAggregateQuerySnapshot alloc] initWithCount:result.ValueOrDie() query:self],
  56. nil);
  57. } else {
  58. completion(nil, MakeNSError(result.status()));
  59. }
  60. });
  61. }
  62. @end
  63. NS_ASSUME_NONNULL_END