Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v3.1.0 [IN PROGRESS] #88

Merged
merged 11 commits into from
Nov 8, 2023
14 changes: 14 additions & 0 deletions force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public virtual inherited sharing class SOQL implements Queryable {
// DEBUGGING
Queryable preview();
// PREDEFINIED
Queryable byRecordType(String recordTypeDeveloperName);
Queryable byId(SObject record);
Queryable byId(Id recordId);
Queryable byIds(Iterable<Id> recordIds);
Expand Down Expand Up @@ -169,6 +170,8 @@ public virtual inherited sharing class SOQL implements Queryable {
// ORDER
FilterGroup anyConditionMatching();
FilterGroup conditionLogic(String order);
// ADDITIONAL
FilterGroup ignoreWhen(Boolean logicExpression);

Boolean hasValues();
}
Expand Down Expand Up @@ -603,6 +606,10 @@ public virtual inherited sharing class SOQL implements Queryable {
return executor.toQueryLocator();
}

public SOQL byRecordType(String rtDeveloperName) {
return whereAre(Filter.recordType().equal(rtDeveloperName));
}

public SOQL byId(SObject record) {
return byId(record.Id);
}
Expand Down Expand Up @@ -1025,6 +1032,13 @@ public virtual inherited sharing class SOQL implements Queryable {
return this;
}

public FilterGroup ignoreWhen(Boolean logicExpression) {
if (logicExpression) {
queryConditions = new List<FilterClause>();
}
return this;
}

public Boolean hasValues() {
return !queryConditions.isEmpty();
}
Expand Down
43 changes: 42 additions & 1 deletion force-app/main/default/classes/SOQL_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ private class SOQL_Test {
}

@IsTest
static void ignoreWhen() {
static void ignoreWhenFilter() {
// Setup
String accountName = '';

Expand All @@ -1288,6 +1288,34 @@ private class SOQL_Test {
Assert.areEqual('SELECT Id FROM Account WHERE (BillingCity = :v1)', soql);
}

@IsTest
static void ignoreWhenFilterGroup() {
// Setup
Boolean isPartnerUser = false;

// Test
String soql = SOQL.of(Account.SObjectType)
.whereAre(
SOQL.FilterGroup
pgajek2 marked this conversation as resolved.
Show resolved Hide resolved
.add(
SOQL.FilterGroup
.add(SOQL.Filter.with(Account.BillingCity).equal('Krakow'))
.add(SOQL.Filter.with(Account.BillingCity).equal('Warsaw'))
.anyConditionMatching()
.ignoreWhen(!isPartnerUser)
)
.add(
SOQL.FilterGroup
.add(SOQL.Filter.with(Account.Industry).equal('IT'))
.add(SOQL.Filter.name().contains('MyAcccount'))
)
)
.toString();

// Verify
Assert.areEqual('SELECT Id FROM Account WHERE ((Industry = :v1 AND Name LIKE :v2))', soql);
}

@IsTest
static void groupBy() {
// Test
Expand Down Expand Up @@ -1474,6 +1502,19 @@ private class SOQL_Test {
Assert.areEqual('SELECT Id FROM Account ALL ROWS', soql);
}

@IsTest
static void byRecordType() {
// Test
SOQL builder = SOQL.of(Case.SObjectType).byRecordType('CaseRecordType');

// Verify
String soql = builder.toString();
Assert.areEqual('SELECT Id FROM Case WHERE RecordType.DeveloperName = :v1', soql);

Map<String, Object> binding = builder.binding();
Assert.areEqual('CaseRecordType', binding.get('v1'));
}

@IsTest
static void byId() {
// Test
Expand Down
43 changes: 43 additions & 0 deletions website/docs/api/soql-filters-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ The following are methods for `FilterGroup`.
- [`anyConditionMatching()`](#anyconditionmatching)
- [`conditionLogic(String order)`](#conditionlogic)

[**ADDITIONAL**](#additional)

- [`ignoreWhen(Boolean logicExpression)`](#ignorewhen)

## ADD CONDITION
### add

Expand Down Expand Up @@ -135,3 +139,42 @@ SOQL.of(Account.SObjectType)
.anyConditionMatching()
).toList();
```

## ADDITIONAL

### ignoreWhen

All group's conditions will be removed when logic expression will evaluate to true.

**Signature**

```apex
FilterGroup ignoreWhen(Boolean logicExpression);
```

**Example**

```sql
SELECT Id
FROM Account
WHERE Industry = 'IT' AND Name LIKE '%MyAccount%'
```

```apex
Boolean isPartnerUser = false;

SOQL.of(Account.SObjectType)
.whereAre(SOQL.FilterGroup
.add(SOQL.FilterGroup
.add(SOQL.Filter.with(Account.BillingCity).equal('Krakow'))
.add(SOQL.Filter.with(Account.BillingCity).equal('Warsaw'))
.anyConditionMatching()
.ignoreWhen(!isPartnerUser)
)
.add(SOQL.FilterGroup
.add(SOQL.Filter.with(Account.Industry).equal('IT'))
.add(SOQL.Filter.name().contains('MyAcccount'))
)
)
.toList();
```
22 changes: 22 additions & 0 deletions website/docs/api/soql.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ The following are methods for `SOQL`.

[**PREDEFINIED**](#predefinied)

- [`byRecordType(String recordTypeDeveloperName)`](#byrecordtype)
- [`byId(SObject record)`](#byid)
- [`byId(Id recordId)`](#byid)
- [`byIds(Iterable<Id> recordIds)`](#byids)
Expand Down Expand Up @@ -1274,6 +1275,27 @@ WHERE ((Id = :v1 OR Name LIKE :v2))
For all predefined methods SOQL instance is returned so you can still adjust query before execution.
Add additional fields with [`.with`](#select).

### byRecordType

**Signature**

```apex
SOQL byRecordType(String recordTypeDeveloperName)
```

**Example**

```sql
SELECT Id
FROM Case
WHERE RecordType.DeveloperName = 'CaseRecordType'
```
```apex
SOQL.of(Case.SObjectType)
.byRecordType('CaseRecordType')
.toList();
```

### byId

**Signature**
Expand Down