Skip to content

Commit f89d482

Browse files
committed
feat: Add sum method to AggregateQuerySnapshot
1 parent c83c27b commit f89d482

File tree

7 files changed

+37
-3
lines changed

7 files changed

+37
-3
lines changed

firestore/src/android/aggregate_query_android.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class AggregateQueryInternal : public Wrapper {
3333
// Firestore Future manager.
3434
enum class AsyncFn {
3535
kGet = 0,
36-
kCount, // Must be the last enum value.
36+
kCount,
37+
kSum, // Must be the last enum value.
3738
};
3839

3940
static void Initialize(jni::Loader& loader);

firestore/src/android/aggregate_query_snapshot_android.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class AggregateQuerySnapshotInternal : public Wrapper {
3434

3535
AggregateQuery query() const;
3636
int64_t count() const;
37+
double sum(const AggregateField& aggregate_field) const;
3738

3839
std::size_t Hash() const;
3940

firestore/src/common/aggregate_query_snapshot.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ int64_t AggregateQuerySnapshot::count() const {
103103
return internal_->count();
104104
}
105105

106+
double AggregateQuerySnapshot::sum(const AggregateField& aggregate_field) const {
107+
if (!internal_) return 0;
108+
return internal_->sum(aggregate_field);
109+
}
110+
106111
bool operator==(const AggregateQuerySnapshot& lhs,
107112
const AggregateQuerySnapshot& rhs) {
108113
return EqualityCompare(lhs.internal_, rhs.internal_);

firestore/src/include/firebase/firestore/aggregate_query_snapshot.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ class AggregateQuerySnapshot {
108108
*/
109109
virtual int64_t count() const;
110110

111+
/**
112+
* @brief Returns the sum of the specified field across all documents in the
113+
* result set of the underlying query.
114+
*
115+
* @return The sum of the specified field across all documents.
116+
*/
117+
virtual double sum(const AggregateField& aggregate_field) const;
118+
111119
/**
112120
* @brief Returns true if this `AggregateQuerySnapshot` is valid, false if it
113121
* is not valid. An invalid `AggregateQuerySnapshot` could be the result of:

firestore/src/main/aggregate_query_main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class AggregateQueryInternal {
5050
enum class AsyncApis {
5151
kGet,
5252
kCount,
53+
kSum,
5354
};
5455

5556
friend class AggregateQuerySnapshotTest;

firestore/src/main/aggregate_query_snapshot_main.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ namespace firebase {
2424
namespace firestore {
2525

2626
AggregateQuerySnapshotInternal::AggregateQuerySnapshotInternal(
27-
api::AggregateQuery&& aggregate_query, int64_t count_result)
27+
api::AggregateQuery&& aggregate_query, int64_t count_result,
28+
// TODO: Update with sum
29+
// double sum_result,
30+
)
2831
: aggregate_query_(std::move(aggregate_query)),
29-
count_result_(count_result) {}
32+
count_result_(count_result)
33+
// TODO: Update with sum
34+
// sum_result_(sum_result),
35+
{}
3036

3137
FirestoreInternal* AggregateQuerySnapshotInternal::firestore_internal() {
3238
return GetFirestoreInternal(&aggregate_query_.query());
@@ -43,10 +49,16 @@ AggregateQuery AggregateQuerySnapshotInternal::query() const {
4349

4450
int64_t AggregateQuerySnapshotInternal::count() const { return count_result_; }
4551

52+
double AggregateQuerySnapshotInternal::sum(const AggregateField& aggregate_field) const {
53+
// TODO: implement
54+
}
55+
4656
bool operator==(const AggregateQuerySnapshotInternal& lhs,
4757
const AggregateQuerySnapshotInternal& rhs) {
4858
return lhs.aggregate_query_ == rhs.aggregate_query_ &&
4959
lhs.count_result_ == rhs.count_result_;
60+
// TODO: Update with sum
61+
// && lhs.sum_result == rhs.sum_result_;
5062
}
5163

5264
} // namespace firestore

firestore/src/main/aggregate_query_snapshot_main.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,20 @@ class AggregateQuerySnapshotInternal {
3535
public:
3636
explicit AggregateQuerySnapshotInternal(api::AggregateQuery&& aggregate_query,
3737
int64_t count);
38+
// TODO: Update with sum
39+
// double sum);
3840

3941
FirestoreInternal* firestore_internal();
4042
const FirestoreInternal* firestore_internal() const;
4143

4244
AggregateQuery query() const;
4345
int64_t count() const;
46+
double sum(const AggregateField& aggregate_field) const;
4447

4548
std::size_t Hash() const {
4649
return util::Hash(aggregate_query_.query().Hash(), count_result_);
50+
// TODO: Update with sum
51+
// , sum_result_);
4752
}
4853

4954
friend bool operator==(const AggregateQuerySnapshotInternal& lhs,
@@ -52,6 +57,7 @@ class AggregateQuerySnapshotInternal {
5257
private:
5358
api::AggregateQuery aggregate_query_;
5459
int64_t count_result_ = 0;
60+
double sum_result_ = 0.0;
5561
};
5662

5763
inline bool operator!=(const AggregateQuerySnapshotInternal& lhs,

0 commit comments

Comments
 (0)