Skip to content

Commit

Permalink
Preview Fix (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgajek2 authored Oct 31, 2023
1 parent e854cb5 commit ff606d6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
33 changes: 27 additions & 6 deletions force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,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 Down Expand Up @@ -1527,6 +1526,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 @@ -1554,6 +1554,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 @@ -1576,12 +1597,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 @@ -1648,11 +1669,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 @@ -2052,6 +2052,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

1 comment on commit ff606d6

@vercel
Copy link

@vercel vercel bot commented on ff606d6 Oct 31, 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.