-
Notifications
You must be signed in to change notification settings - Fork 0
/
SOQLBuilder.apxc
385 lines (290 loc) · 13.3 KB
/
SOQLBuilder.apxc
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
/**
*
* @author: Jimens Lima - me@jimens.com
*
*/
public class SOQLBuilder {
private String query {get;set;}
private Integer fieldSize {get;set;}
private static SOQLBuilder instance {get;set;}
private static Set<String> defaultFields = new Set<String>{'createdbyid','lastmodifiedbyid','connectionreceivedid','connectionsentid','recordtypeid','ownerid'};
private static Set<String> nameFields = new Set<String>{'CreatedBy.FirstName','CreatedBy.LastName','LastModifiedBy.FirstName','LastModifiedBy.LastName'};
private SOQLBuilder(){}
/**
* Construct a SOQL Query based on SObject String name.
* In this case, all fields will be retrieved.
*
* @return a SoqlBuilder instance.
*/
public static SOQLBuilder selectx(String sObjectName){
instance = new SOQLBuilder();
try{
String fields = '';
Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(sObjectName).getDescribe().fields.getMap();
instance.fieldSize = 1;
String fieldName = '';
for(String key : objectFields.keySet()) {
Schema.SObjectField objField = objectFields.get(key);
Schema.DescribeFieldResult fieldResult = objField.getDescribe();
if(defaultFields.contains(key) || !fieldResult.isAccessible()){
continue;
}
String fieldCamelCase = fieldResult.getName();
key = fieldCamelCase;
if(Schema.DisplayType.REFERENCE==fieldResult.getType()){
if(key.endsWith('__c')){
key = key.replace('__c', '__r');
}
fieldName += ' ' + key + '.Name, ';
}else{
fieldName += ' ' + key + ', ';
}
instance.fieldSize++;
}
for(String key : nameFields){
fieldName += ' ' + key + ', ';
}
fields = fieldName.substring(0, fieldName.length()-2);
fields += ' FROM '+sObjectName;
instance.query = 'SELECT '+ fields;
}catch(Exception e){
// System.debug(SFUtils.getStackTrace(e));
}
return instance;
}
/***
* Remove a record by ID
*/
public static void removeById(String sObjectName,String objId){
String query = 'SELECT Id FROM '+sObjectName+ ' WHERE ID= \'' + objId + '\'';
List<SObject> recordsToRemove = Database.query(query);
if(!recordsToRemove.isEmpty()){
delete recordsToRemove;
}
}
public SOQLBuilder wherex(String fieldName, String value){
if(!SFUtils.isEmpty(fieldName) && !SFUtils.isEmpty(value)){
instance.query += ' WHERE '+ fieldName + ' = \'' + value + '\'';
}else{
throw new ApplicationException('Invalid where clause!');
}
return instance;
}
public SOQLBuilder likex(String fieldName, String value){
if(!SFUtils.isEmpty(fieldName) && !SFUtils.isEmpty(value)){
String param = '\'%'+value+'%\'';
instance.query += ' WHERE '+ fieldName + ' LIKE ' + param;
}else{
throw new ApplicationException('Invalid like clause!');
}
return instance;
}
public SOQLBuilder inx(String fieldName, List<String> values){
if(!SFUtils.isEmpty(fieldName) && !SFUtils.isEmpty(values)){
String inStmt = SFUtils.generateInStatement(values);
instance.query += ' WHERE ' + fieldName +' '+ inStmt;
}else{
throw new ApplicationException('Invalid in clause!');
}
return instance;
}
public SOQLBuilder inx(String fieldName, List<SObject> values){
if(!SFUtils.isEmpty(fieldName) && !SFUtils.isEmpty(values)){
String inStmt = SFUtils.generateInStatement(values);
instance.query += ' WHERE ' + + fieldName +' '+ inStmt;
}else{
throw new ApplicationException('Invalid in clause');
}
return instance;
}
public SOQLBuilder inx(String fieldName, Set<ID> values){
if(!SFUtils.isEmpty(fieldName) && !SFUtils.isEmpty(values)){
String inStmt = SFUtils.generateInStatement(values);
instance.query += ' WHERE ' + + fieldName +' '+ inStmt;
}else{
throw new ApplicationException('Invalid in clause');
}
return instance;
}
public SOQLBuilder notIn(String fieldName, List<String> inValues){
if(!SFUtils.isEmpty(inValues)){
String inStmt = SFUtils.generateInStatement(inValues);
instance.query += ' WHERE '+fieldName+ ' NOT '+ inStmt;
}else{
throw new ApplicationException('Invalid in clause');
}
return instance;
}
public SOQLBuilder notIn(String fieldName, List<SObject> inValues){
if(!SFUtils.isEmpty(inValues)){
String inStmt = SFUtils.generateInStatement(inValues);
instance.query += ' WHERE '+fieldName+ ' NOT '+ inStmt;
}else{
throw new ApplicationException('Invalid in clause');
}
return instance;
}
public SOQLBuilder differentOf(String fieldName,String value){
instance.query += ' WHERE ' + fieldName +' != \''+ value + '\'';
return instance;
}
public SOQLBuilder andEquals(String fieldName, String fieldValue){
if(!SFUtils.isEmpty(fieldName) && !SFUtils.isEmpty(fieldValue)){
instance.query += ' AND ' + fieldName +' = \''+ fieldValue + '\'';
}else{
throw new ApplicationException('Invalid AND clause!');
}
return instance;
}
public SOQLBuilder andNotEquals(String fieldName, String fieldValue){
if(!SFUtils.isEmpty(fieldName) && !SFUtils.isEmpty(fieldValue)){
instance.query += ' AND ' + fieldName +' != \''+ fieldValue + '\'';
}else{
throw new ApplicationException('Invalid AND clause!');
}
return instance;
}
public String toSOQL(){
return instance.query;
}
public Integer getFieldSize(){
return fieldSize;
}
/**
* Execute a Query limited to 100 elements.
* Avoiding the SFDC Limits.
*
* @return A SObject List with 100 elements.
*/
public List<SObject> execute(){
return execute(100);
}
public List<SObject> execute(Integer queryLimit){
if(queryLimit>0){
instance.query += ' LIMIT '+queryLimit;
return Database.query(instance.query);
}
return null;
}
public List<SObject> executeAll(){
return Database.query(instance.query);
}
/**
* Retrieve a List of Lists with FieldDefinition element.
* Each FieldDefinition iteration element represents an entire SObject row.
*/
public List<List<FieldDefinition>> retrieveOrderedFields(){
//System.debug('instance.query='+instance.query);
List<SObject> queryResult = Database.query(instance.query);
List<List<FieldDefinition>> fieldResults = new List<List<FieldDefinition>>();
for(SObject sObj : queryResult){
List<FieldDefinition> fieldIteration = new List<FieldDefinition>();
for (Schema.SObjectField fieldName : sObj.getSObjectType().getDescribe().fields.getMap().values()) {
try{
if(sObj!=null){
Schema.DescribeFieldResult fieldResult = fieldName.getDescribe();
FieldDefinition field = new FieldDefinition();
field.name = fieldResult.getName();
field.label = fieldResult.getLabel();
field.type = fieldResult.getType();
if(Schema.DisplayType.REFERENCE==fieldResult.getType()){
String fieldStrName = fieldResult.getName();
if(fieldStrName.endsWith('__c')){
fieldStrName = fieldStrName.replace('__c', '__r');
}
field.stringValue = String.valueOf(sObj.getSObject(fieldStrName).get('Name'));
}else{
field.objValue = sObj.get(fieldResult.getName());
field.stringValue = String.valueOf(field.objValue);
}
fieldIteration.add(field);
}
}catch(Exception e){
System.debug(SFUtils.getStackTrace(e));
}
}
fieldIteration.sort();
fieldResults.add(fieldIteration);
}
return fieldResults;
}
/**
* Retrieve a List of Lists with FieldDefinition element.
* Each FieldDefinition iteration element represents an entire SObject row.
*/
public List<List<FieldDefinition>> retrieveFields(){
//System.debug('instance.query='+instance.query);
List<SObject> queryResult = Database.query(instance.query);
List<List<FieldDefinition>> fieldResults = new List<List<FieldDefinition>>();
for(SObject sObj : queryResult){
List<FieldDefinition> fieldIteration = new List<FieldDefinition>();
for (Schema.SObjectField fieldName : sObj.getSObjectType().getDescribe().fields.getMap().values()) {
try{
Schema.DescribeFieldResult fieldResult = fieldName.getDescribe();
FieldDefinition field = new FieldDefinition();
field.name = fieldResult.getName();
field.label = fieldResult.getLabel();
field.type = fieldResult.getType();
if(Schema.DisplayType.REFERENCE==fieldResult.getType()){
String fieldStrName = fieldResult.getName();
if(fieldStrName.endsWith('__c')){
fieldStrName = fieldStrName.replace('__c', '__r');
}
field.stringValue = String.valueOf(sObj.getSObject(fieldStrName).get('Name'));
}else{
field.objValue = sObj.get(fieldResult.getName());
field.stringValue = String.valueOf(field.objValue);
}
fieldIteration.add(field);
}catch(Exception e){
//System.debug(SFUtils.getStackTrace(e));
}
}
//fieldIteration.sort();
fieldResults.add(fieldIteration);
}
return fieldResults;
}
/**
* Describe the existing fields on a SObject.
* @return List of FieldDefinition.
*/
public static List<FieldDefinition> describe(String sObjectName){
List<FieldDefinition> fieldIteration = new List<FieldDefinition>();
try{
String fields = '';
Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(sObjectName).getDescribe().fields.getMap();
String fieldName = '';
if(objectFields!=null){
for(String key : objectFields.keySet()) {
Schema.SObjectField objField = objectFields.get(key);
Schema.DescribeFieldResult fieldResult = objField.getDescribe();
if(defaultFields.contains(key)){
continue;
}
if(fieldResult.getLabel()==null){
continue;
}
FieldDefinition field = new FieldDefinition();
field.name = fieldResult.getName();
field.label = fieldResult.getLabel();
field.type = fieldResult.getType();
field.isLookup = (fieldResult.isIdLookup() && !fieldResult.isExternalID());
field.isRestrictedPicklist = fieldResult.isRestrictedPicklist();
if(Schema.DisplayType.PICKLIST == fieldResult.getType() || Schema.DisplayType.MULTIPICKLIST == fieldResult.getType()){
List<FieldDefinition.PicklistEntry> lov = new List<FieldDefinition.PicklistEntry>();
for(Schema.PicklistEntry entry : fieldResult.getPicklistValues()){
lov.add(new FieldDefinition.PicklistEntry(entry));
}
field.pickListValues = lov;
}
fieldIteration.add(field);
}
fieldIteration.sort();
}
}catch(Exception e){
//System.debug(e.getStackTraceString());
}
return fieldIteration;
}
}