Skip to content

Commit

Permalink
toMap with custom key (#85)
Browse files Browse the repository at this point in the history
* toMap with custom key

* toMap with custom key and type

* toAggregatedMap

* refactoring

* toMap and toAggregatedMap

* refactoring

* Fix toMap cast

* documentation update
  • Loading branch information
pgajek2 authored Sep 24, 2023
1 parent 031b69d commit 020a518
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
39 changes: 38 additions & 1 deletion force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public virtual inherited sharing class SOQL implements Queryable {
List<SObject> toList();
List<AggregateResult> toAggregated();
Map<Id, SObject> toMap();
Map<String, SObject> toMap(SObjectField keyField);
Map<String, List<SObject>> toAggregatedMap(SObjectField keyField);
Database.QueryLocator toQueryLocator();
}

Expand Down Expand Up @@ -246,6 +248,8 @@ public virtual inherited sharing class SOQL implements Queryable {
private QueryBuilder builder;
private Executor executor;

private String ofObject;

public static SOQL of(SObjectType ofObject) {
return new SOQL(ofObject);
}
Expand All @@ -259,6 +263,7 @@ public virtual inherited sharing class SOQL implements Queryable {
}

public SOQL(String ofObject) {
this.ofObject = ofObject;
binder = new Binder();
executor = new Executor();
builder = new QueryBuilder(ofObject);
Expand Down Expand Up @@ -565,7 +570,39 @@ public virtual inherited sharing class SOQL implements Queryable {
}

public Map<Id, SObject> toMap() {
return new Map<Id, SObject>(toList());
Map<Id, SObject> idToSObject = (Map<Id, SObject>) Type.forName('Map<Id, ' + ofObject + ' >').newInstance();
idToSObject.putAll(toList());
return idToSObject;
}

public Map<String, SObject> toMap(SObjectField keyField) {
with(keyField);

Map<String, SObject> cutomKeyToRecord = (Map<String, SObject>) Type.forName('Map<String, ' + ofObject + ' >').newInstance();

for (SObject record : toList()) {
cutomKeyToRecord.put(record.get(keyField) + '', record);
}

return cutomKeyToRecord;
}

public Map<String, List<SObject>> toAggregatedMap(SObjectField keyField) {
with(keyField);

Map<String, List<SObject>> cutomKeyToRecords = (Map<String, List<SObject>>) Type.forName('Map<String, List<' + ofObject + ' >>').newInstance();

for (SObject record : toList()) {
String customKey = record.get(keyField) + '';

if (!cutomKeyToRecords.containsKey(customKey)) {
cutomKeyToRecords.put(customKey, new List<SObject>());
}

cutomKeyToRecords.get(customKey).add(record);
}

return cutomKeyToRecords;
}

public Database.QueryLocator toQueryLocator() {
Expand Down
47 changes: 46 additions & 1 deletion force-app/main/default/classes/SOQL_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1882,15 +1882,60 @@ private class SOQL_Test {
List<Account> accounts = insertAccounts();

// Test
Map<Id, SObject> result = SOQL.of(Account.SObjectType).toMap();
Map<Id, Account> result = (Map<Id, Account>) SOQL.of(Account.SObjectType).toMap();

// Verify
Assert.areEqual(accounts.size(), result.size());

for (Account acc : accounts) {
Assert.isNotNull(result.get(acc.Id));
}
}

@IsTest
static void toMapWithCustomKey() {
// Setup
List<Account> accounts = insertAccounts();

// Test
Map<String, Account> result = (Map<String, Account>) SOQL.of(Account.SObjectType).toMap(Account.Name);

// Verify
Assert.areEqual(accounts.size(), result.size());

for (Account acc : accounts) {
Assert.isNotNull(result.get(acc.Name));
}
}

@IsTest
static void toAggregatedMapWithCustomKey() {
// Setup
List<Account> accounts = insertAccounts();

// Test
Map<String, List<Account>> result = SOQL.of(Account.SObjectType).toAggregatedMap(Account.Name);

// Verify
Assert.areEqual(accounts.size(), result.size());

for (Account acc : accounts) {
Assert.isNotNull(result.get(acc.Name));
}
}

@IsTest
static void toAggregatedMapWithEmptyCustomKey() {
// Setup
List<Account> accounts = insertAccounts();

// Test
Map<String, List<Account>> result = SOQL.of(Account.SObjectType).toAggregatedMap(Account.Industry);

// Verify
Assert.areEqual(1, result.size()); // grouped by empty Industry
}

@IsTest
static void toQueryLocator() {
// Test
Expand Down
30 changes: 30 additions & 0 deletions website/docs/api/soql.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ The following are methods for `SOQL`.
- [`toList()`](#tolist)
- [`toAggregated()`](#toaggregated)
- [`toMap()`](#tomap)
- [`toMap(SObjectField keyField)`](#tomap-with-custom-key)
- [`toAggregatedMap(SObjectField keyField)`](#toaggregatedmap)
- [`toQueryLocator()`](#toquerylocator)

## INIT
Expand Down Expand Up @@ -1480,6 +1482,34 @@ Map<Id, SObject> toMap()
SOQL.of(Account.SObjectType).toMap();
```

### toMap with custom key

**Signature**

```apex
Map<String, SObject> toMap(SObjectField keyField)
```

**Example**

```apex
Map<String, Account> nameToAccount = (Map<String, Account>) SOQL.of(Account.SObjectType).toMap(Account.Name);
```

### toAggregatedMap

**Signature**

```apex
Map<String, List<SObject>> toAggregatedMap(SObjectField keyField)
```

**Example**

```apex
Map<String, Account> industryToAccounts = SOQL.of(Account.SObjectType).toAggregatedMap(Account.Industry);
```

### toQueryLocator

**Signature**
Expand Down

0 comments on commit 020a518

Please sign in to comment.