svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / DefaultExpressionBuilder.java @ 47248
History | View | Annotate | Download (49.4 KB)
1 | 44006 | jjdelcerro | package org.gvsig.expressionevaluator.impl; |
---|---|---|---|
2 | 43020 | jjdelcerro | |
3 | 45523 | jjdelcerro | import java.awt.Color; |
4 | 43020 | jjdelcerro | import java.text.MessageFormat; |
5 | 44763 | jjdelcerro | import java.text.ParseException; |
6 | import java.text.SimpleDateFormat; |
||
7 | 43020 | jjdelcerro | import java.util.ArrayList; |
8 | import java.util.Collections; |
||
9 | 44763 | jjdelcerro | import java.util.Date; |
10 | 43020 | jjdelcerro | import java.util.HashSet; |
11 | import java.util.List; |
||
12 | 46050 | omartinez | import java.util.Map; |
13 | 43020 | jjdelcerro | import java.util.Objects; |
14 | import java.util.Set; |
||
15 | import org.apache.commons.lang3.StringUtils; |
||
16 | 44198 | jjdelcerro | import org.gvsig.expressionevaluator.Code; |
17 | 44274 | jjdelcerro | import org.gvsig.expressionevaluator.Expression; |
18 | 43020 | jjdelcerro | |
19 | 44006 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionBuilder; |
20 | 44644 | jjdelcerro | import static org.gvsig.expressionevaluator.ExpressionBuilder.PARAMETER_TYPE_CONSTANT; |
21 | import static org.gvsig.expressionevaluator.ExpressionBuilder.PARAMETER_TYPE_VARIABLE; |
||
22 | 44759 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
23 | 44644 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
24 | 44198 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionUtils; |
25 | import org.gvsig.expressionevaluator.Formatter; |
||
26 | 44759 | jjdelcerro | import org.gvsig.expressionevaluator.ReprMethod; |
27 | 44790 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
28 | import org.gvsig.tools.dataTypes.Coercion; |
||
29 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
30 | 46050 | omartinez | import org.gvsig.tools.util.PropertiesSupportHelper; |
31 | 43093 | jjdelcerro | |
32 | 44198 | jjdelcerro | @SuppressWarnings({"UseSpecificCatch" ,"OverridableMethodCallInConstructor"}) |
33 | 44006 | jjdelcerro | public class DefaultExpressionBuilder implements ExpressionBuilder { |
34 | 44198 | jjdelcerro | |
35 | private static final String FORMAT_QUOTE_FOR_STRINGS = "'"; |
||
36 | private static final String FORMAT_QUOTE_FOR_IDENTIFIERS = "\""; |
||
37 | 44006 | jjdelcerro | |
38 | 44198 | jjdelcerro | private static final String FORMAT_TRUE = "TRUE"; |
39 | private static final String FORMAT_FALSE = "FALSE"; |
||
40 | 43020 | jjdelcerro | |
41 | 44198 | jjdelcerro | private static final String FORMAT_GROUP = "( {0} )"; |
42 | 43093 | jjdelcerro | |
43 | 44198 | jjdelcerro | private static final String FORMAT_ISNULL = "( ({0}) IS NULL )"; |
44 | 44361 | jjdelcerro | private static final String FORMAT_NOTISNULL = "( ({0}) IS NOT NULL )"; |
45 | 44198 | jjdelcerro | private static final String FORMAT_OPERATOR_NOT = "( NOT ({0}) )"; |
46 | 43093 | jjdelcerro | |
47 | 44262 | jjdelcerro | private static final String FORMAT_OPERATOR_AND = "({0} AND {1})"; |
48 | 45327 | omartinez | private static final String FORMAT_OPERATOR_OR = "({0} OR {1})"; |
49 | // private static final String FORMAT_OPERATOR_OR2 = "{0} OR {1}";
|
||
50 | // private static final String FORMAT_OPERATOR_OR2 = "({0}) OR ({1})";
|
||
51 | 44198 | jjdelcerro | private static final String FORMAT_OPERATOR_EQ = "( ({0}) = ({1}) )"; |
52 | private static final String FORMAT_OPERATOR_NE = "( ({0}) <> ({1}) )"; |
||
53 | private static final String FORMAT_OPERATOR_GT = "( ({0}) > ({1}) )"; |
||
54 | private static final String FORMAT_OPERATOR_GE = "( ({0}) >= ({1}) )"; |
||
55 | private static final String FORMAT_OPERATOR_LT = "( ({0}) < ({1}) )"; |
||
56 | private static final String FORMAT_OPERATOR_LE = "( ({0}) <= ({1}) )"; |
||
57 | private static final String FORMAT_OPERATOR_LIKE = "( ({0}) LIKE ({1}) )"; |
||
58 | private static final String FORMAT_OPERATOR_ILIKE = "( ({0}) ILIKE ({1}) )"; |
||
59 | private static final String FORMAT_OPERATOR_ADD = "{0} + {1}"; |
||
60 | private static final String FORMAT_OPERATOR_SUBST = "{0} - {1}"; |
||
61 | 44262 | jjdelcerro | private static final String FORMAT_OPERATOR_MULT = "({0} * {1})"; |
62 | private static final String FORMAT_OPERATOR_DIV = "({0} / {1})"; |
||
63 | 44198 | jjdelcerro | private static final String FORMAT_OPERATOR_CONCAT = "{0} || {1}"; |
64 | 46050 | omartinez | private final PropertiesSupportHelper propertiesHelper; |
65 | 43093 | jjdelcerro | |
66 | 44769 | jjdelcerro | public class GroupBase extends AbstractValue implements Group { |
67 | 43093 | jjdelcerro | |
68 | 43020 | jjdelcerro | protected Value value;
|
69 | 43093 | jjdelcerro | |
70 | 43020 | jjdelcerro | public GroupBase(Value value) {
|
71 | this.value = value;
|
||
72 | } |
||
73 | 46105 | omartinez | |
74 | @Override
|
||
75 | public GroupBase clone() throws CloneNotSupportedException { |
||
76 | GroupBase other = (GroupBase) super.clone();
|
||
77 | other.value = value.clone(); |
||
78 | return other;
|
||
79 | } |
||
80 | 43020 | jjdelcerro | |
81 | @Override
|
||
82 | 44198 | jjdelcerro | public Value value() {
|
83 | 43020 | jjdelcerro | return value;
|
84 | } |
||
85 | |||
86 | @Override
|
||
87 | public void accept(Visitor visitor, VisitorFilter filter) { |
||
88 | 46505 | fdiaz | boolean visitChildren = true; |
89 | if (filter==null || filter.accept(this)) { |
||
90 | visitor.visit(this);
|
||
91 | } else {
|
||
92 | visitChildren = !filter.skipChildren(); |
||
93 | } |
||
94 | if(visitChildren){
|
||
95 | this.value.accept(visitor, filter);
|
||
96 | } |
||
97 | 43020 | jjdelcerro | } |
98 | 43093 | jjdelcerro | |
99 | 43020 | jjdelcerro | @Override
|
100 | 44376 | jjdelcerro | public void replace(Value target, Value replacement) { |
101 | if( this.value == target ) { |
||
102 | this.value = replacement;
|
||
103 | } else {
|
||
104 | this.value.replace(target, replacement);
|
||
105 | } |
||
106 | } |
||
107 | |||
108 | @Override
|
||
109 | 43020 | jjdelcerro | public String toString() { |
110 | 44769 | jjdelcerro | return this.toString(formatter()); |
111 | 43020 | jjdelcerro | } |
112 | 44198 | jjdelcerro | |
113 | @Override
|
||
114 | public String toString(Formatter<Value> formatter) { |
||
115 | 44296 | jjdelcerro | if( formatter!=null && formatter.canApply(this) ) { |
116 | 44198 | jjdelcerro | return formatter.format(this); |
117 | } |
||
118 | return MessageFormat.format(FORMAT_GROUP, this.value.toString()); |
||
119 | } |
||
120 | 43020 | jjdelcerro | } |
121 | |||
122 | 44769 | jjdelcerro | public class VariableBase extends AbstractValue implements Variable { |
123 | 43093 | jjdelcerro | |
124 | 43020 | jjdelcerro | protected String name; |
125 | 44644 | jjdelcerro | protected ExpressionBuilder builder;
|
126 | |||
127 | public VariableBase(ExpressionBuilder builder, String name) { |
||
128 | 47114 | jjdelcerro | if( StringUtils.contains(name, '"') ) { |
129 | throw new IllegalArgumentException("The name of a variable should not contain double quotes."); |
||
130 | } |
||
131 | 43020 | jjdelcerro | this.name = name;
|
132 | 44644 | jjdelcerro | this.builder = builder;
|
133 | 43020 | jjdelcerro | } |
134 | 46105 | omartinez | |
135 | public VariableBase clone() throws CloneNotSupportedException { |
||
136 | VariableBase other = (VariableBase) super.clone();
|
||
137 | return other;
|
||
138 | } |
||
139 | 43093 | jjdelcerro | |
140 | 43020 | jjdelcerro | @Override
|
141 | 44198 | jjdelcerro | public String name() { |
142 | 43020 | jjdelcerro | return this.name; |
143 | } |
||
144 | |||
145 | @Override
|
||
146 | public String toString() { |
||
147 | 44769 | jjdelcerro | return this.toString(formatter()); |
148 | 44198 | jjdelcerro | } |
149 | |||
150 | @Override
|
||
151 | public String toString(Formatter<Value> formatter) { |
||
152 | 44296 | jjdelcerro | if( formatter!=null && formatter.canApply(this) ) { |
153 | 44198 | jjdelcerro | return formatter.format(this); |
154 | } |
||
155 | 44644 | jjdelcerro | return this.builder.identifier(this.name); |
156 | 43020 | jjdelcerro | } |
157 | |||
158 | @Override
|
||
159 | public int compareTo(Variable o) { |
||
160 | 44198 | jjdelcerro | return this.name.compareTo(o.name()); |
161 | 43020 | jjdelcerro | } |
162 | |||
163 | @Override
|
||
164 | public boolean equals(Object obj) { |
||
165 | 43093 | jjdelcerro | if (!(obj instanceof Variable)) { |
166 | 43020 | jjdelcerro | return false; |
167 | } |
||
168 | 44198 | jjdelcerro | return this.name.equals(((Variable) obj).name()); |
169 | 43020 | jjdelcerro | } |
170 | |||
171 | @Override
|
||
172 | public int hashCode() { |
||
173 | int hash = 7; |
||
174 | hash = 37 * hash + Objects.hashCode(this.name); |
||
175 | return hash;
|
||
176 | } |
||
177 | } |
||
178 | |||
179 | 44769 | jjdelcerro | public class ParameterBase extends AbstractValue implements Parameter { |
180 | 43093 | jjdelcerro | |
181 | protected String name; |
||
182 | 43020 | jjdelcerro | protected Object value; |
183 | 44644 | jjdelcerro | protected int type; |
184 | 43093 | jjdelcerro | |
185 | public ParameterBase() {
|
||
186 | 44644 | jjdelcerro | this.type = PARAMETER_TYPE_CONSTANT;
|
187 | 43093 | jjdelcerro | this.name = null; |
188 | this.value = null; |
||
189 | 43020 | jjdelcerro | } |
190 | 46105 | omartinez | |
191 | public ParameterBase clone() throws CloneNotSupportedException { |
||
192 | ParameterBase other = (ParameterBase) super.clone();
|
||
193 | return other;
|
||
194 | } |
||
195 | 43020 | jjdelcerro | |
196 | @Override
|
||
197 | public Parameter as_constant() {
|
||
198 | 44644 | jjdelcerro | this.type = PARAMETER_TYPE_CONSTANT;
|
199 | 43093 | jjdelcerro | if (this.value == null && this.name != null) { |
200 | this.value = this.name; |
||
201 | } |
||
202 | 43020 | jjdelcerro | return this; |
203 | } |
||
204 | 43093 | jjdelcerro | |
205 | 43020 | jjdelcerro | @Override
|
206 | public Parameter as_variable() {
|
||
207 | 44644 | jjdelcerro | this.type = PARAMETER_TYPE_VARIABLE;
|
208 | 43093 | jjdelcerro | if (this.value != null && this.name == null) { |
209 | this.name = (String) this.value; |
||
210 | } |
||
211 | 43020 | jjdelcerro | return this; |
212 | } |
||
213 | 44644 | jjdelcerro | |
214 | 43020 | jjdelcerro | @Override
|
215 | 44198 | jjdelcerro | public String name() { |
216 | 43093 | jjdelcerro | switch (this.type) { |
217 | 44644 | jjdelcerro | case PARAMETER_TYPE_VARIABLE:
|
218 | 43093 | jjdelcerro | return this.name; |
219 | 44644 | jjdelcerro | case PARAMETER_TYPE_CONSTANT:
|
220 | 43093 | jjdelcerro | if (this.value == null) { |
221 | return null; |
||
222 | } |
||
223 | return this.value.toString(); |
||
224 | 43020 | jjdelcerro | default:
|
225 | 43093 | jjdelcerro | if (this.name != null) { |
226 | return this.name; |
||
227 | } |
||
228 | if (this.value != null) { |
||
229 | return this.value.toString(); |
||
230 | } |
||
231 | 43020 | jjdelcerro | return null; |
232 | } |
||
233 | } |
||
234 | |||
235 | @Override
|
||
236 | 44644 | jjdelcerro | public int type() { |
237 | return this.type; |
||
238 | 43020 | jjdelcerro | } |
239 | |||
240 | @Override
|
||
241 | 44644 | jjdelcerro | public boolean is_constant() { |
242 | return this.type == PARAMETER_TYPE_CONSTANT; |
||
243 | 43020 | jjdelcerro | } |
244 | |||
245 | @Override
|
||
246 | public boolean is_variable() { |
||
247 | 44644 | jjdelcerro | return this.type == PARAMETER_TYPE_VARIABLE; |
248 | 43020 | jjdelcerro | } |
249 | |||
250 | @Override
|
||
251 | 43093 | jjdelcerro | public Parameter value(Object value) { |
252 | this.value = value;
|
||
253 | return this; |
||
254 | } |
||
255 | |||
256 | @Override
|
||
257 | public Parameter name(String name) { |
||
258 | 44644 | jjdelcerro | this.type = PARAMETER_TYPE_VARIABLE;
|
259 | 43093 | jjdelcerro | this.name = name;
|
260 | return this; |
||
261 | } |
||
262 | |||
263 | @Override
|
||
264 | 44198 | jjdelcerro | public Object value() { |
265 | 43020 | jjdelcerro | try {
|
266 | 43093 | jjdelcerro | switch (this.type) { |
267 | 44644 | jjdelcerro | case PARAMETER_TYPE_CONSTANT:
|
268 | 43020 | jjdelcerro | return this.value; |
269 | 44644 | jjdelcerro | case PARAMETER_TYPE_VARIABLE:
|
270 | 43020 | jjdelcerro | default:
|
271 | 44652 | jjdelcerro | return this.value; |
272 | 43020 | jjdelcerro | } |
273 | 43093 | jjdelcerro | } catch (Exception ex) { |
274 | throw new RuntimeException("Can't get value from parameter.", ex); |
||
275 | 43020 | jjdelcerro | } |
276 | } |
||
277 | |||
278 | @Override
|
||
279 | public String toString() { |
||
280 | 44769 | jjdelcerro | return this.toString(formatter()); |
281 | 44198 | jjdelcerro | } |
282 | |||
283 | @Override
|
||
284 | public String toString(Formatter<Value> formatter) { |
||
285 | 44296 | jjdelcerro | if( formatter!=null && formatter.canApply(this) ) { |
286 | 44198 | jjdelcerro | return formatter.format(this); |
287 | } |
||
288 | 43093 | jjdelcerro | switch (this.type) { |
289 | 44644 | jjdelcerro | case PARAMETER_TYPE_CONSTANT:
|
290 | return Objects.toString(this.value); |
||
291 | |||
292 | case PARAMETER_TYPE_VARIABLE:
|
||
293 | 43020 | jjdelcerro | default:
|
294 | return "?"; |
||
295 | } |
||
296 | 43093 | jjdelcerro | } |
297 | 43020 | jjdelcerro | } |
298 | |||
299 | 44769 | jjdelcerro | public class ConstantBase extends AbstractValue implements Constant { |
300 | 43093 | jjdelcerro | |
301 | 43020 | jjdelcerro | protected Object value; |
302 | 44644 | jjdelcerro | protected ExpressionBuilder builder;
|
303 | |||
304 | public ConstantBase(ExpressionBuilder builder, Object value) { |
||
305 | 43020 | jjdelcerro | this.value = value;
|
306 | 44644 | jjdelcerro | this.builder = builder;
|
307 | 43020 | jjdelcerro | } |
308 | 43093 | jjdelcerro | |
309 | 46105 | omartinez | public Constant clone() throws CloneNotSupportedException { |
310 | ConstantBase other = (ConstantBase) super.clone();
|
||
311 | return other;
|
||
312 | } |
||
313 | |||
314 | 43020 | jjdelcerro | @Override
|
315 | 44198 | jjdelcerro | public Object value() { |
316 | 43020 | jjdelcerro | return this.value; |
317 | } |
||
318 | |||
319 | @Override
|
||
320 | public String toString() { |
||
321 | 44769 | jjdelcerro | return this.toString(formatter()); |
322 | 44198 | jjdelcerro | } |
323 | |||
324 | @Override
|
||
325 | public String toString(Formatter<Value> formatter) { |
||
326 | 44296 | jjdelcerro | if( formatter!=null && formatter.canApply(this) ) { |
327 | 44198 | jjdelcerro | return formatter.format(this); |
328 | } |
||
329 | if( this.value==null ) { |
||
330 | return "NULL"; |
||
331 | } |
||
332 | if( this.value instanceof byte[] ) { |
||
333 | 44644 | jjdelcerro | return "DECODE('"+this.builder.bytearray_hex((byte[])this.value)+"','hex')"; |
334 | 44198 | jjdelcerro | } |
335 | 43093 | jjdelcerro | if (this.value instanceof String) { |
336 | 44644 | jjdelcerro | return this.builder.string((String) this.value); |
337 | 43020 | jjdelcerro | } |
338 | 43093 | jjdelcerro | if (this.value instanceof Boolean) { |
339 | if (((Boolean) this.value)) { |
||
340 | 44198 | jjdelcerro | return FORMAT_TRUE;
|
341 | 43020 | jjdelcerro | } else {
|
342 | 44198 | jjdelcerro | return FORMAT_FALSE;
|
343 | 43020 | jjdelcerro | } |
344 | } |
||
345 | 44759 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
346 | ReprMethod repr = manager.getReprMethod(this.value);
|
||
347 | return repr.repr(value);
|
||
348 | 43020 | jjdelcerro | } |
349 | 46010 | jjdelcerro | |
350 | public ExpressionBuilder builder() {
|
||
351 | return this.builder; // Ojo, no esta en el API. |
||
352 | } |
||
353 | 46105 | omartinez | |
354 | 43020 | jjdelcerro | } |
355 | |||
356 | 44769 | jjdelcerro | public class CustomBase extends AbstractValue implements Custom { |
357 | 43093 | jjdelcerro | |
358 | 43020 | jjdelcerro | protected Object value; |
359 | 43093 | jjdelcerro | |
360 | 43020 | jjdelcerro | // Esto es para permitir declarar parametros y columnas en una seccion
|
361 | // custom.
|
||
362 | protected List<Value> values; |
||
363 | 43093 | jjdelcerro | |
364 | 43020 | jjdelcerro | public CustomBase(Object value) { |
365 | this.value = value;
|
||
366 | } |
||
367 | 46105 | omartinez | |
368 | public Custom clone() throws CloneNotSupportedException { |
||
369 | CustomBase other = (CustomBase) super.clone();
|
||
370 | if(other.values!=null) { |
||
371 | for (int i = 0; i < values.size(); i++) { |
||
372 | Value v = (Value) values.get(i).clone(); |
||
373 | other.values.set(i, v); |
||
374 | } |
||
375 | } |
||
376 | |||
377 | return other;
|
||
378 | } |
||
379 | 43020 | jjdelcerro | |
380 | @Override
|
||
381 | public void accept(Visitor visitor, VisitorFilter filter) { |
||
382 | 46505 | fdiaz | boolean visitChildren = true; |
383 | if (filter==null || filter.accept(this)) { |
||
384 | visitor.visit(this);
|
||
385 | } else {
|
||
386 | visitChildren = !filter.skipChildren(); |
||
387 | } |
||
388 | if(visitChildren){
|
||
389 | if (this.values != null) { |
||
390 | for (Value theValue : values) {
|
||
391 | theValue.accept(visitor, filter); |
||
392 | } |
||
393 | 43020 | jjdelcerro | } |
394 | } |
||
395 | } |
||
396 | 43093 | jjdelcerro | |
397 | 43020 | jjdelcerro | @Override
|
398 | 44376 | jjdelcerro | public void replace(Value target, Value replacement) { |
399 | if( this.values == null ) { |
||
400 | return;
|
||
401 | } |
||
402 | for (int i = 0; i < values.size(); i++) { |
||
403 | Value theValue = values.get(i); |
||
404 | if( target == theValue ) {
|
||
405 | values.set(i, replacement); |
||
406 | } else {
|
||
407 | theValue.replace(target, replacement); |
||
408 | } |
||
409 | } |
||
410 | } |
||
411 | |||
412 | @Override
|
||
413 | 44198 | jjdelcerro | public Object value() { |
414 | 43020 | jjdelcerro | return this.value; |
415 | } |
||
416 | |||
417 | @Override
|
||
418 | public Custom add(Variable variable) {
|
||
419 | 43093 | jjdelcerro | if (this.values == null) { |
420 | 43020 | jjdelcerro | this.values = new ArrayList<>(); |
421 | } |
||
422 | this.values.add(variable);
|
||
423 | return this; |
||
424 | } |
||
425 | |||
426 | @Override
|
||
427 | public Custom add(Parameter parameter) {
|
||
428 | 43093 | jjdelcerro | if (this.values == null) { |
429 | 43020 | jjdelcerro | this.values = new ArrayList<>(); |
430 | } |
||
431 | this.values.add(parameter);
|
||
432 | return this; |
||
433 | } |
||
434 | |||
435 | @Override
|
||
436 | public String toString() { |
||
437 | 44769 | jjdelcerro | return this.toString(formatter()); |
438 | 43020 | jjdelcerro | } |
439 | 44198 | jjdelcerro | |
440 | 43020 | jjdelcerro | @Override
|
441 | 44198 | jjdelcerro | public String toString(Formatter<Value> formatter) { |
442 | 44296 | jjdelcerro | if( formatter!=null && formatter.canApply(this) ) { |
443 | 44198 | jjdelcerro | return formatter.format(this); |
444 | 43020 | jjdelcerro | } |
445 | 44198 | jjdelcerro | return Objects.toString(this.value, ""); |
446 | 43020 | jjdelcerro | } |
447 | 43093 | jjdelcerro | } |
448 | 43020 | jjdelcerro | |
449 | 44769 | jjdelcerro | public class FunctionBase extends AbstractValue implements Function { |
450 | 43020 | jjdelcerro | |
451 | protected String name; |
||
452 | protected String format; |
||
453 | 44750 | jjdelcerro | protected List<Value> parameters; |
454 | 43093 | jjdelcerro | |
455 | 43020 | jjdelcerro | public FunctionBase(String name, String format) { |
456 | this.name = name;
|
||
457 | this.format = format;
|
||
458 | } |
||
459 | 46105 | omartinez | |
460 | public FunctionBase clone() throws CloneNotSupportedException { |
||
461 | FunctionBase other = (FunctionBase) super.clone();
|
||
462 | if (other.parameters != null) { |
||
463 | for (int i = 0; i < parameters.size(); i++) { |
||
464 | Value v = (Value) parameters.get(i).clone(); |
||
465 | other.parameters.set(i, v); |
||
466 | } |
||
467 | } |
||
468 | 43093 | jjdelcerro | |
469 | 46105 | omartinez | return other;
|
470 | } |
||
471 | |||
472 | 44020 | jjdelcerro | public FunctionBase(String name) { |
473 | this(name,null); |
||
474 | } |
||
475 | 44748 | jjdelcerro | |
476 | 43020 | jjdelcerro | @Override
|
477 | 44750 | jjdelcerro | public List<Value> parameters() { |
478 | 43093 | jjdelcerro | if (this.parameters == null) { |
479 | 43020 | jjdelcerro | this.parameters = new ArrayList<>(); |
480 | } |
||
481 | return this.parameters; |
||
482 | 43093 | jjdelcerro | } |
483 | 43020 | jjdelcerro | |
484 | @Override
|
||
485 | 44769 | jjdelcerro | public Function format(String format) { |
486 | this.format = format;
|
||
487 | return this; |
||
488 | } |
||
489 | |||
490 | @Override
|
||
491 | 43020 | jjdelcerro | public Function parameter(Value parameter) {
|
492 | 44750 | jjdelcerro | this.parameters().add(parameter);
|
493 | 43020 | jjdelcerro | return this; |
494 | } |
||
495 | |||
496 | @Override
|
||
497 | 44198 | jjdelcerro | public String name() { |
498 | 43020 | jjdelcerro | return this.name; |
499 | } |
||
500 | |||
501 | @Override
|
||
502 | 47184 | jjdelcerro | public String name(String name) { |
503 | this.name = name;
|
||
504 | return this.name; |
||
505 | } |
||
506 | |||
507 | @Override
|
||
508 | 43020 | jjdelcerro | public void accept(Visitor visitor, VisitorFilter filter) { |
509 | 46505 | fdiaz | boolean visitChildren = true; |
510 | if (filter==null || filter.accept(this)) { |
||
511 | visitor.visit(this);
|
||
512 | } else {
|
||
513 | visitChildren = !filter.skipChildren(); |
||
514 | } |
||
515 | if(visitChildren){
|
||
516 | if( this.parameters!=null ) { |
||
517 | for (Value argument : this.parameters) { |
||
518 | if( argument!=null ) { |
||
519 | argument.accept(visitor, filter); |
||
520 | } |
||
521 | 44644 | jjdelcerro | } |
522 | } |
||
523 | 43020 | jjdelcerro | } |
524 | } |
||
525 | |||
526 | @Override
|
||
527 | 44376 | jjdelcerro | public void replace(Value target, Value replacement) { |
528 | 44750 | jjdelcerro | if( this.parameters!=null ) { |
529 | 44376 | jjdelcerro | for (int i = 0; i < parameters.size(); i++) { |
530 | 44750 | jjdelcerro | Value argument = parameters.get(i); |
531 | 46104 | omartinez | if (argument != null) { |
532 | if( argument == target ) {
|
||
533 | parameters.set(i, replacement); |
||
534 | } else {
|
||
535 | argument.replace(target, replacement); |
||
536 | } |
||
537 | 44376 | jjdelcerro | } |
538 | } |
||
539 | 44750 | jjdelcerro | } |
540 | 44376 | jjdelcerro | } |
541 | |||
542 | @Override
|
||
543 | 43020 | jjdelcerro | public String toString() { |
544 | 44769 | jjdelcerro | return this.toString(formatter()); |
545 | 44198 | jjdelcerro | } |
546 | |||
547 | @Override
|
||
548 | public String toString(Formatter<Value> formatter) { |
||
549 | 44296 | jjdelcerro | if( formatter!=null && formatter.canApply(this) ) { |
550 | 44198 | jjdelcerro | return formatter.format(this); |
551 | } |
||
552 | 44020 | jjdelcerro | if( this.format==null ) { |
553 | StringBuilder builder = new StringBuilder(); |
||
554 | builder.append(name); |
||
555 | builder.append("(");
|
||
556 | if (this.parameters != null && !this.parameters.isEmpty()) { |
||
557 | boolean first = true; |
||
558 | 44750 | jjdelcerro | for (Value argument : this.parameters) { |
559 | 44020 | jjdelcerro | if( first ) {
|
560 | first=false;
|
||
561 | 44750 | jjdelcerro | builder.append(argument.toString(formatter)); |
562 | 44020 | jjdelcerro | } else {
|
563 | builder.append(", ");
|
||
564 | 44750 | jjdelcerro | builder.append(argument.toString(formatter)); |
565 | 44020 | jjdelcerro | } |
566 | } |
||
567 | } |
||
568 | builder.append(")");
|
||
569 | return builder.toString();
|
||
570 | } |
||
571 | 43093 | jjdelcerro | if (this.parameters != null && !this.parameters.isEmpty()) { |
572 | 43020 | jjdelcerro | List<String> values = new ArrayList<>(); |
573 | 44750 | jjdelcerro | for (Value argument : this.parameters) { |
574 | values.add(argument.toString(formatter)); |
||
575 | 43020 | jjdelcerro | } |
576 | return MessageFormat.format(format, values.toArray()); |
||
577 | } else {
|
||
578 | return this.format; |
||
579 | } |
||
580 | } |
||
581 | } |
||
582 | 43093 | jjdelcerro | |
583 | 44198 | jjdelcerro | public class MethodBase extends FunctionBase implements Method { |
584 | |||
585 | 44376 | jjdelcerro | private Value instance;
|
586 | 44198 | jjdelcerro | |
587 | public MethodBase(Value instance, String name) { |
||
588 | super(name);
|
||
589 | this.instance = instance;
|
||
590 | } |
||
591 | 46105 | omartinez | |
592 | public MethodBase clone() throws CloneNotSupportedException { |
||
593 | MethodBase other = (MethodBase) super.clone();
|
||
594 | other.instance = instance.clone(); |
||
595 | return other;
|
||
596 | } |
||
597 | |||
598 | 44198 | jjdelcerro | |
599 | @Override
|
||
600 | public Value instance() {
|
||
601 | return this.instance; |
||
602 | } |
||
603 | |||
604 | @Override
|
||
605 | public void accept(Visitor visitor, VisitorFilter filter) { |
||
606 | 46505 | fdiaz | boolean visitChildren = true; |
607 | if (filter==null || filter.accept(this)) { |
||
608 | visitor.visit(this);
|
||
609 | } else {
|
||
610 | visitChildren = !filter.skipChildren(); |
||
611 | } |
||
612 | if(visitChildren){
|
||
613 | this.instance.accept(visitor, filter);
|
||
614 | } |
||
615 | 44198 | jjdelcerro | } |
616 | 44376 | jjdelcerro | |
617 | @Override
|
||
618 | public void replace(Value target, Value replacement) { |
||
619 | if( this.instance == target ) { |
||
620 | this.instance = replacement;
|
||
621 | } else {
|
||
622 | this.instance.replace(target, replacement);
|
||
623 | } |
||
624 | } |
||
625 | 44198 | jjdelcerro | |
626 | @Override
|
||
627 | public String toString(Formatter<Value> formatter) { |
||
628 | 44296 | jjdelcerro | if( formatter!=null && formatter.canApply(this) ) { |
629 | 44198 | jjdelcerro | return formatter.format(this); |
630 | } |
||
631 | StringBuilder builder = new StringBuilder(); |
||
632 | builder.append(this.instance.toString(formatter));
|
||
633 | builder.append("->");
|
||
634 | builder.append(this.name());
|
||
635 | builder.append("(");
|
||
636 | if (this.parameters != null && !this.parameters.isEmpty()) { |
||
637 | boolean first = true; |
||
638 | 44750 | jjdelcerro | for (Value argument : this.parameters) { |
639 | 44198 | jjdelcerro | if( first ) {
|
640 | first=false;
|
||
641 | 44750 | jjdelcerro | builder.append(argument.toString(formatter)); |
642 | 44198 | jjdelcerro | } else {
|
643 | builder.append(", ");
|
||
644 | 44750 | jjdelcerro | builder.append(argument.toString(formatter)); |
645 | 44198 | jjdelcerro | } |
646 | } |
||
647 | } |
||
648 | builder.append(")");
|
||
649 | return builder.toString();
|
||
650 | } |
||
651 | } |
||
652 | |||
653 | 45164 | jjdelcerro | public class BinaryOperatorBase extends FunctionBase implements BinaryOperator { |
654 | 43020 | jjdelcerro | |
655 | 45164 | jjdelcerro | private static final int LEFT = 0; |
656 | private static final int RIGHT = 1; |
||
657 | |||
658 | 43020 | jjdelcerro | public BinaryOperatorBase(String name, String format) { |
659 | 45164 | jjdelcerro | super(name, format);
|
660 | this.parameters = new ArrayList<>(); |
||
661 | this.parameters.add(null); |
||
662 | this.parameters.add(null); |
||
663 | 43020 | jjdelcerro | } |
664 | 43093 | jjdelcerro | |
665 | 43020 | jjdelcerro | @Override
|
666 | 45164 | jjdelcerro | public Function parameter(Value parameter) {
|
667 | throw new UnsupportedOperationException("BinaryOperator can support add parameters."); |
||
668 | 43020 | jjdelcerro | } |
669 | 44376 | jjdelcerro | |
670 | @Override
|
||
671 | 45164 | jjdelcerro | public String name() { |
672 | return this.name; |
||
673 | 43020 | jjdelcerro | } |
674 | |||
675 | @Override
|
||
676 | 44198 | jjdelcerro | public BinaryOperator left(Value operand) {
|
677 | 45164 | jjdelcerro | this.parameters.set(LEFT, operand);
|
678 | 43020 | jjdelcerro | return this; |
679 | } |
||
680 | |||
681 | @Override
|
||
682 | 44198 | jjdelcerro | public BinaryOperator right(Value operand) {
|
683 | 45164 | jjdelcerro | this.parameters.set(RIGHT, operand);
|
684 | 43020 | jjdelcerro | return this; |
685 | } |
||
686 | |||
687 | @Override
|
||
688 | 44198 | jjdelcerro | public Value left() {
|
689 | 45164 | jjdelcerro | return this.parameters.get(LEFT); |
690 | 43020 | jjdelcerro | } |
691 | |||
692 | @Override
|
||
693 | 44198 | jjdelcerro | public Value right() {
|
694 | 45164 | jjdelcerro | return this.parameters.get(RIGHT); |
695 | 43020 | jjdelcerro | } |
696 | 43093 | jjdelcerro | |
697 | 43020 | jjdelcerro | @Override
|
698 | public String toString() { |
||
699 | 44769 | jjdelcerro | return this.toString(formatter()); |
700 | 43020 | jjdelcerro | } |
701 | 44198 | jjdelcerro | |
702 | @Override
|
||
703 | public String toString(Formatter<Value> formatter) { |
||
704 | 44296 | jjdelcerro | if( formatter!=null && formatter.canApply(this) ) { |
705 | 44198 | jjdelcerro | return formatter.format(this); |
706 | } |
||
707 | if( this.format==null ) { |
||
708 | StringBuilder builder = new StringBuilder(); |
||
709 | builder.append("(");
|
||
710 | 45164 | jjdelcerro | builder.append(this.left().toString(formatter));
|
711 | 44198 | jjdelcerro | builder.append(" ");
|
712 | builder.append(this.name);
|
||
713 | builder.append(" ");
|
||
714 | 45164 | jjdelcerro | builder.append(this.right().toString(formatter));
|
715 | 44198 | jjdelcerro | builder.append(")");
|
716 | return builder.toString();
|
||
717 | } else {
|
||
718 | return MessageFormat.format( |
||
719 | format, |
||
720 | 45164 | jjdelcerro | this.left().toString(formatter),
|
721 | this.right().toString(formatter)
|
||
722 | 44198 | jjdelcerro | ); |
723 | } |
||
724 | } |
||
725 | 43020 | jjdelcerro | } |
726 | |||
727 | protected Value value;
|
||
728 | 44644 | jjdelcerro | protected ExpressionEvaluatorManager manager;
|
729 | 44769 | jjdelcerro | protected Formatter<ExpressionBuilder.Value> formatter; |
730 | 43093 | jjdelcerro | |
731 | 44644 | jjdelcerro | public DefaultExpressionBuilder(ExpressionEvaluatorManager manager) {
|
732 | this.manager = manager;
|
||
733 | 46050 | omartinez | this.propertiesHelper = new PropertiesSupportHelper(); |
734 | 43020 | jjdelcerro | } |
735 | 44769 | jjdelcerro | |
736 | 46050 | omartinez | @Override
|
737 | public Object getProperty(String name) { |
||
738 | return this.propertiesHelper.getProperty(name); |
||
739 | } |
||
740 | |||
741 | 44769 | jjdelcerro | @Override
|
742 | 46050 | omartinez | public void setProperty(String name, Object value) { |
743 | this.propertiesHelper.setProperty(name, value);
|
||
744 | } |
||
745 | |||
746 | @Override
|
||
747 | public Map<String, Object> getProperties() { |
||
748 | return this.propertiesHelper.getProperties(); |
||
749 | } |
||
750 | |||
751 | @Override
|
||
752 | public void setProperties(Class filter, final Object... values) { |
||
753 | for (int i = 0; i < values.length; i+=2) { |
||
754 | propertiesHelper.setProperty((String) values[i], values[i+1]); |
||
755 | } |
||
756 | this.accept(new Visitor() { |
||
757 | @Override
|
||
758 | public void visit(Visitable v) { |
||
759 | for (int i = 0; i < values.length; i+=2) { |
||
760 | ((Value)v).setProperty((String) values[i], values[i+1]); |
||
761 | } |
||
762 | } |
||
763 | }, new ClassVisitorFilter(filter) );
|
||
764 | } |
||
765 | |||
766 | @Override
|
||
767 | 44769 | jjdelcerro | public Formatter<ExpressionBuilder.Value> formatter() { |
768 | if( this.formatter == null ) { |
||
769 | this.formatter = this.manager.getExpressionBuilderFormatter(); |
||
770 | } |
||
771 | return this.formatter; |
||
772 | } |
||
773 | 43020 | jjdelcerro | |
774 | @Override
|
||
775 | 44259 | jjdelcerro | public boolean isEmpty() { |
776 | return value == null; |
||
777 | } |
||
778 | |||
779 | @Override
|
||
780 | 43020 | jjdelcerro | public ExpressionBuilder createExpressionBuilder() {
|
781 | 44644 | jjdelcerro | return new DefaultExpressionBuilder(this.manager); |
782 | 43020 | jjdelcerro | } |
783 | 43093 | jjdelcerro | |
784 | 43020 | jjdelcerro | @Override
|
785 | 44198 | jjdelcerro | public Value value() {
|
786 | 43020 | jjdelcerro | return this.value; |
787 | } |
||
788 | 43093 | jjdelcerro | |
789 | 43020 | jjdelcerro | @Override
|
790 | 44198 | jjdelcerro | public ExpressionBuilder value(Value value) {
|
791 | 43020 | jjdelcerro | this.value = value;
|
792 | return this; |
||
793 | } |
||
794 | |||
795 | @Override
|
||
796 | public String toString() { |
||
797 | 44889 | omartinez | if (this.value == null) { |
798 | return ""; |
||
799 | } |
||
800 | 43020 | jjdelcerro | return this.value.toString(); |
801 | } |
||
802 | |||
803 | @Override
|
||
804 | 44198 | jjdelcerro | public String toString(Formatter<Value> formatter) { |
805 | 44889 | omartinez | if (this.value == null) { |
806 | return ""; |
||
807 | } |
||
808 | 44198 | jjdelcerro | return this.value.toString(formatter); |
809 | } |
||
810 | 44889 | omartinez | |
811 | @Override
|
||
812 | public String build() { |
||
813 | if (this.value == null) { |
||
814 | return null; |
||
815 | } |
||
816 | return this.value.toString(); |
||
817 | } |
||
818 | |||
819 | @Override
|
||
820 | public String build(Formatter<Value> formatter) { |
||
821 | if (this.value == null) { |
||
822 | return null; |
||
823 | } |
||
824 | return this.value.toString(formatter); |
||
825 | } |
||
826 | 44198 | jjdelcerro | |
827 | @Override
|
||
828 | public Value toValue(String expression) { |
||
829 | try {
|
||
830 | Code code = ExpressionUtils.compile(expression); |
||
831 | 46505 | fdiaz | code.link(ExpressionUtils.createSymbolTable()); |
832 | 44198 | jjdelcerro | return code.toValue(this); |
833 | } catch(Throwable ex) { |
||
834 | return custom(expression);
|
||
835 | } |
||
836 | } |
||
837 | |||
838 | @Override
|
||
839 | 43020 | jjdelcerro | public void accept(Visitor visitor, VisitorFilter filter) { |
840 | 43093 | jjdelcerro | if( this.value == null) { |
841 | return;
|
||
842 | } |
||
843 | 43020 | jjdelcerro | this.value.accept(visitor, filter);
|
844 | } |
||
845 | 43093 | jjdelcerro | |
846 | 43020 | jjdelcerro | @Override
|
847 | 44198 | jjdelcerro | public String quote_for_identifiers() { |
848 | return FORMAT_QUOTE_FOR_IDENTIFIERS;
|
||
849 | 43020 | jjdelcerro | } |
850 | 44198 | jjdelcerro | |
851 | 43020 | jjdelcerro | @Override
|
852 | 44198 | jjdelcerro | public String quote_for_strings() { |
853 | return FORMAT_QUOTE_FOR_STRINGS;
|
||
854 | 43020 | jjdelcerro | } |
855 | 44198 | jjdelcerro | |
856 | 43020 | jjdelcerro | @Override
|
857 | public String string(String s) { |
||
858 | 44198 | jjdelcerro | String quote = this.quote_for_strings(); |
859 | 43034 | jjdelcerro | // No se porque no esta disponible wrapIfMissing
|
860 | // return StringUtils.wrapIfMissing(s,quote);
|
||
861 | 43020 | jjdelcerro | if (s.startsWith(quote)) {
|
862 | return s;
|
||
863 | } |
||
864 | 44198 | jjdelcerro | return quote + StringUtils.replace(s,quote,quote+quote) + quote;
|
865 | 43020 | jjdelcerro | } |
866 | 43093 | jjdelcerro | |
867 | 43020 | jjdelcerro | @Override
|
868 | public String identifier(String id) { |
||
869 | 44198 | jjdelcerro | String quote = this.quote_for_identifiers(); |
870 | 43034 | jjdelcerro | // No se porque no esta disponible wrapIfMissing
|
871 | // return StringUtils.wrapIfMissing(id,quote);
|
||
872 | 43020 | jjdelcerro | if (id.startsWith(quote)) {
|
873 | return id;
|
||
874 | } |
||
875 | return quote + id + quote;
|
||
876 | } |
||
877 | |||
878 | @Override
|
||
879 | 44006 | jjdelcerro | public String bytearray_hex(byte[] data) { |
880 | 43020 | jjdelcerro | StringBuilder builder = new StringBuilder(); |
881 | for (byte abyte : data) { |
||
882 | int v = abyte & 0xff; |
||
883 | builder.append(String.format("%02x", v)); |
||
884 | } |
||
885 | return builder.toString();
|
||
886 | } |
||
887 | |||
888 | 44006 | jjdelcerro | @Override
|
889 | public String bytearray_0x(byte[] data) { |
||
890 | 43355 | jjdelcerro | return "0x" + bytearray_hex(data); |
891 | } |
||
892 | |||
893 | 44006 | jjdelcerro | @Override
|
894 | public String bytearray_x(byte[] data) { |
||
895 | 43355 | jjdelcerro | return "x'" + bytearray_hex(data) + "'"; |
896 | 43302 | jjdelcerro | } |
897 | |||
898 | 43093 | jjdelcerro | |
899 | 43020 | jjdelcerro | @Override
|
900 | 44198 | jjdelcerro | public Constant bytearray(byte[] data) { |
901 | 44644 | jjdelcerro | return new ConstantBase(this, data); |
902 | 44198 | jjdelcerro | } |
903 | |||
904 | @Override
|
||
905 | 43020 | jjdelcerro | public Variable variable(String name) { |
906 | 44644 | jjdelcerro | return new VariableBase(this, name); |
907 | 43020 | jjdelcerro | } |
908 | |||
909 | @Override
|
||
910 | public Variable column(String name) { |
||
911 | 44644 | jjdelcerro | return new VariableBase(this, name); |
912 | 43020 | jjdelcerro | } |
913 | 44855 | jjdelcerro | |
914 | 43020 | jjdelcerro | @Override
|
915 | 44855 | jjdelcerro | public Value column(String tableName, String columnName) { |
916 | return getattr(tableName,columnName);
|
||
917 | } |
||
918 | |||
919 | @Override
|
||
920 | 43093 | jjdelcerro | public Parameter parameter(String name) { |
921 | 44198 | jjdelcerro | List<Parameter> parameters = this.parameters(); |
922 | for (Parameter parameter : parameters) {
|
||
923 | if( StringUtils.equalsIgnoreCase(name, parameter.name()) ) {
|
||
924 | return parameter;
|
||
925 | } |
||
926 | 43093 | jjdelcerro | } |
927 | 44198 | jjdelcerro | Parameter parameter = this.parameter();
|
928 | 43093 | jjdelcerro | parameter.name(name); |
929 | return parameter;
|
||
930 | 43020 | jjdelcerro | } |
931 | 43093 | jjdelcerro | |
932 | 43687 | jjdelcerro | @Override
|
933 | 43093 | jjdelcerro | public Parameter parameter() {
|
934 | return new ParameterBase(); |
||
935 | } |
||
936 | |||
937 | 43020 | jjdelcerro | @Override
|
938 | public Constant constant(Object value) { |
||
939 | 46505 | fdiaz | if(value == null){ |
940 | return VALUE_NULL;
|
||
941 | } |
||
942 | 44644 | jjdelcerro | return new ConstantBase(this, value); |
943 | 43020 | jjdelcerro | } |
944 | |||
945 | @Override
|
||
946 | 44790 | jjdelcerro | public Constant constant(Object value, Class theClass) { |
947 | try {
|
||
948 | DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager(); |
||
949 | Coercion coercion = dataTypeManager.getDataType(theClass).getCoercion(); |
||
950 | return this.constant(coercion.coerce(value)); |
||
951 | } catch (Exception ex) { |
||
952 | throw new RuntimeException("Can't coerce value ("+Objects.toString(value)+") to "+theClass.getSimpleName()+".", ex); |
||
953 | } |
||
954 | } |
||
955 | |||
956 | @Override
|
||
957 | public Constant constant(Object value, int type) { |
||
958 | try {
|
||
959 | DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager(); |
||
960 | Coercion coercion = dataTypeManager.getCoercion(type); |
||
961 | return this.constant(coercion.coerce(value)); |
||
962 | } catch (Exception ex) { |
||
963 | throw new RuntimeException("Can't coerce value ("+Objects.toString(value)+") to "+type+".", ex); |
||
964 | } |
||
965 | } |
||
966 | |||
967 | @Override
|
||
968 | 43020 | jjdelcerro | public Group group(Value value) { |
969 | return new GroupBase(value); |
||
970 | } |
||
971 | 43093 | jjdelcerro | |
972 | 43020 | jjdelcerro | @Override
|
973 | public Custom custom(Object value) { |
||
974 | return new CustomBase(value); |
||
975 | } |
||
976 | |||
977 | 44198 | jjdelcerro | @Override
|
978 | public Method method(Value instance, String name, Value... values) { |
||
979 | MethodBase method = new MethodBase(instance, name);
|
||
980 | for (Value theValue : values) {
|
||
981 | method.parameter(theValue); |
||
982 | } |
||
983 | return method;
|
||
984 | } |
||
985 | |||
986 | @Override
|
||
987 | 44020 | jjdelcerro | public Function function(String name, Value... values) { |
988 | FunctionBase func = new FunctionBase(name);
|
||
989 | for (Value theValue : values) {
|
||
990 | func.parameter(theValue); |
||
991 | } |
||
992 | return func;
|
||
993 | } |
||
994 | |||
995 | public Function builtin_function(String name, String format, Value... values) { |
||
996 | 43093 | jjdelcerro | FunctionBase func = new FunctionBase(name, format);
|
997 | 44006 | jjdelcerro | for (Value theValue : values) {
|
998 | func.parameter(theValue); |
||
999 | 43020 | jjdelcerro | } |
1000 | return func;
|
||
1001 | } |
||
1002 | 43093 | jjdelcerro | |
1003 | 44198 | jjdelcerro | @Override
|
1004 | public BinaryOperator binaryOperator(String name, Value leftOperand, Value rightOperand) { |
||
1005 | return binaryOperator(name, null, leftOperand, rightOperand); |
||
1006 | } |
||
1007 | |||
1008 | 43020 | jjdelcerro | public BinaryOperator binaryOperator(String name, String format, Value leftOperand, Value rightOperand) { |
1009 | 43093 | jjdelcerro | BinaryOperator operator = new BinaryOperatorBase(name, format);
|
1010 | 44198 | jjdelcerro | operator.left(leftOperand); |
1011 | operator.right(rightOperand); |
||
1012 | 43020 | jjdelcerro | return operator;
|
1013 | } |
||
1014 | 43093 | jjdelcerro | |
1015 | 43020 | jjdelcerro | @Override
|
1016 | 44198 | jjdelcerro | public List<Variable> variables() { |
1017 | 43020 | jjdelcerro | final Set<Variable> vars = new HashSet<>(); |
1018 | 44748 | jjdelcerro | this.accept((Visitable value1) -> {
|
1019 | if (!vars.contains((Variable) value1)) {
|
||
1020 | vars.add((Variable) value1); |
||
1021 | } |
||
1022 | 43093 | jjdelcerro | }, new ClassVisitorFilter(Variable.class));
|
1023 | 43020 | jjdelcerro | List<Variable> lvars = new ArrayList<>(vars); |
1024 | Collections.sort(lvars);
|
||
1025 | return lvars;
|
||
1026 | } |
||
1027 | 43093 | jjdelcerro | |
1028 | 43020 | jjdelcerro | @Override
|
1029 | 44198 | jjdelcerro | public List<Parameter> parameters() { |
1030 | final List<Parameter> params = new ArrayList<>(); |
||
1031 | 44748 | jjdelcerro | this.accept((Visitable value1) -> {
|
1032 | params.add((Parameter) value1); |
||
1033 | 43093 | jjdelcerro | }, new ClassVisitorFilter(Parameter.class));
|
1034 | 43020 | jjdelcerro | return params;
|
1035 | } |
||
1036 | 44198 | jjdelcerro | |
1037 | 43020 | jjdelcerro | @Override
|
1038 | 44198 | jjdelcerro | public List<String> parameters_names() { |
1039 | List<String> params = new ArrayList<>(); |
||
1040 | for (Parameter param : parameters()) {
|
||
1041 | Object theValue = param.value();
|
||
1042 | String s;
|
||
1043 | switch(param.type()) {
|
||
1044 | 44644 | jjdelcerro | case PARAMETER_TYPE_CONSTANT:
|
1045 | 44198 | jjdelcerro | if( theValue==null ) { |
1046 | s = "NULL";
|
||
1047 | } else if( theValue instanceof String ) { |
||
1048 | s = "'" + (String)theValue + "'"; |
||
1049 | |||
1050 | } else if( theValue instanceof byte[] ) { |
||
1051 | s = bytearray_0x((byte[]) theValue); |
||
1052 | |||
1053 | } else {
|
||
1054 | s = theValue.toString(); |
||
1055 | } |
||
1056 | break;
|
||
1057 | 44644 | jjdelcerro | case PARAMETER_TYPE_VARIABLE:
|
1058 | 44198 | jjdelcerro | default:
|
1059 | s = "\"" + param.name() + "\""; |
||
1060 | } |
||
1061 | // if( !params.contains(s) ) { // Ojo que deben ir todos, incluso duplicados.
|
||
1062 | params.add(s); |
||
1063 | // }
|
||
1064 | } |
||
1065 | // Collections.sort(params); Ojo, no deben ordenarse.
|
||
1066 | return params;
|
||
1067 | 43020 | jjdelcerro | } |
1068 | 44198 | jjdelcerro | |
1069 | @Override
|
||
1070 | public List<String> variables_names() { |
||
1071 | List<String> vars = new ArrayList<>(); |
||
1072 | for (Variable var : this.variables()) { |
||
1073 | vars.add(var.name()); |
||
1074 | } |
||
1075 | Collections.sort(vars);
|
||
1076 | return vars;
|
||
1077 | } |
||
1078 | |||
1079 | @Override
|
||
1080 | 43020 | jjdelcerro | public ExpressionBuilder set(Value value) {
|
1081 | this.value = value;
|
||
1082 | return this; |
||
1083 | } |
||
1084 | |||
1085 | @Override
|
||
1086 | public ExpressionBuilder and(Value value) {
|
||
1087 | 43093 | jjdelcerro | if (this.value == null) { |
1088 | 43020 | jjdelcerro | return this.set(value); |
1089 | } |
||
1090 | 44198 | jjdelcerro | BinaryOperator operator = binaryOperator(OPERATOR_AND, FORMAT_OPERATOR_AND, this.value, value);
|
1091 | 43020 | jjdelcerro | this.value = operator;
|
1092 | return this; |
||
1093 | } |
||
1094 | |||
1095 | @Override
|
||
1096 | public ExpressionBuilder or(Value value) {
|
||
1097 | 43093 | jjdelcerro | if (this.value == null) { |
1098 | 43020 | jjdelcerro | return this.set(value); |
1099 | } |
||
1100 | 44198 | jjdelcerro | BinaryOperator operator = binaryOperator(OPERATOR_OR, FORMAT_OPERATOR_OR, this.value, value);
|
1101 | 43020 | jjdelcerro | this.value = operator;
|
1102 | return this; |
||
1103 | } |
||
1104 | |||
1105 | @Override
|
||
1106 | 44198 | jjdelcerro | public Function is_null(Value value) {
|
1107 | return builtin_function("IS NULL", FORMAT_ISNULL, value); |
||
1108 | 43020 | jjdelcerro | } |
1109 | |||
1110 | @Override
|
||
1111 | 44198 | jjdelcerro | public Function not_is_null(Value value) {
|
1112 | 44361 | jjdelcerro | return builtin_function("IS NOT NULL", FORMAT_NOTISNULL, value); |
1113 | 43020 | jjdelcerro | } |
1114 | |||
1115 | @Override
|
||
1116 | public Function not(Value value) {
|
||
1117 | 44198 | jjdelcerro | return builtin_function(OPERATOR_NOT, FORMAT_OPERATOR_NOT, value);
|
1118 | 43020 | jjdelcerro | } |
1119 | |||
1120 | @Override
|
||
1121 | public BinaryOperator and(Value leftOperand, Value rightOperand) {
|
||
1122 | 44198 | jjdelcerro | return binaryOperator(OPERATOR_AND, FORMAT_OPERATOR_AND, leftOperand, rightOperand);
|
1123 | 43020 | jjdelcerro | } |
1124 | |||
1125 | @Override
|
||
1126 | 44274 | jjdelcerro | public BinaryOperator and(Expression leftOperand, Expression rightOperand) { |
1127 | return binaryOperator(
|
||
1128 | OPERATOR_AND, |
||
1129 | FORMAT_OPERATOR_AND, |
||
1130 | leftOperand.getCode().toValue(), |
||
1131 | rightOperand.getCode().toValue() |
||
1132 | ); |
||
1133 | } |
||
1134 | |||
1135 | @Override
|
||
1136 | public BinaryOperator and(Expression leftOperand, Value rightOperand) { |
||
1137 | return binaryOperator(
|
||
1138 | OPERATOR_AND, |
||
1139 | FORMAT_OPERATOR_AND, |
||
1140 | leftOperand.getCode().toValue(), |
||
1141 | rightOperand |
||
1142 | ); |
||
1143 | } |
||
1144 | |||
1145 | @Override
|
||
1146 | 43020 | jjdelcerro | public BinaryOperator or(Value leftOperand, Value rightOperand) {
|
1147 | 44198 | jjdelcerro | return binaryOperator(OPERATOR_OR, FORMAT_OPERATOR_OR, leftOperand, rightOperand);
|
1148 | 43020 | jjdelcerro | } |
1149 | |||
1150 | @Override
|
||
1151 | public BinaryOperator eq(Value leftOperand, Value rightOperand) {
|
||
1152 | 44198 | jjdelcerro | return binaryOperator("=", FORMAT_OPERATOR_EQ, leftOperand, rightOperand); |
1153 | 43020 | jjdelcerro | } |
1154 | 43093 | jjdelcerro | |
1155 | 43020 | jjdelcerro | @Override
|
1156 | public BinaryOperator ne(Value leftOperand, Value rightOperand) {
|
||
1157 | 44198 | jjdelcerro | return binaryOperator("<>", FORMAT_OPERATOR_NE, leftOperand, rightOperand); |
1158 | 43093 | jjdelcerro | } |
1159 | 43020 | jjdelcerro | |
1160 | @Override
|
||
1161 | public BinaryOperator gt(Value op1, Value op2) {
|
||
1162 | 44198 | jjdelcerro | return binaryOperator(">", FORMAT_OPERATOR_GT, op1, op2); |
1163 | 43020 | jjdelcerro | } |
1164 | |||
1165 | @Override
|
||
1166 | public BinaryOperator ge(Value op1, Value op2) {
|
||
1167 | 44198 | jjdelcerro | return binaryOperator(">=", FORMAT_OPERATOR_GE, op1, op2); |
1168 | 43020 | jjdelcerro | } |
1169 | |||
1170 | @Override
|
||
1171 | public BinaryOperator lt(Value op1, Value op2) {
|
||
1172 | 44198 | jjdelcerro | return binaryOperator("<", FORMAT_OPERATOR_LT, op1, op2); |
1173 | 43020 | jjdelcerro | } |
1174 | |||
1175 | @Override
|
||
1176 | public BinaryOperator le(Value op1, Value op2) {
|
||
1177 | 44198 | jjdelcerro | return binaryOperator("<=", FORMAT_OPERATOR_LE, op1, op2); |
1178 | 43020 | jjdelcerro | } |
1179 | |||
1180 | @Override
|
||
1181 | public BinaryOperator like(Value op1, Value op2) {
|
||
1182 | 44198 | jjdelcerro | return binaryOperator(OPERATOR_LIKE, FORMAT_OPERATOR_LIKE, op1, op2);
|
1183 | 43020 | jjdelcerro | } |
1184 | |||
1185 | @Override
|
||
1186 | public BinaryOperator ilike(Value op1, Value op2) {
|
||
1187 | 44198 | jjdelcerro | return binaryOperator(OPERATOR_ILIKE, FORMAT_OPERATOR_ILIKE, op1, op2);
|
1188 | 43020 | jjdelcerro | } |
1189 | |||
1190 | @Override
|
||
1191 | public BinaryOperator add(Value op1, Value op2) {
|
||
1192 | 44198 | jjdelcerro | return binaryOperator(OPERATOR_ADD, FORMAT_OPERATOR_ADD, op1, op2);
|
1193 | 43020 | jjdelcerro | } |
1194 | |||
1195 | @Override
|
||
1196 | public BinaryOperator subst(Value op1, Value op2) {
|
||
1197 | 44198 | jjdelcerro | return binaryOperator(OPERATOR_SUBST, FORMAT_OPERATOR_SUBST, op1, op2);
|
1198 | 43020 | jjdelcerro | } |
1199 | |||
1200 | @Override
|
||
1201 | public BinaryOperator mult(Value op1, Value op2) {
|
||
1202 | 44198 | jjdelcerro | return binaryOperator(OPERATOR_MULT, FORMAT_OPERATOR_MULT, op1, op2);
|
1203 | 43020 | jjdelcerro | } |
1204 | |||
1205 | @Override
|
||
1206 | public BinaryOperator div(Value op1, Value op2) {
|
||
1207 | 44198 | jjdelcerro | return binaryOperator(OPERATOR_DIV, FORMAT_OPERATOR_DIV, op1, op2);
|
1208 | 43020 | jjdelcerro | } |
1209 | |||
1210 | @Override
|
||
1211 | 45164 | jjdelcerro | public Function concat(Value... values) {
|
1212 | if( values.length==2 ) { |
||
1213 | return binaryOperator(OPERATOR_CONCAT, FORMAT_OPERATOR_CONCAT, values[0], values[1]); |
||
1214 | } |
||
1215 | FunctionBase func = new FunctionBase(FUNCTION_CONCAT);
|
||
1216 | for (Value theValue : values) {
|
||
1217 | func.parameter(theValue); |
||
1218 | } |
||
1219 | return func;
|
||
1220 | 43020 | jjdelcerro | } |
1221 | 45164 | jjdelcerro | |
1222 | 44051 | omartinez | @Override
|
1223 | 44038 | jjdelcerro | public Function iif(Value condition, Value iftrue, Value iffalse) {
|
1224 | 44198 | jjdelcerro | return function(FUNCTION_IIF, condition, iftrue, iffalse);
|
1225 | 44038 | jjdelcerro | } |
1226 | |||
1227 | 44051 | omartinez | @Override
|
1228 | 44038 | jjdelcerro | public Function ifnull(Value value, Value iftrue, Value iffalse) {
|
1229 | 44198 | jjdelcerro | return function(FUNCTION_IFNULL, value, iftrue, iffalse);
|
1230 | 44038 | jjdelcerro | } |
1231 | 44051 | omartinez | |
1232 | @Override
|
||
1233 | public Function left(Value str, Value size) {
|
||
1234 | 44198 | jjdelcerro | return function(FUNCTION_LEFT, str, size);
|
1235 | 44051 | omartinez | } |
1236 | |||
1237 | @Override
|
||
1238 | public Function right(Value str, Value len) {
|
||
1239 | 44198 | jjdelcerro | return function(FUNCTION_RIGHT, str, len);
|
1240 | 44051 | omartinez | } |
1241 | |||
1242 | @Override
|
||
1243 | public Function locate(Value search, Value str, Value start) {
|
||
1244 | 44198 | jjdelcerro | return function(FUNCTION_LOCATE, search, str, start);
|
1245 | 44051 | omartinez | } |
1246 | |||
1247 | @Override
|
||
1248 | public Function position(Value search, Value str) {
|
||
1249 | 44198 | jjdelcerro | return function(FUNCTION_POSITION, search, str);
|
1250 | 44051 | omartinez | } |
1251 | |||
1252 | @Override
|
||
1253 | public Function lpad(Value str, Value len, Value padstr) {
|
||
1254 | 44198 | jjdelcerro | return function(FUNCTION_LPAD, str, len, padstr);
|
1255 | 44051 | omartinez | } |
1256 | |||
1257 | @Override
|
||
1258 | public Function rpad(Value str, Value len, Value padstr) {
|
||
1259 | 44198 | jjdelcerro | return function(FUNCTION_RPAD, str, len, padstr);
|
1260 | 44051 | omartinez | } |
1261 | |||
1262 | @Override
|
||
1263 | public Function ltrim(Value str) {
|
||
1264 | 44198 | jjdelcerro | return function(FUNCTION_LTRIM, str);
|
1265 | 44051 | omartinez | } |
1266 | |||
1267 | @Override
|
||
1268 | public Function rtrim(Value str) {
|
||
1269 | 44198 | jjdelcerro | return function(FUNCTION_RTRIM, str);
|
1270 | 44051 | omartinez | } |
1271 | |||
1272 | @Override
|
||
1273 | public Function trim(Value str) {
|
||
1274 | 44198 | jjdelcerro | return function(FUNCTION_TRIM, str);
|
1275 | 44051 | omartinez | } |
1276 | |||
1277 | @Override
|
||
1278 | public Function repeat(Value str, Value size) {
|
||
1279 | 44198 | jjdelcerro | return function(FUNCTION_REPEAT, str, size);
|
1280 | 44051 | omartinez | } |
1281 | |||
1282 | @Override
|
||
1283 | public Function replace(Value str, Value search, Value replstr) {
|
||
1284 | 44198 | jjdelcerro | return function(FUNCTION_REPLACE, str, search, replstr);
|
1285 | 44051 | omartinez | } |
1286 | |||
1287 | @Override
|
||
1288 | public Function ascii(Value str) {
|
||
1289 | 44198 | jjdelcerro | return function(FUNCTION_ASCII, str);
|
1290 | 44051 | omartinez | } |
1291 | |||
1292 | @Override
|
||
1293 | public Function lenght(Value str) {
|
||
1294 | 44198 | jjdelcerro | return function(FUNCTION_LENGHT, str);
|
1295 | 44051 | omartinez | } |
1296 | |||
1297 | @Override
|
||
1298 | public Function instr(Value str, Value search, Value start) {
|
||
1299 | 44198 | jjdelcerro | return function(FUNCTION_INSTR, str, search, start);
|
1300 | 44051 | omartinez | } |
1301 | |||
1302 | @Override
|
||
1303 | public Function lower(Value str) {
|
||
1304 | 44198 | jjdelcerro | return function(FUNCTION_LOWER, str);
|
1305 | 44051 | omartinez | } |
1306 | |||
1307 | @Override
|
||
1308 | public Function upper(Value str) {
|
||
1309 | 44198 | jjdelcerro | return function(FUNCTION_UPPER, str);
|
1310 | 44051 | omartinez | } |
1311 | |||
1312 | @Override
|
||
1313 | public Function space(Value size) {
|
||
1314 | 44198 | jjdelcerro | return function(FUNCTION_SPACE, size);
|
1315 | 44051 | omartinez | } |
1316 | |||
1317 | @Override
|
||
1318 | public Function substring(Value str, Value start, Value len) {
|
||
1319 | 44198 | jjdelcerro | return function(FUNCTION_SUBSTRING, str, start, len);
|
1320 | 44051 | omartinez | } |
1321 | |||
1322 | @Override
|
||
1323 | public Function acos(Value num) {
|
||
1324 | 44198 | jjdelcerro | return function(FUNCTION_ACOS, num);
|
1325 | 44051 | omartinez | } |
1326 | |||
1327 | @Override
|
||
1328 | public Function asin(Value num) {
|
||
1329 | 44198 | jjdelcerro | return function(FUNCTION_ASIN, num);
|
1330 | 44051 | omartinez | } |
1331 | |||
1332 | @Override
|
||
1333 | public Function atan(Value num) {
|
||
1334 | 44198 | jjdelcerro | return function(FUNCTION_ATAN, num);
|
1335 | 44051 | omartinez | } |
1336 | |||
1337 | @Override
|
||
1338 | public Function cos(Value num) {
|
||
1339 | 44198 | jjdelcerro | return function(FUNCTION_COS, num);
|
1340 | 44051 | omartinez | } |
1341 | |||
1342 | @Override
|
||
1343 | public Function cosh(Value num) {
|
||
1344 | 44198 | jjdelcerro | return function(FUNCTION_COSH, num);
|
1345 | 44051 | omartinez | } |
1346 | |||
1347 | @Override
|
||
1348 | public Function cot(Value num) {
|
||
1349 | 44198 | jjdelcerro | return function(FUNCTION_COT, num);
|
1350 | 44051 | omartinez | } |
1351 | |||
1352 | @Override
|
||
1353 | public Function bitand(Value num1, Value num2) {
|
||
1354 | 44198 | jjdelcerro | return function(FUNCTION_BITAND, num1, num2);
|
1355 | 44051 | omartinez | } |
1356 | |||
1357 | @Override
|
||
1358 | public Function bitor(Value num1, Value num2) {
|
||
1359 | 44198 | jjdelcerro | return function(FUNCTION_BITOR, num1, num2);
|
1360 | 44051 | omartinez | } |
1361 | |||
1362 | @Override
|
||
1363 | public Function bitxor(Value num1, Value num2) {
|
||
1364 | 44198 | jjdelcerro | return function(FUNCTION_BITXOR, num1, num2);
|
1365 | 44051 | omartinez | } |
1366 | |||
1367 | @Override
|
||
1368 | public Function ceil(Value num) {
|
||
1369 | 44198 | jjdelcerro | return function(FUNCTION_CEIL, num);
|
1370 | 44051 | omartinez | } |
1371 | |||
1372 | @Override
|
||
1373 | public Function degrees(Value num) {
|
||
1374 | 44198 | jjdelcerro | return function(FUNCTION_DEGREES, num);
|
1375 | 44051 | omartinez | } |
1376 | |||
1377 | @Override
|
||
1378 | public Function exp(Value num) {
|
||
1379 | 44198 | jjdelcerro | return function(FUNCTION_EXP, num);
|
1380 | 44051 | omartinez | } |
1381 | |||
1382 | @Override
|
||
1383 | public Function floor(Value num) {
|
||
1384 | 44198 | jjdelcerro | return function(FUNCTION_FLOOR, num);
|
1385 | 44051 | omartinez | } |
1386 | |||
1387 | @Override
|
||
1388 | public Function log(Value num) {
|
||
1389 | 44198 | jjdelcerro | return function(FUNCTION_LOG, num);
|
1390 | 44051 | omartinez | } |
1391 | |||
1392 | @Override
|
||
1393 | public Function log10(Value num) {
|
||
1394 | 44198 | jjdelcerro | return function(FUNCTION_LOG10, num);
|
1395 | 44051 | omartinez | } |
1396 | |||
1397 | @Override
|
||
1398 | 44644 | jjdelcerro | public Function pi() {
|
1399 | return function(FUNCTION_PI);
|
||
1400 | 44051 | omartinez | } |
1401 | |||
1402 | @Override
|
||
1403 | 44644 | jjdelcerro | public Function abs(Value num) {
|
1404 | return function(FUNCTION_ABS, num);
|
||
1405 | } |
||
1406 | |||
1407 | @Override
|
||
1408 | 44051 | omartinez | public Function power(Value num) {
|
1409 | 44198 | jjdelcerro | return function(FUNCTION_POWER, num);
|
1410 | 44051 | omartinez | } |
1411 | |||
1412 | @Override
|
||
1413 | public Function radians(Value num) {
|
||
1414 | 44198 | jjdelcerro | return function(FUNCTION_RADIANS, num);
|
1415 | 44051 | omartinez | } |
1416 | |||
1417 | @Override
|
||
1418 | public Function rand(Value num) {
|
||
1419 | 44198 | jjdelcerro | return function(FUNCTION_RAND, num);
|
1420 | 44051 | omartinez | } |
1421 | |||
1422 | @Override
|
||
1423 | public Function round(Value num) {
|
||
1424 | 44198 | jjdelcerro | return function(FUNCTION_ROUND, num);
|
1425 | 44051 | omartinez | } |
1426 | |||
1427 | @Override
|
||
1428 | public Function sqrt(Value num) {
|
||
1429 | 44198 | jjdelcerro | return function(FUNCTION_SQRT, num);
|
1430 | 44051 | omartinez | } |
1431 | |||
1432 | @Override
|
||
1433 | public Function sign(Value num) {
|
||
1434 | 44198 | jjdelcerro | return function(FUNCTION_SIGN, num);
|
1435 | 44051 | omartinez | } |
1436 | |||
1437 | @Override
|
||
1438 | public Function sin(Value num) {
|
||
1439 | 44198 | jjdelcerro | return function(FUNCTION_SIN, num);
|
1440 | 44051 | omartinez | } |
1441 | |||
1442 | @Override
|
||
1443 | public Function sinh(Value num) {
|
||
1444 | 44198 | jjdelcerro | return function(FUNCTION_SINH, num);
|
1445 | 44051 | omartinez | } |
1446 | |||
1447 | @Override
|
||
1448 | public Function tan(Value num) {
|
||
1449 | 44198 | jjdelcerro | return function(FUNCTION_TAN, num);
|
1450 | 44051 | omartinez | } |
1451 | @Override
|
||
1452 | public Function tanh(Value num) {
|
||
1453 | 44198 | jjdelcerro | return function(FUNCTION_TANH, num);
|
1454 | 44051 | omartinez | } |
1455 | |||
1456 | @Override
|
||
1457 | public Function zero() {
|
||
1458 | 44198 | jjdelcerro | return function(FUNCTION_ZERO);
|
1459 | 44051 | omartinez | } |
1460 | 44053 | omartinez | |
1461 | @Override
|
||
1462 | public Function chr(Value num) {
|
||
1463 | 44198 | jjdelcerro | return function(FUNCTION_CHR, num);
|
1464 | } |
||
1465 | |||
1466 | @Override
|
||
1467 | public Function cast(Value object, Value typeName) {
|
||
1468 | return function(FUNCTION_CAST, object, typeName);
|
||
1469 | } |
||
1470 | |||
1471 | @Override
|
||
1472 | public Function decode(Value value, Value format) {
|
||
1473 | return function(FUNCTION_DECODE, value, format);
|
||
1474 | } |
||
1475 | |||
1476 | @Override
|
||
1477 | public Function toDouble(Value num) {
|
||
1478 | return function(FUNCTION_TODOUBLE, num);
|
||
1479 | } |
||
1480 | |||
1481 | @Override
|
||
1482 | public Function toFloat(Value num) {
|
||
1483 | return function(FUNCTION_TOFLOAT, num);
|
||
1484 | } |
||
1485 | |||
1486 | @Override
|
||
1487 | public Function toLong(Value num) {
|
||
1488 | return function(FUNCTION_TOLONG, num);
|
||
1489 | } |
||
1490 | |||
1491 | @Override
|
||
1492 | public Function toInteger(Value num) {
|
||
1493 | return function(FUNCTION_TOINTEGER, num);
|
||
1494 | } |
||
1495 | |||
1496 | @Override
|
||
1497 | public Function toStr(Value object) {
|
||
1498 | return function(FUNCTION_TOSTR, object);
|
||
1499 | } |
||
1500 | |||
1501 | 44253 | jjdelcerro | @Override
|
1502 | 44262 | jjdelcerro | public Function list() {
|
1503 | return function(FUNCTION_LIST);
|
||
1504 | } |
||
1505 | 44376 | jjdelcerro | |
1506 | @Override
|
||
1507 | public Function tuple() {
|
||
1508 | return function(FUNCTION_TUPLE);
|
||
1509 | } |
||
1510 | |||
1511 | @Override
|
||
1512 | 44838 | jjdelcerro | public Function tuple(Object... values) { |
1513 | 44376 | jjdelcerro | Function fn = function(FUNCTION_TUPLE); |
1514 | 44838 | jjdelcerro | for (Object theValue : values) { |
1515 | if( theValue instanceof ExpressionBuilder.Value ) { |
||
1516 | fn.parameter((Value) theValue); |
||
1517 | } else {
|
||
1518 | fn.parameter(constant(theValue)); |
||
1519 | } |
||
1520 | 44376 | jjdelcerro | } |
1521 | return fn;
|
||
1522 | } |
||
1523 | |||
1524 | 44644 | jjdelcerro | @Override
|
1525 | public String repr(Object value) { |
||
1526 | return this.manager.getReprMethod(value).repr(value); |
||
1527 | } |
||
1528 | 44748 | jjdelcerro | |
1529 | @Override
|
||
1530 | 44750 | jjdelcerro | public Function getattr(String objectId, String attributeId) { |
1531 | 44748 | jjdelcerro | Function fn = function(FUNCTION_GETATTR); |
1532 | 44750 | jjdelcerro | fn.parameter(variable(objectId)); |
1533 | fn.parameter(variable(attributeId)); |
||
1534 | 44748 | jjdelcerro | return fn;
|
1535 | } |
||
1536 | |||
1537 | 44750 | jjdelcerro | @Override
|
1538 | 44763 | jjdelcerro | public Function date(Date date) { |
1539 | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); |
||
1540 | return function(FUNCTION_DATE, constant(df.format(date)));
|
||
1541 | } |
||
1542 | |||
1543 | @Override
|
||
1544 | public Function time(Date time) { |
||
1545 | SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); |
||
1546 | return function(FUNCTION_TIME, constant(df.format(time)));
|
||
1547 | } |
||
1548 | |||
1549 | @Override
|
||
1550 | public Function timestamp(Date timestamp) { |
||
1551 | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
||
1552 | return function(FUNCTION_TIMESTAMP, constant(df.format(timestamp)));
|
||
1553 | } |
||
1554 | |||
1555 | @Override
|
||
1556 | public Function date(String date) { |
||
1557 | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); |
||
1558 | Date d;
|
||
1559 | try {
|
||
1560 | d = df.parse(date); |
||
1561 | } catch (ParseException ex) { |
||
1562 | throw new IllegalArgumentException("Can't parse argument '"+date+"' as date.", ex); |
||
1563 | } |
||
1564 | return function(FUNCTION_DATE, constant(df.format(d)));
|
||
1565 | } |
||
1566 | |||
1567 | @Override
|
||
1568 | public Function time(String time) { |
||
1569 | SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); |
||
1570 | Date t;
|
||
1571 | try {
|
||
1572 | t = df.parse(time); |
||
1573 | } catch (ParseException ex) { |
||
1574 | throw new IllegalArgumentException("Can't parse argument '"+time+"' as date.", ex); |
||
1575 | } |
||
1576 | return function(FUNCTION_TIME, constant(df.format(t)));
|
||
1577 | } |
||
1578 | |||
1579 | @Override
|
||
1580 | public Function timestamp(String timestamp) { |
||
1581 | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
||
1582 | Date ts;
|
||
1583 | try {
|
||
1584 | ts = df.parse(timestamp); |
||
1585 | } catch (ParseException ex) { |
||
1586 | throw new IllegalArgumentException("Can't parse argument '"+timestamp+"' as date.", ex); |
||
1587 | } |
||
1588 | return function(FUNCTION_TIMESTAMP, constant(df.format(ts)));
|
||
1589 | } |
||
1590 | |||
1591 | @Override
|
||
1592 | 45523 | jjdelcerro | public Function color(Color color) { |
1593 | return function(FUNCTION_COLOR, constant(color.getRed()), constant(color.getGreen()), constant(color.getBlue()), constant(color.getAlpha()));
|
||
1594 | } |
||
1595 | |||
1596 | @Override
|
||
1597 | public Function color(Value red, Value green, Value blue, Value alfa) {
|
||
1598 | return function(FUNCTION_COLOR, red, green, blue, alfa);
|
||
1599 | } |
||
1600 | |||
1601 | @Override
|
||
1602 | 44750 | jjdelcerro | public Function date(Value date) {
|
1603 | return function(FUNCTION_DATE, date);
|
||
1604 | } |
||
1605 | |||
1606 | @Override
|
||
1607 | public Function time(Value time) {
|
||
1608 | return function(FUNCTION_TIME, time);
|
||
1609 | } |
||
1610 | |||
1611 | @Override
|
||
1612 | public Function timestamp(Value timestamp) {
|
||
1613 | return function(FUNCTION_TIMESTAMP, timestamp);
|
||
1614 | } |
||
1615 | |||
1616 | @Override
|
||
1617 | public Function current_date() {
|
||
1618 | return function(FUNCTION_CURRENT_DATE);
|
||
1619 | } |
||
1620 | |||
1621 | @Override
|
||
1622 | public Function current_time() {
|
||
1623 | return function(FUNCTION_CURRENT_TIME);
|
||
1624 | } |
||
1625 | |||
1626 | @Override
|
||
1627 | public Function current_timestamp() {
|
||
1628 | return function(FUNCTION_CURRENT_TIMESTAMP);
|
||
1629 | } |
||
1630 | |||
1631 | @Override
|
||
1632 | public Function date_add(Value datefield, Value valueToAdd, Value date) {
|
||
1633 | return function(FUNCTION_DATEADD, datefield, valueToAdd, date);
|
||
1634 | } |
||
1635 | 46104 | omartinez | |
1636 | @Override
|
||
1637 | public Function let(String name, Value value) { |
||
1638 | return function(FUNCTION_LET, this.variable(name), value); |
||
1639 | } |
||
1640 | 44750 | jjdelcerro | |
1641 | @Override
|
||
1642 | public Function date_diff(Value datefield, Value valueToSubst, Value date) {
|
||
1643 | return function(FUNCTION_DATEDIFF, datefield, valueToSubst, date);
|
||
1644 | } |
||
1645 | |||
1646 | @Override
|
||
1647 | public Function to_date(Value date, Value format) {
|
||
1648 | return function(FUNCTION_TO_DATE, date, format);
|
||
1649 | } |
||
1650 | |||
1651 | @Override
|
||
1652 | public Function to_timestamp(Value timestamp, Value format) {
|
||
1653 | return function(FUNCTION_TO_TIMESTAMP, timestamp, format);
|
||
1654 | } |
||
1655 | |||
1656 | @Override
|
||
1657 | public Function extract(Value datefield, Value source) {
|
||
1658 | return function(FUNCTION_EXTRACT, datefield, source);
|
||
1659 | } |
||
1660 | |||
1661 | 43020 | jjdelcerro | } |