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

History | View | Annotate | Download (55.1 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 44612 jjdelcerro
    private static final String FORMAT_ST_FORCE2D = "ST_Force2D({0})";
48 44198 jjdelcerro
    private static final String FORMAT_ST_INTERSECTS = "ST_Intersects(({0}), ({1}))";
49
    private static final String FORMAT_ST_GEOMFROMTEXT = "ST_GeomFromText(({0}), ({1}))";
50
    private static final String FORMAT_ST_GEOMFROMWKB = "ST_GeomFromWKB(({0}), ({1}))";
51
    private static final String FORMAT_ST_GEOMFROMEWKB = "ST_GeomFromEWKB(({0}), ({1}))";
52
    private static final String FORMAT_ST_SIMPLIFY = "ST_Simplify(({0}), ({1}))";
53 43093 jjdelcerro
54 44198 jjdelcerro
    private static final String FORMAT_ISNULL = "( ({0}) IS NULL )";
55 44361 jjdelcerro
    private static final String FORMAT_NOTISNULL = "( ({0}) IS NOT NULL )";
56 44198 jjdelcerro
    private static final String FORMAT_OPERATOR_NOT = "( NOT ({0}) )";
57 43093 jjdelcerro
58 44262 jjdelcerro
    private static final String FORMAT_OPERATOR_AND = "({0} AND {1})";
59 44198 jjdelcerro
    private static final String FORMAT_OPERATOR_OR = "{0} OR {1}";
60
    private static final String FORMAT_OPERATOR_EQ = "( ({0}) = ({1}) )";
61
    private static final String FORMAT_OPERATOR_NE = "( ({0}) <> ({1}) )";
62
    private static final String FORMAT_OPERATOR_GT = "( ({0}) > ({1}) )";
63
    private static final String FORMAT_OPERATOR_GE = "( ({0}) >= ({1}) )";
64
    private static final String FORMAT_OPERATOR_LT = "( ({0}) < ({1}) )";
65
    private static final String FORMAT_OPERATOR_LE = "( ({0}) <= ({1}) )";
66
    private static final String FORMAT_OPERATOR_LIKE = "( ({0}) LIKE ({1}) )";
67
    private static final String FORMAT_OPERATOR_ILIKE = "( ({0}) ILIKE ({1}) )";
68
    private static final String FORMAT_OPERATOR_ADD = "{0} + {1}";
69
    private static final String FORMAT_OPERATOR_SUBST = "{0} - {1}";
70 44262 jjdelcerro
    private static final String FORMAT_OPERATOR_MULT = "({0} * {1})";
71
    private static final String FORMAT_OPERATOR_DIV = "({0} / {1})";
72 44198 jjdelcerro
    private static final String FORMAT_OPERATOR_CONCAT = "{0} || {1}";
73 43093 jjdelcerro
74 43020 jjdelcerro
    public class GroupBase extends AbstractValue implements Group {
75 43093 jjdelcerro
76 43020 jjdelcerro
        protected Value value;
77 43093 jjdelcerro
78 43020 jjdelcerro
        public GroupBase(Value value) {
79
            this.value = value;
80
        }
81
82
        @Override
83 44198 jjdelcerro
        public Value value() {
84 43020 jjdelcerro
            return value;
85
        }
86
87
        @Override
88
        public void accept(Visitor visitor, VisitorFilter filter) {
89 43093 jjdelcerro
            super.accept(visitor, filter);
90
            this.value.accept(visitor, filter);
91 43020 jjdelcerro
        }
92 43093 jjdelcerro
93 43020 jjdelcerro
        @Override
94 44376 jjdelcerro
        public void replace(Value target, Value replacement) {
95
            if( this.value == target ) {
96
                this.value = replacement;
97
            } else {
98
                this.value.replace(target, replacement);
99
            }
100
        }
101
102
        @Override
103 43020 jjdelcerro
        public String toString() {
104 44198 jjdelcerro
            return this.toString(EMPTY_FORMATTER);
105 43020 jjdelcerro
        }
106 44198 jjdelcerro
107
        @Override
108
        public String toString(Formatter<Value> formatter) {
109 44296 jjdelcerro
            if( formatter!=null && formatter.canApply(this) ) {
110 44198 jjdelcerro
                return formatter.format(this);
111
            }
112
            return MessageFormat.format(FORMAT_GROUP, this.value.toString());
113
        }
114 43020 jjdelcerro
    }
115
116
    public class VariableBase extends AbstractValue implements Variable {
117 43093 jjdelcerro
118 43020 jjdelcerro
        protected String name;
119 43093 jjdelcerro
120 43020 jjdelcerro
        public VariableBase(String name) {
121
            this.name = name;
122
        }
123 43093 jjdelcerro
124 43020 jjdelcerro
        @Override
125 44198 jjdelcerro
        public String name() {
126 43020 jjdelcerro
            return this.name;
127
        }
128
129
        @Override
130
        public String toString() {
131 44198 jjdelcerro
            return this.toString(EMPTY_FORMATTER);
132
        }
133
134
        @Override
135
        public String toString(Formatter<Value> formatter) {
136 44296 jjdelcerro
            if( formatter!=null && formatter.canApply(this) ) {
137 44198 jjdelcerro
                return formatter.format(this);
138
            }
139 43020 jjdelcerro
            return identifier(this.name);
140
        }
141
142
        @Override
143
        public int compareTo(Variable o) {
144 44198 jjdelcerro
            return this.name.compareTo(o.name());
145 43020 jjdelcerro
        }
146
147
        @Override
148
        public boolean equals(Object obj) {
149 43093 jjdelcerro
            if (!(obj instanceof Variable)) {
150 43020 jjdelcerro
                return false;
151
            }
152 44198 jjdelcerro
            return this.name.equals(((Variable) obj).name());
153 43020 jjdelcerro
        }
154
155
        @Override
156
        public int hashCode() {
157
            int hash = 7;
158
            hash = 37 * hash + Objects.hashCode(this.name);
159
            return hash;
160
        }
161
    }
162
163
    public class ParameterBase extends AbstractValue implements Parameter {
164 43093 jjdelcerro
165
        protected String name;
166 43020 jjdelcerro
        protected Object value;
167
        protected ParameterType type;
168
        protected Value srs;
169 43093 jjdelcerro
170 44198 jjdelcerro
        /*
171
        Para un parametre de tipo Geometria, el srs sera siempre un Constant
172
        excepto cuando se le asigne una geometria contante al parametro y esta
173
        tenga un SRS asignado. En este caso, tanto la geometria como el SRS
174
        se trataran como parametros.
175

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