Skip to content

Commit 14894b9

Browse files
committed
Code Refactoring
1 parent 880a561 commit 14894b9

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

force-app/main/default/classes/main/cached-soql/SOQLCache.cls

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
8686
}
8787

8888
String ofObject = records[0].getSObjectType().toString();
89+
CacheStorageProxy proxy = new CacheStorageProxy(ofObject);
8990

9091
// Record deletion will trigger an automatic cache refresh when the query is executed.
91-
new CacheStorageProxy(ofObject).apexTransaction().removeRecordsFromCache(records);
92-
new CacheStorageProxy(ofObject).orgCache().removeRecordsFromCache(records);
93-
new CacheStorageProxy(ofObject).sessionCache().removeRecordsFromCache(records);
92+
proxy.apexTransaction().removeRecordsFromCache(records);
93+
proxy.orgCache().removeRecordsFromCache(records);
94+
proxy.sessionCache().removeRecordsFromCache(records);
9495
}
9596

9697
// Backward support
@@ -112,7 +113,7 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
112113
public SOQL.Queryable currentyQuery = null;
113114

114115
protected SOQLCache(SObjectType ofObject) {
115-
this(ofObject + '');
116+
this(ofObject.toString());
116117
}
117118

118119
protected SOQLCache(String ofObject) {
@@ -183,7 +184,7 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
183184
}
184185

185186
public Cacheable whereEqual(SObjectField field, Object value) {
186-
return this.whereEqual(field + '', value);
187+
return this.whereEqual(field.toString(), value);
187188
}
188189

189190
public Cacheable whereEqual(String field, Object value) {
@@ -216,7 +217,7 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
216217
}
217218

218219
public Id toId() {
219-
return this.toObject().Id;
220+
return this.toObject()?.Id;
220221
}
221222

222223
public Boolean doExist() {
@@ -369,8 +370,6 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
369370
}
370371

371372
public void save(List<SObject> databaseRecords) {
372-
List<CacheItem> newCacheRecords = new List<CacheItem>();
373-
374373
if (this.isRecordMissingFromCache()) {
375374
storage.addRecordsToCache(databaseRecords);
376375
} else if (databaseRecords.isEmpty()) { // record does not exist in database anymore
@@ -471,12 +470,12 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
471470
}
472471

473472
public void with(SObjectField field) {
474-
this.cachedFields.add(field + '');
473+
this.cachedFields.add(field.toString());
475474
}
476475

477476
public void with(List<SObjectField> fields) {
478477
for (SObjectField field : fields) {
479-
this.cachedFields.add(field + '');
478+
this.cachedFields.add(field.toString());
480479
}
481480
}
482481

@@ -490,11 +489,12 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
490489
}
491490

492491
public Boolean hasAllRequestedFields(SObject record) {
493-
Boolean hasCachedFields = true;
494492
for (String field : this.cachedFields) {
495-
hasCachedFields &= record.isSet(field);
493+
if (!record.isSet(field)) {
494+
return false;
495+
}
496496
}
497-
return hasCachedFields;
497+
return true;
498498
}
499499
}
500500

@@ -516,7 +516,7 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
516516
}
517517

518518
public Boolean isRecentEnough(CacheItem cachedItem) {
519-
Decimal recordAgeInHoursSinceCached = (System.now().getTime() - cachedItem.cachedDate.getTime()) / (1000 * 60 * 60);
519+
Decimal recordAgeInHoursSinceCached = (System.now().getTime() - cachedItem.cachedDate.getTime()) / 3600000;
520520
return recordAgeInHoursSinceCached <= this.maxRecordAgeInHours;
521521
}
522522
}
@@ -555,12 +555,12 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
555555
public SObject toObject() {
556556
List<SObject> records = this.toList();
557557

558-
if (records.size() > 1) {
559-
throw new QueryException('List has more than 1 row for assignment to SObject');
558+
if (records.isEmpty()) {
559+
return null;
560560
}
561561

562-
if (records.size() == 0) {
563-
return null;
562+
if (records.size() > 1) {
563+
throw new QueryException('List has more than 1 row for assignment to SObject');
564564
}
565565

566566
return records[0];

force-app/main/default/classes/main/standard-soql/SOQL.cls

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -405,20 +405,23 @@ public virtual inherited sharing class SOQL implements Queryable {
405405

406406
@TestVisible // deprecated
407407
private static void setMock(String mockId, SObject record) {
408-
SOQL.queryIdToMock.put(mockId, new List<SoqlMock>{ new SoqlMock().useLegacyMockingBehavior() });
409-
SOQL.queryIdToMock.get(mockId).get(SOQL.queryIdToMock.get(mockId).size() - 1).thenReturn(record);
408+
SOQL.createLegacyMock(mockId).thenReturn(record);
410409
}
411410

412411
@TestVisible // deprecated
413412
private static void setMock(String mockId, List<SObject> records) {
414-
SOQL.queryIdToMock.put(mockId, new List<SoqlMock>{ new SoqlMock().useLegacyMockingBehavior() });
415-
SOQL.queryIdToMock.get(mockId).get(SOQL.queryIdToMock.get(mockId).size() - 1).thenReturn(records);
413+
SOQL.createLegacyMock(mockId).thenReturn(records);
416414
}
417415

418416
@TestVisible // deprecated
419417
private static void setCountMock(String mockId, Integer amount) {
420-
SOQL.queryIdToMock.put(mockId, new List<SoqlMock>{ new SoqlMock().useLegacyMockingBehavior() });
421-
SOQL.queryIdToMock.get(mockId).get(SOQL.queryIdToMock.get(mockId).size() - 1).thenReturn(amount);
418+
SOQL.createLegacyMock(mockId).thenReturn(amount);
419+
}
420+
421+
private static SoqlMock createLegacyMock(String mockId) {
422+
SoqlMock mock = new SoqlMock().useLegacyMockingBehavior();
423+
SOQL.queryIdToMock.put(mockId, new List<SoqlMock>{ mock });
424+
return mock;
422425
}
423426

424427
// Implementation
@@ -944,11 +947,11 @@ public virtual inherited sharing class SOQL implements Queryable {
944947

945948
public Boolean doExist() {
946949
this.builder.fields.clearAllFields(); // other fields not needed
947-
return this.toList().size() > 0;
950+
return this.setLimit(1).toList().size() > 0;
948951
}
949952

950953
public override String toString() {
951-
binder = new Binder(); // clear binding before query build
954+
binder.reset(); // clear binding before query build
952955
return this.builder.toString();
953956
}
954957

@@ -1125,15 +1128,15 @@ public virtual inherited sharing class SOQL implements Queryable {
11251128
}
11261129

11271130
public override String toString() {
1128-
String query = '';
1129-
1131+
List<String> queryParts = new List<String>();
1132+
11301133
for (QueryClause clause : this.clauses) {
11311134
if (clause != null) {
1132-
query += ' ' + clause.toString();
1135+
queryParts.add(clause.toString());
11331136
}
11341137
}
1135-
1136-
return query.trim();
1138+
1139+
return String.join(queryParts, ' ').trim();
11371140
}
11381141
}
11391142

@@ -2329,6 +2332,11 @@ public virtual inherited sharing class SOQL implements Queryable {
23292332
return 'v' + bindIndex;
23302333
}
23312334

2335+
public void reset() {
2336+
this.bindIndex = 0;
2337+
this.binding.clear();
2338+
}
2339+
23322340
public Map<String, Object> getBindingMap() {
23332341
return binding;
23342342
}
@@ -2429,7 +2437,7 @@ public virtual inherited sharing class SOQL implements Queryable {
24292437

24302438
private void stripAdditionalPlainFields(Map<String, Object> recordFilteredFields, SObject record, Set<String> requestedFields) {
24312439
for (String field : requestedFields) {
2432-
recordFilteredFields.put(field, record.get(field) ?? null);
2440+
recordFilteredFields.put(field, record.get(field));
24332441
}
24342442
}
24352443

@@ -2539,12 +2547,12 @@ public virtual inherited sharing class SOQL implements Queryable {
25392547
public SObject toObject() {
25402548
List<SObject> records = toList();
25412549

2542-
if (records.size() > 1) {
2543-
throw new QueryException('List has more than 1 row for assignment to SObject');
2550+
if (records.isEmpty()) {
2551+
return null; // handle: List has no rows for assignment to SObject
25442552
}
25452553

2546-
if (records.size() == 0) {
2547-
return null; // handle: List has no rows for assignment to SObject
2554+
if (records.size() > 1) {
2555+
throw new QueryException('List has more than 1 row for assignment to SObject');
25482556
}
25492557

25502558
return records[0];
@@ -2621,7 +2629,9 @@ public virtual inherited sharing class SOQL implements Queryable {
26212629

26222630
SOQL.syncQueriesIssued++;
26232631

2624-
if (SOQL.syncQueriesIssued > 100) {
2632+
final Integer MAX_QUERIES_ISSUED_IN_SYNCHRONOUS_TRANSACTION = 100;
2633+
2634+
if (SOQL.syncQueriesIssued > MAX_QUERIES_ISSUED_IN_SYNCHRONOUS_TRANSACTION) {
26252635
throw new QueryException('Too many SOQL queries.');
26262636
}
26272637
}

0 commit comments

Comments
 (0)