Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 751f81a

Browse files
committed
Allow developers to opt out of sorting select fields in selector subclasses, in order to improve performance.
1 parent 72bfa44 commit 751f81a

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

fflib/src/classes/fflib_SObjectSelector.cls

+22-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ public abstract with sharing class fflib_SObjectSelector
5959
* Order by field
6060
**/
6161
private String m_orderBy;
62+
63+
/**
64+
* Sort the query fields in the select statement (defaults to true, at the expense of performance).
65+
* Switch this off if you need more performant queries.
66+
**/
67+
private Boolean m_sortSelectFields;
6268

6369
/**
6470
* Describe helper
@@ -106,10 +112,21 @@ public abstract with sharing class fflib_SObjectSelector
106112
* @param includeFieldSetFields Set to true if the Selector queries are to include Fieldset fields as well
107113
**/
108114
public fflib_SObjectSelector(Boolean includeFieldSetFields, Boolean enforceCRUD, Boolean enforceFLS)
115+
{
116+
this(includeFieldSetFields, enforceCRUD, enforceFLS, true);
117+
}
118+
119+
/**
120+
* Constructs the Selector
121+
*
122+
* @param includeFieldSetFields Set to true if the Selector queries are to include Fieldset fields as well
123+
**/
124+
public fflib_SObjectSelector(Boolean includeFieldSetFields, Boolean enforceCRUD, Boolean enforceFLS, Boolean sortSelectFields)
109125
{
110126
m_includeFieldSetFields = includeFieldSetFields;
111127
m_enforceCRUD = enforceCRUD;
112128
m_enforceFLS = enforceFLS;
129+
m_sortSelectFields = sortSelectFields;
113130
}
114131

115132
/**
@@ -366,7 +383,8 @@ public abstract with sharing class fflib_SObjectSelector
366383
if(includeSelectorFields)
367384
{
368385
// select the Selector fields and Fieldsets and set order
369-
queryFactory.selectFields(new Set<SObjectField>(getSObjectFieldList()));
386+
queryFactory.selectFields(getSObjectFieldList());
387+
370388
List<Schema.FieldSet> fieldSetList = getSObjectFieldSetList();
371389
if(m_includeFieldSetFields && fieldSetList != null)
372390
for(Schema.FieldSet fieldSet : fieldSetList)
@@ -394,7 +412,9 @@ public abstract with sharing class fflib_SObjectSelector
394412
fieldSortOrder = fflib_QueryFactory.SortOrder.ASCENDING;
395413
queryFactory.addOrdering(fieldNamePart, fieldSortOrder);
396414
}
397-
415+
416+
queryFactory.setSortSelectFields(m_sortSelectFields);
417+
398418
return queryFactory;
399419
}
400420
}

fflib/src/classes/fflib_SObjectSelectorTest.cls

+37-3
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private with sharing class fflib_SObjectSelectorTest
144144
return; // Abort the test if unable to create a user with low enough acess
145145
System.runAs(testUser)
146146
{
147-
Testfflib_SObjectSelector selector = new Testfflib_SObjectSelector(false, false, false);
147+
Testfflib_SObjectSelector selector = new Testfflib_SObjectSelector(false, false, false, true);
148148
try
149149
{
150150
List<Account> result = (List<Account>) selector.selectSObjectsById(idSet);
@@ -205,16 +205,50 @@ private with sharing class fflib_SObjectSelectorTest
205205
}
206206
}
207207

208+
209+
@isTest
210+
static void testWithoutSorting()
211+
{
212+
//Given
213+
Testfflib_SObjectSelector selector = new Testfflib_SObjectSelector(false, false, false, false);
214+
fflib_QueryFactory qf = selector.newQueryFactory();
215+
216+
Set<String> expectedSelectFields = new Set<String>{ 'Name', 'Id', 'AccountNumber', 'AnnualRevenue' };
217+
if (UserInfo.isMultiCurrencyOrganization())
218+
{
219+
expectedSelectFields.add('CurrencyIsoCode');
220+
}
221+
222+
//When
223+
String soql = qf.toSOQL();
224+
225+
//Then
226+
Pattern soqlPattern = Pattern.compile('SELECT (.*) FROM Account ORDER BY Name DESC NULLS FIRST , AnnualRevenue ASC NULLS FIRST ');
227+
Matcher soqlMatcher = soqlPattern.matcher(soql);
228+
soqlMatcher.matches();
229+
230+
List<String> actualSelectFields = soqlMatcher.group(1).deleteWhiteSpace().split(',');
231+
System.assertEquals(expectedSelectFields, new Set<String>(actualSelectFields));
232+
}
233+
234+
private static void assertEqualsSelectFields(String expectedSelectFields, String actualSelectFields)
235+
{
236+
Set<String> expected = new Set<String>(expectedSelectFields.deleteWhiteSpace().split(','));
237+
Set<String> actual = new Set<String>(actualSelectFields.deleteWhiteSpace().split(','));
238+
239+
System.assertEquals(expected, actual);
240+
}
241+
208242
private class Testfflib_SObjectSelector extends fflib_SObjectSelector
209243
{
210244
public Testfflib_SObjectSelector()
211245
{
212246
super();
213247
}
214248

215-
public Testfflib_SObjectSelector(Boolean includeFieldSetFields, Boolean enforceCRUD, Boolean enforceFLS)
249+
public Testfflib_SObjectSelector(Boolean includeFieldSetFields, Boolean enforceCRUD, Boolean enforceFLS, Boolean sortSelectFields)
216250
{
217-
super(includeFieldSetFields, enforceCRUD, enforceFLS);
251+
super(includeFieldSetFields, enforceCRUD, enforceFLS, sortSelectFields);
218252
}
219253

220254
public List<Schema.SObjectField> getSObjectFieldList()

0 commit comments

Comments
 (0)