Skip to content

Commit 6165233

Browse files
author
Tom Fuda
committed
Code review changes. Adds a whole bunch of tests of the LightweightQueryField implementation
1 parent e4a4934 commit 6165233

File tree

2 files changed

+302
-51
lines changed

2 files changed

+302
-51
lines changed

fflib/src/classes/fflib_QueryFactory.cls

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
7171
/* This can optionally be enforced (or not) by calling the setEnforceFLS method prior to calling
7272
/* one of the selectField or selectFieldset methods.
7373
**/
74+
@TestVisible
7475
private Boolean enforceFLS = false;
75-
76+
@TestVisible
7677
private Boolean lightweight = false;
7778

7879
/**
@@ -137,6 +138,15 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
137138
return ((fflib_QueryFactory)obj).toSOQL() == this.toSOQL();
138139
}
139140

141+
/**
142+
* Construct a new fflib_QueryFactory instance with no options other than the FROM caluse.
143+
* You *must* call selectField(s) before {@link #toSOQL} will return a valid, runnable query.
144+
* @param table the SObject to be used in the FROM clause of the resultant query. This sets the value of {@link #table}.
145+
**/
146+
public fflib_QueryFactory(Schema.SObjectType table){
147+
this(table, false);
148+
}
149+
140150
/**
141151
* Construct a new fflib_QueryFactory instance, allowing you to use LightweightQueryFields
142152
* to build the query. This offers significant performance improvement in query build time
@@ -145,37 +155,37 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
145155
* @param lightweight a Boolean that specifies whether the LightweightQueryField is to be used when building the query.
146156
**/
147157
public fflib_QueryFactory(Schema.SObjectType table, Boolean lightweight) {
148-
this(table);
158+
this.table = table;
159+
this.fields = new List<QueryField>();
160+
this.order = new List<Ordering>();
149161
this.lightweight = lightweight;
150-
if (lightweight) {
151-
this.enforceFLS = false;
152-
}
162+
this.enforceFLS = false;
153163
}
154164

155165
/**
156-
* Construct a new fflib_QueryFactory instance with no options other than the FROM caluse.
166+
* Construct a new fflib_QueryFactory instance with no options other than the FROM clause and the relationship.
167+
* This should be used when constructing a subquery query for addition to a parent query.
168+
* Objects created with this constructor cannot be added to another object using the subselectQuery method.
157169
* You *must* call selectField(s) before {@link #toSOQL} will return a valid, runnable query.
158-
* @param table the SObject to be used in the FROM clause of the resultant query. This sets the value of {@link #table}.
170+
* @param relationship the ChildRelationship to be used in the FROM Clause of the resultant Query (when set overrides value of table). This sets the value of {@link #relationship} and {@link #table}.
159171
**/
160-
public fflib_QueryFactory(Schema.SObjectType table){
161-
this.table = table;
162-
fields = new List<QueryField>();
163-
order = new List<Ordering>();
164-
enforceFLS = false;
172+
private fflib_QueryFactory(Schema.ChildRelationship relationship){
173+
this(relationship, false);
165174
}
166-
175+
167176
/**
168177
* Construct a new fflib_QueryFactory instance with no options other than the FROM clause and the relationship.
169178
* This should be used when constructing a subquery query for addition to a parent query.
170179
* Objects created with this constructor cannot be added to another object using the subselectQuery method.
171180
* You *must* call selectField(s) before {@link #toSOQL} will return a valid, runnable query.
172181
* @param relationship the ChildRelationship to be used in the FROM Clause of the resultant Query (when set overrides value of table). This sets the value of {@link #relationship} and {@link #table}.
182+
* @param lightweight a Boolean that specifies whether the LightweightQueryField is to be used when building the query.
173183
**/
174-
private fflib_QueryFactory(Schema.ChildRelationship relationship){
175-
this(relationship.getChildSObject());
184+
private fflib_QueryFactory(Schema.ChildRelationship relationship, Boolean lightweight){
185+
this(relationship.getChildSObject(), lightweight);
176186
this.relationship = relationship;
177187
}
178-
188+
179189
/**
180190
* This method checks to see if the User has Read Access on {@link #table}.
181191
* Asserts true if User has access.
@@ -230,12 +240,24 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
230240
throw new InvalidFieldException(null,this.table);
231241
if (enforceFLS)
232242
fflib_SecurityUtils.checkFieldIsReadable(table, field);
233-
if (lightweight)
234-
this.fields.add(new LightweightQueryField(field));
235-
else
236-
this.fields.add(new QueryField(field));
243+
this.fields.add(getQueryFieldFromToken(field));
237244
return this;
238245
}
246+
247+
/**
248+
* Returns the appropriate QueryField implementation, based on the "lightweight" flag
249+
* @param field the {@link Schema.SObjectField} for the QueryField
250+
* @returns either a QueryField, or LightweightQueryField object for the specified SObjectField
251+
**/
252+
private QueryField getQueryFieldFromToken(Schema.SObjectField field) {
253+
QueryField qf;
254+
if (this.lightweight)
255+
qf = new LightweightQueryField(field);
256+
else
257+
qf = new QueryField(field);
258+
return qf;
259+
}
260+
239261
/**
240262
* Selects multiple fields. This acts the same as calling {@link #selectField(String)} multiple times.
241263
* @param fieldNames the Set of field API names to select.
@@ -265,11 +287,8 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
265287
if(token == null)
266288
throw new InvalidFieldException();
267289
if (enforceFLS)
268-
fflib_SecurityUtils.checkFieldIsReadable(table, token);
269-
if (lightweight)
270-
this.fields.add(new LightweightQueryField(token));
271-
else
272-
this.fields.add(new QueryField(token));
290+
fflib_SecurityUtils.checkFieldIsReadable(table, token);
291+
this.fields.add(getQueryFieldFromToken(token));
273292
}
274293
return this;
275294
}
@@ -283,11 +302,8 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
283302
if(token == null)
284303
throw new InvalidFieldException();
285304
if (enforceFLS)
286-
fflib_SecurityUtils.checkFieldIsReadable(table, token);
287-
if (lightweight)
288-
this.fields.add(new LightweightQueryField(token));
289-
else
290-
this.fields.add(new QueryField(token));
305+
fflib_SecurityUtils.checkFieldIsReadable(table, token);
306+
this.fields.add(getQueryFieldFromToken(token));
291307
}
292308
return this;
293309
}
@@ -361,13 +377,6 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
361377
return new Set<QueryField>(this.fields);
362378
}
363379

364-
/**
365-
* @returns the selected fields as a List<QueryField>
366-
**/
367-
public List<QueryField> getSelectedFieldsAsList() {
368-
return this.fields;
369-
}
370-
371380
/**
372381
* Add a subquery query to this query. If a subquery for this relationship already exists, it will be returned.
373382
* If not, a new one will be created and returned.
@@ -456,7 +465,7 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
456465
return subselectQueryMap.get(relationship);
457466
}
458467

459-
fflib_QueryFactory subselectQuery = new fflib_QueryFactory(relationship);
468+
fflib_QueryFactory subselectQuery = new fflib_QueryFactory(relationship, this.lightweight);
460469

461470
if(assertIsAccessible){
462471
subSelectQuery.assertIsAccessible();
@@ -534,7 +543,7 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
534543
**/
535544
public fflib_QueryFactory addOrdering(SObjectField field, SortOrder direction, Boolean nullsLast){
536545
order.add(
537-
new Ordering(this.lightweight ? new LightweightQueryField(field) : new QueryField(field), direction, nullsLast)
546+
new Ordering(getQueryFieldFromToken(field), direction, nullsLast)
538547
);
539548
return this;
540549
}
@@ -572,7 +581,7 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
572581
**/
573582
public fflib_QueryFactory addOrdering(SObjectField field, SortOrder direction){
574583
order.add(
575-
new Ordering(this.lightweight ? new LightweightQueryField(field) : new QueryField(field), direction)
584+
new Ordering(getQueryFieldFromToken(field), direction)
576585
);
577586
return this;
578587
}
@@ -630,7 +639,7 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
630639
**/
631640
public fflib_QueryFactory deepClone(){
632641

633-
fflib_QueryFactory clone = new fflib_QueryFactory(this.table)
642+
fflib_QueryFactory clone = new fflib_QueryFactory(this.table, this.lightweight)
634643
.setLimit(this.limitCount)
635644
.setCondition(this.conditionExpression)
636645
.setEnforceFLS(this.enforceFLS);
@@ -726,16 +735,17 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
726735
}
727736

728737
public override Boolean equals(Object obj) {
729-
// compareTo does all the heavy lifting needed to determine equality
730-
return compareTo(obj) == 0;
738+
return ((obj != null)
739+
&& (obj instanceof LightweightQueryField)
740+
&& (this.fieldName == ((LightweightQueryField) obj).fieldName));
731741
}
732742

733743
public override Integer compareTo(Object obj) {
734744
if (obj == null || !(obj instanceof LightweightQueryField))
735745
return 1;
736746

737747
if (this.fieldName == null) {
738-
if (((LightweightQueryField) obj).toString() == null)
748+
if (((LightweightQueryField) obj).fieldName == null)
739749
// Both objects are non-null, but their fieldName is null
740750
return 0;
741751
else
@@ -744,7 +754,7 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
744754
}
745755

746756
// Both objects have non-null fieldNames, so just return the result of String.compareTo
747-
return this.fieldName.compareTo(((LightweightQueryField) obj).toString());
757+
return this.fieldName.compareTo(((LightweightQueryField) obj).fieldName);
748758
}
749759
}
750760

0 commit comments

Comments
 (0)