-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAggregateQuery.cls
More file actions
396 lines (320 loc) · 14.8 KB
/
AggregateQuery.cls
File metadata and controls
396 lines (320 loc) · 14.8 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*******************************************************************************************************
* This file is part of the Nebula Query & Search project, released under the MIT License. *
* See LICENSE file or go to https://github.com/jongpie/NebulaQueryAndSearch for full license details. *
******************************************************************************************************/
/**
* @group SOQL Queries
* @description Handles generating & executing aggregate queries
* @see SOQL
* @see Query
*/
@SuppressWarnings(
'PMD.ApexDoc, PMD.ApexSharingViolations, PMD.AvoidGlobalModifier, PMD.CyclomaticComplexity, PMD.ExcessiveParameterList, PMD.ExcessivePublicCount, PMD.FieldDeclarationsShouldBeAtStart'
)
global class AggregateQuery extends SOQL {
private SOQL.GroupingDimension groupingDimension;
private List<AggregateField> aggregateFields;
private List<AggregateQueryFilter> havingClauseFilters;
protected String havingClauseFilterLogic;
private String countQuery;
global AggregateQuery(Schema.SObjectType sobjectType) {
super(sobjectType, false);
this.aggregateFields = new List<AggregateField>();
this.havingClauseFilters = new List<AggregateQueryFilter>();
this.havingClauseFilterLogic = '';
}
global AggregateQuery groupByField(Schema.SObjectField field) {
return this.groupByFields(new List<Schema.SObjectField>{ field });
}
global AggregateQuery groupByField(SOQL.QueryField queryField) {
return this.groupByFields(new List<SOQL.QueryField>{ queryField });
}
global AggregateQuery groupByFields(List<Schema.SObjectField> fields) {
List<SOQL.QueryField> queryFields = new List<SOQL.QueryField>();
for (Schema.SObjectField field : fields) {
queryFields.add(new SOQL.QueryField(field));
}
return this.groupByFields(queryFields);
}
global AggregateQuery groupByFields(List<SOQL.QueryField> queryFields) {
super.doAddFields(queryFields, null);
return this.setHasChanged();
}
global AggregateQuery groupByFieldSet(Schema.FieldSet fieldSet) {
List<SOQL.QueryField> queryFields = new List<SOQL.QueryField>();
for (Schema.FieldSetMember fieldSetMember : fieldSet.getFields()) {
queryFields.add(new SOQL.QueryField(this.sobjectType, fieldSetMember.getFieldPath()));
}
return this.groupByFields(queryFields);
}
global AggregateQuery usingGroupingDimension(SOQL.GroupingDimension groupingDimension) {
this.groupingDimension = groupingDimension;
return this.setHasChanged();
}
global AggregateQuery addAggregate(SOQL.Aggregate aggregateFunction, Schema.SObjectField field) {
return this.addAggregate(aggregateFunction, field, null);
}
global AggregateQuery addAggregate(SOQL.Aggregate aggregateFunction, Schema.SObjectField field, String fieldAlias) {
return this.addAggregate(aggregateFunction, new SOQL.QueryField(field), fieldAlias);
}
global AggregateQuery addAggregate(SOQL.Aggregate aggregateFunction, SOQL.QueryField queryField) {
return this.addAggregate(aggregateFunction, queryField, null);
}
global AggregateQuery addAggregate(SOQL.Aggregate aggregateFunction, SOQL.QueryField queryField, String fieldAlias) {
this.aggregateFields.add(new AggregateField(this.getSObjectType(), aggregateFunction, queryField, fieldAlias));
return this.setHasChanged();
}
global AggregateQuery havingAggregate(SOQL.Aggregate aggregateFunction, Schema.SObjectField field, SOQL.Operator operator, Object value) {
return this.havingAggregate(aggregateFunction, new SOQL.QueryField(field), operator, value);
}
global AggregateQuery havingAggregate(SOQL.Aggregate aggregateFunction, Schema.SObjectField field, SOQL.Operator operator, Object value, String bindWithKey) {
return this.havingAggregate(aggregateFunction, new SOQL.QueryField(field), operator, value, bindWithKey);
}
global AggregateQuery havingAggregate(SOQL.Aggregate aggregateFunction, SOQL.QueryField queryField, SOQL.Operator operator, Object value) {
return this.havingAggregate(aggregateFunction, queryField, operator, value, this.generateNextBindVariableKey());
}
global AggregateQuery havingAggregate(SOQL.Aggregate aggregateFunction, SOQL.QueryField queryField, SOQL.Operator operator, Object value, String bindWithKey) {
this.havingClauseFilters.add(
new AggregateQueryFilter(aggregateFunction, queryField, operator, value, bindWithKey)
);
this.havingClauseFilterLogic += (this.havingClauseFilters.size() == 1 ? '1' : ' AND ' + this.havingClauseFilters.size());
return this.setHasChanged();
}
global AggregateQuery orHavingAggregate(List<AggregateQueryFilter> filters) {
this.havingClauseFilterLogic = this.doOrFilter(filters, this.havingClauseFilters, this.havingClauseFilterLogic);
return this.setHasChanged();
}
global AggregateQuery setHavingFilterLogic(String filterLogic) {
this.havingClauseFilterLogic = filterLogic;
return this.setHasChanged();
}
global AggregateQuery filterWhere(Schema.SObjectField field, SOQL.Operator operator, Object value) {
return this.filterWhere(new SOQL.QueryField(field), operator, value);
}
global AggregateQuery filterWhere(Schema.SObjectField field, SOQL.Operator operator, Object value, String bindWithKey) {
return this.filterWhere(new SOQL.QueryField(field), operator, value, bindWithKey);
}
global AggregateQuery filterWhere(SOQL.QueryField queryField, SOQL.Operator operator, Object value) {
return this.filterWhere(new SOQL.QueryFilter(queryField, operator, value));
}
global AggregateQuery filterWhere(SOQL.QueryField queryField, SOQL.Operator operator, Object value, String bindWithKey) {
return this.filterWhere(new SOQL.QueryFilter(queryField, operator, value, bindWithKey));
}
global AggregateQuery filterWhere(SOQL.QueryFilter filter) {
return this.filterWhere(new List<SOQL.QueryFilter>{ filter });
}
global AggregateQuery filterWhere(List<SOQL.QueryFilter> filters) {
super.doFilterWhere(filters);
return this.setHasChanged();
}
global AggregateQuery orFilterWhere(List<SOQL.QueryFilter> filters) {
super.doOrFilterWhere(filters);
return this.setHasChanged();
}
global AggregateQuery setWhereFilterLogic(String filterLogic) {
super.doSetWhereFilterLogic(filterLogic);
return this.setHasChanged();
}
global AggregateQuery withAccessLevel(System.AccessLevel accessLevel) {
super.doWithAccessLevel(accessLevel);
return this.setHasChanged();
}
global AggregateQuery orderByField(Schema.SObjectField field) {
return this.orderByField(field, null);
}
global AggregateQuery orderByField(SOQL.QueryField queryField) {
return this.orderByField(queryField, null);
}
global AggregateQuery orderByField(Schema.SObjectField field, SOQL.SortOrder sortOrder) {
return this.orderByField(field, sortOrder, null);
}
global AggregateQuery orderByField(SOQL.QueryField queryField, SOQL.SortOrder sortOrder) {
return this.orderByField(queryField, sortOrder, null);
}
global AggregateQuery orderByField(Schema.SObjectField field, SOQL.SortOrder sortOrder, Boolean sortNullsFirst) {
return this.orderByField(new SOQL.QueryField(field), sortOrder, sortNullsFirst);
}
global AggregateQuery orderByField(SOQL.QueryField queryField, SOQL.SortOrder sortOrder, Boolean sortNullsFirst) {
super.doOrderBy(queryField, sortOrder, sortNullsFirst);
return this.setHasChanged();
}
global AggregateQuery orderByAggregate(SOQL.Aggregate aggregateFunction, Schema.SObjectField field) {
return this.orderByAggregate(aggregateFunction, field, null);
}
global AggregateQuery orderByAggregate(SOQL.Aggregate aggregateFunction, Schema.SObjectField field, SOQL.SortOrder sortOrder) {
return this.orderByAggregate(aggregateFunction, field, sortOrder, null);
}
global AggregateQuery orderByAggregate(SOQL.Aggregate aggregateFunction, Schema.SObjectField field, SOQL.SortOrder sortOrder, Boolean sortNullsFirst) {
return this.orderByAggregate(aggregateFunction, new SOQL.QueryField(field), sortOrder, sortNullsFirst);
}
global AggregateQuery orderByAggregate(SOQL.Aggregate aggregateFunction, SOQL.QueryField queryField) {
return this.orderByAggregate(aggregateFunction, queryField, null);
}
global AggregateQuery orderByAggregate(SOQL.Aggregate aggregateFunction, SOQL.QueryField queryField, SOQL.SortOrder sortOrder) {
return this.orderByAggregate(aggregateFunction, queryField, sortOrder, null);
}
global AggregateQuery orderByAggregate(SOQL.Aggregate aggregateFunction, SOQL.QueryField queryField, SOQL.SortOrder sortOrder, Boolean sortNullsFirst) {
super.doOrderBy(aggregateFunction.name() + '(' + queryField + ')', sortOrder, sortNullsFirst);
return this.setHasChanged();
}
global AggregateQuery limitTo(Integer numberOfRecords) {
super.doLimitTo(numberOfRecords);
return this.setHasChanged();
}
global AggregateQuery offsetBy(Integer offset) {
super.doOffsetBy(offset);
return this.setHasChanged();
}
global AggregateQuery setBind(String key, Object value) {
super.doSetBind(key, value);
return this.setHasChanged();
}
global AggregateQuery setBinds(Map<String, Object> binds) {
super.doSetBinds(binds);
return this.setHasChanged();
}
global AggregateQuery removeBind(String key) {
super.doRemoveBind(key);
return this.setHasChanged();
}
global AggregateQuery clearBinds() {
super.doClearBinds();
return this.setHasChanged();
}
global AggregateQuery generateBindVariableKeys() {
super.doGenerateBindVariableKeys();
return this;
}
// TODO decide if this should be global
public AggregateQuery cacheResults() {
super.doCacheResults();
return this;
}
// TODO decide if this should be global
@SuppressWarnings('PMD.AvoidDebugStatements')
public override String getQuery() {
if (this.query != null && !this.hasChanged) {
return this.query;
}
String queryFieldString = super.doGetQueryFieldString();
String aggregateQueryFieldString = this.getAggregateQueryFieldString();
String aggregateFieldDelimiter = !String.isEmpty(queryFieldString) && !String.isEmpty(aggregateQueryFieldString) ? ', ' : '';
String combinedFieldsString = queryFieldString + aggregateFieldDelimiter + aggregateQueryFieldString;
if (String.isBlank(combinedFieldsString)) {
Schema.SObjectField idField = this.getSObjectType().getDescribe().fields.getMap().get('Id');
combinedFieldsString = new AggregateField(this.getSObjectType(), SOQL.Aggregate.COUNT, new SOQL.QueryField(idField), null).toString();
}
this.query =
'SELECT ' +
combinedFieldsString +
' FROM ' +
this.sobjectType +
super.doGetUsingScopeString() +
super.doGetWhereClauseString() +
this.getGroupByString() +
this.getHavingClauseString() +
super.doGetOrderByString() +
super.doGetLimitCountString() +
super.doGetOffetString();
System.debug(System.LoggingLevel.FINEST, this.query);
return this.query;
}
global String getCountQuery() {
if (this.countQuery != null && !this.hasChanged) {
return this.countQuery;
}
this.countQuery =
'SELECT COUNT()' +
' FROM ' +
this.sobjectType +
super.doGetUsingScopeString() +
super.doGetWhereClauseString() +
this.getGroupByString() +
this.getHavingClauseString() +
super.doGetOrderByString() +
super.doGetLimitCountString() +
super.doGetOffetString();
System.debug(System.LoggingLevel.FINEST, this.countQuery);
return this.countQuery;
}
// TODO consider renaming to getCountResult()
@SuppressWarnings('PMD.ApexSOQLInjection')
global Integer getResultCount() {
return Database.countQueryWithBinds(
this.getCountQuery(),
this.doGetBindsMap(),
this.doGetAccessLevel()
);
}
global AggregateResult getFirstResult() {
return (AggregateResult) super.doGetFirstResult();
}
global List<AggregateResult> getResults() {
return (List<AggregateResult>) super.doGetResults();
}
private AggregateQuery setHasChanged() {
this.doSetHasChanged();
return this;
}
private String getAggregateQueryFieldString() {
if (this.aggregateFields.isEmpty()) {
return '';
}
List<String> aggregateFieldStrings = new List<String>();
for (AggregateQuery.AggregateField aggregatedField : this.aggregateFields) {
aggregateFieldStrings.add(aggregatedField.toString());
}
aggregateFieldStrings.sort();
return String.join(aggregateFieldStrings, ', ');
}
private String getGroupByString() {
String queryFieldString = super.doGetQueryFieldString();
String groupByTextString = ' GROUP BY ';
String groupingDimensionClosingString = '';
if (this.groupingDimension != null) {
groupByTextString += this.groupingDimension.name() + '(';
groupingDimensionClosingString = ')';
}
return String.isEmpty(queryFieldString) ? '' : groupByTextString + queryFieldString + groupingDimensionClosingString;
}
private String getHavingClauseString() {
return this.doGetFilterableClauseString('HAVING', this.havingClauseFilters, this.havingClauseFilterLogic);
}
private class AggregateField {
private Schema.SObjectType sobjectType;
private String aggregateFieldPath;
public AggregateField(Schema.SObjectType sobjectType, SOQL.Aggregate aggregateFunction, SOQL.QueryField queryField, String fieldAlias) {
this.sobjectType = sobjectType;
this.aggregateFieldPath = this.getAggregateFieldPath(aggregateFunction, queryField, fieldAlias);
}
public override String toString() {
return this.aggregateFieldPath;
}
private String getAggregateFieldPath(SOQL.Aggregate aggregateFunction, SOQL.QueryField queryField, String fieldAlias) {
String fieldApiName = queryField.getDescribe().getName();
fieldAlias = !String.isEmpty(fieldAlias) ? String.escapeSingleQuotes(fieldAlias) : aggregateFunction.name() + '__' + fieldApiName;
// Example: MIN(Schema.Lead.MyField__c) is auto-aliased to MyField__c__MIN
return aggregateFunction.name() + '(' + fieldApiName + ') ' + fieldAlias;
}
}
global class AggregateQueryFilter extends SOQL.QueryFilter {
private SOQL.Aggregate aggregateFunction;
public AggregateQueryFilter(SOQL.Aggregate aggregateFunction, SOQL.QueryField queryField, SOQL.Operator operator, Object value, String bindKey)
{
super(queryField, operator, value, bindKey);
this.aggregateFunction = aggregateFunction;
}
public override String toString()
{
return String.format(
'{0}({1}) {2} {3}',
new List<String> {
this.aggregateFunction.name(),
this.queryField.toString(),
SOQL.getOperatorValue(this.operator),
(String.isNotBlank(this.bindKey) ? ':' + this.bindKey : new QueryArgument(this.value).toString())
}
);
}
}
}