@@ -73,6 +73,8 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
73
73
**/
74
74
private Boolean enforceFLS ;
75
75
76
+ private Boolean sortSelectFields = true ;
77
+
76
78
/**
77
79
* The relationship and subselectQueryMap variables are used to support subselect queries. Subselects can be added to
78
80
* a query, as long as it isn't a subselect query itself. You may have many subselects inside
@@ -173,6 +175,17 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
173
175
return this ;
174
176
}
175
177
178
+ /**
179
+ * Sets a flag to indicate that this query should have ordered
180
+ * query fields in the select statement (this at a small cost to performance).
181
+ * If you are processing large query sets, you should switch this off.
182
+ * @param whether or not select fields should be sorted in the soql statement.
183
+ **/
184
+ public fflib_QueryFactory setSortSelectFields (Boolean doSort ){
185
+ this .sortSelectFields = doSort ;
186
+ return this ;
187
+ }
188
+
176
189
/**
177
190
* Selects a single field from the SObject specified in {@link #table}.
178
191
* Selecting fields is idempotent, if this field is already selected calling this method will have no additional impact.
@@ -408,6 +421,10 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
408
421
}
409
422
410
423
fflib_QueryFactory subselectQuery = new fflib_QueryFactory (relationship );
424
+
425
+ // The child queryFactory should be configured in the same way as the parent by default - can override after if required
426
+ subSelectQuery .setSortSelectFields (sortSelectFields );
427
+
411
428
if (assertIsAccessible ){
412
429
subSelectQuery .assertIsAccessible ();
413
430
}
@@ -537,13 +554,17 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
537
554
if (fields .size () == 0 ){
538
555
if (enforceFLS ) fflib_SecurityUtils .checkFieldIsReadable (table , ' Id' );
539
556
result += ' Id ' ;
540
- }else {
557
+ }else if ( sortSelectFields ) {
541
558
List <QueryField > fieldsToQuery = new List <QueryField >(fields );
542
559
fieldsToQuery .sort (); // delegates to QueryFilter's comparable implementation
543
560
for (QueryField field : fieldsToQuery ){
544
561
result += field + ' , ' ;
545
562
}
546
- }
563
+ }else {
564
+ for (QueryField field : fields )
565
+ result += field + ' , ' ;
566
+ }
567
+
547
568
if (subselectQueryMap != null && ! subselectQueryMap .isEmpty ()){
548
569
for (fflib_QueryFactory childRow : subselectQueryMap .values ()){
549
570
result += ' (' + childRow .toSOQL () + ' ), ' ;
0 commit comments