Skip to content
Open
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
32 changes: 27 additions & 5 deletions force-app/main/default/classes/cached-soql/CacheManager.cls
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,24 @@ public with sharing class CacheManager {
protected abstract Cache.Partition getPartition(String partitionName);

public Boolean contains(String key) {
return this.isAvailable() ? this.platformCachePartition.contains(key) : this.fallback.contains(key);
if (this.isAvailable()) {
return this.platformCachePartition.contains(key);
}
return this.fallback.contains(key);
}

public Set<String> getKeys() {
return this.isAvailable() ? this.platformCachePartition.getKeys() : this.fallback.getKeys();
if (this.isAvailable()) {
return this.platformCachePartition.getKeys();
}
return this.fallback.getKeys();
}

public Object get(String key) {
return this.isAvailable() ? this.platformCachePartition.get(key) : this.fallback.get(key);
if (this.isAvailable()) {
return this.platformCachePartition.get(key);
}
return this.fallback.get(key);
}

public void remove(String key) {
Expand Down Expand Up @@ -144,7 +153,13 @@ public with sharing class CacheManager {

public override Cache.Partition getPartition(String partitionName) {
try {
return Cache.Org.getPartition(partitionName);
Cache.OrgPartition partition = Cache.Org.getPartition(partitionName);

if (partition.getCapacity() == 0) {
return null;
}

return partition;
} catch (Cache.Org.OrgCacheException e) {
return null;
}
Expand All @@ -161,7 +176,14 @@ public with sharing class CacheManager {
if (!Cache.Session.isAvailable()) {
return null;
}
return Cache.Session.getPartition(partitionName);

Cache.SessionPartition partition = Cache.Session.getPartition(partitionName);

if (partition.getCapacity() == 0) {
return null;
}

return partition;
} catch (Cache.Session.SessionCacheException e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2025 Beyond The Cloud Sp. z o.o. (BeyondTheCloud.Dev)
* Copyright (c) 2026 Beyond The Cloud Sp. z o.o. (BeyondTheCloud.Dev)
* Licensed under the MIT License (https://github.com/beyond-the-cloud-dev/cache-manager/blob/main/LICENSE)
**/
@IsTest
Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/classes/cached-soql/SOQLCache.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2025 Beyond The Cloud Sp. z o.o. (BeyondTheCloud.Dev)
* Licensed under the MIT License (https://github.com/beyond-the-cloud-dev/soql-lib/blob/main/LICENSE)
*
* v6.10.2
* v6.11.0
*
* PMD False Positives:
* - ExcessivePublicCount: It is a library class and exposes all necessary methods to construct a query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) 2025 Beyond The Cloud Sp. z o.o. (BeyondTheCloud.Dev)
* Licensed under the MIT License (https://github.com/beyond-the-cloud-dev/soql-lib/blob/main/LICENSE)
*
* v6.10.2
* v6.11.0
*
* PMD False Positives:
* - CyclomaticComplexity: It is a library and we tried to put everything into ONE test class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2025 Beyond The Cloud Sp. z o.o. (BeyondTheCloud.Dev)
* Licensed under the MIT License (https://github.com/beyond-the-cloud-dev/soql-lib/blob/main/LICENSE)
*
* v6.10.2
* v6.11.0
*
* PMD False Positives:
* - CognitiveComplexity: It is a library and we tried to put everything into ONE class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) 2025 Beyond The Cloud Sp. z o.o. (BeyondTheCloud.Dev)
* Licensed under the MIT License (https://github.com/beyond-the-cloud-dev/soql-lib/blob/main/LICENSE)
*
* v6.10.2
* v6.11.0
*
* PMD False Positives:
* - CyclomaticComplexity: It is a library and we tried to put everything into ONE test class
Expand Down
134 changes: 122 additions & 12 deletions force-app/main/default/classes/standard-soql/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2026 Beyond The Cloud Sp. z o.o. (BeyondTheCloud.Dev)
* Licensed under the MIT License (https://github.com/beyond-the-cloud-dev/soql-lib/blob/main/LICENSE)
*
* v6.10.2
* v6.11.0
*
* PMD False Positives:
* - ExcessivePublicCount: It is a library class and exposes all necessary methods to construct a query
Expand Down Expand Up @@ -135,6 +135,7 @@ public virtual inherited sharing class SOQL implements Queryable {
Queryable orderBy(String field, String direction);
Queryable orderBy(String relationshipName, SObjectField field);
Queryable orderByCount(SObjectField field);
Queryable orderBy(Distance distance);
Queryable sortDesc();
Queryable sort(String direction);
Queryable nullsLast();
Expand Down Expand Up @@ -203,12 +204,14 @@ public virtual inherited sharing class SOQL implements Queryable {
Map<String, List<String>> toAggregatedMap(SObjectField keyField, String relationshipName, SObjectField targetKeyField);

// for internal use
Builder builder();
Map<String, Object> binding();
}

@NamespaceAccessible
public interface SubQuery {
SubQuery of(String ofObject);
SubQuery of(String ofObject, Queryable query);
// SELECT
SubQuery with(SObjectField field);
SubQuery with(SObjectField field1, SObjectField field2);
Expand All @@ -231,6 +234,7 @@ public virtual inherited sharing class SOQL implements Queryable {
SubQuery orderBy(SObjectField field);
SubQuery orderBy(String field);
SubQuery orderBy(String relationshipName, SObjectField field);
SubQuery orderBy(Distance distance);
SubQuery sortDesc();
SubQuery sort(String direction);
SubQuery nullsLast();
Expand Down Expand Up @@ -271,6 +275,7 @@ public virtual inherited sharing class SOQL implements Queryable {
Filter with(SObjectField field);
Filter with(String field);
Filter with(String relationshipName, SObjectField field);
Filter with(Distance distance);
// COMPARATORS
Filter isNull();
Filter isNotNull();
Expand Down Expand Up @@ -395,6 +400,19 @@ public virtual inherited sharing class SOQL implements Queryable {
Map<String, Object> getPopulatedFieldsAsMap();
}

@NamespaceAccessible
public interface Distance {
// field
Distance of(SObjectField ofField);
Distance of(String relationship, SObjectField ofField);
// comparison
Distance between(System.Location location);
Distance between(Decimal latitude, Decimal longitude);
// unit
Distance mi();
Distance km();
}

@NamespaceAccessible
public static SubQuery SubQuery {
get {
Expand Down Expand Up @@ -444,6 +462,22 @@ public virtual inherited sharing class SOQL implements Queryable {
}
}

@NamespaceAccessible
public static Distance Distance {
get {
return new SoqlDistance();
}
}

@NamespaceAccessible
public static Integer getQueries() {
if (System.Test.isRunningTest() && !System.isBatch() && !System.isFuture() && !System.isQueueable() && !System.isScheduled()) {
return SOQL.syncQueriesIssued;
}

return Limits.getQueries();
}

// Mocking

@NamespaceAccessible
Expand Down Expand Up @@ -507,7 +541,7 @@ public virtual inherited sharing class SOQL implements Queryable {
private static Binder binder = new Binder();
private static Integer syncQueriesIssued = 0;

private SoqlBuilder builder;
private Builder builder;
private Executor executor;
private Converter converter;

Expand All @@ -518,7 +552,7 @@ public virtual inherited sharing class SOQL implements Queryable {

@NamespaceAccessible
public SOQL(String ofObject) {
this.builder = new SoqlBuilder(ofObject);
this.builder = new Builder(ofObject);
this.executor = new Executor(builder);
this.converter = new Converter(ofObject);

Expand Down Expand Up @@ -993,6 +1027,12 @@ public virtual inherited sharing class SOQL implements Queryable {
return this.orderBy('COUNT(' + field.toString() + ')');
}

@NamespaceAccessible
public Queryable orderBy(Distance distance) {
this.builder.orderBys.newOrderBy().with(distance);
return this;
}

@NamespaceAccessible
public Queryable sortDesc() {
return this.sort('DESC');
Expand Down Expand Up @@ -1105,6 +1145,10 @@ public virtual inherited sharing class SOQL implements Queryable {
return binder.getBindingMap();
}

public Builder builder() {
return this.builder;
}

@NamespaceAccessible
public Id toId() {
this.builder.fields.clearAllFields(); // other fields not needed
Expand Down Expand Up @@ -1337,10 +1381,10 @@ public virtual inherited sharing class SOQL implements Queryable {
String toString();
}

private class SoqlBuilder implements QueryClause {
private class Builder implements QueryClause {
private List<QueryClause> clauses = new QueryClause[12];

public SoqlBuilder(String ofObject) {
public Builder(String ofObject) {
this.clauses.set(0, new SoqlFields(ofObject));
this.clauses.set(2, new SoqlFrom(ofObject));
}
Expand Down Expand Up @@ -1643,11 +1687,17 @@ public virtual inherited sharing class SOQL implements Queryable {
}

private class SoqlSubQuery implements SubQuery {
private SoqlBuilder builder;
private Builder builder;
private String childRelationshipName;

public SubQuery of(String ofObject) {
this.builder = new SoqlBuilder(ofObject);
this.builder = new Builder(ofObject);
this.childRelationshipName = ofObject;
return this;
}

public SubQuery of(String ofObject, Queryable query) {
this.builder = query.builder();
this.childRelationshipName = ofObject;
return this;
}
Expand Down Expand Up @@ -1746,6 +1796,11 @@ public virtual inherited sharing class SOQL implements Queryable {
return this;
}

public SubQuery orderBy(Distance distance) {
this.builder.orderBys.newOrderBy().with(distance);
return this;
}

public SubQuery sortDesc() {
this.builder.latestOrderBy.sortingOrder('DESC');
return this;
Expand Down Expand Up @@ -2043,6 +2098,11 @@ public virtual inherited sharing class SOQL implements Queryable {
return this;
}

public Filter with(Distance distance) {
this.field = distance.toString();
return this;
}

public Filter isNull() {
return this.equal(null);
}
Expand Down Expand Up @@ -2310,10 +2370,10 @@ public virtual inherited sharing class SOQL implements Queryable {
}

private class SoqlJoinQuery implements InnerJoin {
private SoqlBuilder builder;
private Builder builder;

public InnerJoin of(SObjectType ofObject) {
this.builder = new SoqlBuilder(ofObject.toString());
this.builder = new Builder(ofObject.toString());
return this;
}

Expand Down Expand Up @@ -2635,6 +2695,10 @@ public virtual inherited sharing class SOQL implements Queryable {
this.orderField = field;
}

public void with(Distance distance) {
this.orderField = distance.toString();
}

public void sortingOrder(String direction) {
this.sortingOrder = direction;
}
Expand Down Expand Up @@ -2815,10 +2879,14 @@ public virtual inherited sharing class SOQL implements Queryable {
}

private void addIdToMockedRecords() {
// Id is always added to mirror standard SOQL behavior
// Id is added to mirror standard SOQL behavior
SObjectType sObjectType = this.mockedRecords[0].getSObjectType();
String sObjectPrefix = sObjectType.getDescribe(SObjectDescribeOptions.DEFERRED).getKeyPrefix();

if (String.isBlank(sObjectPrefix)) {
return;
}

for (SObject record : this.mockedRecords) {
record.put('Id', record?.Id ?? IdGenerator.get(sObjectPrefix));
}
Expand Down Expand Up @@ -2927,6 +2995,48 @@ public virtual inherited sharing class SOQL implements Queryable {
}
}

private class SoqlDistance implements Distance {
private String field;
private String unit = 'km';
private String comparisonLocation;

public Distance of(SObjectField ofField) {
this.field = ofField.toString();
return this;
}

public Distance of(String relationship, SObjectField ofField) {
this.field = relationship + '.' + ofField.toString();
return this;
}

public Distance between(System.Location location) {
this.comparisonLocation = 'GEOLOCATION(' + location.getLatitude() + ',' + location.getLongitude() + ')';
return this;
}

public Distance between(Decimal latitude, Decimal longitude) {
return this.between(Location.newInstance(latitude, longitude));
}

public Distance mi() {
return this.unit('mi');
}

public Distance km() {
return this.unit('km');
}

private Distance unit(String unit) {
this.unit = unit;
return this;
}

public override String toString() {
return 'DISTANCE(' + this.field + ', ' + this.comparisonLocation + ',\' ' + this.unit + '\')';
}
}

private class ExceptionMock {
private QueryException queryException;

Expand All @@ -2943,10 +3053,10 @@ public virtual inherited sharing class SOQL implements Queryable {
private DatabaseQuery sharingExecutor;
private AccessLevel accessMode;
private AccessType accessType;
private SoqlBuilder builder;
private Builder builder;
private List<SoqlMock> mocks = new List<SoqlMock>();

public Executor(SoqlBuilder builder) {
public Executor(Builder builder) {
this.builder = builder;
this.accessMode = AccessLevel.USER_MODE;
this.sharingExecutor = new InheritedSharing();
Expand Down
Loading
Loading