FIRAggregateQuery.mm 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "Firestore/Source/API/FIRAggregateField+Internal.h"
  18. #import "Firestore/Source/API/FIRAggregateQuerySnapshot+Internal.h"
  19. #import "Firestore/Source/API/FIRQuery+Internal.h"
  20. #include "Firestore/core/src/api/aggregate_query.h"
  21. #include "Firestore/core/src/util/error_apple.h"
  22. using firebase::firestore::api::AggregateQuery;
  23. using firebase::firestore::model::AggregateField;
  24. using firebase::firestore::model::ObjectValue;
  25. using firebase::firestore::util::StatusOr;
  26. NS_ASSUME_NONNULL_BEGIN
  27. #pragma mark - FIRAggregateQuery
  28. @implementation FIRAggregateQuery {
  29. FIRQuery *_query;
  30. std::unique_ptr<AggregateQuery> _aggregateQuery;
  31. }
  32. - (instancetype)initWithQueryAndAggregations:(FIRQuery *)query
  33. aggregations:(NSArray<FIRAggregateField *> *)aggregations {
  34. if (self = [super init]) {
  35. _query = query;
  36. std::vector<AggregateField> _aggregateFields;
  37. for (FIRAggregateField *field in aggregations) {
  38. _aggregateFields.push_back([field createInternalValue]);
  39. }
  40. _aggregateQuery =
  41. absl::make_unique<AggregateQuery>(query.apiQuery.Aggregate(std::move(_aggregateFields)));
  42. }
  43. return self;
  44. }
  45. #pragma mark - NSObject Methods
  46. - (BOOL)isEqual:(nullable id)other {
  47. if (other == self) return YES;
  48. if (![[other class] isEqual:[self class]]) return NO;
  49. auto otherQuery = static_cast<FIRAggregateQuery *>(other);
  50. return [_query isEqual:otherQuery->_query] && *_aggregateQuery == *(otherQuery->_aggregateQuery);
  51. }
  52. - (NSUInteger)hash {
  53. return _aggregateQuery->Hash();
  54. }
  55. #pragma mark - Public Methods
  56. - (FIRQuery *)query {
  57. return _query;
  58. }
  59. - (void)aggregationWithSource:(FIRAggregateSource)source
  60. completion:(void (^)(FIRAggregateQuerySnapshot *_Nullable snapshot,
  61. NSError *_Nullable error))completion {
  62. _aggregateQuery->GetAggregate([self, completion](const StatusOr<ObjectValue> &result) {
  63. if (result.ok()) {
  64. completion([[FIRAggregateQuerySnapshot alloc] initWithObject:result.ValueOrDie() query:self],
  65. nil);
  66. } else {
  67. completion(nil, MakeNSError(result.status()));
  68. }
  69. });
  70. }
  71. @end
  72. NS_ASSUME_NONNULL_END