/
ODataFilterVisitor.java
343 lines (316 loc) · 12.5 KB
/
ODataFilterVisitor.java
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
/*
* JBoss, Home of Professional Open Source.
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
package org.teiid.translator.odata4;
import static org.teiid.language.SQLConstants.Reserved.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.teiid.language.*;
import org.teiid.language.SQLConstants.Tokens;
import org.teiid.language.visitor.HierarchyVisitor;
import org.teiid.metadata.BaseColumn;
import org.teiid.metadata.Column;
import org.teiid.metadata.FunctionMethod;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.metadata.Table;
import org.teiid.olingo.common.ODataTypeManager;
import org.teiid.translator.SourceSystemFunctions;
import org.teiid.translator.TranslatorException;
/**
* Only walks the filter version of the query
*/
public class ODataFilterVisitor extends HierarchyVisitor {
private static Map<String, String> infixFunctions = new HashMap<String, String>();
static {
infixFunctions.put("%", "mod");//$NON-NLS-1$ //$NON-NLS-2$
infixFunctions.put("+", "add");//$NON-NLS-1$ //$NON-NLS-2$
infixFunctions.put("-", "sub");//$NON-NLS-1$ //$NON-NLS-2$
infixFunctions.put("*", "mul");//$NON-NLS-1$ //$NON-NLS-2$
infixFunctions.put("/", "div");//$NON-NLS-1$ //$NON-NLS-2$
}
protected StringBuilder filter = new StringBuilder();
private ODataExecutionFactory ef;
protected ArrayList<TranslatorException> exceptions = new ArrayList<TranslatorException>();
private ODataQuery query;
private RuntimeMetadata metadata;
private ODataDocumentNode filterOnElement;
private BaseColumn currentExpression;
public ODataFilterVisitor(ODataExecutionFactory ef, RuntimeMetadata metadata, ODataQuery query) {
this.ef = ef;
this.query = query;
this.metadata = metadata;
}
public void appendFilter(Condition condition) throws TranslatorException{
append(condition);
if (!this.exceptions.isEmpty()) {
throw this.exceptions.get(0);
}
if (this.filter.length() > 0) {
this.filterOnElement.addFilter(this.filter.toString());
}
}
@Override
public void visit(Comparison obj) {
append(obj.getLeftExpression());
this.filter.append(Tokens.SPACE);
switch(obj.getOperator()) {
case EQ:
this.filter.append("eq"); //$NON-NLS-1$
break;
case NE:
this.filter.append("ne"); //$NON-NLS-1$
break;
case LT:
this.filter.append("lt"); //$NON-NLS-1$
break;
case LE:
this.filter.append("le"); //$NON-NLS-1$
break;
case GT:
this.filter.append("gt"); //$NON-NLS-1$
break;
case GE:
this.filter.append("ge"); //$NON-NLS-1$
break;
}
this.filter.append(Tokens.SPACE);
BaseColumn old = setCurrentExpression(obj.getLeftExpression());
appendRightComparison(obj);
this.currentExpression = old;
}
private BaseColumn setCurrentExpression(Expression leftExpression) {
BaseColumn old = currentExpression;
if (leftExpression instanceof ColumnReference) {
ColumnReference cr = (ColumnReference)leftExpression;
currentExpression = cr.getMetadataObject();
} else if (leftExpression instanceof Function) {
Function function = (Function)leftExpression;
currentExpression = function.getMetadataObject().getOutputParameter();
} else {
currentExpression = null;
}
return old;
}
protected void appendRightComparison(Comparison obj) {
append(obj.getRightExpression());
}
@Override
public void visit(IsNull obj) {
if (obj.isNegated()) {
this.filter.append(NOT.toLowerCase()).append(Tokens.LPAREN);
}
appendNested(obj.getExpression());
this.filter.append(Tokens.SPACE);
this.filter.append("eq").append(Tokens.SPACE); //$NON-NLS-1$
this.filter.append(NULL.toLowerCase());
if (obj.isNegated()) {
this.filter.append(Tokens.RPAREN);
}
}
private void appendNested(Expression ex) {
boolean useParens = ex instanceof Condition;
if (useParens) {
this.filter.append(Tokens.LPAREN);
}
append(ex);
if (useParens) {
this.filter.append(Tokens.RPAREN);
}
}
@Override
public void visit(AndOr obj) {
String opString = obj.getOperator().name().toLowerCase();
appendNestedCondition(obj, obj.getLeftCondition());
this.filter.append(Tokens.SPACE)
.append(opString)
.append(Tokens.SPACE);
appendNestedCondition(obj, obj.getRightCondition());
}
protected void appendNestedCondition(AndOr parent, Condition condition) {
if (condition instanceof AndOr) {
AndOr nested = (AndOr)condition;
if (nested.getOperator() != parent.getOperator()) {
this.filter.append(Tokens.LPAREN);
append(condition);
this.filter.append(Tokens.RPAREN);
return;
}
}
append(condition);
}
private String odataType(String type, String runtimeType) {
if (type == null) {
type = ODataTypeManager.odataType(runtimeType)
.getFullQualifiedName().getFullQualifiedNameAsString();
}
return type;
}
@Override
public void visit(ColumnReference obj) {
Column column = obj.getMetadataObject();
// check if the column on psedo column, then move it to the parent.
String pseudo = ODataMetadataProcessor.getPseudo(column);
ODataDocumentNode schemaElement = this.query.getSchemaElement((Table)column.getParent());
if (pseudo != null) {
try {
Table columnParent = (Table)column.getParent();
Table pseudoColumnParent = this.metadata.getTable(
ODataMetadataProcessor.getMerge(columnParent));
schemaElement = this.query.getSchemaElement(pseudoColumnParent);
} catch (TranslatorException e) {
this.exceptions.add(e);
}
}
if (this.filterOnElement == null) {
this.filterOnElement = schemaElement;
} else if (schemaElement.isExpandType() && (!this.filterOnElement.isExpandType())) {
this.exceptions.add(new TranslatorException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID17026)));
}
if (this.filterOnElement.isComplexType()) {
if (pseudo == null) {
this.filter.append(this.filterOnElement.getName()).append("/").append(column.getName());
} else {
this.filter.append(pseudo);
}
} else {
if (pseudo == null) {
this.filter.append(column.getName());
} else {
this.filter.append(pseudo);
}
}
}
protected boolean isInfixFunction(String function) {
return infixFunctions.containsKey(function);
}
@Override
public void visit(Function obj) {
if (this.ef.getFunctionModifiers().containsKey(obj.getName())) {
this.ef.getFunctionModifiers().get(obj.getName()).translate(obj);
}
String name = obj.getName();
List<Expression> args = obj.getParameters();
if(isInfixFunction(name)) {
this.filter.append(Tokens.LPAREN);
if(args != null) {
for(int i=0; i<args.size(); i++) {
append(args.get(i));
if(i < (args.size()-1)) {
this.filter.append(Tokens.SPACE);
this.filter.append(infixFunctions.get(name));
this.filter.append(Tokens.SPACE);
}
}
}
this.filter.append(Tokens.RPAREN);
}
else {
FunctionMethod method = obj.getMetadataObject();
if (name.startsWith(method.getCategory())) {
name = name.substring(method.getCategory().length()+1);
}
this.filter.append(name).append(Tokens.LPAREN);
if (name.equals("cast")) {
append(args.get(0));
this.filter.append(Tokens.COMMA);
Literal literal = (Literal)args.get(1);
String type = ODataTypeManager
.odataType((String) literal.getValue())
.getFullQualifiedName().getFullQualifiedNameAsString();
this.filter.append(type);
} else {
if (args != null && args.size() != 0) {
if (SourceSystemFunctions.ENDSWITH.equalsIgnoreCase(name)) {
append(args.get(1));
this.filter.append(Tokens.COMMA);
append(args.get(0));
} else {
BaseColumn old = currentExpression;
for (int i = 0; i < args.size(); i++) {
currentExpression = method.getInputParameters().get(Math.min(i, method.getInputParameters().size() -1));
append(args.get(i));
if (i < args.size()-1) {
this.filter.append(Tokens.COMMA);
}
}
currentExpression = old;
}
}
}
this.filter.append(Tokens.RPAREN);
}
}
public void visit(In obj) {
for (int i = 0; i < obj.getRightExpressions().size(); i++) {
Expression expr = obj.getRightExpressions().get(i);
if (i != 0) {
this.filter.append(" or ");
}
visitNode(obj.getLeftExpression());
this.filter.append(" eq ");
BaseColumn old = setCurrentExpression(obj.getLeftExpression());
visitNode(expr);
this.currentExpression = old;
}
}
@Override
public void visit(Literal obj) {
try {
String odataType = ODataTypeManager.odataType(obj.getType()).toString();
if (currentExpression != null) {
//TODO: this is an attempt at contextually figuring out the type, but it
//may not be sufficient in all cases
odataType = odataType(currentExpression.getNativeType(), currentExpression.getRuntimeType());
}
this.filter.append(ODataTypeManager.convertToODataURIValue(obj.getValue(), odataType));
} catch (EdmPrimitiveTypeException e) {
this.exceptions.add(new TranslatorException(e));
}
}
@Override
public void visit(Not obj) {
this.filter.append(NOT)
.append(Tokens.SPACE)
.append(Tokens.LPAREN);
append(obj.getCriteria());
this.filter.append(Tokens.RPAREN);
}
public void append(LanguageObject obj) {
visitNode(obj);
}
protected void append(List<? extends LanguageObject> items) {
if (items != null && items.size() != 0) {
for (int i = 0; i < items.size(); i++) {
append(items.get(i));
}
}
}
protected void append(LanguageObject[] items) {
if (items != null && items.length != 0) {
for (int i = 0; i < items.length; i++) {
append(items[i]);
}
}
}
}