1414**/
1515@SuppressWarnings(' PMD.ExcessivePublicCount, PMD.ExcessiveClassLength, PMD.CyclomaticComplexity, PMD.CognitiveComplexity, PMD.PropertyNamingConventions, PMD.FieldDeclarationsShouldBeAtStart, PMD.ApexDoc, PMD.ExcessiveParameterList' )
1616public virtual inherited sharing class SOQL implements Queryable {
17-
1817 public static SubQuery SubQuery {
1918 get {
2019 return new SoqlSubQuery ();
@@ -59,8 +58,9 @@ public virtual inherited sharing class SOQL implements Queryable {
5958 Queryable with (String relationshipName , SObjectField field1 , SObjectField field2 , SObjectField field3 );
6059 Queryable with (String relationshipName , SObjectField field1 , SObjectField field2 , SObjectField field3 , SObjectField field4 );
6160 Queryable with (String relationshipName , SObjectField field1 , SObjectField field2 , SObjectField field3 , SObjectField field4 , SObjectField field5 );
62- Queryable with (String relationshipName , List <SObjectField > fields );
61+ Queryable with (String relationshipName , Iterable <SObjectField > fields );
6362 Queryable with (SubQuery subQuery );
63+ Queryable withFieldSet (String fieldSetName );
6464 // SELECT - AGGREGATE FUNCTIONS
6565 Queryable count ();
6666 Queryable count (SObjectField field );
@@ -144,6 +144,7 @@ public virtual inherited sharing class SOQL implements Queryable {
144144 Queryable byId (Id recordId );
145145 Queryable byIds (Iterable <Id > recordIds );
146146 Queryable byIds (List <SObject > records );
147+ Queryable byRecordType (String recordTypeDeveloperName );
147148 // RESULT
148149 Boolean doExist ();
149150 String toString ();
@@ -170,7 +171,7 @@ public virtual inherited sharing class SOQL implements Queryable {
170171 SubQuery with (SObjectField field1 , SObjectField field2 , SObjectField field3 , SObjectField field4 );
171172 SubQuery with (SObjectField field1 , SObjectField field2 , SObjectField field3 , SObjectField field4 , SObjectField field5 );
172173 SubQuery with (List <SObjectField > fields );
173- SubQuery with (String relationshipName , List <SObjectField > fields );
174+ SubQuery with (String relationshipName , Iterable <SObjectField > fields );
174175 SubQuery with (SubQuery subQuery );
175176 // WHERE
176177 SubQuery whereAre (FilterGroup filterGroup );
@@ -232,10 +233,8 @@ public virtual inherited sharing class SOQL implements Queryable {
232233 Filter contains (String prefix , String value , String suffix );
233234 Filter notContains (String prefix , String value , String suffix );
234235 Filter isIn (Iterable <Object > iterable );
235- Filter isIn (List <Object > inList );
236236 Filter isIn (InnerJoin joinQuery );
237237 Filter notIn (Iterable <Object > iterable );
238- Filter notIn (List <Object > inList );
239238 Filter notIn (InnerJoin joinQuery );
240239 Filter includesAll (Iterable <String > values );
241240 Filter includesSome (Iterable <String > values );
@@ -327,6 +326,11 @@ public virtual inherited sharing class SOQL implements Queryable {
327326 return with (String .join (fields , ' ,' ));
328327 }
329328
329+ public SOQL withFieldSet (String fieldSetName ) {
330+ builder .fields .withFieldSet (fieldSetName );
331+ return this ;
332+ }
333+
330334 public SOQL with (String fields ) {
331335 builder .fields .with (fields );
332336 return this ;
@@ -357,7 +361,7 @@ public virtual inherited sharing class SOQL implements Queryable {
357361 return with (relationshipName , new List <SObjectField >{ field1 , field2 , field3 , field4 , field5 });
358362 }
359363
360- public SOQL with (String relationshipName , List <SObjectField > fields ) {
364+ public SOQL with (String relationshipName , Iterable <SObjectField > fields ) {
361365 builder .fields .with (relationshipName , fields );
362366 return this ;
363367 }
@@ -762,6 +766,10 @@ public virtual inherited sharing class SOQL implements Queryable {
762766 return whereAre (Filter .id ().isIn (records ));
763767 }
764768
769+ public SOQL byRecordType (String recordTypeDeveloperName ) {
770+ return whereAre (Filter .recordType ().equal (recordTypeDeveloperName ));
771+ }
772+
765773 public interface QueryClause {
766774 String toString ();
767775 }
@@ -770,7 +778,7 @@ public virtual inherited sharing class SOQL implements Queryable {
770778 private List <QueryClause > clauses = new QueryClause [10 ];
771779
772780 public QueryBuilder (String ofObject ) {
773- clauses .set (0 , new SoqlFields ());
781+ clauses .set (0 , new SoqlFields (ofObject ));
774782 clauses .set (2 , new SoqlFrom (ofObject ));
775783 }
776784
@@ -782,36 +790,28 @@ public virtual inherited sharing class SOQL implements Queryable {
782790
783791 public SoqlSubQueries subQueries {
784792 get {
785- if (clauses [1 ] == null ) {
786- clauses .set (1 , new SoqlSubQueries ());
787- }
793+ createWhenNotExist (1 , new SoqlSubQueries ());
788794 return (SoqlSubQueries ) clauses [1 ];
789795 }
790796 }
791797
792798 public SoqlScope scope {
793799 get {
794- if (clauses [3 ] == null ) {
795- clauses .set (3 , new SoqlScope ());
796- }
800+ createWhenNotExist (3 , new SoqlScope ());
797801 return (SoqlScope ) clauses [3 ];
798802 }
799803 }
800804
801805 public MainFilterGroup conditions {
802806 get {
803- if (clauses [4 ] == null ) {
804- clauses .set (4 , new MainFilterGroup ());
805- }
807+ createWhenNotExist (4 , new MainFilterGroup ());
806808 return (MainFilterGroup ) clauses [4 ];
807809 }
808810 }
809811
810812 public SoqlGroupBy groupBy {
811813 get {
812- if (clauses [5 ] == null ) {
813- clauses .set (5 , new SoqlGroupBy ());
814- }
814+ createWhenNotExist (5 , new SoqlGroupBy ());
815815 return (SoqlGroupBy ) clauses [5 ];
816816 }
817817 }
@@ -824,61 +824,63 @@ public virtual inherited sharing class SOQL implements Queryable {
824824
825825 public SoqlOrderBys orderBys {
826826 get {
827- if (clauses [6 ] == null ) {
828- clauses .set (6 , new SoqlOrderBys ());
829- }
827+ createWhenNotExist (6 , new SoqlOrderBys ());
830828 return (SoqlOrderBys ) clauses [6 ];
831829 }
832830 }
833831
834832 public SoqlLimit soqlLimit {
835833 get {
836- if (clauses [7 ] == null ) {
837- clauses .set (7 , new SoqlLimit ());
838- }
834+ createWhenNotExist (7 , new SoqlLimit ());
839835 return (SoqlLimit ) clauses [7 ];
840836 }
841837 }
842838
843839 public SoqlOffset soqlOffset {
844840 get {
845- if (clauses [8 ] == null ) {
846- clauses .set (8 , new SoqlOffset ());
847- }
841+ createWhenNotExist (8 , new SoqlOffset ());
848842 return (SoqlOffset ) clauses [8 ];
849843 }
850844 }
851845
852846 public SoqlFor soqlFor {
853847 get {
854- if (clauses [9 ] == null ) {
855- clauses .set (9 , new SoqlFor ());
856- }
848+ createWhenNotExist (9 , new SoqlFor ());
857849 return (SoqlFor ) clauses [9 ];
858850 }
859851 }
860852
853+ public void createWhenNotExist (Integer position , QueryClause clause ) {
854+ if (clauses [position ] == null ) {
855+ clauses .set (position , clause );
856+ }
857+ }
858+
861859 public override String toString () {
862860 binder = new Binder ();
863861
864- List < String > soqlParts = new List < String >() ;
862+ String query = ' ' ;
865863
866864 for (QueryClause clause : clauses ) {
867- if (clause = = null ) {
868- continue ;
865+ if (clause ! = null ) {
866+ query += ' ' + clause . toString () ;
869867 }
870- soqlParts .add (clause .toString ());
871868 }
872869
873- return String . join ( soqlParts , ' ' ) .trim ();
870+ return query .trim ();
874871 }
875872 }
876873
877874 private class SoqlFields implements QueryClause {
875+ private String ofObject ;
878876 private Set <String > fields = new Set <String >();
879877 private Set <String > aggregateFunctions = new Set <String >();
880878 private Set <String > groupedFields = new Set <String >();
881879
880+ public SoqlFields (String ofObject ) {
881+ this .ofObject = ofObject ;
882+ }
883+
882884 public void count () {
883885 clearAllFields (); // COUNT() must be the only element in the SELECT list.
884886 withAggregateFunction (' COUNT()' , ' ' );
@@ -983,6 +985,18 @@ public virtual inherited sharing class SOQL implements Queryable {
983985 }
984986 }
985987
988+ public void withFieldSet (String fieldSetName ) {
989+ FieldSet fieldSet = Schema .describeSObjects (new List <String >{ ofObject })[0 ].FieldSets .getMap ().get (fieldSetName );
990+
991+ if (fieldSet == null ) {
992+ throw new QueryException (' FieldSet with name ' + fieldSetName + ' does not exist!' );
993+ }
994+
995+ for (Schema .FieldSetMember field : fieldSet .getFields ()) {
996+ with (field .getFieldPath ());
997+ }
998+ }
999+
9861000 public void with (SObjectField field ) {
9871001 fields .add (field .getDescribe ().getName ());
9881002 }
@@ -995,7 +1009,7 @@ public virtual inherited sharing class SOQL implements Queryable {
9951009 fields .add (' FORMAT(' + field + ' ) ' + alias );
9961010 }
9971011
998- public void with (String relationshipPath , List <SObjectField > fields ) {
1012+ public void with (String relationshipPath , Iterable <SObjectField > fields ) {
9991013 for (SObjectField field : fields ) {
10001014 with (relationshipPath , field );
10011015 }
@@ -1079,7 +1093,7 @@ public virtual inherited sharing class SOQL implements Queryable {
10791093 return this ;
10801094 }
10811095
1082- public SubQuery with (String relationshipName , List <SObjectField > fields ) {
1096+ public SubQuery with (String relationshipName , Iterable <SObjectField > fields ) {
10831097 builder .fields .with (relationshipName , fields );
10841098 return this ;
10851099 }
@@ -1206,8 +1220,8 @@ public virtual inherited sharing class SOQL implements Queryable {
12061220 }
12071221 }
12081222
1209- public interface FilterClause {
1210- Boolean isEmpty ();
1223+ private interface FilterClause {
1224+ Boolean hasValue ();
12111225 }
12121226
12131227 private virtual class SoqlFilterGroup implements FilterGroup {
@@ -1228,10 +1242,9 @@ public virtual inherited sharing class SOQL implements Queryable {
12281242 }
12291243
12301244 public FilterGroup add (FilterClause condition ) {
1231- if (condition .isEmpty ()) {
1232- return this ;
1245+ if (condition .hasValue ()) {
1246+ queryConditions . add ( condition ) ;
12331247 }
1234- queryConditions .add (condition );
12351248 return this ;
12361249 }
12371250
@@ -1306,8 +1319,8 @@ public virtual inherited sharing class SOQL implements Queryable {
13061319 this .filterGroup = filterGroup ;
13071320 }
13081321
1309- public Boolean isEmpty () {
1310- return ! filterGroup .hasValues ();
1322+ public Boolean hasValue () {
1323+ return filterGroup .hasValues ();
13111324 }
13121325
13131326 public override String toString () {
@@ -1322,8 +1335,8 @@ public virtual inherited sharing class SOQL implements Queryable {
13221335 this .filter = filter ;
13231336 }
13241337
1325- public Boolean isEmpty () {
1326- return ! filter .hasValue ();
1338+ public Boolean hasValue () {
1339+ return filter .hasValue ();
13271340 }
13281341
13291342 public override String toString () {
@@ -1338,8 +1351,8 @@ public virtual inherited sharing class SOQL implements Queryable {
13381351 conditionString = dynamicCondition ;
13391352 }
13401353
1341- public Boolean isEmpty () {
1342- return String .isEmpty (conditionString );
1354+ public Boolean hasValue () {
1355+ return String .isNotEmpty (conditionString );
13431356 }
13441357
13451358 public override String toString () {
@@ -1456,7 +1469,7 @@ public virtual inherited sharing class SOQL implements Queryable {
14561469 }
14571470
14581471 private String formattedString (String value ) {
1459- return value == null ? value : value .trim ();
1472+ return value ?? value .trim ();
14601473 }
14611474
14621475 public Filter isIn (Iterable <Object > iterable ) {
@@ -1588,9 +1601,7 @@ public virtual inherited sharing class SOQL implements Queryable {
15881601
15891602 public void setGroupByFunction (String newGroupByFunction ) {
15901603 if (String .isNotEmpty (groupByFunction ) && groupByFunction != newGroupByFunction ) {
1591- QueryException e = new QueryException ();
1592- e .setMessage (' You cant use GROUP BY, GROUP BY ROLLUP and GROUP BY CUBE in the same query.' );
1593- throw e ;
1604+ throw new QueryException (' You cant use GROUP BY, GROUP BY ROLLUP and GROUP BY CUBE in the same query.' );
15941605 }
15951606 this .groupByFunction = newGroupByFunction ;
15961607 }
@@ -1787,9 +1798,7 @@ public virtual inherited sharing class SOQL implements Queryable {
17871798 List <SObject > records = toList ();
17881799
17891800 if (records .size () > 1 ) {
1790- QueryException e = new QueryException ();
1791- e .setMessage (' List has more than 1 row for assignment to SObject' );
1792- throw e ;
1801+ throw new QueryException (' List has more than 1 row for assignment to SObject' );
17931802 }
17941803
17951804 if (records .size () == 0 ) {
0 commit comments