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