Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…selector into release/v3.1.0
  • Loading branch information
pgajek2 committed Oct 31, 2023
2 parents c6a71d5 + ff606d6 commit ee30b14
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
42 changes: 31 additions & 11 deletions force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,7 @@ public virtual inherited sharing class SOQL implements Queryable {
}

public SOQL preview() {
System.debug(LoggingLevel.ERROR, '\n\n============ SOQL Preview ============\n' + toString() + '\n=======================================\n');
System.debug(LoggingLevel.ERROR, '\n\n============ SOQL Binding ============\n' + JSON.serializePretty(binding()) + '\n=======================================\n');
executor.withPreview();
return this;
}

Expand All @@ -549,11 +548,10 @@ public virtual inherited sharing class SOQL implements Queryable {
public Set<String> toValuesOf(SObjectField fieldToExtract) {
// https://salesforce.stackexchange.com/questions/393308/get-a-list-of-one-column-from-a-soql-result
builder.fields.clearAllFields(); // other fields not needed
try {
return new Map<String, SObject>(with(fieldToExtract, 'Id').groupBy(fieldToExtract).toAggregated()).keySet();
} catch (ListException e) { // Row with null Id at index: 0
return new Set<String>();
}
return new Map<String, SObject>(with(fieldToExtract, 'Id')
.whereAre(Filter.with(fieldToExtract).isNotNull())
.groupBy(fieldToExtract).toAggregated())
.keySet();
}

public Integer toInteger() {
Expand Down Expand Up @@ -1537,6 +1535,7 @@ public virtual inherited sharing class SOQL implements Queryable {
private AccessType accessType;
private String mockId;
private String ofObject;
private Boolean preview = false;
private QueryBuilder builder;

public Executor(String ofObject, QueryBuilder builder) {
Expand Down Expand Up @@ -1564,6 +1563,27 @@ public virtual inherited sharing class SOQL implements Queryable {
mockId = id;
}

public void withPreview() {
preview = true;
}

private String buildSOQL() {
if (preview) {
String soql = builder.toString();
System.debug(LoggingLevel.ERROR, '\n\n============ SOQL Preview ============\n' + soql + '\n=======================================\n');
return soql;
}

return builder.toString();
}

private Map<String,Object> buildBinding() {
if (preview) {
System.debug(LoggingLevel.ERROR, '\n\n============ SOQL Binding ============\n' + JSON.serializePretty(binder.getBindingMap()) + '\n=======================================\n');
}
return binder.getBindingMap();
}

public SObject toObject() {
List<SObject> records = toList();

Expand All @@ -1586,12 +1606,12 @@ public virtual inherited sharing class SOQL implements Queryable {
}

if (accessType == null) {
return sharingExecutor.toSObjects(builder.toString(), binder.getBindingMap(), accessMode);
return sharingExecutor.toSObjects(buildSOQL(), buildBinding(), accessMode);
}

return Security.stripInaccessible(
accessType,
sharingExecutor.toSObjects(builder.toString(), binder.getBindingMap(), accessMode)
sharingExecutor.toSObjects(buildSOQL(), buildBinding(), accessMode)
).getRecords();
}

Expand Down Expand Up @@ -1658,11 +1678,11 @@ public virtual inherited sharing class SOQL implements Queryable {
return mock.getCountMock(mockId);
}

return sharingExecutor.toInteger(builder.toString(), binder.getBindingMap(), accessMode);
return sharingExecutor.toInteger(buildSOQL(), buildBinding(), accessMode);
}

public Database.QueryLocator toQueryLocator() {
return Database.getQueryLocatorWithBinds(builder.toString(), binder.getBindingMap(), accessMode);
return Database.getQueryLocatorWithBinds(buildSOQL(), buildBinding(), accessMode);
}
}

Expand Down
13 changes: 13 additions & 0 deletions force-app/main/default/classes/SOQL_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2080,6 +2080,19 @@ private class SOQL_Test {
SOQL.of(Account.SObjectType).preview().toList();
}

@SuppressWarnings('PMD.ApexUnitTestClassShouldHaveAsserts')
@IsTest
static void previewWithConditions() {
// Test
SOQL.of(Account.SObjectType)
.whereAre(SOQL.FilterGroup
.add(SOQL.Filter.with(Account.Name).equal('Test'))
.add(SOQL.Filter.with(Account.Industry).equal('IT'))
)
.preview()
.toList();
}

@SuppressWarnings('PMD.ApexUnitTestClassShouldHaveAsserts')
@IsTest
static void previewCount() {
Expand Down

0 comments on commit ee30b14

Please sign in to comment.