diff --git a/force-app/main/default/classes/SOQL.cls b/force-app/main/default/classes/SOQL.cls index 4aec1e6..c5abffb 100644 --- a/force-app/main/default/classes/SOQL.cls +++ b/force-app/main/default/classes/SOQL.cls @@ -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; } @@ -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) { @@ -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 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 records = toList(); @@ -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(); } @@ -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); } } diff --git a/force-app/main/default/classes/SOQL_Test.cls b/force-app/main/default/classes/SOQL_Test.cls index 2ae8426..fea1095 100644 --- a/force-app/main/default/classes/SOQL_Test.cls +++ b/force-app/main/default/classes/SOQL_Test.cls @@ -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() {