-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWhereLogicHandler.cls
More file actions
297 lines (262 loc) · 10.5 KB
/
WhereLogicHandler.cls
File metadata and controls
297 lines (262 loc) · 10.5 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
/*
* MIT License
*
* Copyright (c) 2024 Zackary Frazier
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
/**
* @description This class is responsible for handling the WHERE clause logic
* @author Zackary Frazier
* @since 3/23/2024
* @group Soql Engine
*/
@SuppressWarnings(
'PMD.StdCyclomaticComplexity,PMD.CognitiveComplexity,PMD.CyclomaticComplexity'
)
public with sharing class WhereLogicHandler extends BooleanLogicHandler {
SObject record;
enum ComparisonStrategy {
DATE_FUNC,
PARENT_FIELD,
FIELD
}
Map<ComparisonStrategy, Type> strategyTypeByEnum = new Map<ComparisonStrategy, Type>{
ComparisonStrategy.DATE_FUNC => WhereLogicDateFuncStrategy.class,
ComparisonStrategy.PARENT_FIELD => WhereLogicParentFieldStrategy.class,
ComparisonStrategy.FIELD => WhereLogicRecordFieldStrategy.class
};
/**
* @description Constructor
* @param options - the options to be used for the handler
*/
public WhereLogicHandler(BooleanLogicOptions options) {
super(options);
this.record = (SObject) this.obj;
}
/**
* @description Checks if the WHERE clause is met
* @param operatorNode
* @return `Boolean`
* @exception QueryException
*/
public override Boolean isCompareConditionMet(Node operatorNode) {
Node fieldNode = operatorNode.left;
Node valueNode = operatorNode.right;
Object fieldValue = getFieldValueFromValueNode(valueNode);
// the Type field on a polymorphic relationship is a special case
ComparisonStrategy strategy = getComparisonStrategy(fieldNode);
Type strategyType = strategyTypeByEnum.get(strategy);
IWhereLogicComparisonStrategy comparisonStrategy = (IWhereLogicComparisonStrategy) strategyType.newInstance();
WhereLogicComparisonResults comparisonResults = comparisonStrategy.getFieldValue(
this.record,
fieldNode
);
String finalField = comparisonResults.getFinalField();
SObject parentRecord = comparisonResults.getParentRecord();
Object sObjectFieldValue = comparisonResults.getRecordFieldValue();
OperatorHandlerFactory handlerFactory = new OperatorHandlerFactory();
IOperatorHandler handler = handlerFactory.getHandler(operatorNode.id);
if (comparisonResults.getIsPolymorphicTypeField()) {
return handler.handle(sObjectFieldValue, fieldValue);
}
Schema.SObjectType parentSot = SchemaService.getSObjectType(
parentRecord
);
Schema.DisplayType fieldType = SchemaService.getFieldType(
parentSot,
finalField
);
validateListHandlers(handler, fieldType);
if (valueNode.nodeType == NodeType.DATE_LITERAL) {
fieldValue = new DateLiteral.Builder()
.setFieldValue((String) fieldValue)
.build();
} else if (valueNode.nodeType != NodeType.VALUE_LIST) {
fieldValue = adjustForSoqlDateString(fieldValue, fieldType);
}
return handler.handle(sObjectFieldValue, fieldValue);
}
private ComparisonStrategy getComparisonStrategy(Node fieldNode) {
String fieldName = fieldNode.id;
Boolean isParentField = fieldName.contains('.');
if (AggregateService.isDateFunc(fieldNode.nodeType, fieldNode.id)) {
return ComparisonStrategy.DATE_FUNC;
} else if (isParentField) {
return ComparisonStrategy.PARENT_FIELD;
} else {
return ComparisonStrategy.FIELD;
}
}
private Object getFieldValueFromValueNode(Node valueNode) {
Object fieldValue = getFieldNodeValue(
valueNode,
this.record,
this.params
);
if (!(fieldValue instanceof List<Object>)) {
fieldValue = String.valueOf(fieldValue);
}
return fieldValue;
}
@SuppressWarnings(
'PMD.CognitiveComplexity,PMD.NcssMethodCount,PMD.CyclomaticComplexity'
)
private Object getFieldNodeValue(
Node valueNode,
sObject sObj,
Map<String, Object> params
) {
Boolean isBindVar = (valueNode.nodeType == NodeType.XBIND_VARIABLE);
Boolean isParamsNull = (params == null);
Boolean isValueInParams = (!isParamsNull &&
params.containsKey(valueNode.id));
if (isBindVar && isParamsNull || isBindVar && !isValueInParams) {
throw new QueryException('Missing bind variable: ' + valueNode.id);
}
if (isBindVar) {
return params.get(valueNode.id);
}
if (valueNode.nodeType == NodeType.VALUE_LIST) {
List<Object> values = new List<Object>();
Node valueListNode = valueNode;
while (valueListNode != null && valueListNode.left != null) {
values.add(valueListNode.left);
valueListNode = valueListNode.left.left;
}
return values;
}
if (valueNode.nodeType == NodeType.XNULL) {
return null;
}
if (valueNode.nodeType == NodeType.SUBQUERY) {
Node subquerySelectNode = valueNode.right;
Node subqueryFieldNode = subquerySelectNode.left;
Integer fieldsList = 0;
Node curr = subqueryFieldNode;
while (curr != null) {
fieldsList++;
curr = curr.left;
}
if (fieldsList > 1) {
throw new QueryException('Subquery must select only one field');
}
Node subqueryFromNode = subquerySelectNode.right;
Node subqueryObjNode = subqueryFromNode.left;
String fieldName = subqueryFieldNode.id;
String objName = subqueryObjNode.id;
String objApiName = SchemaService.getSObjectName(objName);
sObject childObj = SchemaService.blankSObjectFrom(objApiName);
if (childObj == null) {
throw new QueryException('Invalid object name: ' + objName);
}
String fieldApiName = null;
Schema.SObjectType childObjSot = SchemaService.getSObjectType(
childObj
);
List<Schema.SObjectField> childObjFields = SchemaService.getFields(
childObjSot
);
for (Schema.SObjectField field : childObjFields) {
fieldApiName = SchemaService.getFieldName(field);
if (fieldApiName == fieldName) {
fieldApiName = fieldApiName;
break;
}
}
if (fieldApiName == null) {
throw new QueryException('Invalid field name: ' + fieldName);
}
Schema.SObjectField childObjField = SchemaService.getField(
childObjSot,
fieldApiName
);
if (childObjField == null) {
throw new QueryException('Invalid field name: ' + fieldName);
}
Schema.DisplayType childObjFieldType = SchemaService.getFieldType(
childObjField
);
if (
childObjFieldType != Schema.DisplayType.REFERENCE &&
childObjFieldType != Schema.DisplayType.ID
) {
throw new QueryException('Subquery must select an ID field');
}
String parentFieldApiName = SchemaService.getFieldName(
childObj,
fieldName
);
Schema.SObjectField parentField = SchemaService.getField(
childObj,
parentFieldApiName
);
Boolean isSameType = SchemaService.idsReferenceSameSObject(
parentField,
childObjField
);
if (!isSameType) {
throw new QueryException(
'Subquery must select a field of the same type as the parent object'
);
}
List<SObject> subQueryObjects = (List<SObject>) MockDatabase.doSelectQueryInternal(
subquerySelectNode,
params
);
List<Id> subQueryIds = new List<Id>();
for (sObject subQueryObj : subQueryObjects) {
subQueryIds.add((Id) subQueryObj.get(fieldApiName));
}
return subQueryIds;
}
return valueNode.Id;
}
private Object adjustForSoqlDateString(
Object fieldValue,
Schema.DisplayType fieldType
) {
Boolean isString = (fieldValue instanceof String);
Boolean isDate = (fieldType == Schema.DisplayType.DATE);
Boolean isDateTime = (fieldType == Schema.DisplayType.DATETIME);
Boolean isDateOrDateTime = isDate || isDateTime;
if (!isString && !isDateOrDateTime) {
return fieldValue;
}
return ((String) fieldValue).toUpperCase();
}
private void validateListHandlers(
IOperatorHandler handler,
Schema.DisplayType fieldType
) {
Boolean isMultiPicklist = (fieldType ==
Schema.DisplayType.MULTIPICKLIST);
if (isMultiPicklist) {
return;
}
Boolean isIncludesHandler = (handler instanceof IncludesHandler);
Boolean isExcludesHandler = (handler instanceof ExcludesHandler);
if (isIncludesHandler || isExcludesHandler) {
throw new QueryException(
'Invalid field type for INCLUDES operator'
);
}
}
}