-
Notifications
You must be signed in to change notification settings - Fork 22
/
AggregateRepository.cls
192 lines (168 loc) · 6.59 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
public without sharing virtual class AggregateRepository extends Repository implements IAggregateRepository {
protected final Set<GroupBy> groupedByFieldNames = new Set<GroupBy>();
private final Set<String> havingFields = new Set<String>();
private List<Aggregation> aggregations;
private Boolean isNumberCountQuery = false;
@TestVisible
private class GroupBy {
private final String selectName;
private final String groupByName;
public GroupBy(String groupByName, String alias) {
this.selectName = groupByName + ' ' + alias;
this.groupByName = groupByName;
}
public GroupBy(String fieldName) {
this.selectName = fieldName;
this.groupByName = fieldName;
}
public String getSelectName() {
return this.selectName;
}
public String getGroupByName() {
return this.groupByName;
}
public Boolean equals(Object other) {
if (other instanceof GroupBy) {
GroupBy that = (GroupBy) other;
return this.groupByName == that.groupByName && this.selectName == that.selectName;
}
return false;
}
}
public AggregateRepository(
Schema.SObjectType repoType,
List<Schema.SObjectField> queryFields,
RepoFactory repoFactory
) {
super(repoType, queryFields, repoFactory);
}
public IAggregateRepository groupBy(DateFunction dateFunction, Schema.SObjectField fieldToken, String alias) {
String groupByName = this.getFormattedDateFunction(dateFunction, fieldToken);
this.groupedByFieldNames.add(new GroupBy(groupByName, alias));
return this;
}
public IAggregateRepository groupBy(Schema.SObjectField fieldToken) {
this.groupedByFieldNames.add(new GroupBy(fieldToken.getDescribe().getName()));
return this;
}
public IAggregateRepository groupBy(List<Schema.SObjectField> parentFieldChain) {
String parentFieldGroupBy = '';
while (parentFieldChain.size() > 1) {
parentFieldGroupBy += parentFieldChain.remove(0).getDescribe().getRelationshipName() + '.';
}
this.groupedByFieldNames.add(new GroupBy(parentFieldGroupBy + parentFieldChain.remove(0).getDescribe().getName()));
return this;
}
public IAggregateRepository addSortOrder(Aggregation aggregate, RepositorySortOrder sortOrder) {
this.fieldToSortOrder.put(aggregate.getBaseAggregation(), sortOrder);
return this;
}
public IAggregateRepository addSortOrder(
DateFunction dateFunction,
Schema.SObjectField fieldToken,
RepositorySortOrder sortOrder
) {
this.fieldToSortOrder.put(this.getFormattedDateFunction(dateFunction, fieldToken), sortOrder);
return this;
}
public IAggregateRepository addHaving(Aggregation aggregation, Query.Operator operator, Object value) {
Query aggQuery = new AggregateQuery(operator, value);
this.havingFields.add(aggregation.getBaseAggregation() + ' ' + aggQuery);
this.bindVars.putAll(aggQuery.getBindVars());
return this;
}
public Integer count() {
return this.count(new List<Query>());
}
public Integer count(Query query) {
return this.count(new List<Query>{ query });
}
public virtual Integer count(List<Query> queries) {
this.isNumberCountQuery = true;
Integer recordCount = Database.countQueryWithBinds(this.getFinalQuery(queries), this.bindVars, this.accessLevel);
this.clearState();
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);
}
this.clearState();
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());
}
}
for (GroupBy groupBy : this.groupedByFieldNames) {
baseFields.add(groupBy.getSelectName());
}
return baseFields.isEmpty() ? super.addSelectFields() : baseFields;
}
protected override String getFinalQuery(List<Query> queries) {
String baseString = super.getFinalQuery(queries);
if (this.groupedByFieldNames.isEmpty() == false) {
String potentialOrderBy = null;
String orderByKey = '\nORDER BY';
if (baseString.contains(orderByKey)) {
potentialOrderBy = baseString.substringAfter(orderByKey);
baseString = baseString.replace(orderByKey + potentialOrderBy, '');
}
baseString += '\nGROUP BY ';
for (GroupBy groupBy : this.groupedByFieldNames) {
baseString += groupBy.getGroupByName() + ',';
}
baseString = baseString.removeEnd(',');
// having is only valid with a grouping
if (this.havingFields.isEmpty() == false) {
baseString += '\nHAVING ' + String.join(this.havingFields, ',');
}
if (potentialOrderBy != null) {
baseString += orderByKey + potentialOrderBy;
}
}
return baseString;
}
protected override void clearState() {
super.clearState();
this.havingFields.clear();
this.groupedByFieldNames.clear();
this.aggregations = null;
}
private String getFormattedDateFunction(DateFunction dateFunction, Schema.SObjectField fieldToken) {
return dateFunction.name() + '(' + fieldToken + ')';
}
private class AggregateQuery extends Query {
public AggregateQuery(Query.Operator op, Object value) {
super('', op, value);
}
}
}