Skip to content

Commit 073c2b7

Browse files
authored
Move query & search execution back to SObjectRepository (#19)
* Created SOQLUtils class to handle parsing a List<Object> into the formatted expected by SOQL for each data type * Moved query & search execution back to SObjectRepository * Deleted some old classes
1 parent c40f63f commit 073c2b7

14 files changed

+179
-336
lines changed

src/classes/CollectionUtils.cls

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/classes/CollectionUtils_Test.cls

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/classes/CollectionUtils_Test.cls-meta.xml

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/classes/LeadQueryRepositoryTest.cls

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/classes/LeadQueryRepositoryTest.cls-meta.xml

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/classes/LeadRepository.cls

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ public without sharing class LeadRepository extends SObjectRepository {
44

55
public LeadRepository() {
66
super(LeadRepository.DEFAULT_FIELD_SET);
7+
// Any conditions added in the constructor will apply to all queries in this class
8+
this.whereIsConverted(false);
79
}
810

911
// Overload the constructor if you want to allow other code to specify the field set used
@@ -13,65 +15,55 @@ public without sharing class LeadRepository extends SObjectRepository {
1315

1416
// ISObjectRepository requires at least 2 methods, getRecord & getList
1517
public Lead getRecord(Id leadId) {
16-
String query = this
17-
.addConditionIdEquals(leadId)
18+
return (Lead)this
19+
.whereIdEquals(leadId)
1820
.setAsUpdate()
19-
.getQuery();
20-
21-
return (Lead)Database.query(query)[0];
21+
.getFirstQueryResult();
2222
}
2323

2424
public List<Lead> getList(List<Id> leadIdList) {
25-
String query = this
26-
.addConditionIdIn(leadIdList)
25+
return (List<Lead>)this
26+
.whereIdIn(leadIdList)
2727
.setAsUpdate()
28-
.getQuery();
29-
30-
return (List<Lead>)Database.query(query);
28+
.getQueryResults();
3129
}
3230

3331
// Add public methods needed that return the query results
3432
// Only methods that return an SObject or collection of SObjects should be made public
3533
public List<Lead> getListForSources(List<String> leadSourceList) {
36-
String query = this
37-
.addCondition(Schema.Lead.LeadSource + ' IN ' + CollectionUtils.toString(leadSourceList))
34+
return (List<Lead>)this
35+
.whereFieldIn(Schema.Lead.LeadSource, leadSourceList)
3836
.orderBy(Schema.Lead.CreatedDate)
39-
.getQuery();
40-
41-
return (List<Lead>)Database.query(query);
37+
.getQueryResults();
4238
}
4339

4440
public List<Lead> getListForStatus(String status, Integer limitCount) {
45-
String query = this
46-
.addConditionIsConverted(false)
47-
.addConditionStatusEquals(status)
41+
return (List<Lead>)this
42+
.whereIsConverted(false)
43+
.whereStatusEquals(status)
4844
.limitCount(limitCount)
4945
.orderBy(Schema.Lead.LastModifiedDate, SObjectRepository.SortOrder.DESCENDING)
5046
.setAsUpdate()
51-
.getQuery();
52-
53-
return (List<Lead>)Database.query(query);
47+
.getQueryResults();
5448
}
5549

5650
public List<Lead> searchInAllFields(String searchTerm) {
57-
String query = this
58-
.addConditionIsConverted(false)
51+
return (List<Lead>)this
52+
.whereIsConverted(false)
5953
.orderBy(Schema.Lead.CreatedDate, SObjectRepository.SortOrder.DESCENDING)
6054
.limitCount(10)
6155
.setAsUpdate() // SOSL cannot use FOR UPDATE. This will execute, but a warning debug statement will indicate that it is ignored
62-
.getSearchQuery(searchTerm, SObjectRepository.SearchGroup.ALL_FIELDS);
63-
64-
return (List<Lead>)Search.query(query)[0];
56+
.getSearchResults(searchTerm, SObjectRepository.SearchGroup.ALL_FIELDS);
6557
}
6658

6759
// You can add additional builder methods for any commonly used filters for this SObject
6860
// All builder methods should be kept as private or protected
69-
private LeadRepository addConditionIsConverted(Boolean bool) {
70-
return (LeadRepository)this.addCondition(Schema.Lead.IsConverted + ' = ' + bool);
61+
private LeadRepository whereIsConverted(Boolean bool) {
62+
return (LeadRepository)this.whereFieldEquals(Schema.Lead.IsConverted, bool);
7163
}
7264

73-
private LeadRepository addConditionStatusEquals(String status) {
74-
return (LeadRepository)this.addCondition(Schema.Lead.Status + ' = ' + StringUtils.wrapInSingleQuotes(status));
65+
private LeadRepository whereStatusEquals(String status) {
66+
return (LeadRepository)this.whereFieldEquals(Schema.Lead.Status, status);
7567
}
7668

7769
}

src/classes/SOQLUtils.cls

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public without sharing class SOQLUtils {
2+
3+
public static String toSOQLString(List<Object> valueList) {
4+
List<String> parsedValueList = new List<String>();
5+
for(Object value : valueList) parsedValueList.add(toSOQLString(value));
6+
return '(' + String.join(parsedValueList, ',') + ')';
7+
}
8+
9+
public static String toSOQLString(Object value) {
10+
if(value == null) return null;
11+
else if(value instanceof Boolean) return String.valueOf((Boolean)value);
12+
else if(value instanceof Date) return String.valueOf((Date)value);
13+
else if(value instanceof Datetime) {
14+
Datetime datetimeValue = (Datetime)value;
15+
return datetimeValue.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'', 'Greenwich Mean Time');
16+
}
17+
else if(value instanceof Decimal) return String.valueOf((Decimal) value);
18+
else if(value instanceof Double) return String.valueOf((Double) value);
19+
else if(value instanceof Integer) return String.valueOf((Integer) value);
20+
else if(value instanceof Long) return String.valueOf((Long) value);
21+
else if(value instanceof SObject) {
22+
SObject record = (SObject)value;
23+
return wrapInSingleQuotes(record.Id);
24+
}
25+
else if(value instanceof String) return wrapInSingleQuotes((String)value);
26+
else return String.valueOf(value);
27+
}
28+
29+
public static String wrapInSingleQuotes(String input) {
30+
if(input.left(1) != '\'') input = '\'' + input;
31+
if(input.right(1) != '\'') input = input + '\'';
32+
return input;
33+
}
34+
35+
}

0 commit comments

Comments
 (0)