diff --git a/force-app/main/default/classes/SOQL.cls b/force-app/main/default/classes/SOQL.cls index 48461d2..cccd18e 100644 --- a/force-app/main/default/classes/SOQL.cls +++ b/force-app/main/default/classes/SOQL.cls @@ -70,8 +70,10 @@ public inherited sharing class SOQL implements Queryable { SOQL groupBy(SObjectField field); SOQL groupByRollup(SObjectField field); - SOQL orderBy(SObjectField field); - SOQL orderBy(String relationshipName, SObjectField field); + SOQL orderBy(String field); // ASC, NULLS FIRST by default + SOQL orderBy(String field, String direction); // dynamic order by, NULLS FIRST by default + SOQL orderBy(SObjectField field); // ASC, NULLS FIRST by default + SOQL orderBy(String relationshipName, SObjectField field); // ASC, NULLS FIRST by default SOQL sortDesc(); SOQL nullsLast(); @@ -104,8 +106,8 @@ public inherited sharing class SOQL implements Queryable { Map toMap(); Database.QueryLocator toQueryLocator(); - SOQL byId(Id recordId); SOQL byId(SObject record); + SOQL byId(Id recordId); SOQL byIds(Set recordIds); SOQL byIds(List recordIds); SOQL byIds(List records); @@ -314,13 +316,23 @@ public inherited sharing class SOQL implements Queryable { return this; } + public SOQL orderBy(String field) { // Order By - ASC, NULLS FIRST by default + builder.orderBys.add(new QOrderBy().with(field)); + return this; + } + + public SOQL orderBy(String field, String direction) { // NULLS FIRST by default + builder.orderBys.add(new QOrderBy().with(field).sortingOrder(direction)); + return this; + } + public SOQL orderBy(SObjectField field) { // Order By - ASC, NULLS FIRST by default - builder.orderBys.add(field); + builder.orderBys.add(new QOrderBy().with(field)); return this; } public SOQL orderBy(String relationshipName, SObjectField field) { - builder.orderBys.add(relationshipName, field); + builder.orderBys.add(new QOrderBy().with(relationshipName, field)); return this; } @@ -431,12 +443,12 @@ public inherited sharing class SOQL implements Queryable { return executor.toQueryLocator(builder.build(), binder.build()); } - public SOQL byId(Id recordId) { - return whereAre(Filter.id().equal(recordId)); + public SOQL byId(SObject record) { + return byId(record.Id); } - public SOQL byId(SObject record) { - return whereAre(Filter.id().equal(record)); + public SOQL byId(Id recordId) { + return whereAre(Filter.id().equal(recordId)); } public SOQL byIds(Set recordIds) { @@ -648,12 +660,12 @@ public inherited sharing class SOQL implements Queryable { } public SubQuery orderBy(SObjectField field) { - builder.orderBys.add(field); + builder.orderBys.add(new QOrderBy().with(field)); return this; } public SubQuery orderBy(String relationshipName, SObjectField field) { - builder.orderBys.add(relationshipName, field); + builder.orderBys.add(new QOrderBy().with(relationshipName, field)); return this; } @@ -756,6 +768,7 @@ public inherited sharing class SOQL implements Queryable { public interface FilterClause { String build(); + Boolean isEmpty(); } public class FilterGroup implements FilterClause, QueryClause, SoqlFiltersGroup { @@ -769,6 +782,9 @@ public inherited sharing class SOQL implements Queryable { } public FilterGroup add(FilterClause condition) { + if (condition.isEmpty()) { + return this; + } queryConditions.add(condition); return this; } @@ -785,6 +801,10 @@ public inherited sharing class SOQL implements Queryable { return this; } + public Boolean isEmpty() { + return queryConditions.isEmpty(); + } + public String build() { if (String.isNotEmpty(stringConditions)) { return 'WHERE ' + stringConditions; @@ -982,6 +1002,10 @@ public inherited sharing class SOQL implements Queryable { return this; } + public Boolean isEmpty() { + return String.isEmpty(field); + } + public String build() { if (joinQuery != null) { return field + ' ' + comperator + ' (' + joinQuery.build() + ')'; @@ -1038,12 +1062,8 @@ public inherited sharing class SOQL implements Queryable { private class QOrderBys implements QueryClause { public List orderBys = new List(); - public void add(SObjectField field) { - orderBys.add(new QOrderBy().with(field)); - } - - public void add(String relationshipName, SObjectField field) { - orderBys.add(new QOrderBy().with(relationshipName, field)); + public void add(QOrderBy orderBy) { + orderBys.add(orderBy); } public QOrderBy recentOrderBy() { @@ -1066,6 +1086,11 @@ public inherited sharing class SOQL implements Queryable { private String sortingOrder = 'ASC'; private String nullsOrder = 'FIRST'; + public QOrderBy with(String field) { + orderField = field; + return this; + } + public QOrderBy with(SObjectField field) { orderField = field.getDescribe().getName(); return this; @@ -1077,7 +1102,12 @@ public inherited sharing class SOQL implements Queryable { } public void sortDesc() { - sortingOrder = 'DESC'; + sortingOrder('DESC'); + } + + public QOrderBy sortingOrder(String direction) { + sortingOrder = direction; + return this; } public void nullsLast() { diff --git a/force-app/main/default/classes/SOQLTest.cls b/force-app/main/default/classes/SOQLTest.cls index 3a69003..90b746c 100644 --- a/force-app/main/default/classes/SOQLTest.cls +++ b/force-app/main/default/classes/SOQLTest.cls @@ -714,6 +714,17 @@ private class SOQLTest { Assert.areEqual('Krakow', binding.get('v2')); } + @IsTest + static void emptyConditionsGroup() { + // Test + SOQL builder = SOQL.of(Account.SObjectType) + .whereAre(SOQL.FilterGroup); + + // Verify + String soql = builder.toString(); + Assert.areEqual('SELECT Id FROM Account', soql); + } + @IsTest static void nestedConditionsGroup() { // Test @@ -825,6 +836,29 @@ private class SOQLTest { Assert.areEqual('SELECT LeadSource, COUNT(Name) cnt FROM Lead GROUP BY ROLLUP(LeadSource)', soql); } + @IsTest + static void orderByString() { + // Test + String soql = SOQL.of(Account.SObjectType) + .orderBy('Industry').sortDesc().nullsLast() + .orderBy('Id') + .toString(); + + // Verify + Assert.areEqual('SELECT Id FROM Account ORDER BY Industry DESC NULLS LAST, Id ASC NULLS FIRST', soql); + } + + @IsTest + static void orderByDynamic() { + // Test + String soql = SOQL.of(Account.SObjectType) + .orderBy('Industry', 'ASC') + .toString(); + + // Verify + Assert.areEqual('SELECT Id FROM Account ORDER BY Industry ASC NULLS FIRST', soql); + } + @IsTest static void orderBy() { // Test @@ -941,7 +975,7 @@ private class SOQLTest { Assert.areEqual('SELECT Id FROM Case WHERE Id = :v1', soql); Map binding = builder.binding(); - Assert.areEqual(cases[0], binding.get('v1')); + Assert.areEqual(cases[0].Id, binding.get('v1')); } @IsTest diff --git a/website/docs/api/soql.md b/website/docs/api/soql.md index 46c1da7..cd230b5 100644 --- a/website/docs/api/soql.md +++ b/website/docs/api/soql.md @@ -494,6 +494,8 @@ QS.of(Lead.SObjectType) ```apex SOQL orderBy(SObjectField field) +SOQL orderBy(String field) +SOQL orderBy(String field, String direction) ``` **Example** @@ -501,11 +503,21 @@ SOQL orderBy(SObjectField field) ```sql SELECT Id FROM Account -ORDER BY Name +ORDER BY Name DESC ``` ```apex SOQL.of(Account.SObjectType) .orderBy(Account.Name) + .sortDesc() + .toList(); + +SOQL.of(Account.SObjectType) + .orderBy('Name') + .sortDesc() + .toList(); + +SOQL.of(Account.SObjectType) + .orderBy('Name', 'DESC') .toList(); ```