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

History | View | Annotate | Download (54.9 KB)

1 44006 jjdelcerro
package org.gvsig.expressionevaluator.impl;
2 43020 jjdelcerro
3
import java.text.MessageFormat;
4
import java.util.ArrayList;
5
import java.util.Collections;
6
import java.util.HashSet;
7
import java.util.List;
8
import java.util.Objects;
9
import java.util.Set;
10
import org.apache.commons.lang3.StringUtils;
11
import org.cresques.cts.IProjection;
12 44198 jjdelcerro
import org.gvsig.expressionevaluator.Code;
13 44274 jjdelcerro
import org.gvsig.expressionevaluator.Expression;
14 43020 jjdelcerro
import org.gvsig.fmap.geom.Geometry;
15 43034 jjdelcerro
import org.gvsig.fmap.geom.primitive.Envelope;
16 43020 jjdelcerro
17 44006 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder;
18 44198 jjdelcerro
import static org.gvsig.expressionevaluator.ExpressionBuilder.EMPTY_FORMATTER;
19
import org.gvsig.expressionevaluator.ExpressionUtils;
20
import org.gvsig.expressionevaluator.Formatter;
21
import org.gvsig.fmap.geom.GeometryUtils;
22 43093 jjdelcerro
23 44198 jjdelcerro
@SuppressWarnings({"UseSpecificCatch" ,"OverridableMethodCallInConstructor"})
24 44006 jjdelcerro
public class DefaultExpressionBuilder implements ExpressionBuilder {
25 44198 jjdelcerro
26
    private static final String FORMAT_QUOTE_FOR_STRINGS = "'";
27
    private static final String FORMAT_QUOTE_FOR_IDENTIFIERS = "\"";
28 44006 jjdelcerro
29 44198 jjdelcerro
    private static final String FORMAT_TRUE = "TRUE";
30
    private static final String FORMAT_FALSE = "FALSE";
31 43020 jjdelcerro
32 44198 jjdelcerro
    private static final String FORMAT_GROUP = "( {0} )";
33 43093 jjdelcerro
34 44198 jjdelcerro
    private static final String FORMAT_ST_SRID = "ST_SRID({0})";
35
    private static final String FORMAT_ST_ASTEXT = "ST_AsText({0})";
36
    private static final String FORMAT_ST_ASBINARY = "ST_AsBinary({0})";
37
    private static final String FORMAT_ST_ASEWKB = "ST_AsEWKB({0})";
38
    private static final String FORMAT_ST_CONTAINS = "ST_Contains(({0}), ({1}))";
39
    private static final String FORMAT_ST_CROSSES = "ST_Crosses(({0}), ({1}))";
40
    private static final String FORMAT_ST_DISJOINT = "ST_Disjoint(({0}), ({1}))";
41
    private static final String FORMAT_ST_EQUALS = "ST_Equals(({0}), ({1}))";
42
    private static final String FORMAT_ST_ISCLOSED = "ST_IsClosed({0})";
43
    private static final String FORMAT_ST_OVERLAPS = "ST_Overlaps(({0}), ({1}))";
44
    private static final String FORMAT_ST_TOUCHES = "ST_Touches(({0}), ({1}))";
45
    private static final String FORMAT_ST_WITHIN = "ST_Within(({0}), ({1}))";
46
    private static final String FORMAT_ST_ENVELOPE = "ST_Envelope({0})";
47
    private static final String FORMAT_ST_INTERSECTS = "ST_Intersects(({0}), ({1}))";
48
    private static final String FORMAT_ST_GEOMFROMTEXT = "ST_GeomFromText(({0}), ({1}))";
49
    private static final String FORMAT_ST_GEOMFROMWKB = "ST_GeomFromWKB(({0}), ({1}))";
50
    private static final String FORMAT_ST_GEOMFROMEWKB = "ST_GeomFromEWKB(({0}), ({1}))";
51
    private static final String FORMAT_ST_SIMPLIFY = "ST_Simplify(({0}), ({1}))";
52 43093 jjdelcerro
53 44198 jjdelcerro
    private static final String FORMAT_ISNULL = "( ({0}) IS NULL )";
54 44361 jjdelcerro
    private static final String FORMAT_NOTISNULL = "( ({0}) IS NOT NULL )";
55 44198 jjdelcerro
    private static final String FORMAT_OPERATOR_NOT = "( NOT ({0}) )";
56 43093 jjdelcerro
57 44262 jjdelcerro
    private static final String FORMAT_OPERATOR_AND = "({0} AND {1})";
58 44198 jjdelcerro
    private static final String FORMAT_OPERATOR_OR = "{0} OR {1}";
59
    private static final String FORMAT_OPERATOR_EQ = "( ({0}) = ({1}) )";
60
    private static final String FORMAT_OPERATOR_NE = "( ({0}) <> ({1}) )";
61
    private static final String FORMAT_OPERATOR_GT = "( ({0}) > ({1}) )";
62
    private static final String FORMAT_OPERATOR_GE = "( ({0}) >= ({1}) )";
63
    private static final String FORMAT_OPERATOR_LT = "( ({0}) < ({1}) )";
64
    private static final String FORMAT_OPERATOR_LE = "( ({0}) <= ({1}) )";
65
    private static final String FORMAT_OPERATOR_LIKE = "( ({0}) LIKE ({1}) )";
66
    private static final String FORMAT_OPERATOR_ILIKE = "( ({0}) ILIKE ({1}) )";
67
    private static final String FORMAT_OPERATOR_ADD = "{0} + {1}";
68
    private static final String FORMAT_OPERATOR_SUBST = "{0} - {1}";
69 44262 jjdelcerro
    private static final String FORMAT_OPERATOR_MULT = "({0} * {1})";
70
    private static final String FORMAT_OPERATOR_DIV = "({0} / {1})";
71 44198 jjdelcerro
    private static final String FORMAT_OPERATOR_CONCAT = "{0} || {1}";
72 43093 jjdelcerro
73 43020 jjdelcerro
    public class GroupBase extends AbstractValue implements Group {
74 43093 jjdelcerro
75 43020 jjdelcerro
        protected Value value;
76 43093 jjdelcerro
77 43020 jjdelcerro
        public GroupBase(Value value) {
78
            this.value = value;
79
        }
80
81
        @Override
82 44198 jjdelcerro
        public Value value() {
83 43020 jjdelcerro
            return value;
84
        }
85
86
        @Override
87
        public void accept(Visitor visitor, VisitorFilter filter) {
88 43093 jjdelcerro
            super.accept(visitor, filter);
89
            this.value.accept(visitor, filter);
90 43020 jjdelcerro
        }
91 43093 jjdelcerro
92 43020 jjdelcerro
        @Override
93 44376 jjdelcerro
        public void replace(Value target, Value replacement) {
94
            if( this.value == target ) {
95
                this.value = replacement;
96
            } else {
97
                this.value.replace(target, replacement);
98
            }
99
        }
100
101
        @Override
102 43020 jjdelcerro
        public String toString() {
103 44198 jjdelcerro
            return this.toString(EMPTY_FORMATTER);
104 43020 jjdelcerro
        }
105 44198 jjdelcerro
106
        @Override
107
        public String toString(Formatter<Value> formatter) {
108 44296 jjdelcerro
            if( formatter!=null && formatter.canApply(this) ) {
109 44198 jjdelcerro
                return formatter.format(this);
110
            }
111
            return MessageFormat.format(FORMAT_GROUP, this.value.toString());
112
        }
113 43020 jjdelcerro
    }
114
115
    public class VariableBase extends AbstractValue implements Variable {
116 43093 jjdelcerro
117 43020 jjdelcerro
        protected String name;
118 43093 jjdelcerro
119 43020 jjdelcerro
        public VariableBase(String name) {
120
            this.name = name;
121
        }
122 43093 jjdelcerro
123 43020 jjdelcerro
        @Override
124 44198 jjdelcerro
        public String name() {
125 43020 jjdelcerro
            return this.name;
126
        }
127
128
        @Override
129
        public String toString() {
130 44198 jjdelcerro
            return this.toString(EMPTY_FORMATTER);
131
        }
132
133
        @Override
134
        public String toString(Formatter<Value> formatter) {
135 44296 jjdelcerro
            if( formatter!=null && formatter.canApply(this) ) {
136 44198 jjdelcerro
                return formatter.format(this);
137
            }
138 43020 jjdelcerro
            return identifier(this.name);
139
        }
140
141
        @Override
142
        public int compareTo(Variable o) {
143 44198 jjdelcerro
            return this.name.compareTo(o.name());
144 43020 jjdelcerro
        }
145
146
        @Override
147
        public boolean equals(Object obj) {
148 43093 jjdelcerro
            if (!(obj instanceof Variable)) {
149 43020 jjdelcerro
                return false;
150
            }
151 44198 jjdelcerro
            return this.name.equals(((Variable) obj).name());
152 43020 jjdelcerro
        }
153
154
        @Override
155
        public int hashCode() {
156
            int hash = 7;
157
            hash = 37 * hash + Objects.hashCode(this.name);
158
            return hash;
159
        }
160
    }
161
162
    public class ParameterBase extends AbstractValue implements Parameter {
163 43093 jjdelcerro
164
        protected String name;
165 43020 jjdelcerro
        protected Object value;
166
        protected ParameterType type;
167
        protected Value srs;
168 43093 jjdelcerro
169 44198 jjdelcerro
        /*
170
        Para un parametre de tipo Geometria, el srs sera siempre un Constant
171
        excepto cuando se le asigne una geometria contante al parametro y esta
172
        tenga un SRS asignado. En este caso, tanto la geometria como el SRS
173
        se trataran como parametros.
174

175
        Si se quiere que el SRS sea un Parameter, se construira como tal y se
176
        asignara a traves del metodo srs(Value srs).
177
        */
178 43093 jjdelcerro
        public ParameterBase() {
179
            this.type = ParameterType.Constant;
180
            this.name = null;
181
            this.value = null;
182 43020 jjdelcerro
        }
183
184
        @Override
185
        public void accept(Visitor visitor, VisitorFilter filter) {
186
            super.accept(visitor, filter);
187 43093 jjdelcerro
            if (this.srs != null) {
188 43020 jjdelcerro
                this.srs.accept(visitor, filter);
189
            }
190
        }
191 43093 jjdelcerro
192 43020 jjdelcerro
        @Override
193 44376 jjdelcerro
        public void replace(Value target, Value replacement) {
194
            if( this.srs == target ) {
195
                this.srs = replacement;
196
            } else {
197
                this.srs.replace(target, replacement);
198
            }
199
        }
200
201
        @Override
202 43093 jjdelcerro
        public Parameter as_geometry_variable() {
203 43020 jjdelcerro
            this.type = ParameterType.Geometry;
204 43093 jjdelcerro
            if (this.value == null && this.name != null) {
205
                this.value = this.name;
206
            }
207 43020 jjdelcerro
            return this;
208
        }
209 43093 jjdelcerro
210 43020 jjdelcerro
        @Override
211
        public Parameter as_constant() {
212
            this.type = ParameterType.Constant;
213 43093 jjdelcerro
            if (this.value == null && this.name != null) {
214
                this.value = this.name;
215
            }
216 43020 jjdelcerro
            return this;
217
        }
218 43093 jjdelcerro
219 43020 jjdelcerro
        @Override
220
        public Parameter as_variable() {
221
            this.type = ParameterType.Variable;
222 43093 jjdelcerro
            if (this.value != null && this.name == null) {
223
                this.name = (String) this.value;
224
            }
225 43020 jjdelcerro
            return this;
226
        }
227 43093 jjdelcerro
228 43020 jjdelcerro
        @Override
229
        public Parameter srs(Value srs) {
230
            this.srs = srs;
231 43093 jjdelcerro
            if( this.type == ParameterType.Variable ) {
232
                this.type = ParameterType.Geometry;
233
            }
234 43020 jjdelcerro
            return this;
235
        }
236
237
        @Override
238
        public Parameter srs(IProjection srs) {
239 44198 jjdelcerro
            this.srs = constant(srs_id(srs));
240 43093 jjdelcerro
            if( this.type == ParameterType.Variable ) {
241
                this.type = ParameterType.Geometry;
242
            }
243 43020 jjdelcerro
            return this;
244
        }
245 43093 jjdelcerro
246 43020 jjdelcerro
        @Override
247 44198 jjdelcerro
        public String name() {
248 43093 jjdelcerro
            switch (this.type) {
249 43020 jjdelcerro
                case Variable:
250
                case Geometry:
251 43093 jjdelcerro
                    return this.name;
252 43020 jjdelcerro
                case Constant:
253 43093 jjdelcerro
                    if (this.value == null) {
254
                        return null;
255
                    }
256
                    return this.value.toString();
257 43020 jjdelcerro
                default:
258 43093 jjdelcerro
                    if (this.name != null) {
259
                        return this.name;
260
                    }
261
                    if (this.value != null) {
262
                        return this.value.toString();
263
                    }
264 43020 jjdelcerro
                    return null;
265
            }
266
        }
267
268
        @Override
269
        public boolean is_constant() {
270
            return this.type == ParameterType.Constant;
271
        }
272
273
        @Override
274
        public boolean is_geometry_variable() {
275
            return this.type == ParameterType.Geometry;
276
        }
277
278
        @Override
279
        public boolean is_variable() {
280
            return this.type == ParameterType.Variable;
281
        }
282
283
        @Override
284 43093 jjdelcerro
        public Parameter value(Object value) {
285 44198 jjdelcerro
            if( value instanceof Geometry ) {
286
                if( this.srs == null ) {
287
                    IProjection proj = ((Geometry)value).getProjection();
288
                    this.srs(parameter().value(proj));
289
                }
290
            }
291 43093 jjdelcerro
            this.value = value;
292
            return this;
293
        }
294
295
        @Override
296
        public Parameter name(String name) {
297
            this.type = ParameterType.Variable;
298
            this.name = name;
299
            return this;
300
        }
301
302
        @Override
303 44198 jjdelcerro
        public Object value() {
304 43020 jjdelcerro
            try {
305 43093 jjdelcerro
                switch (this.type) {
306 43020 jjdelcerro
                    case Constant:
307 44198 jjdelcerro
                        if( this.value instanceof Geometry ) {
308
                            Geometry geometry = (Geometry) this.value;
309
                            switch (geometry_support_type()) {
310 43020 jjdelcerro
                                case EWKB:
311 44198 jjdelcerro
                                    return GeometryUtils.toEWKB(geometry);
312 43020 jjdelcerro
                                case WKB:
313 44198 jjdelcerro
                                    return GeometryUtils.toWKB(geometry);
314 43020 jjdelcerro
                                case WKT:
315
                                default:
316 44198 jjdelcerro
                                    return GeometryUtils.toWKT(geometry);
317 43020 jjdelcerro
                            }
318 43093 jjdelcerro
                        } else if (this.value instanceof IProjection) {
319 44198 jjdelcerro
                            return srs_id((IProjection) this.value);
320 43093 jjdelcerro
                        }
321 43020 jjdelcerro
                        return this.value;
322
                    case Variable:
323
                    case Geometry:
324
                    default:
325 43093 jjdelcerro
                        return this.value;
326 43020 jjdelcerro
                }
327 43093 jjdelcerro
            } catch (Exception ex) {
328
                throw new RuntimeException("Can't get value from parameter.", ex);
329 43020 jjdelcerro
            }
330
        }
331
332
        @Override
333 44198 jjdelcerro
        public ParameterType type() {
334 43020 jjdelcerro
            return this.type;
335
        }
336 43093 jjdelcerro
337 43020 jjdelcerro
        @Override
338 44198 jjdelcerro
        public Value srs() {
339 43020 jjdelcerro
            return this.srs;
340
        }
341 43093 jjdelcerro
342 43020 jjdelcerro
        @Override
343
        public String toString() {
344 44198 jjdelcerro
            return this.toString(EMPTY_FORMATTER);
345
        }
346
347
        @Override
348
        public String toString(Formatter<Value> formatter) {
349 44296 jjdelcerro
            if( formatter!=null && formatter.canApply(this) ) {
350 44198 jjdelcerro
                return formatter.format(this);
351
            }
352 43093 jjdelcerro
            switch (this.type) {
353 43020 jjdelcerro
                case Constant:
354 44198 jjdelcerro
                    if( value instanceof Geometry ) {
355
                        switch (geometry_support_type()) {
356
                            case EWKB:
357
                                return MessageFormat.format(
358
                                    FORMAT_ST_GEOMFROMEWKB,
359
                                    "?",
360
                                    getSRS(formatter)
361
                                );
362
                            case WKB:
363
                                return MessageFormat.format(
364
                                    FORMAT_ST_GEOMFROMWKB,
365
                                    "?",
366
                                    getSRS(formatter)
367
                                );
368
                            case WKT:
369
                            default:
370
                                return MessageFormat.format(
371
                                    FORMAT_ST_GEOMFROMTEXT,
372
                                    "?",
373
                                    getSRS(formatter)
374
                                );
375
                        }
376
                    }
377 43020 jjdelcerro
                case Variable:
378
                default:
379
                    return "?";
380
                case Geometry:
381 44198 jjdelcerro
                    switch (geometry_support_type()) {
382 43020 jjdelcerro
                        case EWKB:
383
                            return MessageFormat.format(
384 44198 jjdelcerro
                                FORMAT_ST_GEOMFROMEWKB,
385
                                "?",
386
                                getSRS(formatter)
387 43020 jjdelcerro
                            );
388
                        case WKB:
389
                            return MessageFormat.format(
390 44198 jjdelcerro
                                FORMAT_ST_GEOMFROMWKB,
391
                                "?",
392
                                getSRS(formatter)
393 43020 jjdelcerro
                            );
394
                        case WKT:
395
                        default:
396
                            return MessageFormat.format(
397 44198 jjdelcerro
                                FORMAT_ST_GEOMFROMTEXT,
398
                                "?",
399
                                getSRS(formatter)
400 43093 jjdelcerro
                            );
401
                    }
402 43020 jjdelcerro
            }
403 43093 jjdelcerro
        }
404 44198 jjdelcerro
405
        private String getSRS(Formatter formatter) {
406
            if( this.srs!=null ) {
407
                return this.srs.toString(formatter);
408
            }
409
            if( this.value instanceof Geometry ) {
410
                IProjection proj = ((Geometry)this.value).getProjection();
411
                Object s = srs_id(proj);
412
                if( s == null ) {
413
                    throw new IllegalArgumentException("A parameter of type Geometry with an invalid SRS.");
414
                }
415
                return s.toString();
416
            }
417
            throw new IllegalArgumentException("The parameter of type Geometry need a SRS.");
418
        }
419 43020 jjdelcerro
    }
420
421
    public class ConstantBase extends AbstractValue implements Constant {
422 43093 jjdelcerro
423 43020 jjdelcerro
        protected Object value;
424 43093 jjdelcerro
425 43020 jjdelcerro
        public ConstantBase(Object value) {
426
            this.value = value;
427
        }
428 43093 jjdelcerro
429 43020 jjdelcerro
        @Override
430 44198 jjdelcerro
        public Object value() {
431 43020 jjdelcerro
            return this.value;
432
        }
433
434
        @Override
435
        public String toString() {
436 44198 jjdelcerro
            return this.toString(EMPTY_FORMATTER);
437
        }
438
439
        @Override
440
        public String toString(Formatter<Value> formatter) {
441 44296 jjdelcerro
            if( formatter!=null && formatter.canApply(this) ) {
442 44198 jjdelcerro
                return formatter.format(this);
443
            }
444
            if( this.value==null ) {
445
                return "NULL";
446
            }
447
            if( this.value instanceof byte[] ) {
448
                return "DECODE('"+bytearray_hex((byte[])this.value)+"','hex')";
449
            }
450 43093 jjdelcerro
            if (this.value instanceof String) {
451 43020 jjdelcerro
                return string((String) this.value);
452
            }
453 44198 jjdelcerro
            if( this.value instanceof Geometry ) {
454
                Geometry geometry = (Geometry) this.value;
455
                switch (geometry_support_type()) {
456
                    case EWKB:
457
                        return MessageFormat.format(
458
                                FORMAT_ST_GEOMFROMEWKB,
459
                                "DECODE('"+bytearray_hex(GeometryUtils.toEWKB(geometry))+"','hex')",
460
                                String.valueOf(srs_id(geometry.getProjection()))
461
                        );
462
                    case WKB:
463
                        return MessageFormat.format(
464
                                FORMAT_ST_GEOMFROMWKB,
465
                                "DECODE('"+bytearray_hex(GeometryUtils.toWKB(geometry))+"','hex')",
466
                                String.valueOf(srs_id(geometry.getProjection()))
467
                        );
468
                    case WKT:
469
                    default:
470
                        return MessageFormat.format(
471
                                FORMAT_ST_GEOMFROMTEXT,
472
                                string(GeometryUtils.toWKT(geometry)),
473
                                String.valueOf(srs_id(geometry.getProjection()))
474
                        );
475
                }
476
            }
477
            if( this.value instanceof IProjection ) {
478
                return Objects.toString(srs_id((IProjection)(this.value)));
479
            }
480 43093 jjdelcerro
            if (this.value instanceof Boolean) {
481
                if (((Boolean) this.value)) {
482 44198 jjdelcerro
                    return FORMAT_TRUE;
483 43020 jjdelcerro
                } else {
484 44198 jjdelcerro
                    return FORMAT_FALSE;
485 43020 jjdelcerro
                }
486
            }
487 44198 jjdelcerro
            return Objects.toString(this.value, "");
488 43020 jjdelcerro
        }
489
    }
490
491
    public class CustomBase extends AbstractValue implements Custom {
492 43093 jjdelcerro
493 43020 jjdelcerro
        protected Object value;
494 43093 jjdelcerro
495 43020 jjdelcerro
        // Esto es para permitir declarar parametros y columnas en una seccion
496
        // custom.
497
        protected List<Value> values;
498 43093 jjdelcerro
499 43020 jjdelcerro
        public CustomBase(Object value) {
500
            this.value = value;
501
        }
502
503
        @Override
504
        public void accept(Visitor visitor, VisitorFilter filter) {
505
            super.accept(visitor, filter);
506 43093 jjdelcerro
            if (this.values != null) {
507 44006 jjdelcerro
                for (Value theValue : values) {
508
                    theValue.accept(visitor, filter);
509 43020 jjdelcerro
                }
510
            }
511
        }
512 43093 jjdelcerro
513 43020 jjdelcerro
        @Override
514 44376 jjdelcerro
        public void replace(Value target, Value replacement) {
515
            if( this.values == null ) {
516
                return;
517
            }
518
            for (int i = 0; i < values.size(); i++) {
519
                Value theValue = values.get(i);
520
                if( target == theValue ) {
521
                    values.set(i, replacement);
522
                } else {
523
                    theValue.replace(target, replacement);
524
                }
525
            }
526
        }
527
528
        @Override
529 44198 jjdelcerro
        public Object value() {
530 43020 jjdelcerro
            return this.value;
531
        }
532
533
        @Override
534
        public Custom add(Variable variable) {
535 43093 jjdelcerro
            if (this.values == null) {
536 43020 jjdelcerro
                this.values = new ArrayList<>();
537
            }
538
            this.values.add(variable);
539
            return this;
540
        }
541
542
        @Override
543
        public Custom add(Parameter parameter) {
544 43093 jjdelcerro
            if (this.values == null) {
545 43020 jjdelcerro
                this.values = new ArrayList<>();
546
            }
547
            this.values.add(parameter);
548
            return this;
549
        }
550
551
        @Override
552
        public String toString() {
553 44198 jjdelcerro
            return this.toString(EMPTY_FORMATTER);
554 43020 jjdelcerro
        }
555 44198 jjdelcerro
556 43020 jjdelcerro
        @Override
557 44198 jjdelcerro
        public String toString(Formatter<Value> formatter) {
558 44296 jjdelcerro
            if( formatter!=null && formatter.canApply(this) ) {
559 44198 jjdelcerro
                return formatter.format(this);
560 43020 jjdelcerro
            }
561 44198 jjdelcerro
            return Objects.toString(this.value, "");
562 43020 jjdelcerro
        }
563 43093 jjdelcerro
    }
564 43020 jjdelcerro
565
    public class FunctionBase extends AbstractValue implements Function {
566
567
        protected String name;
568
        protected String format;
569
        protected List<Value> parameters;
570 43093 jjdelcerro
571 43020 jjdelcerro
        public FunctionBase(String name, String format) {
572
            this.name = name;
573
            this.format = format;
574
        }
575 43093 jjdelcerro
576 44020 jjdelcerro
        public FunctionBase(String name) {
577
            this(name,null);
578
        }
579
580 43020 jjdelcerro
        @Override
581
        public List<Value> parameters() {
582 43093 jjdelcerro
            if (this.parameters == null) {
583 43020 jjdelcerro
                this.parameters = new ArrayList<>();
584
            }
585
            return this.parameters;
586 43093 jjdelcerro
        }
587 43020 jjdelcerro
588
        @Override
589
        public Function parameter(Value parameter) {
590
            this.parameters().add(parameter);
591
            return this;
592
        }
593
594
        @Override
595 44198 jjdelcerro
        public String name() {
596 43020 jjdelcerro
            return this.name;
597
        }
598
599
        @Override
600
        public void accept(Visitor visitor, VisitorFilter filter) {
601 43093 jjdelcerro
            super.accept(visitor, filter);
602 43020 jjdelcerro
            for (Value value : this.parameters) {
603 43093 jjdelcerro
                value.accept(visitor, filter);
604 43020 jjdelcerro
            }
605
        }
606
607
        @Override
608 44376 jjdelcerro
        public void replace(Value target, Value replacement) {
609
            for (int i = 0; i < parameters.size(); i++) {
610
                Value value = parameters.get(i);
611
                if( value == target ) {
612
                    parameters.set(i, replacement);
613
                } else {
614
                    value.replace(target, replacement);
615
                }
616
            }
617
        }
618
619
        @Override
620 43020 jjdelcerro
        public String toString() {
621 44198 jjdelcerro
            return this.toString(EMPTY_FORMATTER);
622
        }
623
624
        @Override
625
        public String toString(Formatter<Value> formatter) {
626 44296 jjdelcerro
            if( formatter!=null && formatter.canApply(this) ) {
627 44198 jjdelcerro
                return formatter.format(this);
628
            }
629 44020 jjdelcerro
            if( this.format==null ) {
630
                StringBuilder builder = new StringBuilder();
631
                builder.append(name);
632
                builder.append("(");
633
                if (this.parameters != null && !this.parameters.isEmpty()) {
634
                    boolean first = true;
635
                    for (Value value : this.parameters) {
636
                        if( first ) {
637
                            first=false;
638 44198 jjdelcerro
                            builder.append(value.toString(formatter));
639 44020 jjdelcerro
                        } else {
640
                            builder.append(", ");
641 44198 jjdelcerro
                            builder.append(value.toString(formatter));
642 44020 jjdelcerro
                        }
643
                    }
644
                }
645
                builder.append(")");
646
                return builder.toString();
647
            }
648 43093 jjdelcerro
            if (this.parameters != null && !this.parameters.isEmpty()) {
649 43020 jjdelcerro
                List<String> values = new ArrayList<>();
650
                for (Value value : this.parameters) {
651 44198 jjdelcerro
                    values.add(value.toString(formatter));
652 43020 jjdelcerro
                }
653
                return MessageFormat.format(format, values.toArray());
654
            } else {
655
                return this.format;
656
            }
657
        }
658
    }
659 43093 jjdelcerro
660 44198 jjdelcerro
    public class MethodBase extends FunctionBase implements Method {
661
662 44376 jjdelcerro
        private Value instance;
663 44198 jjdelcerro
664
        public MethodBase(Value instance, String name) {
665
            super(name);
666
            this.instance = instance;
667
        }
668
669
        @Override
670
        public Value instance() {
671
            return this.instance;
672
        }
673
674
        @Override
675
        public void accept(Visitor visitor, VisitorFilter filter) {
676
            this.instance.accept(visitor, filter);
677
            super.accept(visitor, filter);
678
        }
679 44376 jjdelcerro
680
        @Override
681
        public void replace(Value target, Value replacement) {
682
            if( this.instance == target ) {
683
                this.instance = replacement;
684
            } else {
685
                this.instance.replace(target, replacement);
686
            }
687
        }
688 44198 jjdelcerro
689
        @Override
690
        public String toString(Formatter<Value> formatter) {
691 44296 jjdelcerro
            if( formatter!=null && formatter.canApply(this) ) {
692 44198 jjdelcerro
                return formatter.format(this);
693
            }
694
            StringBuilder builder = new StringBuilder();
695
            builder.append(this.instance.toString(formatter));
696
            builder.append("->");
697
            builder.append(this.name());
698
            builder.append(name);
699
            builder.append("(");
700
            if (this.parameters != null && !this.parameters.isEmpty()) {
701
                boolean first = true;
702
                for (Value value : this.parameters) {
703
                    if( first ) {
704
                        first=false;
705
                        builder.append(value.toString(formatter));
706
                    } else {
707
                        builder.append(", ");
708
                        builder.append(value.toString(formatter));
709
                    }
710
                }
711
            }
712
            builder.append(")");
713
            return builder.toString();
714
        }
715
    }
716
717 43020 jjdelcerro
    public class BinaryOperatorBase extends AbstractValue implements BinaryOperator {
718
719
        protected String name;
720
        protected String format;
721
        protected Value left;
722
        protected Value right;
723 43093 jjdelcerro
724 43020 jjdelcerro
        public BinaryOperatorBase(String name, String format) {
725
            this.name = name;
726
            this.format = format;
727
        }
728 43093 jjdelcerro
729 43020 jjdelcerro
        @Override
730 44198 jjdelcerro
        public String name() {
731 43020 jjdelcerro
            return this.name;
732
        }
733
734
        @Override
735 44376 jjdelcerro
        public void replace(Value target, Value replacement) {
736
            if( target == this.left ) {
737
                this.left = replacement;
738
            } else {
739
                this.left.replace(target, replacement);
740
            }
741
            if( target == this.right ) {
742
                this.right = replacement;
743
            } else {
744
                this.right.replace(target, replacement);
745
            }
746
        }
747
748
        @Override
749 43020 jjdelcerro
        public void accept(Visitor visitor, VisitorFilter filter) {
750 43093 jjdelcerro
            super.accept(visitor, filter);
751 43020 jjdelcerro
            this.left.accept(visitor, filter);
752
            this.right.accept(visitor, filter);
753
        }
754
755
        @Override
756 44198 jjdelcerro
        public BinaryOperator left(Value operand) {
757 43020 jjdelcerro
            this.left = operand;
758
            return this;
759
        }
760
761
        @Override
762 44198 jjdelcerro
        public BinaryOperator right(Value operand) {
763 43020 jjdelcerro
            this.right = operand;
764
            return this;
765
        }
766
767
        @Override
768 44198 jjdelcerro
        public Value left() {
769 43020 jjdelcerro
            return this.left;
770
        }
771
772
        @Override
773 44198 jjdelcerro
        public Value right() {
774 43020 jjdelcerro
            return this.right;
775
        }
776 43093 jjdelcerro
777 43020 jjdelcerro
        @Override
778
        public String toString() {
779 44198 jjdelcerro
            return this.toString(EMPTY_FORMATTER);
780 43020 jjdelcerro
        }
781 44198 jjdelcerro
782
        @Override
783
        public String toString(Formatter<Value> formatter) {
784 44296 jjdelcerro
            if( formatter!=null && formatter.canApply(this) ) {
785 44198 jjdelcerro
                return formatter.format(this);
786
            }
787
            if( this.format==null ) {
788
                StringBuilder builder = new StringBuilder();
789
                builder.append("(");
790
                builder.append(this.left.toString(formatter));
791
                builder.append(" ");
792
                builder.append(this.name);
793
                builder.append(" ");
794
                builder.append(this.right.toString(formatter));
795
                builder.append(")");
796
                return builder.toString();
797
            } else {
798
                return MessageFormat.format(
799
                        format,
800
                        this.left.toString(formatter),
801
                        this.right.toString(formatter)
802
                );
803
            }
804
        }
805 43020 jjdelcerro
    }
806
807 44198 jjdelcerro
    protected GeometrySupportType geometrySupportType;
808 43020 jjdelcerro
    protected Value value;
809 43093 jjdelcerro
810 44006 jjdelcerro
    public DefaultExpressionBuilder() {
811 44198 jjdelcerro
        this.geometrySupportType = GeometrySupportType.WKB;
812 43020 jjdelcerro
    }
813
814
    @Override
815 44259 jjdelcerro
    public boolean isEmpty() {
816
        return value == null;
817
    }
818
819
    @Override
820 43020 jjdelcerro
    public ExpressionBuilder createExpressionBuilder() {
821 44006 jjdelcerro
        return new DefaultExpressionBuilder();
822 43020 jjdelcerro
    }
823 43093 jjdelcerro
824 43020 jjdelcerro
    @Override
825 44198 jjdelcerro
    public GeometrySupportType geometry_support_type() {
826
        return this.geometrySupportType;
827 43093 jjdelcerro
    }
828
829 43020 jjdelcerro
    @Override
830 44198 jjdelcerro
    public ExpressionBuilder geometry_support_type(GeometrySupportType geometrySupportType) {
831
        this.geometrySupportType = geometrySupportType;
832
        return this;
833
    }
834
835
    @Override
836
    public Value value() {
837 43020 jjdelcerro
        return this.value;
838
    }
839 43093 jjdelcerro
840 43020 jjdelcerro
    @Override
841 44198 jjdelcerro
    public ExpressionBuilder value(Value value) {
842 43020 jjdelcerro
        this.value = value;
843
        return this;
844
    }
845
846
    @Override
847
    public String toString() {
848
        return this.value.toString();
849
    }
850
851
    @Override
852 44198 jjdelcerro
    public String toString(Formatter<Value> formatter) {
853
        return this.value.toString(formatter);
854
    }
855
856
    @Override
857
    public Value toValue(String expression) {
858
        try {
859
            Code code = ExpressionUtils.compile(expression);
860
            return code.toValue(this);
861
        } catch(Throwable ex) {
862
            return custom(expression);
863
        }
864
    }
865
866
    @Override
867 43020 jjdelcerro
    public void accept(Visitor visitor, VisitorFilter filter) {
868 43093 jjdelcerro
        if( this.value == null) {
869
            return;
870
        }
871 43020 jjdelcerro
        this.value.accept(visitor, filter);
872
    }
873 43093 jjdelcerro
874 43020 jjdelcerro
    @Override
875 44198 jjdelcerro
    public String quote_for_identifiers() {
876
        return FORMAT_QUOTE_FOR_IDENTIFIERS;
877 43020 jjdelcerro
    }
878 44198 jjdelcerro
879 43020 jjdelcerro
    @Override
880 44198 jjdelcerro
    public String quote_for_strings() {
881
        return FORMAT_QUOTE_FOR_STRINGS;
882 43020 jjdelcerro
    }
883 44198 jjdelcerro
884 43020 jjdelcerro
    @Override
885
    public String string(String s) {
886 44198 jjdelcerro
        String quote = this.quote_for_strings();
887 43034 jjdelcerro
//        No se porque no esta disponible wrapIfMissing
888
//        return StringUtils.wrapIfMissing(s,quote);
889 43020 jjdelcerro
        if (s.startsWith(quote)) {
890
            return s;
891
        }
892 44198 jjdelcerro
        return quote + StringUtils.replace(s,quote,quote+quote) + quote;
893 43020 jjdelcerro
    }
894 43093 jjdelcerro
895 43020 jjdelcerro
    @Override
896
    public String identifier(String id) {
897 44198 jjdelcerro
        String quote = this.quote_for_identifiers();
898 43034 jjdelcerro
//        No se porque no esta disponible wrapIfMissing
899
//        return StringUtils.wrapIfMissing(id,quote);
900 43020 jjdelcerro
        if (id.startsWith(quote)) {
901
            return id;
902
        }
903
        return quote + id + quote;
904
    }
905
906
    @Override
907 44006 jjdelcerro
    public String bytearray_hex(byte[] data) {
908 43020 jjdelcerro
        StringBuilder builder = new StringBuilder();
909
        for (byte abyte : data) {
910
            int v = abyte & 0xff;
911
            builder.append(String.format("%02x", v));
912
        }
913
        return builder.toString();
914
    }
915
916 44006 jjdelcerro
    @Override
917
    public String bytearray_0x(byte[] data) {
918 43355 jjdelcerro
        return "0x" + bytearray_hex(data);
919
    }
920
921 44006 jjdelcerro
    @Override
922
    public String bytearray_x(byte[] data) {
923 43355 jjdelcerro
        return "x'" + bytearray_hex(data) + "'";
924 43302 jjdelcerro
    }
925
926 43020 jjdelcerro
    @Override
927 44198 jjdelcerro
    public Object srs_id(IProjection projection) {
928 44006 jjdelcerro
        if( projection==null ) {
929
            return 0;
930
        }
931
        return ProjectionUtils.getCode(projection);
932 43020 jjdelcerro
    }
933 43093 jjdelcerro
934 43020 jjdelcerro
    @Override
935 44198 jjdelcerro
    public Constant bytearray(byte[] data) {
936
        return new ConstantBase(data);
937
    }
938
939
    @Override
940 43020 jjdelcerro
    public Constant srs(IProjection projection) {
941 44198 jjdelcerro
        return constant(projection);
942 43020 jjdelcerro
    }
943
944
    @Override
945
    public Variable variable(String name) {
946
        return new VariableBase(name);
947
    }
948
949
    @Override
950
    public Variable column(String name) {
951
        return new VariableBase(name);
952
    }
953 43739 jjdelcerro
954 43020 jjdelcerro
    @Override
955 43093 jjdelcerro
    public Parameter parameter(String name) {
956 44198 jjdelcerro
        List<Parameter> parameters = this.parameters();
957
        for (Parameter parameter : parameters) {
958
            if( StringUtils.equalsIgnoreCase(name, parameter.name()) ) {
959
                return parameter;
960
            }
961 43093 jjdelcerro
        }
962 44198 jjdelcerro
        Parameter parameter = this.parameter();
963 43093 jjdelcerro
        parameter.name(name);
964
        return parameter;
965 43020 jjdelcerro
    }
966 43093 jjdelcerro
967 43687 jjdelcerro
    @Override
968 43093 jjdelcerro
    public Parameter parameter() {
969
        return new ParameterBase();
970
    }
971
972 43020 jjdelcerro
    @Override
973
    public Constant constant(Object value) {
974
        return new ConstantBase(value);
975
    }
976
977
    @Override
978
    public Group group(Value value) {
979
        return new GroupBase(value);
980
    }
981 43093 jjdelcerro
982 43020 jjdelcerro
    @Override
983 44198 jjdelcerro
    public Constant geometry(Geometry geom, IProjection projection) {
984
        geom.setProjection(projection);
985
        return new ConstantBase(geom);
986 43020 jjdelcerro
    }
987
988
    @Override
989 44198 jjdelcerro
    public Constant geometry(Geometry geom) {
990 44006 jjdelcerro
        if( geom.getProjection()==null ) {
991
            throw new IllegalArgumentException("The geometry does not have an associated projection. Use 'geometry(Geometry, IProjection)'.");
992
        }
993 44198 jjdelcerro
        return new ConstantBase(geom);
994 44006 jjdelcerro
    }
995
996
    @Override
997 44198 jjdelcerro
    public Constant envelope(Envelope envelope, IProjection projection) {
998
        Geometry geom = envelope.getGeometry();
999
        geom.setProjection(projection);
1000
        return new ConstantBase(geom);
1001 43034 jjdelcerro
    }
1002
1003
    @Override
1004 44198 jjdelcerro
    public Constant envelope(Envelope envelope) {
1005 44006 jjdelcerro
        if( envelope.getProjection()==null ) {
1006
            throw new IllegalArgumentException("The envelope does not have an associated projection. Use 'envelope(Geometry, IProjection)'.");
1007
        }
1008 44198 jjdelcerro
        Geometry geom = envelope.getGeometry();
1009
        return new ConstantBase(geom);
1010 44006 jjdelcerro
    }
1011
1012
    @Override
1013 43020 jjdelcerro
    public Custom custom(Object value) {
1014
        return new CustomBase(value);
1015
    }
1016
1017 44198 jjdelcerro
    @Override
1018
    public Method method(Value instance, String name, Value... values) {
1019
        MethodBase method = new MethodBase(instance, name);
1020
        for (Value theValue : values) {
1021
            method.parameter(theValue);
1022
        }
1023
        return method;
1024
    }
1025
1026
    @Override
1027 44020 jjdelcerro
    public Function function(String name, Value... values) {
1028
        FunctionBase func = new FunctionBase(name);
1029
        for (Value theValue : values) {
1030
            func.parameter(theValue);
1031
        }
1032
        return func;
1033
    }
1034
1035
    public Function builtin_function(String name, String format, Value... values) {
1036 43093 jjdelcerro
        FunctionBase func = new FunctionBase(name, format);
1037 44006 jjdelcerro
        for (Value theValue : values) {
1038
            func.parameter(theValue);
1039 43020 jjdelcerro
        }
1040
        return func;
1041
    }
1042 43093 jjdelcerro
1043 44198 jjdelcerro
    @Override
1044
    public BinaryOperator binaryOperator(String name, Value leftOperand, Value rightOperand) {
1045
        return binaryOperator(name, null, leftOperand, rightOperand);
1046
    }
1047
1048 43020 jjdelcerro
    public BinaryOperator binaryOperator(String name, String format, Value leftOperand, Value rightOperand) {
1049 43093 jjdelcerro
        BinaryOperator operator = new BinaryOperatorBase(name, format);
1050 44198 jjdelcerro
        operator.left(leftOperand);
1051
        operator.right(rightOperand);
1052 43020 jjdelcerro
        return operator;
1053
    }
1054 43093 jjdelcerro
1055 43020 jjdelcerro
    @Override
1056 44198 jjdelcerro
    public List<Variable> variables() {
1057 43020 jjdelcerro
        final Set<Variable> vars = new HashSet<>();
1058
        this.accept(new Visitor() {
1059
            @Override
1060
            public void visit(Visitable value) {
1061 44198 jjdelcerro
                if( !vars.contains((Variable)value) ) {
1062
                    vars.add((Variable)value);
1063
                }
1064 43020 jjdelcerro
            }
1065 43093 jjdelcerro
        }, new ClassVisitorFilter(Variable.class));
1066 43020 jjdelcerro
        List<Variable> lvars = new ArrayList<>(vars);
1067
        Collections.sort(lvars);
1068
        return lvars;
1069
    }
1070 43093 jjdelcerro
1071 43020 jjdelcerro
    @Override
1072 44198 jjdelcerro
    public List<Parameter> parameters() {
1073
        final List<Parameter>  params = new ArrayList<>();
1074 43020 jjdelcerro
        this.accept(new Visitor() {
1075
            @Override
1076
            public void visit(Visitable value) {
1077 43093 jjdelcerro
                params.add((Parameter) value);
1078 43020 jjdelcerro
            }
1079 43093 jjdelcerro
        }, new ClassVisitorFilter(Parameter.class));
1080 43020 jjdelcerro
        return params;
1081
    }
1082 44198 jjdelcerro
1083 43020 jjdelcerro
    @Override
1084 44198 jjdelcerro
    public List<String> parameters_names() {
1085
        List<String> params = new ArrayList<>();
1086
        for (Parameter param : parameters()) {
1087
            Object theValue = param.value();
1088
            String s;
1089
            switch(param.type()) {
1090
                case Constant:
1091
                    if( theValue==null ) {
1092
                        s = "NULL";
1093
                    } else if( theValue instanceof String ) {
1094
                        s = "'" + (String)theValue + "'";
1095
1096
                    } else if( theValue instanceof byte[] ) {
1097
                        s = bytearray_0x((byte[]) theValue);
1098
1099
                    } else {
1100
                        s = theValue.toString();
1101
                    }
1102
                    break;
1103
                case Geometry:
1104
                    if( param.name()==null ) {
1105
                        s = bytearray_0x((byte[]) theValue);
1106
                    } else {
1107
                        s = "\"" + param.name() + "\"";
1108
                    }
1109
                    break;
1110
                case Variable:
1111
                default:
1112
                    s = "\"" + param.name() + "\"";
1113
            }
1114
//            if( !params.contains(s) ) { // Ojo que deben ir todos, incluso duplicados.
1115
                params.add(s);
1116
//            }
1117
        }
1118
        // Collections.sort(params); Ojo, no deben ordenarse.
1119
        return params;
1120 43020 jjdelcerro
    }
1121 44198 jjdelcerro
1122
    @Override
1123
    public List<String> variables_names() {
1124
        List<String> vars = new ArrayList<>();
1125
        for (Variable var : this.variables()) {
1126
            vars.add(var.name());
1127
        }
1128
        Collections.sort(vars);
1129
        return vars;
1130
    }
1131
1132
    @Override
1133
    public Function as_geometry(Value value) {
1134
        return builtin_function(FUNCTION_ST_ASBINARY, FORMAT_ST_ASBINARY, value);
1135
    }
1136 43020 jjdelcerro
1137
    @Override
1138
    public ExpressionBuilder set(Value value) {
1139
        this.value = value;
1140
        return this;
1141
    }
1142
1143
    @Override
1144
    public ExpressionBuilder and(Value value) {
1145 43093 jjdelcerro
        if (this.value == null) {
1146 43020 jjdelcerro
            return this.set(value);
1147
        }
1148 44198 jjdelcerro
        BinaryOperator operator = binaryOperator(OPERATOR_AND, FORMAT_OPERATOR_AND, this.value, value);
1149 43020 jjdelcerro
        this.value = operator;
1150
        return this;
1151
    }
1152
1153
    @Override
1154
    public ExpressionBuilder or(Value value) {
1155 43093 jjdelcerro
        if (this.value == null) {
1156 43020 jjdelcerro
            return this.set(value);
1157
        }
1158 44198 jjdelcerro
        BinaryOperator operator = binaryOperator(OPERATOR_OR, FORMAT_OPERATOR_OR, this.value, value);
1159 43020 jjdelcerro
        this.value = operator;
1160
        return this;
1161
    }
1162
1163
    @Override
1164
    public Function ST_Intersects(Value geom1, Value geom2) {
1165 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_INTERSECTS, FORMAT_ST_INTERSECTS, geom1, geom2);
1166 43020 jjdelcerro
    }
1167
1168
    @Override
1169
    public Function ST_SRID(Value geom) {
1170 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_SRID, FORMAT_ST_SRID, geom);
1171 43020 jjdelcerro
    }
1172
1173
    @Override
1174
    public Function ST_Envelope(Value geom) {
1175 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_ENVELOPE, FORMAT_ST_ENVELOPE, geom);
1176 43020 jjdelcerro
    }
1177
1178
    @Override
1179
    public Function ST_AsText(Value geom) {
1180 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_ASTEXT, FORMAT_ST_ASTEXT, geom);
1181 43020 jjdelcerro
    }
1182
1183
    @Override
1184
    public Function ST_AsBinary(Value geom) {
1185 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_ASBINARY, FORMAT_ST_ASBINARY, geom);
1186 43020 jjdelcerro
    }
1187
1188
    @Override
1189
    public Function ST_AsEWKB(Value geom) {
1190 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_ASEWKB, FORMAT_ST_ASEWKB, geom);
1191 43020 jjdelcerro
    }
1192
1193
    @Override
1194
    public Function ST_GeomFromText(Value geom, Value crs) {
1195 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_GEOMFROMTEXT, FORMAT_ST_GEOMFROMTEXT, geom, crs);
1196 43020 jjdelcerro
    }
1197
1198
    @Override
1199
    public Function ST_GeomFromWKB(Value geom, Value crs) {
1200 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_GEOMFROMWKB, FORMAT_ST_GEOMFROMWKB, geom, crs);
1201 43020 jjdelcerro
    }
1202
1203
    @Override
1204
    public Function ST_GeomFromEWKB(Value geom, Value crs) {
1205 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_GEOMFROMEWKB, FORMAT_ST_GEOMFROMEWKB, geom, crs);
1206 43020 jjdelcerro
    }
1207
1208
    @Override
1209 43355 jjdelcerro
    public Function ST_Simplify(Value geom, Value tolerance) {
1210 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_SIMPLIFY, FORMAT_ST_SIMPLIFY, tolerance);
1211 43355 jjdelcerro
    }
1212
1213
    @Override
1214 43034 jjdelcerro
    public Function ST_Disjoint(Value geom1, Value geom2) {
1215 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_DISJOINT, FORMAT_ST_DISJOINT, geom1, geom2);
1216 43034 jjdelcerro
    }
1217 43093 jjdelcerro
1218 43034 jjdelcerro
    @Override
1219 43020 jjdelcerro
    public Function ST_Contains(Value geom1, Value geom2) {
1220 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_CONTAINS, FORMAT_ST_CONTAINS, geom1, geom2);
1221 43020 jjdelcerro
    }
1222
1223
    @Override
1224 43034 jjdelcerro
    public Function ST_Equals(Value geom1, Value geom2) {
1225 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_EQUALS, FORMAT_ST_EQUALS, geom1, geom2);
1226 43034 jjdelcerro
    }
1227
1228
    @Override
1229 43020 jjdelcerro
    public Function ST_Crosses(Value geom1, Value geom2) {
1230 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_CROSSES, FORMAT_ST_CROSSES, geom1, geom2);
1231 43020 jjdelcerro
    }
1232
1233
    @Override
1234
    public Function ST_IsClosed(Value geom) {
1235 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_ISCLOSED, FORMAT_ST_ISCLOSED, geom);
1236 43020 jjdelcerro
    }
1237
1238
    @Override
1239
    public Function ST_Overlaps(Value geom1, Value geom2) {
1240 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_OVERLAPS, FORMAT_ST_OVERLAPS, geom1, geom2);
1241 43020 jjdelcerro
    }
1242
1243
    @Override
1244
    public Function ST_Touches(Value geom1, Value geom2) {
1245 44198 jjdelcerro
        return builtin_function(FUNCTION_ST_TOUCHES, FORMAT_ST_TOUCHES, geom1, geom2);
1246 43020 jjdelcerro
    }
1247
1248
    @Override
1249
    public Function ST_Within(Value geom1, Value geom2) {
1250 44198 jjdelcerro
        return builtin_function("ST_Within", FORMAT_ST_WITHIN, geom1, geom2);
1251 43020 jjdelcerro
    }
1252
1253
    @Override
1254 44364 jjdelcerro
    public Function ST_Area(Value geom) {
1255
        return function("ST_Area", geom);
1256
    }
1257
1258
    @Override
1259
    public Function ST_Buffer(Value geom) {
1260
        return function("ST_Buffer", geom);
1261
    }
1262
1263
    @Override
1264 44568 jjdelcerro
    public Function ST_Buffer(Value geom, Value dist) {
1265
        return function("ST_Buffer", geom, dist);
1266
    }
1267
1268
    @Override
1269 44364 jjdelcerro
    public Function ST_Centroid(Value geom) {
1270
        return function("ST_Centroid", geom);
1271
    }
1272
1273
    @Override
1274
    public Function ST_CoveredBy(Value geom1, Value geom2) {
1275
        return function("ST_CoveredBy", geom1, geom2);
1276
    }
1277
1278
    @Override
1279
    public Function ST_Covers(Value geom1, Value geom2) {
1280
        return function("ST_Covers", geom1, geom2);
1281
    }
1282
1283
    @Override
1284
    public Function ST_Diference(Value geom1, Value geom2) {
1285
        return function("ST_Diference", geom1, geom2);
1286
    }
1287
1288
    @Override
1289
    public Function ST_Dimension(Value geom) {
1290
        return function("ST_Dimension", geom);
1291
    }
1292
1293
    @Override
1294
    public Function ST_Distance(Value geom1, Value geom2) {
1295
        return function("ST_Distance", geom1, geom2);
1296
    }
1297
1298
    @Override
1299
    public Function ST_EndPoint(Value geom) {
1300
        return function("ST_EndPoint", geom);
1301
    }
1302
1303
    @Override
1304
    public Function ST_Intersection(Value geom1, Value geom2) {
1305
        return function("ST_Intersection", geom1, geom2);
1306
    }
1307
1308
    @Override
1309
    public Function ST_IsSimple(Value geom) {
1310
        return function("ST_IsSimple", geom);
1311
    }
1312
1313
    @Override
1314
    public Function ST_IsValid(Value geom) {
1315
        return function("ST_IsValid", geom);
1316
    }
1317
1318
    @Override
1319
    public Function ST_NumGeometries(Value geom) {
1320
        return function("ST_NumGeometries", geom);
1321
    }
1322
1323
    @Override
1324
    public Function ST_NumPoints(Value geom) {
1325
        return function("ST_NumPoints", geom);
1326
    }
1327
1328
    @Override
1329
    public Function ST_Perimeter(Value geom) {
1330
        return function("ST_Perimeter", geom);
1331
    }
1332
1333
    @Override
1334
    public Function ST_PointN(Value geom, Value n) {
1335
        return function("ST_PointN", geom, n);
1336
    }
1337
1338
    @Override
1339
    public Function ST_StartPoint(Value geom) {
1340
        return function("ST_StartPoint", geom);
1341
    }
1342
1343
    @Override
1344
    public Function ST_Union(Value geom1, Value geom2) {
1345
        return function("ST_Union", geom1, geom2);
1346
    }
1347
1348
    @Override
1349
    public Function ST_X(Value geom) {
1350
        return function("ST_X", geom);
1351
    }
1352
1353
    @Override
1354
    public Function ST_Y(Value geom) {
1355
        return function("ST_Y", geom);
1356
    }
1357
1358
    @Override
1359
    public Function ST_Z(Value geom) {
1360
        return function("ST_Z", geom);
1361
    }
1362
1363
    @Override
1364 44198 jjdelcerro
    public Function is_null(Value value) {
1365
        return builtin_function("IS NULL", FORMAT_ISNULL, value);
1366 43020 jjdelcerro
    }
1367
1368
    @Override
1369 44198 jjdelcerro
    public Function not_is_null(Value value) {
1370 44361 jjdelcerro
        return builtin_function("IS NOT NULL", FORMAT_NOTISNULL, value);
1371 43020 jjdelcerro
    }
1372
1373
    @Override
1374
    public Function not(Value value) {
1375 44198 jjdelcerro
        return builtin_function(OPERATOR_NOT, FORMAT_OPERATOR_NOT, value);
1376 43020 jjdelcerro
    }
1377
1378
    @Override
1379
    public BinaryOperator and(Value leftOperand, Value rightOperand) {
1380 44198 jjdelcerro
        return binaryOperator(OPERATOR_AND, FORMAT_OPERATOR_AND, leftOperand, rightOperand);
1381 43020 jjdelcerro
    }
1382
1383
    @Override
1384 44274 jjdelcerro
    public BinaryOperator and(Expression leftOperand, Expression rightOperand) {
1385
        return binaryOperator(
1386
                OPERATOR_AND,
1387
                FORMAT_OPERATOR_AND,
1388
                leftOperand.getCode().toValue(),
1389
                rightOperand.getCode().toValue()
1390
        );
1391
    }
1392
1393
    @Override
1394
    public BinaryOperator and(Expression leftOperand, Value rightOperand) {
1395
        return binaryOperator(
1396
                OPERATOR_AND,
1397
                FORMAT_OPERATOR_AND,
1398
                leftOperand.getCode().toValue(),
1399
                rightOperand
1400
        );
1401
    }
1402
1403
    @Override
1404 43020 jjdelcerro
    public BinaryOperator or(Value leftOperand, Value rightOperand) {
1405 44198 jjdelcerro
        return binaryOperator(OPERATOR_OR, FORMAT_OPERATOR_OR, leftOperand, rightOperand);
1406 43020 jjdelcerro
    }
1407
1408
    @Override
1409
    public BinaryOperator eq(Value leftOperand, Value rightOperand) {
1410 44198 jjdelcerro
        return binaryOperator("=", FORMAT_OPERATOR_EQ, leftOperand, rightOperand);
1411 43020 jjdelcerro
    }
1412 43093 jjdelcerro
1413 43020 jjdelcerro
    @Override
1414
    public BinaryOperator ne(Value leftOperand, Value rightOperand) {
1415 44198 jjdelcerro
        return binaryOperator("<>", FORMAT_OPERATOR_NE, leftOperand, rightOperand);
1416 43093 jjdelcerro
    }
1417 43020 jjdelcerro
1418
    @Override
1419
    public BinaryOperator gt(Value op1, Value op2) {
1420 44198 jjdelcerro
        return binaryOperator(">", FORMAT_OPERATOR_GT, op1, op2);
1421 43020 jjdelcerro
    }
1422
1423
    @Override
1424
    public BinaryOperator ge(Value op1, Value op2) {
1425 44198 jjdelcerro
        return binaryOperator(">=", FORMAT_OPERATOR_GE, op1, op2);
1426 43020 jjdelcerro
    }
1427
1428
    @Override
1429
    public BinaryOperator lt(Value op1, Value op2) {
1430 44198 jjdelcerro
        return binaryOperator("<", FORMAT_OPERATOR_LT, op1, op2);
1431 43020 jjdelcerro
    }
1432
1433
    @Override
1434
    public BinaryOperator le(Value op1, Value op2) {
1435 44198 jjdelcerro
        return binaryOperator("<=", FORMAT_OPERATOR_LE, op1, op2);
1436 43020 jjdelcerro
    }
1437
1438
    @Override
1439
    public BinaryOperator like(Value op1, Value op2) {
1440 44198 jjdelcerro
        return binaryOperator(OPERATOR_LIKE, FORMAT_OPERATOR_LIKE, op1, op2);
1441 43020 jjdelcerro
    }
1442
1443
    @Override
1444
    public BinaryOperator ilike(Value op1, Value op2) {
1445 44198 jjdelcerro
        return binaryOperator(OPERATOR_ILIKE, FORMAT_OPERATOR_ILIKE, op1, op2);
1446 43020 jjdelcerro
    }
1447
1448
    @Override
1449
    public BinaryOperator add(Value op1, Value op2) {
1450 44198 jjdelcerro
        return binaryOperator(OPERATOR_ADD, FORMAT_OPERATOR_ADD, op1, op2);
1451 43020 jjdelcerro
    }
1452
1453
    @Override
1454
    public BinaryOperator subst(Value op1, Value op2) {
1455 44198 jjdelcerro
        return binaryOperator(OPERATOR_SUBST, FORMAT_OPERATOR_SUBST, op1, op2);
1456 43020 jjdelcerro
    }
1457
1458
    @Override
1459
    public BinaryOperator mult(Value op1, Value op2) {
1460 44198 jjdelcerro
        return binaryOperator(OPERATOR_MULT, FORMAT_OPERATOR_MULT, op1, op2);
1461 43020 jjdelcerro
    }
1462
1463
    @Override
1464
    public BinaryOperator div(Value op1, Value op2) {
1465 44198 jjdelcerro
        return binaryOperator(OPERATOR_DIV, FORMAT_OPERATOR_DIV, op1, op2);
1466 43020 jjdelcerro
    }
1467
1468
    @Override
1469
    public BinaryOperator concat(Value op1, Value op2) {
1470 44198 jjdelcerro
        return binaryOperator(OPERATOR_CONCAT, FORMAT_OPERATOR_CONCAT, op1, op2);
1471 43020 jjdelcerro
    }
1472 44051 omartinez
1473
    @Override
1474 44038 jjdelcerro
    public Function iif(Value condition, Value iftrue, Value iffalse) {
1475 44198 jjdelcerro
        return function(FUNCTION_IIF, condition, iftrue, iffalse);
1476 44038 jjdelcerro
    }
1477
1478 44051 omartinez
    @Override
1479 44038 jjdelcerro
    public Function ifnull(Value value, Value iftrue, Value iffalse) {
1480 44198 jjdelcerro
        return function(FUNCTION_IFNULL, value, iftrue, iffalse);
1481 44038 jjdelcerro
    }
1482 44051 omartinez
1483
    @Override
1484
    public Function left(Value str, Value size) {
1485 44198 jjdelcerro
       return function(FUNCTION_LEFT, str, size);
1486 44051 omartinez
    }
1487
1488
    @Override
1489
    public Function right(Value str, Value len) {
1490 44198 jjdelcerro
       return function(FUNCTION_RIGHT, str, len);
1491 44051 omartinez
    }
1492
1493
    @Override
1494
    public Function locate(Value search, Value str, Value start) {
1495 44198 jjdelcerro
       return function(FUNCTION_LOCATE, search, str, start);
1496 44051 omartinez
    }
1497
1498
    @Override
1499
    public Function position(Value search, Value str) {
1500 44198 jjdelcerro
       return function(FUNCTION_POSITION, search, str);
1501 44051 omartinez
    }
1502
1503
    @Override
1504
    public Function lpad(Value str, Value len, Value padstr) {
1505 44198 jjdelcerro
       return function(FUNCTION_LPAD, str, len, padstr);
1506 44051 omartinez
    }
1507
1508
    @Override
1509
    public Function rpad(Value str, Value len, Value padstr) {
1510 44198 jjdelcerro
       return function(FUNCTION_RPAD, str, len, padstr);
1511 44051 omartinez
    }
1512
1513
    @Override
1514
    public Function ltrim(Value str) {
1515 44198 jjdelcerro
       return function(FUNCTION_LTRIM, str);
1516 44051 omartinez
    }
1517
1518
    @Override
1519
    public Function rtrim(Value str) {
1520 44198 jjdelcerro
       return function(FUNCTION_RTRIM, str);
1521 44051 omartinez
    }
1522
1523
    @Override
1524
    public Function trim(Value str) {
1525 44198 jjdelcerro
       return function(FUNCTION_TRIM, str);
1526 44051 omartinez
    }
1527
1528
    @Override
1529
    public Function repeat(Value str, Value size) {
1530 44198 jjdelcerro
       return function(FUNCTION_REPEAT, str, size);
1531 44051 omartinez
    }
1532
1533
    @Override
1534
    public Function replace(Value str, Value search, Value replstr) {
1535 44198 jjdelcerro
       return function(FUNCTION_REPLACE, str, search, replstr);
1536 44051 omartinez
    }
1537
1538
    @Override
1539
    public Function ascii(Value str) {
1540 44198 jjdelcerro
       return function(FUNCTION_ASCII, str);
1541 44051 omartinez
    }
1542
1543
    @Override
1544
    public Function lenght(Value str) {
1545 44198 jjdelcerro
       return function(FUNCTION_LENGHT, str);
1546 44051 omartinez
    }
1547
1548
    @Override
1549
    public Function instr(Value str, Value search, Value start) {
1550 44198 jjdelcerro
       return function(FUNCTION_INSTR, str, search, start);
1551 44051 omartinez
    }
1552
1553
    @Override
1554
    public Function lower(Value str) {
1555 44198 jjdelcerro
       return function(FUNCTION_LOWER, str);
1556 44051 omartinez
    }
1557
1558
    @Override
1559
    public Function upper(Value str) {
1560 44198 jjdelcerro
       return function(FUNCTION_UPPER, str);
1561 44051 omartinez
    }
1562
1563
    @Override
1564
    public Function space(Value size) {
1565 44198 jjdelcerro
       return function(FUNCTION_SPACE, size);
1566 44051 omartinez
    }
1567
1568
    @Override
1569
    public Function substring(Value str, Value start, Value len) {
1570 44198 jjdelcerro
       return function(FUNCTION_SUBSTRING, str, start, len);
1571 44051 omartinez
    }
1572
1573
    @Override
1574
    public Function acos(Value num) {
1575 44198 jjdelcerro
       return function(FUNCTION_ACOS, num);
1576 44051 omartinez
    }
1577
1578
    @Override
1579
    public Function asin(Value num) {
1580 44198 jjdelcerro
       return function(FUNCTION_ASIN, num);
1581 44051 omartinez
    }
1582
1583
    @Override
1584
    public Function atan(Value num) {
1585 44198 jjdelcerro
       return function(FUNCTION_ATAN, num);
1586 44051 omartinez
    }
1587
1588
    @Override
1589
    public Function cos(Value num) {
1590 44198 jjdelcerro
       return function(FUNCTION_COS, num);
1591 44051 omartinez
    }
1592
1593
    @Override
1594
    public Function cosh(Value num) {
1595 44198 jjdelcerro
       return function(FUNCTION_COSH, num);
1596 44051 omartinez
    }
1597
1598
    @Override
1599
    public Function cot(Value num) {
1600 44198 jjdelcerro
       return function(FUNCTION_COT, num);
1601 44051 omartinez
    }
1602
1603
    @Override
1604
    public Function bitand(Value num1, Value num2) {
1605 44198 jjdelcerro
       return function(FUNCTION_BITAND, num1, num2);
1606 44051 omartinez
    }
1607
1608
    @Override
1609
    public Function bitor(Value num1, Value num2) {
1610 44198 jjdelcerro
       return function(FUNCTION_BITOR, num1, num2);
1611 44051 omartinez
    }
1612
1613
    @Override
1614
    public Function bitxor(Value num1, Value num2) {
1615 44198 jjdelcerro
       return function(FUNCTION_BITXOR, num1, num2);
1616 44051 omartinez
    }
1617
1618
    @Override
1619
    public Function ceil(Value num) {
1620 44198 jjdelcerro
       return function(FUNCTION_CEIL, num);
1621 44051 omartinez
    }
1622
1623
    @Override
1624
    public Function degrees(Value num) {
1625 44198 jjdelcerro
       return function(FUNCTION_DEGREES, num);
1626 44051 omartinez
    }
1627
1628
    @Override
1629
    public Function exp(Value num) {
1630 44198 jjdelcerro
       return function(FUNCTION_EXP, num);
1631 44051 omartinez
    }
1632
1633
    @Override
1634
    public Function floor(Value num) {
1635 44198 jjdelcerro
       return function(FUNCTION_FLOOR, num);
1636 44051 omartinez
    }
1637
1638
    @Override
1639
    public Function log(Value num) {
1640 44198 jjdelcerro
       return function(FUNCTION_LOG, num);
1641 44051 omartinez
    }
1642
1643
    @Override
1644
    public Function log10(Value num) {
1645 44198 jjdelcerro
       return function(FUNCTION_LOG10, num);
1646 44051 omartinez
    }
1647
1648
    @Override
1649
    public Function pi(Value num) {
1650 44198 jjdelcerro
       return function(FUNCTION_PI, num);
1651 44051 omartinez
    }
1652
1653
    @Override
1654
    public Function power(Value num) {
1655 44198 jjdelcerro
       return function(FUNCTION_POWER, num);
1656 44051 omartinez
    }
1657
1658
    @Override
1659
    public Function radians(Value num) {
1660 44198 jjdelcerro
       return function(FUNCTION_RADIANS, num);
1661 44051 omartinez
    }
1662
1663
    @Override
1664
    public Function rand(Value num) {
1665 44198 jjdelcerro
       return function(FUNCTION_RAND, num);
1666 44051 omartinez
    }
1667
1668
    @Override
1669
    public Function round(Value num) {
1670 44198 jjdelcerro
       return function(FUNCTION_ROUND, num);
1671 44051 omartinez
    }
1672
1673
    @Override
1674
    public Function sqrt(Value num) {
1675 44198 jjdelcerro
       return function(FUNCTION_SQRT, num);
1676 44051 omartinez
    }
1677
1678
    @Override
1679
    public Function sign(Value num) {
1680 44198 jjdelcerro
       return function(FUNCTION_SIGN, num);
1681 44051 omartinez
    }
1682
1683
    @Override
1684
    public Function sin(Value num) {
1685 44198 jjdelcerro
       return function(FUNCTION_SIN, num);
1686 44051 omartinez
    }
1687
1688
    @Override
1689
    public Function sinh(Value num) {
1690 44198 jjdelcerro
       return function(FUNCTION_SINH, num);
1691 44051 omartinez
    }
1692
1693
    @Override
1694
    public Function tan(Value num) {
1695 44198 jjdelcerro
       return function(FUNCTION_TAN, num);
1696 44051 omartinez
    }
1697
    @Override
1698
    public Function tanh(Value num) {
1699 44198 jjdelcerro
       return function(FUNCTION_TANH, num);
1700 44051 omartinez
    }
1701
1702
    @Override
1703
    public Function zero() {
1704 44198 jjdelcerro
       return function(FUNCTION_ZERO);
1705 44051 omartinez
    }
1706 44053 omartinez
1707
    @Override
1708
    public Function chr(Value num) {
1709 44198 jjdelcerro
       return function(FUNCTION_CHR, num);
1710
    }
1711
1712
    @Override
1713
    public Function ST_ExtentAggregate(Value geom) {
1714
       return function(FUNCTION_ST_EXTENTAGGREGATE, geom);
1715 44053 omartinez
    }
1716 44198 jjdelcerro
1717
    @Override
1718
    public Function ST_UnionAggregate(Value geom) {
1719
       return function(FUNCTION_ST_UNIONAGGREGATE, geom);
1720
    }
1721
1722
    @Override
1723
    public Function cast(Value object, Value typeName) {
1724
       return function(FUNCTION_CAST, object, typeName);
1725
    }
1726
1727
    @Override
1728
    public Function decode(Value value, Value format) {
1729
       return function(FUNCTION_DECODE, value, format);
1730
    }
1731
1732
    @Override
1733
    public Function toDouble(Value num) {
1734
       return function(FUNCTION_TODOUBLE, num);
1735
    }
1736
1737
    @Override
1738
    public Function toFloat(Value num) {
1739
       return function(FUNCTION_TOFLOAT, num);
1740
    }
1741
1742
    @Override
1743
    public Function toLong(Value num) {
1744
       return function(FUNCTION_TOLONG, num);
1745
    }
1746
1747
    @Override
1748
    public Function toInteger(Value num) {
1749
       return function(FUNCTION_TOINTEGER, num);
1750
    }
1751
1752
    @Override
1753
    public Function toStr(Value object) {
1754
       return function(FUNCTION_TOSTR, object);
1755
    }
1756
1757 44253 jjdelcerro
    @Override
1758
    public Function ST_Point(Value x, Value y) {
1759
       return function(FUNCTION_ST_POINT, x, y);
1760
    }
1761 44198 jjdelcerro
1762 44253 jjdelcerro
    @Override
1763 44431 jjdelcerro
    public Function ST_MakePoint(Value x, Value y) {
1764
       return function(FUNCTION_ST_MAKEPOINT, x, y);
1765
    }
1766
1767
    @Override
1768
    public Function ST_MakePoint(Value x, Value y, Value z) {
1769
       return function(FUNCTION_ST_MAKEPOINT, x, y, z);
1770
    }
1771
1772
    @Override
1773
    public Function ST_MakePoint(Value x, Value y, Value z, Value m) {
1774
       return function(FUNCTION_ST_MAKEPOINT, x, y, z, m);
1775
    }
1776
1777
    @Override
1778 44253 jjdelcerro
    public Function ST_SetSRID(Value geom, Value srid) {
1779
       return function(FUNCTION_ST_POINT, geom, srid);
1780
    }
1781 44262 jjdelcerro
1782
    @Override
1783
    public Function list() {
1784
        return function(FUNCTION_LIST);
1785
    }
1786 44376 jjdelcerro
1787
    @Override
1788
    public Function tuple() {
1789
        return function(FUNCTION_TUPLE);
1790
    }
1791
1792
    @Override
1793
    public Function tuple(Constant... values) {
1794
        Function fn = function(FUNCTION_TUPLE);
1795
        for (Value theValue : values) {
1796
            fn.parameter(this.constant(theValue));
1797
        }
1798
        return fn;
1799
    }
1800
1801
1802 43020 jjdelcerro
}