-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoql.cls
More file actions
2250 lines (2046 loc) · 72.7 KB
/
Soql.cls
File metadata and controls
2250 lines (2046 loc) · 72.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @description Handles all SOQL query operations with the Salesforce database. Provides a mockable, fluent query builder interface with comprehensive operator support and plugin capabilities.
*/
@SuppressWarnings(
'PMD.AvoidGlobalModifier,PMD.CognitiveComplexity,PMD.CyclomaticComplexity,PMD.ExcessiveClassLength,PMD.ExcessivePublicCount'
)
global inherited sharing virtual class Soql extends Soql.Builder {
global static final Operator EQUALS = new Soql.Operator('=');
global static final Operator NOT_EQUALS = new Soql.Operator('!=');
global static final Operator IN_COLLECTION = new Soql.Operator('IN');
global static final Operator NOT_IN_COLLECTION = new Soql.Operator('NOT IN');
global static final Operator GREATER_THAN = new Soql.Operator('>');
global static final Operator GREATER_OR_EQUAL = new Soql.Operator('>=');
global static final Operator LESS_THAN = new Soql.Operator('<');
global static final Operator LESS_OR_EQUAL = new Soql.Operator('<=');
global static final Operator STARTS_WITH = new Soql.StartsWithOperator('LIKE');
global static final Operator NOT_STARTS_WITH = new Soql.StartsWithOperator('NOT LIKE');
global static final Operator ENDS_WITH = new Soql.EndsWithOperator('LIKE');
global static final Operator NOT_ENDS_WITH = new Soql.EndsWithOperator('NOT LIKE');
global static final Operator CONTAINS = new Soql.ContainsOperator('LIKE');
global static final Operator NOT_CONTAINS = new Soql.ContainsOperator('NOT LIKE');
// Other constants
private static final Soql.LogicType DEFAULT_LOGIC_TYPE = Soql.LogicType.ALL_CONDITIONS;
private static final String ID_REFERENCE = 'Id';
// Member Properties:
/**
* @description Unique identifier for this query instance that can be referenced in mocks.
*/
global String identifier { get; private set; }
/**
* @description Constructor for Soql instance with DatabaseLayer factory.
* @param factory The DatabaseLayer factory instance
*/
public Soql(DatabaseLayer factory) {
this();
}
/**
* @description Protected constructor for Soql instance initialization.
*/
protected Soql() {
super();
}
/**
* @description Executes an aggregate query and returns mockable AggregateResult wrappers.
* @return List of Soql.AggregateResult objects wrapping Schema.AggregateResult
*/
global List<Soql.AggregateResult> aggregateQuery() {
return (List<Soql.AggregateResult>) this.initRequest(Soql.Operation.AGGREGATE_QUERY)?.process();
}
/**
* @description Executes a COUNT() query and returns the number of matching records.
* @return Integer count of matching records
*/
global Integer countQuery() {
return (Integer) this.initRequest(Soql.Operation.COUNT_QUERY)?.process();
}
/**
* @description Returns query results as a mockable Cursor wrapper.
* @return Soql.Cursor object wrapping Database.Cursor
*/
global Soql.Cursor getCursor() {
return (Soql.Cursor) this.initRequest(Soql.Operation.GET_CURSOR)?.process();
}
/**
* @description Returns query results as a mockable QueryLocator wrapper for batch processing.
* @return Soql.QueryLocator object wrapping Database.QueryLocator
*/
global Soql.QueryLocator getQueryLocator() {
return (Soql.QueryLocator) this.initRequest(Soql.Operation.GET_QUERY_LOCATOR)?.process();
}
/**
* @description Executes the SOQL query and returns matching records.
* @return List of SObject records matching the query criteria
*/
global List<SObject> query() {
return (List<SObject>) this.initRequest(Soql.Operation.QUERY)?.process();
}
/**
* @description Executes the query and deserializes results to the specified type.
* @param returnType The type to deserialize query results into
* @return Query results deserialized as the specified type
*/
global Object query(Type returnType) {
Object result = this.initRequest(Soql.Operation.QUERY)?.process();
return JSON.deserialize(JSON.serialize(result), returnType);
}
/**
* @description Executes the query and returns only the first matching record.
* @return First SObject matching the query criteria, or null if no matches
*/
global SObject queryFirst() {
Integer originalLimit = this.rowLimit;
this.setRowLimit(1);
List<SObject> results = this.query();
this.setRowLimit(originalLimit);
return results?.isEmpty() == false ? results?.get(0) : null;
}
/**
* @description Sets an identifier for this query that can be referenced in mocks.
* @param identifier Unique identifier for this query instance
* @return This Soql instance for method chaining
*/
global Soql setQueryIdentifier(String identifier) {
this.identifier = identifier;
return this;
}
/**
* @description Initializes a SOQL request object for the specified operation.
* @param operation The SOQL operation to perform
* @return A configured Soql.Request object
*/
protected virtual Soql.Request initRequest(Soql.Operation operation) {
return new Soql.Request(this, operation);
}
// **** ENUMS *** //
/**
* @description SOQL aggregate and date functions available for use in SELECT clauses.
*/
global enum Function {
AVG,
CALENDAR_MONTH,
CALENDAR_QUARTER,
CALENDAR_YEAR,
COUNT,
COUNT_DISTINCT,
DAY_IN_MONTH,
DAY_IN_WEEK,
DAY_IN_YEAR,
DAY_ONLY,
FISCAL_MONTH,
FISCAL_QUARTER,
FISCAL_YEAR,
FORMAT,
HOUR_IN_DAY,
MIN,
MAX,
SUM,
WEEK_IN_MONTH,
WEEK_IN_YEAR
}
/**
* @description Logical operators for combining criteria in WHERE clauses.
*/
global enum LogicType {
ALL_CONDITIONS,
ANY_CONDITIONS
}
/**
* @description Null ordering options for ORDER BY clauses.
*/
global enum NullOrder {
NULLS_FIRST,
NULLS_LAST
}
/**
* @description Types of SOQL operations that can be performed.
*/
global enum Operation {
AGGREGATE_QUERY,
COUNT_QUERY,
GET_CURSOR,
GET_QUERY_LOCATOR,
QUERY
}
/**
* @description Row locking scopes for FOR UPDATE clauses.
*/
global enum Scope {
// https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_using_scope.htm
DELEGATED,
EVERYTHING,
MINE,
MINE_AND_MY_GROUPS,
MY_TERRITORY,
MY_TEAM_TERRITORY,
TEAM
}
/**
* @description Sort directions for ORDER BY clauses.
*/
global enum SortDirection {
ASCENDING,
DESCENDING
}
/**
* @description Usage types for FOR VIEW and FOR UPDATE clauses.
*/
global enum Usage {
// https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_for_view.htm
// https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_for_reference.htm
// https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_for_update.htm
ALL_ROWS,
FOR_VIEW,
FOR_REFERENCE,
FOR_UPDATE
}
// **** INTERFACE **** //
/**
* @description Marker interface for criteria that can be used in WHERE/HAVING clauses.
*/
global interface Criteria {
/**
* @description Returns the string representation of this criteria.
* @return String representation for use in SOQL clauses
*/
String toString();
}
/**
* @description Interface for plugins that can process SOQL operations before/after execution.
*/
global interface PreAndPostProcessor {
/**
* @description Processes SOQL requests before execution.
* @param request The SOQL request being processed
*/
void processPreSoql(Soql.Request request);
/**
* @description Processes SOQL requests after execution.
* @param request The SOQL request that was processed
* @param results The results from the SOQL operation
*/
void processPostSoql(Soql.Request request, Object results);
/**
* @description Processes SOQL errors.
* @param request The SOQL request that failed
* @param error The exception that occurred
*/
void processSoqlError(Soql.Request request, Exception error);
}
/**
* @description Marker interface for objects that can be used in SELECT clauses.
*/
global interface Selectable {
/**
* @description Returns the string representation of this selectable.
* @return String representation for use in SELECT clauses
*/
String toString();
}
// **** INNER **** //
/**
* @description Mockable wrapper for Schema.AggregateResult that enables testing of aggregate queries.
*/
global virtual class AggregateResult {
protected Map<String, Object> params;
/**
* @description Constructor that wraps a Schema.AggregateResult.
* @param result The Schema.AggregateResult to wrap
*/
protected AggregateResult(Schema.AggregateResult result) {
this.params = result?.getPopulatedFieldsAsMap() ?? new Map<String, Object>();
}
/**
* @description Retrieves the value for the specified field or alias.
* @param key The field name or alias to retrieve
* @return The value associated with the key
*/
global Object get(String key) {
return this.params?.get(key);
}
}
/**
* @description Represents an aggregate function in a SOQL query (COUNT, SUM, AVG, etc.).
*/
global class Aggregation implements Soql.Selectable {
private String alias;
private String innerFieldName;
private Function function;
/**
* @description Constructor for aggregation with function and field name.
* @param function The aggregation function to apply
* @param innerFieldName The field name to aggregate
*/
global Aggregation(Soql.Function function, String innerFieldName) {
this.alias = '';
this.innerFieldName = innerFieldName;
this.function = function;
}
/**
* @description Constructor for aggregation with function and SObjectField.
* @param function The aggregation function to apply
* @param field The SObjectField to aggregate
*/
global Aggregation(Soql.Function function, SObjectField field) {
this(function, field?.toString());
}
/**
* @description Constructor for aggregation with function and parent field.
* @param function The aggregation function to apply
* @param field The parent field to aggregate
*/
global Aggregation(Soql.Function function, Soql.ParentField field) {
this(function, field?.toString());
}
/**
* @description Constructor for aggregation with only function.
* @param function The aggregation function to apply
*/
global Aggregation(Soql.Function function) {
this(function, '');
}
/**
* @description Returns the aggregation as a formatted SOQL string.
* @return The aggregation formatted for SOQL queries
*/
global override String toString() {
String formatted = String.format(
'{0}({1}) {2}',
new List<String>{ this.function?.toString(), this.innerFieldName, this.alias }
);
return formatted?.trim();
}
/**
* @description Sets an alias for the aggregation result.
* @param alias The alias to use for this aggregation
* @return This Aggregation instance for method chaining
*/
global Soql.Aggregation withAlias(String alias) {
this.alias = alias;
return this;
}
}
/**
* @description Represents a bind variable for parameterized SOQL queries.
*/
global class Binder {
/**
* @description The bind variable key.
*/
global String key { get; private set; }
/**
* @description The bind variable value.
*/
global Object value { get; private set; }
/**
* @description Constructor for bind variable with key and value.
* @param key The bind variable key
* @param value The bind variable value
*/
global Binder(String key, Object value) {
this.key = key;
this.setValue(value);
}
/**
* @description Constructor for bind variable with key only.
* @param key The bind variable key
*/
global Binder(String key) {
this(key, null);
}
/**
* @description Retrieves the bind variable key.
* @return The bind variable key
*/
global String getKey() {
return this.key;
}
/**
* @description Retrieves the bind variable value.
* @return The bind variable value
*/
global Object getValue() {
return this.value;
}
/**
* @description Sets the bind variable value.
* @param value The value to bind
* @return This Binder instance for method chaining
*/
global Soql.Binder setValue(Object value) {
this.value = value;
return this;
}
/**
* @description Returns the bind variable as a SOQL parameter string.
* @return The bind variable formatted as ':key'
*/
global override String toString() {
return ':' + this.getKey();
}
}
/**
* @description Abstract base class providing fluent interface for building SOQL queries.
*/
global abstract class Builder {
/**
* @description The configured access level for the query execution.
*/
global transient System.AccessLevel accessLevel { get; protected set; }
/**
* @description Map containing bind variable name-value pairs for parameterized SOQL queries.
* Keys are variable names (without ':' prefix), values are the bound objects.
*/
global Map<String, Object> binds { get; protected set; }
/**
* @description The SObjectType name for the FROM clause.
*/
global String entity { get; protected set; }
/**
* @description List of GROUP BY field names.
*/
global List<String> groupByClauses { get; protected set; }
/**
* @description HAVING clause criteria for aggregate queries.
*/
global Soql.ConditionalLogic havingCriteria { get; protected set; }
/**
* @description List of ORDER BY clauses.
*/
global List<String> orderByClauses { get; protected set; }
/**
* @description Maximum number of rows to return (LIMIT clause).
*/
global Integer rowLimit { get; protected set; }
/**
* @description Number of rows to skip (OFFSET clause).
*/
global Integer rowOffset { get; protected set; }
/**
* @description Query scope (USING SCOPE clause).
*/
global Soql.Scope scope { get; protected set; }
/**
* @description Set of SELECT clause field names.
*/
global Set<String> selectClauses { get; protected set; }
/**
* @description Query usage type (FOR UPDATE, FOR VIEW, etc.).
*/
global Soql.Usage usage { get; protected set; }
/**
* @description WHERE clause criteria for filtering records.
*/
global Soql.ConditionalLogic whereCriteria { get; protected set; }
/**
* @description WITH clause for query hints (e.g., SECURITY_ENFORCED).
*/
global String withClause { get; protected set; }
// Read-only properties included for JSON output:
/**
* @description Serializable representation of the access level.
*/
private String accessLevelName {
// Note: System.AccessLevel is a weird enum - it isn't serializable,
// it does not have an enum name() property, and toString() does not output its name either:
get {
return (this.accessLevel == System.AccessLevel.SYSTEM_MODE) ? 'SYSTEM_MODE' : 'USER_MODE';
}
}
/**
* @description Constructor that initializes the builder with default values.
*/
protected Builder() {
this.reset();
}
/**
* @description Converts the builder to a SOQL query string.
* @return The complete SOQL query string
*/
global virtual override String toString() {
// Outputs a SOQL Query string, following the syntax defined here:
// https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select.htm#:~:text=SOQL%20query%20syntax%20consists%20of,typeOfField%20whenExpression%5B...%5D
List<String> queryArgs = new List<String>{ this.getSelect(), this.getFrom(), this.getOptionalArgs() };
return String.format('SELECT {0} FROM {1} {2}', queryArgs)?.trim();
}
/**
* @description Adds multiple bind variables from a map.
* @param bindMap Map of bind variable keys to values
* @return This Builder instance for method chaining
*/
global Soql.Builder addBind(Map<String, Object> bindMap) {
this.binds?.putAll(bindMap);
return this;
}
/**
* @description Adds a single bind variable.
* @param key The bind variable key
* @param value The bind variable value
* @return This Builder instance for method chaining
*/
global Soql.Builder addBind(String key, Object value) {
return this.addBind(new Map<String, Object>{ key => value });
}
/**
* @description Adds a bind variable using a Binder object.
* @param binder The Binder object containing key and value
* @return This Builder instance for method chaining
*/
global Soql.Builder addBind(Soql.Binder binder) {
return this.addBind(binder?.getKey(), binder?.getValue());
}
/**
* @description Sets the access level for the query (USER_MODE or SYSTEM_MODE).
* @param accessLevel The access level to use
* @return This Builder instance for method chaining
*/
global Soql.Builder setAccessLevel(System.AccessLevel accessLevel) {
this.accessLevel = accessLevel;
return this;
}
/**
* @description Resets the builder to its default state.
* @return This Builder instance for method chaining
*/
global Soql.Builder reset() {
this.accessLevel = System.AccessLevel.USER_MODE;
this.binds = new Map<String, Object>();
this.groupByClauses = new List<String>();
this.havingCriteria = new Soql.ConditionalLogic();
this.orderByClauses = new List<String>();
this.selectClauses = new Set<String>{ ID_REFERENCE };
this.whereCriteria = new Soql.ConditionalLogic();
return this;
}
/**
* @description Casts this builder to an InnerQuery for use in subqueries.
* @return This builder as an InnerQuery
*/
global Soql.InnerQuery toInnerQuery() {
return (Soql.InnerQuery) this;
}
/**
* @description Casts this builder to a Soql instance for query execution.
* @return This builder as a Soql instance
*/
global Soql toSoql() {
return (Soql) this;
}
/**
* @description Casts this builder to a Subquery for use in SELECT clauses.
* @return This builder as a Subquery
*/
global Soql.Subquery toSubquery() {
return (Soql.Subquery) this;
}
// * SELECT *
/**
* @description Adds all fields of the object to the SELECT clause.
* @return This Builder instance for method chaining
*/
@SuppressWarnings('PMD.EagerlyLoadedDescribeSObjectResult')
global Soql.Builder selectAll() {
// Note: Not sure why this PMD rule is flagging; the describe is explicitly deferred
List<Schema.DescribeSObjectResult> describes = Schema.describeSObjects(
new List<String>{ this.entity },
Schema.SObjectDescribeOptions.DEFERRED
);
Schema.DescribeSObjectResult describe = describes?.isEmpty() == false ? describes?.get(0) : null;
List<SObjectField> allFields = describe?.fields?.getMap()?.values() ?? new List<SObjectField>();
for (SObjectField field : allFields) {
this.addSelect(field);
}
return this;
}
/**
* @description Adds a field to the SELECT clause with an alias.
* @param fieldName The field name to select
* @param alias The alias for the field
* @return This Builder instance for method chaining
*/
global Soql.Builder addSelect(String fieldName, String alias) {
String clause = String.join(new List<String>{ fieldName, alias }, ' ')?.trim();
this.selectClauses?.add(clause);
return this;
}
/**
* @description Adds an SObjectField to the SELECT clause with an alias.
* @param field The SObjectField to select
* @param alias The alias for the field
* @return This Builder instance for method chaining
*/
global Soql.Builder addSelect(SObjectField field, String alias) {
return this.addSelect(field?.toString(), alias);
}
/**
* @description Adds a parent field to the SELECT clause with an alias.
* @param field The parent field to select
* @param alias The alias for the field
* @return This Builder instance for method chaining
*/
global Soql.Builder addSelect(Soql.ParentField field, String alias) {
return this.addSelect(field?.toString(), alias);
}
/**
* @description Adds a field to the SELECT clause.
* @param fieldName The field name to select
* @return This Builder instance for method chaining
*/
global Soql.Builder addSelect(String fieldName) {
return this.addSelect(fieldName, '');
}
/**
* @description Adds multiple SObjectFields to the SELECT clause.
* @param fields List of SObjectFields to select
* @return This Builder instance for method chaining
*/
global Soql.Builder addSelect(List<SObjectField> fields) {
for (SObjectField field : fields) {
if (field != null) {
this.addSelect(field?.toString());
}
}
return this;
}
/**
* @description Adds 5 SObjectFields to the SELECT clause.
* @param field1 First SObjectField to select
* @param field2 Second SObjectField to select
* @param field3 Third SObjectField to select
* @param field4 Fourth SObjectField to select
* @param field5 Fifth SObjectField to select
* @return This Builder instance for method chaining
*/
@SuppressWarnings('PMD.ExcessiveParameterList')
global Soql.Builder addSelect(
SObjectField field1,
SObjectField field2,
SObjectField field3,
SObjectField field4,
SObjectField field5
) {
// Currently supporting 5 SObjectFields per addSelect call:
return this.addSelect(new List<SObjectField>{ field1, field2, field3, field4, field5 });
}
/**
* @description Adds 4 SObjectFields to the SELECT clause.
* @param field1 First SObjectField to select
* @param field2 Second SObjectField to select
* @param field3 Third SObjectField to select
* @param field4 Fourth SObjectField to select
* @return This Builder instance for method chaining
*/
@SuppressWarnings('PMD.ExcessiveParameterList')
global Soql.Builder addSelect(
SObjectField field1,
SObjectField field2,
SObjectField field3,
SObjectField field4
) {
return this.addSelect(field1, field2, field3, field4, null);
}
/**
* @description Adds 3 SObjectFields to the SELECT clause.
* @param field1 First SObjectField to select
* @param field2 Second SObjectField to select
* @param field3 Third SObjectField to select
* @return This Builder instance for method chaining
*/
global Soql.Builder addSelect(SObjectField field1, SObjectField field2, SObjectField field3) {
return this.addSelect(field1, field2, field3, null);
}
/**
* @description Adds 2 SObjectFields to the SELECT clause.
* @param field1 First SObjectField to select
* @param field2 Second SObjectField to select
* @return This Builder instance for method chaining
*/
global Soql.Builder addSelect(SObjectField field1, SObjectField field2) {
return this.addSelect(field1, field2, null);
}
/**
* @description Adds a single SObjectField to the SELECT clause.
* @param field The SObjectField to select
* @return This Builder instance for method chaining
*/
global Soql.Builder addSelect(SObjectField field) {
SObjectField nullField;
return this.addSelect(field, nullField);
}
/**
* @description Adds multiple Selectable objects to the SELECT clause.
* @param fields List of Selectable objects to select
* @return This Builder instance for method chaining
*/
global Soql.Builder addSelect(List<Soql.Selectable> fields) {
for (Soql.Selectable field : fields) {
if (field != null) {
this.handleSelectableLogic(field);
this.addSelect(field?.toString());
}
}
return this;
}
/**
* @description Adds 5 Selectable objects to the SELECT clause.
* @param field1 First Selectable object to select
* @param field2 Second Selectable object to select
* @param field3 Third Selectable object to select
* @param field4 Fourth Selectable object to select
* @param field5 Fifth Selectable object to select
* @return This Builder instance for method chaining
*/
@SuppressWarnings('PMD.ExcessiveParameterList')
global Soql.Builder addSelect(
Soql.Selectable field1,
Soql.Selectable field2,
Soql.Selectable field3,
Soql.Selectable field4,
Soql.Selectable field5
) {
// Currently supporting 5 selectables per addSelect call:
return this.addSelect(new List<Soql.Selectable>{ field1, field2, field3, field4, field5 });
}
/**
* @description Adds 4 Selectable objects to the SELECT clause.
* @param field1 First Selectable object to select
* @param field2 Second Selectable object to select
* @param field3 Third Selectable object to select
* @param field4 Fourth Selectable object to select
* @return This Builder instance for method chaining
*/
@SuppressWarnings('PMD.ExcessiveParameterList')
global Soql.Builder addSelect(
Soql.Selectable field1,
Soql.Selectable field2,
Soql.Selectable field3,
Soql.Selectable field4
) {
return this.addSelect(field1, field2, field3, field4, null);
}
/**
* @description Adds 3 Selectable objects to the SELECT clause.
* @param field1 First Selectable object to select
* @param field2 Second Selectable object to select
* @param field3 Third Selectable object to select
* @return This Builder instance for method chaining
*/
global Soql.Builder addSelect(Soql.Selectable field1, Soql.Selectable field2, Soql.Selectable field3) {
return this.addSelect(field1, field2, field3, null);
}
/**
* @description Adds 2 Selectable objects to the SELECT clause.
* @param field1 First Selectable object to select
* @param field2 Second Selectable object to select
* @return This Builder instance for method chaining
*/
global Soql.Builder addSelect(Soql.Selectable field1, Soql.Selectable field2) {
return this.addSelect(field1, field2, null);
}
/**
* @description Adds a single Selectable object to the SELECT clause.
* @param field The Selectable object to select
* @return This Builder instance for method chaining
*/
global Soql.Builder addSelect(Soql.Selectable field) {
return this.addSelect(field, null);
}
/**
* @description Removes all fields from the SELECT clause.
* @return This Builder instance for method chaining
*/
global Soql.Builder deselectAll() {
// Remove all SELECT fields from the current query
this.selectClauses?.clear();
return this;
}
/**
* @description Removes a specific field from the SELECT clause.
* @param fieldName The field name to remove
* @return This Builder instance for method chaining
*/
global Soql.Builder deselect(String fieldName) {
// Remove a field from the current query
this.selectClauses?.remove(fieldName);
return this;
}
/**
* @description Removes a specific SObjectField from the SELECT clause.
* @param field The SObjectField to remove
* @return This Builder instance for method chaining
*/
global Soql.Builder deselect(SObjectField field) {
return this.deselect(field?.toString());
}
/**
* @description Removes a specific Selectable object from the SELECT clause.
* @param selectable The Selectable object to remove
* @return This Builder instance for method chaining
*/
global Soql.Builder deselect(Soql.Selectable selectable) {
return this.deselect(selectable?.toString());
}
// * FROM *
/**
* @description Sets the FROM clause object type.
* @param objectType The SObjectType to query from
* @return This Builder instance for method chaining
*/
global Soql.Builder setFrom(SObjectType objectType) {
this.entity = objectType?.toString();
return this;
}
// * USING SCOPE *
/**
* @description Sets the USING SCOPE clause.
* @param scope The scope to use (MINE, EVERYTHING, etc.)
* @return This Builder instance for method chaining
*/
global Soql.Builder setScope(Soql.Scope scope) {
this.scope = scope;
return this;
}
// * WHERE *
/**
* @description Adds a criteria object to the WHERE clause.
* @param criteria The criteria to add
* @return This Builder instance for method chaining
*/
global Soql.Builder addWhere(Soql.Criteria criteria) {
this.whereCriteria?.addCondition(criteria);
return this;
}
/**
* @description Adds a WHERE condition using field name, operator, and value.
* @param fieldName The field name to filter on
* @param operator The comparison operator
* @param value The value to compare against
* @return This Builder instance for method chaining
*/
global Soql.Builder addWhere(String fieldName, Soql.Operator operator, Object value) {
// Shorthand overload constructs a Condition object for the caller
return this.addWhere(new Soql.Condition(fieldName, operator, value));
}
/**
* @description Adds a WHERE condition using SObjectField, operator, and value.
* @param field The SObjectField to filter on
* @param operator The comparison operator
* @param value The value to compare against
* @return This Builder instance for method chaining
*/
global Soql.Builder addWhere(SObjectField field, Soql.Operator operator, Object value) {
return this.addWhere(field?.toString(), operator, value);
}
/**
* @description Adds a WHERE condition using parent field, operator, and value.
* @param field The parent field to filter on
* @param operator The comparison operator
* @param value The value to compare against
* @return This Builder instance for method chaining
*/
global Soql.Builder addWhere(Soql.ParentField field, Soql.Operator operator, Object value) {
return this.addWhere(field?.toString(), operator, value);
}
/**
* @description Adds a WHERE condition using field name, operator, and inner query.
* @param fieldName The field name to filter on
* @param operator The comparison operator
* @param innerQuery The inner query to compare against
* @return This Builder instance for method chaining
*/
global Soql.Builder addWhere(String fieldName, Soql.Operator operator, Soql.InnerQuery innerQuery) {
// If the current `value` object is an InnerQuery, add its binds to the parent query:
this.addBindsFromChildQuery(innerQuery);
return this.addWhere(fieldName, operator, innerQuery?.toString());
}
/**
* @description Adds a WHERE condition using SObjectField, operator, and inner query.
* @param field The SObjectField to filter on
* @param operator The comparison operator
* @param innerQuery The inner query to compare against
* @return This Builder instance for method chaining
*/
global Soql.Builder addWhere(SObjectField field, Soql.Operator operator, Soql.InnerQuery innerQuery) {
return this.addWhere(field?.toString(), operator, innerQuery);
}
/**
* @description Adds a WHERE condition using field name, operator, and bind variable.
* @param fieldName The field name to filter on
* @param operator The comparison operator
* @param binder The bind variable to compare against
* @return This Builder instance for method chaining
*/
global Soql.Builder addWhere(String fieldName, Soql.Operator operator, Soql.Binder binder) {
// If the current `value` object is a Binder, bind the values so that callers don't need to do so manually
return this.addWhere(fieldName, operator, binder?.toString())?.addBind(binder);
}
/**
* @description Adds a WHERE condition using SObjectField, operator, and bind variable.
* @param field The SObjectField to filter on
* @param operator The comparison operator
* @param binder The bind variable to compare against
* @return This Builder instance for method chaining
*/
global Soql.Builder addWhere(SObjectField field, Soql.Operator operator, Soql.Binder binder) {
return this.addWhere(field?.toString(), operator, binder);
}
/**
* @description Adds a WHERE condition using parent field, operator, and bind variable.
* @param field The parent field to filter on
* @param operator The comparison operator
* @param binder The bind variable to compare against
* @return This Builder instance for method chaining
*/
global Soql.Builder addWhere(Soql.ParentField field, Soql.Operator operator, Soql.Binder binder) {
return this.addWhere(field?.toString(), operator, binder);
}
/**
* @description Sets the logic type for combining WHERE conditions (AND/OR).
* @param newLogicType The logic type to use for WHERE conditions
* @return This Builder instance for method chaining
*/
global Soql.Builder setOuterWhereLogic(Soql.LogicType newLogicType) {
this.whereCriteria?.setLogicType(newLogicType);
return this;
}
// * WITH *
/**
* @description Adds SECURITY_ENFORCED clause to the query and sets access level to SYSTEM_MODE.
* @return This Builder instance for method chaining
*/
global Soql.Builder withSecurityEnforced() {
this.withClause = 'SECURITY_ENFORCED';
// Note: Cannot use the WITH SECURITY_ENFORCED clause in queries using USER_MODE access level.
this.setAccessLevel(System.AccessLevel.SYSTEM_MODE);
return this;
}