diff --git a/force-app/main/default/classes/SOQL.cls b/force-app/main/default/classes/SOQL.cls index df720b0..3cb567b 100644 --- a/force-app/main/default/classes/SOQL.cls +++ b/force-app/main/default/classes/SOQL.cls @@ -250,8 +250,6 @@ public virtual inherited sharing class SOQL implements Queryable { private QueryBuilder builder; private Executor executor; - private String ofObject; - public static SOQL of(SObjectType ofObject) { return new SOQL(ofObject); } @@ -265,10 +263,9 @@ public virtual inherited sharing class SOQL implements Queryable { } public SOQL(String ofObject) { - this.ofObject = ofObject; binder = new Binder(); - executor = new Executor(); builder = new QueryBuilder(ofObject); + executor = new Executor(ofObject, builder, binder); } public SOQL with(SObjectField field) { @@ -557,15 +554,15 @@ public virtual inherited sharing class SOQL implements Queryable { if (!builder.fields.hasCount()) { count(); } - return executor.toInteger(builder.toString(), binder.getBindingMap()); + return executor.toInteger(); } public SObject toObject() { - return executor.toObject(builder.toString(), binder.getBindingMap()); + return executor.toObject(); } public List toList() { - return executor.toList(builder.toString(), binder.getBindingMap()); + return executor.toList(); } public List toAggregated() { @@ -573,80 +570,33 @@ public virtual inherited sharing class SOQL implements Queryable { } public Map toMap() { - Map recordPerId = (Map) Type.forName('Map').newInstance(); - recordPerId.putAll(toList()); - return recordPerId; + return executor.toMap(); } public Map toMap(SObjectField keyField) { with(keyField); - - Map recordPerCustomKey = (Map) Type.forName('Map').newInstance(); - - for (SObject record : toList()) { - recordPerCustomKey.put(String.valueOf(record.get(keyField)), record); - } - - return recordPerCustomKey; + return executor.toMap(keyField); } public Map toMap(SObjectField keyField, SObjectField valueField) { builder.fields.clearAllFields(); // other fields not needed - with(keyField, valueField); - - Map customValuePerCustomKey = new Map(); - - for (SObject record : toList()) { - customValuePerCustomKey.put( - String.valueOf(record.get(keyField)), - String.valueOf(record.get(valueField)) - ); - } - - return customValuePerCustomKey; + return executor.toMap(keyField, valueField); } public Map> toAggregatedMap(SObjectField keyField) { with(keyField); - - Map> recordsPerCustomKey = (Map>) Type.forName('Map>').newInstance(); - - for (SObject record : toList()) { - String key = String.valueOf(record.get(keyField)); - - if (!recordsPerCustomKey.containsKey(key)) { - recordsPerCustomKey.put(key, new List()); - } - - recordsPerCustomKey.get(key).add(record); - } - - return recordsPerCustomKey; + return executor.toAggregatedMap(keyField); } public Map> toAggregatedMap(SObjectField keyField, SObjectField valueField) { builder.fields.clearAllFields(); // other fields not needed - with(keyField, valueField); - - Map> customValuesPerCustomKey = new Map>(); - - for (SObject record : toList()) { - String key = String.valueOf(record.get(keyField)); - - if (!customValuesPerCustomKey.containsKey(key)) { - customValuesPerCustomKey.put(key, new List()); - } - - customValuesPerCustomKey.get(key).add(String.valueOf(record.get(valueField))); - } - - return customValuesPerCustomKey; + return executor.toAggregatedMap(keyField, valueField); } public Database.QueryLocator toQueryLocator() { - return executor.toQueryLocator(builder.toString(), binder.getBindingMap()); + return executor.toQueryLocator(); } public SOQL byId(SObject record) { @@ -1577,6 +1527,15 @@ public virtual inherited sharing class SOQL implements Queryable { private DatabaseQuery sharingExecutor = new InheritedSharing(); private AccessType accessType; private String mockId; + private String ofObject; + private QueryBuilder builder; + private Binder binder; + + public Executor(String ofObject, QueryBuilder builder, Binder binder) { + this.ofObject = ofObject; + this.builder = builder; + this.binder = binder; + } public void withSharing() { sharingExecutor = new WithSharing(); @@ -1598,8 +1557,8 @@ public virtual inherited sharing class SOQL implements Queryable { mockId = id; } - public SObject toObject(String query, Map binding) { - List records = toList(query, binding); + public SObject toObject() { + List records = toList(); if (records.size() > 1) { QueryException e = new QueryException(); @@ -1614,31 +1573,89 @@ public virtual inherited sharing class SOQL implements Queryable { return records[0]; } - public List toList(String query, Map binding) { + public List toList() { if (mock.hasMock(mockId)) { return mock.getSObjectsMock(mockId); } if (accessType == null) { - return sharingExecutor.toSObjects(query, binding, accessMode); + return sharingExecutor.toSObjects(builder.toString(), binder.getBindingMap(), accessMode); } return Security.stripInaccessible( accessType, - sharingExecutor.toSObjects(query, binding, accessMode) + sharingExecutor.toSObjects(builder.toString(), binder.getBindingMap(), accessMode) ).getRecords(); } - public Integer toInteger(String query, Map binding) { + public Map toMap() { + Map recordPerId = (Map) Type.forName('Map').newInstance(); + recordPerId.putAll(toList()); + return recordPerId; + } + + public Map toMap(SObjectField keyField) { + Map recordPerCustomKey = (Map) Type.forName('Map').newInstance(); + + for (SObject record : toList()) { + recordPerCustomKey.put(String.valueOf(record.get(keyField)), record); + } + + return recordPerCustomKey; + } + + public Map toMap(SObjectField keyField, SObjectField valueField) { + Map customValuePerCustomKey = new Map(); + + for (SObject record : toList()) { + customValuePerCustomKey.put(String.valueOf(record.get(keyField)), String.valueOf(record.get(valueField))); + } + + return customValuePerCustomKey; + } + + public Map> toAggregatedMap(SObjectField keyField) { + Map> recordsPerCustomKey = (Map>) Type.forName('Map>').newInstance(); + + for (SObject record : toList()) { + String key = String.valueOf(record.get(keyField)); + + if (!recordsPerCustomKey.containsKey(key)) { + recordsPerCustomKey.put(key, new List()); + } + + recordsPerCustomKey.get(key).add(record); + } + + return recordsPerCustomKey; + } + + public Map> toAggregatedMap(SObjectField keyField, SObjectField valueField) { + Map> customValuesPerCustomKey = new Map>(); + + for (SObject record : toList()) { + String key = String.valueOf(record.get(keyField)); + + if (!customValuesPerCustomKey.containsKey(key)) { + customValuesPerCustomKey.put(key, new List()); + } + + customValuesPerCustomKey.get(key).add(String.valueOf(record.get(valueField))); + } + + return customValuesPerCustomKey; + } + + public Integer toInteger() { if (mock.hasCountMock(mockId)) { return mock.getCountMock(mockId); } - return sharingExecutor.toInteger(query, binding, accessMode); + return sharingExecutor.toInteger(builder.toString(), binder.getBindingMap(), accessMode); } - public Database.QueryLocator toQueryLocator(String query, Map binding) { - return Database.getQueryLocatorWithBinds(query, binding, accessMode); + public Database.QueryLocator toQueryLocator() { + return Database.getQueryLocatorWithBinds(builder.toString(), binder.getBindingMap(), accessMode); } }