Skip to content

Commit

Permalink
empty conditions fix (#29)
Browse files Browse the repository at this point in the history
* empty conditions fix

* dynamic order by

* order by test

* remove commented code
  • Loading branch information
pgajek2 authored May 18, 2023
1 parent 68dc052 commit c88f9ae
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 20 deletions.
66 changes: 48 additions & 18 deletions force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -104,8 +106,8 @@ public inherited sharing class SOQL implements Queryable {
Map<Id, SObject> toMap();
Database.QueryLocator toQueryLocator();

SOQL byId(Id recordId);
SOQL byId(SObject record);
SOQL byId(Id recordId);
SOQL byIds(Set<Id> recordIds);
SOQL byIds(List<Id> recordIds);
SOQL byIds(List<SObject> records);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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<Id> recordIds) {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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() + ')';
Expand Down Expand Up @@ -1038,12 +1062,8 @@ public inherited sharing class SOQL implements Queryable {
private class QOrderBys implements QueryClause {
public List<QOrderBy> orderBys = new List<QOrderBy>();

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() {
Expand All @@ -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;
Expand All @@ -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() {
Expand Down
36 changes: 35 additions & 1 deletion force-app/main/default/classes/SOQLTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -941,7 +975,7 @@ private class SOQLTest {
Assert.areEqual('SELECT Id FROM Case WHERE Id = :v1', soql);

Map<String, Object> binding = builder.binding();
Assert.areEqual(cases[0], binding.get('v1'));
Assert.areEqual(cases[0].Id, binding.get('v1'));
}

@IsTest
Expand Down
14 changes: 13 additions & 1 deletion website/docs/api/soql.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,18 +494,30 @@ QS.of(Lead.SObjectType)

```apex
SOQL orderBy(SObjectField field)
SOQL orderBy(String field)
SOQL orderBy(String field, String direction)
```

**Example**

```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();
```

Expand Down

1 comment on commit c88f9ae

@vercel
Copy link

@vercel vercel bot commented on c88f9ae May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.