Statistics
| Revision:

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 @ 46010

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