Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 38 additions & 39 deletions force-app/main/default/classes/cached-soql/SOQLCache.cls
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ global virtual inherited sharing class SOQLCache implements Cacheable {
Object toValueOf(SObjectField fieldToExtract);
}

protected virtual SOQL.Queryable initialQuery() {
global virtual SOQL.Queryable initialQuery() {
return null;
}

protected virtual List<SObjectField> additionalAllowedConditionFields() {
global virtual List<SObjectField> additionalAllowedConditionFields() {
return new List<SObjectField>();
}

Expand All @@ -86,8 +86,7 @@ global virtual inherited sharing class SOQLCache implements Cacheable {
void thenReturn(SObject record);
}

@TestVisible
private static Mockable mock(String mockId) {
global static Mockable mock(String mockId) {
if (!SOQLCache.queryIdToMock.containsKey(mockId)) {
SOQLCache.queryIdToMock.put(mockId, new List<SoqlMock>());
}
Expand Down Expand Up @@ -127,11 +126,11 @@ global virtual inherited sharing class SOQLCache implements Cacheable {
private SOQL.Queryable initialQuery = null;
private SOQL.Queryable currentQuery = null;

protected SOQLCache(SObjectType ofObject) {
global SOQLCache(SObjectType ofObject) {
this(ofObject.toString());
}

protected SOQLCache(String ofObject) {
global SOQLCache(String ofObject) {
this.initialQuery = this.initialQuery()?.systemMode()?.withoutSharing();
this.currentQuery = SOQL.of(ofObject).systemMode().withoutSharing();

Expand All @@ -142,153 +141,153 @@ global virtual inherited sharing class SOQLCache implements Cacheable {
this.with('Id');
}

public Cacheable cacheInApexTransaction() {
global Cacheable cacheInApexTransaction() {
this.cache.storage.apexTransaction();
return this;
}

public Cacheable cacheInOrgCache() {
global Cacheable cacheInOrgCache() {
this.cache.storage.orgCache();
return this;
}

public Cacheable cacheInSessionCache() {
global Cacheable cacheInSessionCache() {
this.cache.storage.sessionCache();
return this;
}

public Cacheable maxHoursWithoutRefresh(Integer hours) {
global Cacheable maxHoursWithoutRefresh(Integer hours) {
this.cache.expiration.maxRecordAgeInHours(hours);
return this;
}

public Cacheable allowFilteringByNonUniqueFields() {
global Cacheable allowFilteringByNonUniqueFields() {
this.cache.filterGroup.allowFilteringByNonUniqueFields();
return this;
}

public Cacheable allowQueryWithoutConditions() {
global Cacheable allowQueryWithoutConditions() {
this.cache.filterGroup.allowQueryWithoutConditions();
return this;
}

public Cacheable with(SObjectField field) {
global Cacheable with(SObjectField field) {
this.initialQuery?.with(field);
this.currentQuery.with(field);
this.cache.fields.with(field);
return this;
}

public Cacheable with(SObjectField field1, SObjectField field2) {
global Cacheable with(SObjectField field1, SObjectField field2) {
return this.with(field1).with(field2);
}

public Cacheable with(SObjectField field1, SObjectField field2, SObjectField field3) {
global Cacheable with(SObjectField field1, SObjectField field2, SObjectField field3) {
return this.with(field1, field2).with(field3);
}

public Cacheable with(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4) {
global Cacheable with(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4) {
return this.with(field1, field2, field3).with(field4);
}

public Cacheable with(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4, SObjectField field5) {
global Cacheable with(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4, SObjectField field5) {
return this.with(field1, field2, field3, field4).with(field5);
}

public Cacheable with(List<SObjectField> fields) {
global Cacheable with(List<SObjectField> fields) {
this.initialQuery?.with(fields);
this.currentQuery.with(fields);
this.cache.fields.with(fields);
return this;
}

public Cacheable with(String fields) {
global Cacheable with(String fields) {
this.initialQuery?.with(fields);
this.currentQuery.with(fields);
this.cache.fields.with(fields);
return this;
}

public Cacheable with(String relationshipName, SObjectField field) {
global Cacheable with(String relationshipName, SObjectField field) {
this.initialQuery?.with(relationshipName, field);
this.currentQuery.with(relationshipName, field);
this.cache.fields.with(relationshipName, field);
return this;
}

public Cacheable with(String relationshipName, SObjectField field1, SObjectField field2) {
global Cacheable with(String relationshipName, SObjectField field1, SObjectField field2) {
return this.with(relationshipName, new List<SObjectField>{ field1, field2 });
}

public Cacheable with(String relationshipName, SObjectField field1, SObjectField field2, SObjectField field3) {
global Cacheable with(String relationshipName, SObjectField field1, SObjectField field2, SObjectField field3) {
return this.with(relationshipName, new List<SObjectField>{ field1, field2, field3 });
}

public Cacheable with(String relationshipName, SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4) {
global Cacheable with(String relationshipName, SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4) {
return this.with(relationshipName, new List<SObjectField>{ field1, field2, field3, field4 });
}

public Cacheable with(String relationshipName, SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4, SObjectField field5) {
global Cacheable with(String relationshipName, SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4, SObjectField field5) {
return this.with(relationshipName, new List<SObjectField>{ field1, field2, field3, field4, field5 });
}

public Cacheable with(String relationshipName, Iterable<SObjectField> fields) {
global Cacheable with(String relationshipName, Iterable<SObjectField> fields) {
this.initialQuery?.with(relationshipName, fields);
this.currentQuery.with(relationshipName, fields);
this.cache.fields.with(relationshipName, fields);
return this;
}

public Cacheable whereEqual(SObjectField field, Object value) {
global Cacheable whereEqual(SObjectField field, Object value) {
return this.whereEqual(field.toString(), value);
}

public Cacheable whereEqual(String field, Object value) {
global Cacheable whereEqual(String field, Object value) {
this.with(field);
this.cache.filterGroup.add(new CacheFilter().with(field).equal(value));
this.currentQuery.whereAre(SOQL.Filter.with(field).equal(value));
return this;
}

public Cacheable stripInaccessible() {
global Cacheable stripInaccessible() {
return this.stripInaccessible(AccessType.READABLE);
}

public Cacheable stripInaccessible(AccessType accessType) {
global Cacheable stripInaccessible(AccessType accessType) {
this.currentQuery.stripInaccessible(accessType);
this.executor.stripInaccessible(accessType);
return this;
}

public Cacheable mockId(String queryIdentifier) {
global Cacheable mockId(String queryIdentifier) {
this.currentQuery.mockId(queryIdentifier);
this.executor.mock(queryIdToMock.get(queryIdentifier));
return this;
}

public Cacheable preview() {
global Cacheable preview() {
this.currentQuery.preview();
return this;
}

public Id toId() {
global Id toId() {
return this.toObject()?.Id;
}

public Id toIdOf(SObjectField field) {
global Id toIdOf(SObjectField field) {
return (Id) this.toObject()?.get(field);
}

public Boolean doExist() {
global Boolean doExist() {
return this.toObject() != null;
}

public Object toValueOf(SObjectField fieldToExtract) {
global Object toValueOf(SObjectField fieldToExtract) {
this.with(fieldToExtract);
return this.toObject()?.get(fieldToExtract);
}

public SObject toObject() {
global SObject toObject() {
this.cache.filterGroup.validateIfQueryHasAtLeastOneCondition();
this.executeInitialQuery();
return this.executor.toObject();
Expand All @@ -301,11 +300,11 @@ global virtual inherited sharing class SOQLCache implements Cacheable {
this.cache.storage.putInitialRecordsToCache(this.initialQuery.toList());
}

public Cacheable byId(SObject record) {
global Cacheable byId(SObject record) {
return byId(record.Id);
}

public Cacheable byId(Id recordId) {
global Cacheable byId(Id recordId) {
whereEqual('Id', recordId);
return this;
}
Expand Down Expand Up @@ -761,4 +760,4 @@ global virtual inherited sharing class SOQLCache implements Cacheable {
}

public class SoqlCacheException extends Exception {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ global inherited sharing class SOQLEvaluator {
Mockable thenReturn(List<SObject> records);
}

@TestVisible
private static Mockable mock(String mockId) {
global static Mockable mock(String mockId) {
if (!SOQLEvaluator.queryIdToMock.containsKey(mockId)) {
SOQLEvaluator.queryIdToMock.put(mockId, new List<SoqlMock>());
}
Expand Down
Loading