Skip to content

Commit

Permalink
selector interface (#25)
Browse files Browse the repository at this point in the history
* selector interface

* update documentation
  • Loading branch information
pgajek2 authored May 15, 2023
1 parent dc094e2 commit 22cd9d4
Show file tree
Hide file tree
Showing 18 changed files with 161 additions and 192 deletions.
4 changes: 4 additions & 0 deletions force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public inherited sharing class SOQL implements Queryable {
}
}

public interface Selector {
SOQL query();
}

public interface Queryable {
SOQL of(SObjectType ofObject);

Expand Down
22 changes: 10 additions & 12 deletions force-app/main/default/classes/selector/AccountSelector.cls
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
public with sharing class AccountSelector {
public static SOQL query {
get {
return SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{
Account.Name,
Account.AccountNumber
})
.systemMode()
.withoutSharing();
}
public with sharing class AccountSelector implements SOQL.Selector {
public static SOQL query() {
return SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{
Account.Name,
Account.AccountNumber
})
.systemMode()
.withoutSharing();
}

public static SOQL byRecordType(String rt) {
return query.with(new List<SObjectField>{
return query().with(new List<SObjectField>{
Account.BillingCity,
Account.BillingCountry
}).whereAre(SOQL.Filter.recordType().equal(rt));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public with sharing class ExampleController {

public static List<Account> getPartnerAccounts(String accountName) {
return AccountSelector.query
return AccountSelector.query()
.with(Account.BillingCity)
.with(Account.BillingCountry)
.whereAre(SOQL.FilterGroup
Expand Down
31 changes: 14 additions & 17 deletions website/docs/basic-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Mocked queries won't make any SOQL's and simply return data set in method defini
public with sharing class ExampleController {
public static List<Account> getPartnerAccounts(String accountName) {
return AccountSelector.query
return AccountSelector.query()
.with(Account.BillingCity)
.with(Account.BillingCountry)
.whereAre(SOQL.FilterGroup
Expand Down Expand Up @@ -220,20 +220,19 @@ private class ExampleControllerTest {
Generic SOQLs can be keep in selector class.

```apex
public inherited sharing class AccountSelector {
public inherited sharing class AccountSelector implements SOQL.Selector {
public static SOQL query {
get {
return SOQL.of(Account.SObjectType).with(new List<SObjectField>{
public static SOQL query() {
return SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{
Account.Name,
Account.AccountNumber
})
.systemMode()
.withoutSharing();
}
}
public static SOQL getByRecordType(String rtDevName) {
public static SOQL byRecordType(String rtDevName) {
return query.with(new List<SObjectField>{
Account.BillingCity,
Account.BillingCountry
Expand All @@ -247,16 +246,14 @@ public inherited sharing class AccountSelector {
The selector class can provide default SOQL configuration like default fields, FLS settings, and sharing rules.

```apex
public inherited sharing class AccountSelector {
public static SOQL query {
get {
return SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{ // default fields
Account.Id,
Account.Name
}).systemMode(); // default FLS mode
}
public inherited sharing class AccountSelector implements SOQL.Selector {
public static SOQL query() {
return SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{ // default fields
Account.Id,
Account.Name
}).systemMode(); // default FLS mode
}
}
```
20 changes: 9 additions & 11 deletions website/docs/examples/debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@ sidebar_position: 14
See query String in debug logs.

```apex
public inherited sharing class AccountSelector {
public static SOQL query {
get {
return SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{
Account.Id,
Account.Name
});
}
public inherited sharing class AccountSelector implements SOQL.Selector {
public static SOQL query() {
return SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{
Account.Id,
Account.Name
});
}
}
public with sharing class MyController {
public static List<Account> getAccounts() {
return AccountSelector.query
return AccountSelector.query()
.with(new List<SObjectField>{
Account.BillingCity,
Account.BillingCountry,
Expand Down
20 changes: 9 additions & 11 deletions website/docs/examples/fls.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,22 @@ Enforce or bypass FLS.
`USER_MODE` is a default option. You can set `SYSTEM_MODE` for all queries by adding `.systemMode()` to selector class.

```apex
public inherited sharing class AccountSelector {
public inherited sharing class AccountSelector implements SOQL.Selector {
public static SOQL query {
get {
return SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{
Account.Id,
Account.Name
})
.systemMode(); //default FLS mode
}
public static SOQL query() {
return SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{
Account.Id,
Account.Name
})
.systemMode(); //default FLS mode
}
}
public with sharing class MyController {
public static List<Account> getAccountInSystemMode() {
return AccountSelector.query
return AccountSelector.query()
.userMode() //override selector FLS mode
.toList();
}
Expand Down
10 changes: 4 additions & 6 deletions website/docs/examples/group-by.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@ FROM Lead
GROUP BY LeadSource
```
```apex
public inherited sharing class LeadSelector {
public inherited sharing class LeadSelector implements SOQL.Selector {
public static SOQL query {
get {
return SOQL.of(Lead.SObjectType);
}
public static SOQL query() {
return SOQL.of(Lead.SObjectType);
}
}
public with sharing class MyController {
public static List<AggregateResult> getGroupedLeads() {
return LeadSelector.query
return LeadSelector.query()
.with(Lead.LeadSource)
.groupBy(Lead.LeadSource)
.toAggregated();
Expand Down
18 changes: 8 additions & 10 deletions website/docs/examples/limit.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@ FROM Account
LIMIT 1000
```
```apex
public inherited sharing class AccountSelector {
public inherited sharing class AccountSelector implements SOQL.Selector {
public static SQOL query {
get {
return SQOL.of(Account.SObjectType)
.with(new List<SObjectField>{
Account.Id,
Account.Name
});
}
public static SQOL query() {
return SQOL.of(Account.SObjectType)
.with(new List<SObjectField>{
Account.Id,
Account.Name
});
}
}
public with sharing class MyController {
public static List<Account> getAccountsWithLimit() {
return AccountSelector.query
return AccountSelector.query()
.setLimit(1000)
.toList();
}
Expand Down
8 changes: 4 additions & 4 deletions website/docs/examples/mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Set mocking ID in Query declaration.
public with sharing class ExampleController {
public static List<Account> getAccountByName(String accountName) {
return AccountSelector.query
return AccountSelector.query()
.with(Account.BillingCity)
.with(Account.BillingCountry)
.whereAre(SOQL.FilterGroup
Expand Down Expand Up @@ -56,7 +56,7 @@ Set mocking ID in Query declaration.
public with sharing class ExampleController {
public static List<Account> getPartnerAccounts(String accountName) {
return AccountSelector.query
return AccountSelector.query()
.with(Account.BillingCity)
.with(Account.BillingCountry)
.whereAre(SOQL.FilterGroup
Expand Down Expand Up @@ -102,7 +102,7 @@ Set mocking ID in Query declaration.
public with sharing class ExampleController {
public static List<Account> getPartnerAccountsCount(String accountName) {
return AccountSelector.query
return AccountSelector.query()
.whereAre(SOQL.FilterGroup
.add(SOQL.Filter.with(Account.Name).contains(accountName))
.add(SOQL.Filter.recordType().equal('Partner'))
Expand Down Expand Up @@ -143,7 +143,7 @@ Set mocking ID in Query declaration.
public with sharing class ExampleController {
public static List<Account> getPartnerAccounts(String accountName) {
return AccountSelector.query
return AccountSelector.query()
.with(Account.BillingCity)
.with(Account.BillingCountry)
.whereAre(SOQL.FilterGroup
Expand Down
18 changes: 8 additions & 10 deletions website/docs/examples/offset.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@ FROM Account
OFFSET 1000
```
```apex
public inherited sharing class AccountSelector {
public inherited sharing class AccountSelector implements SOQL.Selector {
public static SOQL query {
get {
return SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{
Account.Id,
Account.Name
});
}
public static SOQL query() {
return SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{
Account.Id,
Account.Name
});
}
}
public with sharing class MyController {
public static List<Account> getAccountsWithOffset() {
return AccountSelector.query
return AccountSelector.query()
.offset(1000)
.toList();
}
Expand Down
20 changes: 9 additions & 11 deletions website/docs/examples/order-by.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,22 @@ FROM Account
ORDER BY Industry DESC NULLS FIRST, Id ASC NULLS FIRST
```
```apex
public inherited sharing class AccountSelector {
public inherited sharing class AccountSelector implements SOQL.Selector {
public static SOQL query {
get {
return SOQL.of(Lead.SObjectType)
.with(new List<SObjectField>{
Account.Id,
Account.Name,
Account.Industry
});
}
public static SOQL query() {
return SOQL.of(Lead.SObjectType)
.with(new List<SObjectField>{
Account.Id,
Account.Name,
Account.Industry
});
}
}
public with sharing class MyController {
public static List<Account> getAccounts() {
return AccountSelector.query
return AccountSelector.query()
.orderBy(Account.Industry).sortDesc()
.orderBy(Account.Id)
.toList();
Expand Down
26 changes: 12 additions & 14 deletions website/docs/examples/result.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,50 @@ sidebar_position: 15
Execut SOQL and get results.

```apex
public inherited sharing class AccountSelector {
public inherited sharing class AccountSelector implements SOQL.Selector {
public static SOQL query {
get {
return SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{
Account.Id,
Account.Name
});
}
return SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{
Account.Id,
Account.Name
});
}
}
public with sharing class MyController {
public static Account getAccountById(Id accountId) {
return (Account) AccountSelector.query
return (Account) AccountSelector.query()
.whereAre(SOQL.Filter.id().equal(accountId))
.toObject();
}
public static List<Account> getAccountsByIds(List<Id> accountIds) {
return AccountSelector.query
return AccountSelector.query()
.whereAre(SOQL.Filter.id().isIn(accountIds))
.toList();
}
public static List<AggregateResult> getUniqueAccountNameAmount() {
return AccountSelector.query
return AccountSelector.query()
.count(Account.Name, 'names')
.toAggregated();
}
public static Integer countAccounts() {
return AccountSelector.query
return AccountSelector.query()
.count()
.toInteger();
}
public static Map<Id, SObject> getAccountMap() {
return AccountSelector.query
return AccountSelector.query()
.toMap();
}
public static Database.QueryLocator getAccountQueryLocator() {
return AccountSelector.query
return AccountSelector.query()
.toQueryLocator();
}
}
Expand Down
Loading

1 comment on commit 22cd9d4

@vercel
Copy link

@vercel vercel bot commented on 22cd9d4 May 15, 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.