-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSOQLBuilder.cls
279 lines (233 loc) · 7.51 KB
/
SOQLBuilder.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
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
public class SOQLBuilder {
public enum Aggregate {
COUNT,
COUNT_DISTINCT,
SUM,
MIN,
MAX
}
private final String NO_FIELDS_PROVIDED = 'No fields provided to query';
private final String SECURITY_ENFORCED_CLAUSE = ' WITH SECURITY_ENFORCED';
private final String SOQL_SELECT = 'SELECT ';
private final String SOQL_FROM = ' FROM ';
private final String SOQL_WHERE = ' WHERE ';
private final String SOQL_GROUPBY = ' GROUP BY ';
private final String SOQL_ORDERBY = ' ORDER BY ';
private final String SOQL_LIMIT = ' LIMIT ';
private final String SOQL_OFFSET = ' OFFSET ';
private final String SOQL_FOR_UPDATE = ' FOR UPDATE';
private final String SOQL_FOR_VIEW = ' FOR VIEW';
private final String SOQL_FOR_REFERENCE = ' FOR REFERENCE';
private Schema.SObjectType sObjectType;
private List<String> fieldStrings = new List<String>();
private List<Schema.SObjectField> fields = new List<Schema.SObjectField>();
private List<Schema.FieldSet> fieldSets = new List<Schema.FieldSet>();
private Boolean securityEnforced = false;
private String whereClause;
private String orderClause;
private String groupClause;
private Integer rowLimit;
private Integer rowOffset;
private Boolean forUpdate = false;
private Boolean forView = false;
private Boolean forReference = false;
private Map<String, Object> binds = new Map<String, Object>();
private Set<String> aggregates = new Set<String>();
public SOQLBuilder(Schema.SObjectType sObjectType) {
this.sObjectType = sObjectType;
}
public SOQLBuilder addAggregate(
Aggregate aggregate,
String field,
String alias
) {
this.aggregates.add(this.getAggregateExpression(aggregate, field, alias));
return this;
}
public List<String> getAggregates() {
return new List<String>(this.aggregates);
}
public String getAggregateExpression(
Aggregate aggregate,
String field,
String alias
) {
return String.format(
'{0}({1}) {2}',
new List<Object>{ aggregate.name(), field, alias }
);
}
public SOQLBuilder selectField(String field) {
return this.selectFields(new List<String>{ field });
}
public SOQLBuilder selectFields(List<String> fields) {
this.fieldStrings.addAll(fields);
return this;
}
public List<String> getFields() {
return this.fieldStrings;
}
public SOQLBuilder selectField(Schema.SObjectField sObjectField) {
return this.selectFields(new List<Schema.SObjectField>{ sObjectField });
}
public SOQLBuilder selectFields(List<Schema.SObjectField> sObjectFields) {
this.fields.addAll(sObjectFields);
return this;
}
public List<Schema.SObjectField> getSObjectFields() {
return this.fields;
}
public SOQLBuilder selectField(Schema.FieldSet fieldSet) {
return this.selectFields(new List<Schema.FieldSet>{ fieldSet });
}
public SOQLBuilder selectFields(List<Schema.FieldSet> fieldSets) {
this.fieldSets.addAll(fieldSets);
return this;
}
public List<Schema.FieldSet> getFieldSets() {
return this.fieldSets;
}
public Schema.SObjectType getSObjectType() {
return this.sObjectType;
}
public SOQLBuilder setWhereClause(String whereClause) {
this.whereClause = whereClause;
return this;
}
public String getWhereClause() {
return this.whereClause;
}
public SOQLBuilder setSecurityEnforced(Boolean securityEnforced) {
this.securityEnforced = securityEnforced;
return this;
}
public Boolean getSecurityEnforced() {
return this.securityEnforced;
}
public SOQLBuilder setForUpdate(Boolean forUpdate) {
this.forUpdate = forUpdate;
return this;
}
public Boolean getForUpdate() {
return this.forUpdate;
}
public SOQLBuilder setForView(Boolean forView) {
this.forView = forView;
return this;
}
public Boolean getForView() {
return this.forView;
}
public SOQLBuilder setForReference(Boolean forReference) {
this.forReference = forReference;
return this;
}
public Boolean getForReference() {
return this.forReference;
}
public SOQLBuilder setRowLimit(Integer rowLimit) {
this.rowLimit = rowLimit;
return this;
}
public Integer getRowLimit() {
return this.rowLimit;
}
public SOQLBuilder setOrderClause(String orderClause) {
this.orderClause = orderClause;
return this;
}
public String getOrderClause() {
return this.orderClause;
}
public SOQLBuilder setGroupClause(String groupClause) {
this.groupClause = groupClause;
return this;
}
public String getGroupClause() {
return this.groupClause;
}
public SOQLBuilder setOffset(Integer rowOffset) {
this.rowOffset = rowOffset;
return this;
}
public Integer getOffset() {
return this.rowOffset;
}
public SOQLBuilder setBindVariable(String key, Object value) {
this.binds.put(key, value);
return this;
}
public Map<String, Object> getBinds() {
return this.binds;
}
private String getBaseQuery() {
set<String> fieldStrings = new Set<String>();
for (Schema.SObjectField field : this.getSObjectFields()) {
Schema.DescribeFieldResult iDescribe = field.getDescribe();
fieldStrings.add(iDescribe.getName().toLowerCase());
}
for (String field : this.getFields()) {
fieldStrings.add(field?.toLowerCase());
}
for (Schema.FieldSet fieldSet : this.getFieldSets()) {
for (Schema.FieldSetMember fsm : fieldSet.getFields()) {
fieldStrings.add(fsm.getFieldPath().toLowerCase());
}
}
fieldStrings.addAll(this.getAggregates());
if (fieldStrings.size() == 0) {
throw new QueryException(NO_FIELDS_PROVIDED);
}
String baseQuery =
SOQL_SELECT +
String.join(new List<String>(fieldStrings), ',') +
SOQL_FROM +
this.getSObjectType().getDescribe().getName();
return baseQuery;
}
/**
* @description Returns the query predicate based on the various clauses
* (i.e. WHERE, ORDER BY, LIMIT, OFFSET)
* @param SOQLBuilder SOQLBuilder object representing query details
*/
private String getPredicate() {
String predicateString = '';
if (!String.isBlank(this.getWhereClause()))
predicateString += SOQL_WHERE + this.getWhereClause();
if (!String.isBlank(this.getGroupClause()))
predicateString += SOQL_GROUPBY + this.getGroupClause();
if (!String.isBlank(this.getOrderClause()))
predicateString += SOQL_ORDERBY + this.getOrderClause();
if (this.getSecurityEnforced())
predicateString += SECURITY_ENFORCED_CLAUSE;
if (this.getRowLimit() != null)
predicateString += SOQL_LIMIT + String.valueOf(this.getRowLimit());
if (this.getOffset() != null)
predicateString += SOQL_OFFSET + String.valueOf(this.getOffset());
if (this.getForReference())
predicateString += SOQL_FOR_REFERENCE;
if (this.getForView())
predicateString += SOQL_FOR_VIEW;
if (this.getForUpdate())
predicateString += SOQL_FOR_UPDATE;
return predicateString;
}
/**
* @description Returns the fully formed query defined by all inputs to the DAO
* base class, including fields, where clause, order clause, row limits, etc...
* @param SOQLBuilder SOQLBuilder object representing query details
*/
public String getQuery() {
String queryString = this.getBaseQuery();
queryString += this.getPredicate();
system.debug(queryString);
return queryString;
}
public Boolean equals(Object ref) {
SOQLBuilder refSoql = (SOQLBuilder) ref;
return this.getQuery() == refSoql.getQuery();
}
public override String toString() {
return this.getQuery();
}
}