forked from jamessimone/apex-dml-mocking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AggregateRepository.cls
88 lines (80 loc) · 3.21 KB
/
AggregateRepository.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
public without sharing virtual class AggregateRepository extends Repository implements IAggregateRepository {
private final Set<String> groupedByFieldNames = new Set<String>();
private List<Aggregation> aggregations;
private Boolean isNumberCountQuery = false;
public AggregateRepository(
Schema.SObjectType repoType,
List<Schema.SObjectField> queryFields,
RepoFactory repoFactory
) {
super(repoType, queryFields, repoFactory);
}
protected AggregateRepository(RepoFactory repoFactory) {
super(repoFactory);
}
public IAggregateRepository groupBy(Schema.SObjectField fieldToken) {
return this.groupBy(fieldToken.getDescribe().getName());
}
public IAggregateRepository groupBy(String fieldName) {
this.groupedByFieldNames.add(fieldName);
return this;
}
public Integer count() {
return this.count(new List<Query>());
}
public Integer count(Query query) {
return this.count(new List<Query>{ query });
}
public Integer count(List<Query> queries) {
this.isNumberCountQuery = true;
Integer recordCount = Database.countQuery(this.getFinalQuery(queries));
this.isNumberCountQuery = false;
return recordCount;
}
public List<AggregateRecord> aggregate(Aggregation aggregation) {
return this.aggregate(new List<Aggregation>{ aggregation }, new List<Query>());
}
public List<AggregateRecord> aggregate(Aggregation aggregation, Query query) {
return this.aggregate(new List<Aggregation>{ aggregation }, new List<Query>{ query });
}
public List<AggregateRecord> aggregate(Aggregation aggregation, List<Query> queries) {
return this.aggregate(new List<Aggregation>{ aggregation }, queries);
}
public List<AggregateRecord> aggregate(List<Aggregation> aggregations) {
return this.aggregate(aggregations, new List<Query>());
}
public List<AggregateRecord> aggregate(List<Aggregation> aggregations, Query query) {
return this.aggregate(aggregations, new List<Query>{ query });
}
public virtual List<AggregateRecord> aggregate(List<Aggregation> aggregations, List<Query> queries) {
this.aggregations = aggregations;
List<AggregateResult> results = (List<AggregateResult>) this.get(queries);
List<AggregateRecord> aggregateRecords = new List<AggregateRecord>();
for (AggregateResult result : results) {
AggregateRecord aggRecord = new AggregateRecord();
aggRecord.putAll(result.getPopulatedFieldsAsMap());
aggregateRecords.add(aggRecord);
}
return aggregateRecords;
}
protected virtual override Set<String> addSelectFields() {
Set<String> baseFields = new Set<String>();
if (this.isNumberCountQuery) {
baseFields.add('COUNT()');
return baseFields;
}
if (this.aggregations != null) {
for (Aggregation agg : aggregations) {
baseFields.add(agg.toString());
}
}
baseFields.addAll(this.groupedByFieldNames);
return baseFields.isEmpty() ? super.addSelectFields() : baseFields;
}
protected override String getFinalQuery(List<Query> queries) {
String baseString = super.getFinalQuery(queries);
return this.groupedByFieldNames.isEmpty()
? baseString
: baseString + '\nGROUP BY ' + String.join(new List<String>(this.groupedByFieldNames), ',');
}
}