Skip to content

Commit 945a7b8

Browse files
committed
Createda new class, QueryGenerator
I have not touched in project in nearly 2 years, so I'm somewhat starting over by creating a new class. Older classes will eventually be repurposed or deleted.
1 parent 72d66d0 commit 945a7b8

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

src/classes/QueryGenerator.cls

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
public class QueryGenerator {
2+
3+
private Schema.FieldSet fieldSet;
4+
private SObjectType sobjectType;
5+
private Schema.DescribeSObjectResult sobjectResult;
6+
private Set<String> queryFields;
7+
8+
public QueryGenerator(Schema.DescribeSObjectResult sobjectResult) {
9+
this(sobjectResult, null);
10+
}
11+
12+
public QueryGenerator(Schema.FieldSet fieldSet) {
13+
this(null, fieldSet);
14+
}
15+
16+
private QueryGenerator(Schema.DescribeSObjectResult sobjectResult, Schema.FieldSet fieldSet) {
17+
this.fieldSet = fieldSet;
18+
this.sobjectResult = sobjectResult;
19+
20+
this.sobjectType = fieldSet != null ? fieldSet.getSObjectType() : sobjectResult.getSobjectType();
21+
this.queryFields = new Set<String>{'Id'};
22+
23+
if(this.fieldSet == null) this.parseSObjectFields();
24+
else this.parseFieldSetMembers();
25+
}
26+
27+
public String buildQuery() {
28+
return this.buildQuery('');
29+
}
30+
31+
public String buildQuery(String whereClause) {
32+
String query =
33+
'SELECT ' + String.join(new List<String>(this.queryFields), ', ')
34+
+ ' FROM ' + this.sobjectType;
35+
36+
if(!String.isEmpty(whereClause)) query = query + whereClause;
37+
38+
return query;
39+
}
40+
41+
public void parseSObjectFields() {
42+
for(Schema.SObjectField sobjectField : this.sobjectResult.fields.getMap().values()) {
43+
this.queryFields.add(sobjectField.getDescribe().getName());
44+
}
45+
}
46+
47+
private void parseFieldSetMembers() {
48+
if(this.fieldSet == null) return;
49+
50+
for(Schema.FieldSetMember field : this.fieldSet.getFields()) this.queryFields.add(field.getFieldPath());
51+
}
52+
53+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>38.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

0 commit comments

Comments
 (0)