As we know, Salesforce does some magic when you do something like:
for (List<Account> accountList: [SELECT Id, Name FROM Account WHERE Name LIKE 'Acme%']) {
// do stuff
}
... such that the code will run more efficiently with higher limits.
and we can do this dynamically like:
for(MyObject__c obj : Database.Query('select Id, Name from MyObject__c'))
{
// do stuff
}
I like to use a the Selector pattern, so I'd rather have the details of the query within my Selector.
So, I'm wondering whether Salesforce would be apply this efficency if I have a method like:
public XAP_DAO_SOQL_SObjectSelectorAbs with(
XAP_DAO_SOQL_QueryFactory queryFactory,
XAP_DAO_SOQL_SObjectConsumerIntf consumer
) {
for (SObject sobj : queryFactory.query()) {
consumer.doWith(sobj);
}
return this;
}
... where queryFactory.query()
returns an instance of List<SObject>
which has been provided by:
public List<SObject> selectFor(XAP_DAO_SOQL_QueryInterpolation queryInterpolation) {
return Database.query(
this.toSoql(queryInterpolation)
);
}
... Or does Salesforce require either [ SELECT ... ]
or Database.Query(' SELECT ... ')
to be exactly after the :
in the for
loop to get the benefit?
for(Sobject sObj: [SELECT ..])
– Brian Kessler Nov 14 '20 at 17:35