Skip to content

Commit 43d2bc8

Browse files
authored
Merge pull request #176 from benedwards44/master
Added support to the getOrderBy() override to support NULLS LAST setting
2 parents a455555 + 07523d1 commit 43d2bc8

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

fflib/src/classes/fflib_SObjectSelector.cls

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,6 @@ public abstract with sharing class fflib_SObjectSelector
420420
// Parse the getOrderBy()
421421
for(String orderBy : getOrderBy().split(','))
422422
{
423-
// TODO: Handle NULLS FIRST and NULLS LAST, http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_orderby.htm
424423
List<String> orderByParts = orderBy.trim().split(' ');
425424
String fieldNamePart = orderByParts[0];
426425
String fieldSortOrderPart = orderByParts.size() > 1 ? orderByParts[1] : null;
@@ -431,7 +430,7 @@ public abstract with sharing class fflib_SObjectSelector
431430
fieldSortOrder = fflib_QueryFactory.SortOrder.DESCENDING;
432431
else if(fieldSortOrderPart.equalsIgnoreCase('ASC'))
433432
fieldSortOrder = fflib_QueryFactory.SortOrder.ASCENDING;
434-
queryFactory.addOrdering(fieldNamePart, fieldSortOrder);
433+
queryFactory.addOrdering(fieldNamePart, fieldSortOrder, orderBy.containsIgnoreCase('NULLS LAST'));
435434
}
436435

437436
queryFactory.setSortSelectFields(m_sortSelectFields);

fflib/src/classes/fflib_SObjectSelectorTest.cls

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private with sharing class fflib_SObjectSelectorTest
151151
{
152152
Testfflib_SObjectSelector selector = new Testfflib_SObjectSelector();
153153
String soql = selector.newQueryFactory().toSOQL();
154-
Pattern p = Pattern.compile('SELECT (.*) FROM Account ORDER BY Name DESC NULLS FIRST , AnnualRevenue ASC NULLS FIRST ');
154+
Pattern p = Pattern.compile('SELECT (.*) FROM Account ORDER BY Name DESC NULLS FIRST , AnnualRevenue ASC NULLS LAST ');
155155
Matcher m = p.matcher(soql);
156156
System.assert(m.matches(), 'Generated SOQL does not match expected pattern. Here is the generated SOQL: ' + soql);
157157
System.assertEquals(1, m.groupCount(), 'Unexpected number of groups captured.');
@@ -208,13 +208,37 @@ private with sharing class fflib_SObjectSelectorTest
208208
String soql = qf.toSOQL();
209209

210210
//Then
211-
Pattern soqlPattern = Pattern.compile('SELECT (.*) FROM Account ORDER BY Name DESC NULLS FIRST , AnnualRevenue ASC NULLS FIRST ');
211+
Pattern soqlPattern = Pattern.compile('SELECT (.*) FROM Account ORDER BY Name DESC NULLS FIRST , AnnualRevenue ASC NULLS LAST ');
212212
Matcher soqlMatcher = soqlPattern.matcher(soql);
213213
soqlMatcher.matches();
214214

215215
List<String> actualSelectFields = soqlMatcher.group(1).deleteWhiteSpace().split(',');
216216
System.assertEquals(expectedSelectFields, new Set<String>(actualSelectFields));
217217
}
218+
219+
// Test case of ordering with NULLS LAST option passed into the ordering method
220+
@isTest
221+
static void testWithOrderingNullsLast()
222+
{
223+
// Build the selector to test with
224+
Testfflib_SObjectSelector selector = new Testfflib_SObjectSelector(false, false, false, false);
225+
fflib_QueryFactory qf = selector.newQueryFactory();
226+
227+
// Add in the expected fields
228+
Set<String> expectedSelectFields = new Set<String>{ 'Name', 'Id', 'AccountNumber', 'AnnualRevenue' };
229+
if (UserInfo.isMultiCurrencyOrganization())
230+
{
231+
expectedSelectFields.add('CurrencyIsoCode');
232+
}
233+
234+
// Generate the SOQL string
235+
String soql = qf.toSOQL();
236+
237+
// Assert that the
238+
Pattern soqlPattern = Pattern.compile('SELECT (.*) FROM Account ORDER BY Name DESC NULLS FIRST , AnnualRevenue ASC NULLS LAST ');
239+
Matcher soqlMatcher = soqlPattern.matcher(soql);
240+
system.assert(soqlMatcher.matches(), 'The SOQL should have that expected.');
241+
}
218242

219243
private static void assertEqualsSelectFields(String expectedSelectFields, String actualSelectFields)
220244
{
@@ -253,7 +277,7 @@ private with sharing class fflib_SObjectSelectorTest
253277

254278
public override String getOrderBy()
255279
{
256-
return 'Name DESC, AnnualRevenue ASC';
280+
return 'Name DESC, AnnualRevenue ASC NULLS LAST';
257281
}
258282
}
259283

0 commit comments

Comments
 (0)