diff --git a/force-app/main/default/classes/SOQL.cls b/force-app/main/default/classes/SOQL.cls index 00281d3..7fef693 100644 --- a/force-app/main/default/classes/SOQL.cls +++ b/force-app/main/default/classes/SOQL.cls @@ -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; } @@ -549,11 +548,10 @@ public virtual inherited sharing class SOQL implements Queryable { public Set 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(with(fieldToExtract, 'Id').groupBy(fieldToExtract).toAggregated()).keySet(); - } catch (ListException e) { // Row with null Id at index: 0 - return new Set(); - } + return new Map(with(fieldToExtract, 'Id') + .whereAre(Filter.with(fieldToExtract).isNotNull()) + .groupBy(fieldToExtract).toAggregated()) + .keySet(); } public Integer toInteger() { @@ -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) { @@ -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 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(); @@ -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(); } @@ -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); } } diff --git a/force-app/main/default/classes/SOQL_Test.cls b/force-app/main/default/classes/SOQL_Test.cls index 908ec5c..be981ee 100644 --- a/force-app/main/default/classes/SOQL_Test.cls +++ b/force-app/main/default/classes/SOQL_Test.cls @@ -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() {