svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.spi / src / main / java / org / gvsig / fmap / dal / feature / spi / ExpressionBuilderBase.java @ 43020
History | View | Annotate | Download (31.3 KB)
1 | 43020 | jjdelcerro | package org.gvsig.fmap.dal.feature.spi; |
---|---|---|---|
2 | |||
3 | import java.text.MessageFormat; |
||
4 | import java.util.ArrayList; |
||
5 | import java.util.Collections; |
||
6 | import java.util.HashMap; |
||
7 | import java.util.HashSet; |
||
8 | import java.util.List; |
||
9 | import java.util.Map; |
||
10 | import java.util.Objects; |
||
11 | import java.util.Set; |
||
12 | import org.apache.commons.lang3.ObjectUtils; |
||
13 | import org.apache.commons.lang3.StringUtils; |
||
14 | import org.cresques.cts.IProjection; |
||
15 | import org.gvsig.fmap.dal.ExpressionBuilder; |
||
16 | import org.gvsig.fmap.dal.ExpressionBuilder.BinaryOperator; |
||
17 | import org.gvsig.fmap.dal.ExpressionBuilder.Config; |
||
18 | import org.gvsig.fmap.dal.ExpressionBuilder.Constant; |
||
19 | import org.gvsig.fmap.dal.ExpressionBuilder.Custom; |
||
20 | import org.gvsig.fmap.dal.ExpressionBuilder.Function; |
||
21 | import org.gvsig.fmap.dal.ExpressionBuilder.GeometrySupportType; |
||
22 | import org.gvsig.fmap.dal.ExpressionBuilder.GeometryValue; |
||
23 | import org.gvsig.fmap.dal.ExpressionBuilder.Group; |
||
24 | import org.gvsig.fmap.dal.ExpressionBuilder.Parameter; |
||
25 | import org.gvsig.fmap.dal.ExpressionBuilder.ParameterType; |
||
26 | import org.gvsig.fmap.dal.ExpressionBuilder.Value; |
||
27 | import org.gvsig.fmap.dal.ExpressionBuilder.Variable; |
||
28 | import org.gvsig.fmap.dal.ExpressionBuilder.Visitable; |
||
29 | import org.gvsig.fmap.dal.ExpressionBuilder.Visitor; |
||
30 | import org.gvsig.fmap.dal.ExpressionBuilder.VisitorFilter; |
||
31 | import org.gvsig.fmap.geom.Geometry; |
||
32 | |||
33 | public class ExpressionBuilderBase implements ExpressionBuilder { |
||
34 | public class ConfigBase implements Config { |
||
35 | |||
36 | protected Map<String,Object> values; |
||
37 | |||
38 | public ConfigBase() {
|
||
39 | this.values = new HashMap<>(); |
||
40 | |||
41 | this.values.put(has_spatial_functions,false); |
||
42 | this.values.put(constant_true, "(1=1)"); |
||
43 | this.values.put(constant_false, "(1=2)"); |
||
44 | this.values.put(quote_for_identifiers, "\""); |
||
45 | this.values.put(quote_for_strings, "'"); |
||
46 | this.values.put(geometry_type_support, GeometrySupportType.WKT);
|
||
47 | this.values.put(group, "( {0} )"); |
||
48 | |||
49 | this.values.put(Find_SRID , "Find_SRID(({0}), ({1}), ({2}))"); |
||
50 | this.values.put(ST_SRID , "ST_SRID({0})"); |
||
51 | this.values.put(ST_AsText , "ST_AsText({0})"); |
||
52 | this.values.put(ST_AsBinary, "ST_AsBinary({0})"); |
||
53 | this.values.put(ST_AsEWKB, "ST_AsWKB({0})"); |
||
54 | this.values.put(ST_Contains, "ST_Contains(({0}), ({1}))"); |
||
55 | this.values.put(ST_Crosses, "ST_Crosses(({0}), ({1}))"); |
||
56 | this.values.put(ST_Disjoint , "ST_Disjoint (({0}), ({1}))"); |
||
57 | this.values.put(ST_IsClosed, "ST_IsClosed({0})"); |
||
58 | this.values.put(ST_Overlaps, "ST_Overlaps(({0}), ({1}))"); |
||
59 | this.values.put(ST_Touches, "ST_Touches(({0}), ({1}))"); |
||
60 | this.values.put(ST_Within, "ST_Within(({0}), ({1}))"); |
||
61 | this.values.put(ST_Envelope, "ST_Envelope({0})"); |
||
62 | this.values.put(ST_Intersects, "ST_Intersects(({0}), ({1}))"); |
||
63 | this.values.put(ST_GeomFromText, "ST_GeomFromText({0}, ({1}))"); |
||
64 | this.values.put(ST_GeomFromWKB, "ST_GeomFromWKB(({0}), ({1}))"); |
||
65 | this.values.put(ST_GeomFromEWKB, "ST_GeomFromEWKB(({0}), ({1}))"); |
||
66 | this.values.put(lcase, "LCASE({0})"); |
||
67 | this.values.put(ucase, "UCASE({0})"); |
||
68 | this.values.put(isNull, "( ({0}) IS NULL )"); |
||
69 | this.values.put(notIsNull, "( ({0}) NOT IS NULL )"); |
||
70 | this.values.put(operator_not, "( NOT ({0}) )"); |
||
71 | |||
72 | this.values.put(operator_AND, "{0} AND {1}"); |
||
73 | this.values.put(operator_OR, "{0} OR {1}"); |
||
74 | this.values.put(operator_EQ, "( ({0}) = ({1}) )"); |
||
75 | this.values.put(operator_NE, "( ({0}) <> ({1}) )"); |
||
76 | this.values.put(operator_GT, "( ({0}) > ({1}) )"); |
||
77 | this.values.put(operator_GE, "( ({0}) >= ({1}) )"); |
||
78 | this.values.put(operator_LT, "( ({0}) < ({1}) )"); |
||
79 | this.values.put(operator_LE, "( ({0}) <= ({1}) )"); |
||
80 | this.values.put(operator_LIKE, "( ({0}) LIKE ({1}) )"); |
||
81 | this.values.put(operator_ILIKE, "( ({0}) ILIKE ({1}) )"); |
||
82 | |||
83 | this.values.put(operator_add, "{0} + {1}"); |
||
84 | this.values.put(operator_subst, "{0} - {1}"); |
||
85 | this.values.put(operator_mult, "{0} * {1}"); |
||
86 | this.values.put(operator_div, "{0} / {1}"); |
||
87 | this.values.put(operator_concat, "{0} || {1}"); |
||
88 | |||
89 | } |
||
90 | |||
91 | @Override
|
||
92 | public boolean has_functionality(String functionality) { |
||
93 | Object x = this.values.get(functionality); |
||
94 | if( x == null ) { |
||
95 | return false; |
||
96 | } |
||
97 | if( x instanceof CharSequence && StringUtils.isEmpty((CharSequence) x) ) { |
||
98 | return false; |
||
99 | } |
||
100 | return true; |
||
101 | } |
||
102 | |||
103 | @Override
|
||
104 | public void remove_functionality(String functionality) { |
||
105 | this.values.remove(functionality);
|
||
106 | } |
||
107 | |||
108 | @Override
|
||
109 | public boolean has_spatial_functions() { |
||
110 | return this.getBoolean(has_spatial_functions); |
||
111 | } |
||
112 | |||
113 | @Override
|
||
114 | public GeometrySupportType getGeometryTypeSupport() {
|
||
115 | return (GeometrySupportType) this.get(geometry_type_support); |
||
116 | } |
||
117 | |||
118 | @Override
|
||
119 | public boolean getBoolean(String name) { |
||
120 | return (boolean) this.values.get(name); |
||
121 | } |
||
122 | |||
123 | @Override
|
||
124 | public String getString(String name) { |
||
125 | return (String) this.values.get(name); |
||
126 | } |
||
127 | |||
128 | @Override
|
||
129 | public Object get(String name) { |
||
130 | return this.values.get(name); |
||
131 | } |
||
132 | |||
133 | @Override
|
||
134 | public void set(String name, Object value) { |
||
135 | this.values.put(name,value);
|
||
136 | } |
||
137 | } |
||
138 | |||
139 | public abstract class AbstractValue implements Value { |
||
140 | |||
141 | @Override
|
||
142 | public void accept(Visitor visitor, VisitorFilter filter) { |
||
143 | if( filter == null || filter.accept(this) ) { |
||
144 | visitor.visit(this);
|
||
145 | } |
||
146 | } |
||
147 | |||
148 | } |
||
149 | |||
150 | public class ClassVisitorFilter implements VisitorFilter { |
||
151 | |||
152 | private final Class classFilter; |
||
153 | |||
154 | public ClassVisitorFilter(Class classFilter) { |
||
155 | this.classFilter = classFilter;
|
||
156 | } |
||
157 | |||
158 | @Override
|
||
159 | public boolean accept(Visitable visitable) { |
||
160 | return classFilter.isInstance(visitable);
|
||
161 | } |
||
162 | |||
163 | } |
||
164 | |||
165 | public class GroupBase extends AbstractValue implements Group { |
||
166 | protected Value value;
|
||
167 | |||
168 | public GroupBase(Value value) {
|
||
169 | this.value = value;
|
||
170 | } |
||
171 | |||
172 | @Override
|
||
173 | public Value getValue() {
|
||
174 | return value;
|
||
175 | } |
||
176 | |||
177 | @Override
|
||
178 | public void accept(Visitor visitor, VisitorFilter filter) { |
||
179 | super.accept(visitor,filter);
|
||
180 | this.value.accept(visitor,filter);
|
||
181 | } |
||
182 | |||
183 | @Override
|
||
184 | public String toString() { |
||
185 | return MessageFormat.format(config.getString(Config.group), this.value.toString()); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | public class VariableBase extends AbstractValue implements Variable { |
||
190 | protected String name; |
||
191 | |||
192 | public VariableBase(String name) { |
||
193 | this.name = name;
|
||
194 | } |
||
195 | |||
196 | @Override
|
||
197 | public String getName() { |
||
198 | return this.name; |
||
199 | } |
||
200 | |||
201 | @Override
|
||
202 | public String toString() { |
||
203 | return identifier(this.name); |
||
204 | } |
||
205 | |||
206 | @Override
|
||
207 | public int compareTo(Variable o) { |
||
208 | return this.name.compareTo(o.getName()); |
||
209 | } |
||
210 | |||
211 | @Override
|
||
212 | public boolean equals(Object obj) { |
||
213 | if( ! (obj instanceof Variable) ) { |
||
214 | return false; |
||
215 | } |
||
216 | return this.name.equals(((Variable)obj).getName()); |
||
217 | } |
||
218 | |||
219 | @Override
|
||
220 | public int hashCode() { |
||
221 | int hash = 7; |
||
222 | hash = 37 * hash + Objects.hashCode(this.name); |
||
223 | return hash;
|
||
224 | } |
||
225 | } |
||
226 | |||
227 | public class ParameterBase extends AbstractValue implements Parameter { |
||
228 | protected Object value; |
||
229 | protected ParameterType type;
|
||
230 | protected Value srs;
|
||
231 | |||
232 | public ParameterBase(Object value) { |
||
233 | this.value = value;
|
||
234 | if( this.value instanceof String ) { |
||
235 | this.type = ParameterType.Variable;
|
||
236 | } else {
|
||
237 | this.type = ParameterType.Constant;
|
||
238 | } |
||
239 | } |
||
240 | |||
241 | @Override
|
||
242 | public void accept(Visitor visitor, VisitorFilter filter) { |
||
243 | super.accept(visitor, filter);
|
||
244 | if( this.srs != null ) { |
||
245 | this.srs.accept(visitor, filter);
|
||
246 | } |
||
247 | } |
||
248 | |||
249 | @Override
|
||
250 | public Parameter as_geometry() {
|
||
251 | this.type = ParameterType.Geometry;
|
||
252 | return this; |
||
253 | } |
||
254 | |||
255 | @Override
|
||
256 | public Parameter as_constant() {
|
||
257 | this.type = ParameterType.Constant;
|
||
258 | return this; |
||
259 | } |
||
260 | |||
261 | @Override
|
||
262 | public Parameter as_variable() {
|
||
263 | this.type = ParameterType.Variable;
|
||
264 | return this; |
||
265 | } |
||
266 | |||
267 | @Override
|
||
268 | public Parameter srs(Value srs) {
|
||
269 | this.srs = srs;
|
||
270 | return this; |
||
271 | } |
||
272 | |||
273 | @Override
|
||
274 | public Parameter srs(IProjection srs) {
|
||
275 | this.srs = constant(getSRSId(srs));
|
||
276 | return this; |
||
277 | } |
||
278 | |||
279 | @Override
|
||
280 | public String getName() { |
||
281 | switch(this.type) { |
||
282 | case Variable:
|
||
283 | case Geometry:
|
||
284 | return (String) this.value; |
||
285 | case Constant:
|
||
286 | default:
|
||
287 | return null; |
||
288 | } |
||
289 | } |
||
290 | |||
291 | @Override
|
||
292 | public boolean is_constant() { |
||
293 | return this.type == ParameterType.Constant; |
||
294 | } |
||
295 | |||
296 | @Override
|
||
297 | public boolean is_geometry_variable() { |
||
298 | return this.type == ParameterType.Geometry; |
||
299 | } |
||
300 | |||
301 | @Override
|
||
302 | public boolean is_variable() { |
||
303 | return this.type == ParameterType.Variable; |
||
304 | } |
||
305 | |||
306 | @Override
|
||
307 | public Object getValue() { |
||
308 | try {
|
||
309 | switch(this.type) { |
||
310 | case Constant:
|
||
311 | if( this.value instanceof Geometry ) { |
||
312 | switch( config.getGeometryTypeSupport() ) {
|
||
313 | case EWKB:
|
||
314 | return bytearray(((Geometry)this.value).convertToEWKB()); |
||
315 | case WKB:
|
||
316 | return bytearray(((Geometry)this.value).convertToWKB()); |
||
317 | case WKT:
|
||
318 | default:
|
||
319 | return ((Geometry)this.value).convertToWKT(); |
||
320 | } |
||
321 | } else if( this.value instanceof IProjection ) { |
||
322 | return getSRSId((IProjection) this.value); |
||
323 | } |
||
324 | return this.value; |
||
325 | case Variable:
|
||
326 | case Geometry:
|
||
327 | default:
|
||
328 | return null; |
||
329 | } |
||
330 | } catch(Exception ex) { |
||
331 | throw new RuntimeException("Can't get value from parameter.",ex); |
||
332 | } |
||
333 | } |
||
334 | |||
335 | @Override
|
||
336 | public ParameterType getType() {
|
||
337 | return this.type; |
||
338 | } |
||
339 | |||
340 | @Override
|
||
341 | public Value getSRS() {
|
||
342 | return this.srs; |
||
343 | } |
||
344 | |||
345 | @Override
|
||
346 | public String toString() { |
||
347 | switch(this.type) { |
||
348 | case Constant:
|
||
349 | case Variable:
|
||
350 | default:
|
||
351 | return "?"; |
||
352 | case Geometry:
|
||
353 | switch( config.getGeometryTypeSupport() ) {
|
||
354 | case EWKB:
|
||
355 | return MessageFormat.format( |
||
356 | config.getString(Config.ST_GeomFromEWKB), |
||
357 | "?",
|
||
358 | String.valueOf(this.srs.toString()) |
||
359 | ); |
||
360 | case WKB:
|
||
361 | return MessageFormat.format( |
||
362 | config.getString(Config.ST_GeomFromWKB), |
||
363 | "?",
|
||
364 | String.valueOf(this.srs.toString()) |
||
365 | ); |
||
366 | case WKT:
|
||
367 | default:
|
||
368 | return MessageFormat.format( |
||
369 | config.getString(Config.ST_GeomFromText), |
||
370 | "?",
|
||
371 | String.valueOf(this.srs.toString()) |
||
372 | ); |
||
373 | } |
||
374 | } |
||
375 | } |
||
376 | } |
||
377 | |||
378 | public class ConstantBase extends AbstractValue implements Constant { |
||
379 | |||
380 | protected Object value; |
||
381 | |||
382 | public ConstantBase(Object value) { |
||
383 | this.value = value;
|
||
384 | } |
||
385 | |||
386 | @Override
|
||
387 | public Object getValue() { |
||
388 | return this.value; |
||
389 | } |
||
390 | |||
391 | @Override
|
||
392 | public String toString() { |
||
393 | if( this.value instanceof String ) { |
||
394 | return string((String) this.value); |
||
395 | } |
||
396 | if( this.value instanceof Boolean ) { |
||
397 | if( ((Boolean)this.value) ) { |
||
398 | return config.getString(Config.constant_true);
|
||
399 | } else {
|
||
400 | return config.getString(Config.constant_false);
|
||
401 | } |
||
402 | } |
||
403 | return ObjectUtils.toString(this.value, ""); |
||
404 | } |
||
405 | } |
||
406 | |||
407 | public class CustomBase extends AbstractValue implements Custom { |
||
408 | |||
409 | protected Object value; |
||
410 | |||
411 | // Esto es para permitir declarar parametros y columnas en una seccion
|
||
412 | // custom.
|
||
413 | protected List<Value> values; |
||
414 | |||
415 | public CustomBase(Object value) { |
||
416 | this.value = value;
|
||
417 | } |
||
418 | |||
419 | @Override
|
||
420 | public void accept(Visitor visitor, VisitorFilter filter) { |
||
421 | super.accept(visitor, filter);
|
||
422 | if( this.values!=null ) { |
||
423 | for (Value value : values) {
|
||
424 | value.accept(visitor, filter); |
||
425 | } |
||
426 | } |
||
427 | } |
||
428 | |||
429 | @Override
|
||
430 | public Object getValue() { |
||
431 | return this.value; |
||
432 | } |
||
433 | |||
434 | @Override
|
||
435 | public Custom add(Variable variable) {
|
||
436 | if( this.values==null ) { |
||
437 | this.values = new ArrayList<>(); |
||
438 | } |
||
439 | this.values.add(variable);
|
||
440 | return this; |
||
441 | } |
||
442 | |||
443 | @Override
|
||
444 | public Custom add(Parameter parameter) {
|
||
445 | if( this.values==null ) { |
||
446 | this.values = new ArrayList<>(); |
||
447 | } |
||
448 | this.values.add(parameter);
|
||
449 | return this; |
||
450 | } |
||
451 | |||
452 | @Override
|
||
453 | public String toString() { |
||
454 | return ObjectUtils.toString(this.value, ""); |
||
455 | } |
||
456 | } |
||
457 | |||
458 | public class GeometryValueBase extends AbstractValue implements GeometryValue { |
||
459 | |||
460 | protected Geometry geometry;
|
||
461 | protected IProjection projection;
|
||
462 | |||
463 | public GeometryValueBase(Geometry geometry, IProjection projection) {
|
||
464 | this.geometry = geometry;
|
||
465 | this.projection = projection;
|
||
466 | } |
||
467 | |||
468 | @Override
|
||
469 | public Geometry getGeometry() {
|
||
470 | return this.geometry; |
||
471 | } |
||
472 | |||
473 | @Override
|
||
474 | public IProjection getSRS() {
|
||
475 | return this.projection; |
||
476 | } |
||
477 | |||
478 | @Override
|
||
479 | public String toString() { |
||
480 | try {
|
||
481 | switch( config.getGeometryTypeSupport() ) {
|
||
482 | case EWKB:
|
||
483 | return MessageFormat.format( |
||
484 | config.getString(Config.ST_GeomFromEWKB), |
||
485 | bytearray(this.geometry.convertToEWKB()),
|
||
486 | String.valueOf(getSRSId(this.projection)) |
||
487 | ); |
||
488 | case WKB:
|
||
489 | return MessageFormat.format( |
||
490 | config.getString(Config.ST_GeomFromWKB), |
||
491 | bytearray(this.geometry.convertToWKB()),
|
||
492 | String.valueOf(getSRSId(this.projection)) |
||
493 | ); |
||
494 | case WKT:
|
||
495 | default:
|
||
496 | return MessageFormat.format( |
||
497 | config.getString(Config.ST_GeomFromText), |
||
498 | string(this.geometry.convertToWKT()),
|
||
499 | String.valueOf(getSRSId(this.projection)) |
||
500 | ); |
||
501 | } |
||
502 | } catch (Exception ex) { |
||
503 | throw new RuntimeException("Can't convert geometry to string.",ex); |
||
504 | } |
||
505 | } |
||
506 | } |
||
507 | |||
508 | public class FunctionBase extends AbstractValue implements Function { |
||
509 | |||
510 | protected String name; |
||
511 | protected String format; |
||
512 | protected List<Value> parameters; |
||
513 | |||
514 | public FunctionBase(String name, String format) { |
||
515 | this.name = name;
|
||
516 | this.format = format;
|
||
517 | } |
||
518 | |||
519 | @Override
|
||
520 | public List<Value> parameters() { |
||
521 | if( this.parameters == null ) { |
||
522 | this.parameters = new ArrayList<>(); |
||
523 | } |
||
524 | return this.parameters; |
||
525 | } |
||
526 | |||
527 | @Override
|
||
528 | public Function parameter(Value parameter) {
|
||
529 | this.parameters().add(parameter);
|
||
530 | return this; |
||
531 | } |
||
532 | |||
533 | @Override
|
||
534 | public String getName() { |
||
535 | return this.name; |
||
536 | } |
||
537 | |||
538 | @Override
|
||
539 | public void accept(Visitor visitor, VisitorFilter filter) { |
||
540 | super.accept(visitor,filter);
|
||
541 | for (Value value : this.parameters) { |
||
542 | value.accept(visitor,filter); |
||
543 | } |
||
544 | } |
||
545 | |||
546 | @Override
|
||
547 | public String toString() { |
||
548 | if( this.parameters!=null && !this.parameters.isEmpty() ) { |
||
549 | List<String> values = new ArrayList<>(); |
||
550 | for (Value value : this.parameters) { |
||
551 | values.add(value.toString()); |
||
552 | } |
||
553 | return MessageFormat.format(format, values.toArray()); |
||
554 | } else {
|
||
555 | return this.format; |
||
556 | } |
||
557 | } |
||
558 | } |
||
559 | |||
560 | public class BinaryOperatorBase extends AbstractValue implements BinaryOperator { |
||
561 | |||
562 | protected String name; |
||
563 | protected String format; |
||
564 | protected Value left;
|
||
565 | protected Value right;
|
||
566 | |||
567 | public BinaryOperatorBase(String name, String format) { |
||
568 | this.name = name;
|
||
569 | this.format = format;
|
||
570 | } |
||
571 | |||
572 | @Override
|
||
573 | public String getName() { |
||
574 | return this.name; |
||
575 | } |
||
576 | |||
577 | @Override
|
||
578 | public void accept(Visitor visitor, VisitorFilter filter) { |
||
579 | super.accept(visitor,filter);
|
||
580 | this.left.accept(visitor, filter);
|
||
581 | this.right.accept(visitor, filter);
|
||
582 | } |
||
583 | |||
584 | @Override
|
||
585 | public BinaryOperator setLeft(Value operand) {
|
||
586 | this.left = operand;
|
||
587 | return this; |
||
588 | } |
||
589 | |||
590 | @Override
|
||
591 | public BinaryOperator setRight(Value operand) {
|
||
592 | this.right = operand;
|
||
593 | return this; |
||
594 | } |
||
595 | |||
596 | @Override
|
||
597 | public Value getLeft() {
|
||
598 | return this.left; |
||
599 | } |
||
600 | |||
601 | @Override
|
||
602 | public Value getRight() {
|
||
603 | return this.right; |
||
604 | } |
||
605 | |||
606 | @Override
|
||
607 | public String toString() { |
||
608 | return MessageFormat.format( |
||
609 | format, |
||
610 | this.left.toString(),
|
||
611 | this.right.toString()
|
||
612 | ); |
||
613 | } |
||
614 | } |
||
615 | |||
616 | protected Value value;
|
||
617 | protected Config config;
|
||
618 | |||
619 | public ExpressionBuilderBase() {
|
||
620 | this.config = new ConfigBase(); |
||
621 | } |
||
622 | |||
623 | @Override
|
||
624 | public ExpressionBuilder createExpressionBuilder() {
|
||
625 | return new ExpressionBuilderBase(); |
||
626 | } |
||
627 | |||
628 | @Override
|
||
629 | public Config getConfig() {
|
||
630 | return this.config; |
||
631 | } |
||
632 | |||
633 | @Override
|
||
634 | public Value getValue() {
|
||
635 | return this.value; |
||
636 | } |
||
637 | |||
638 | @Override
|
||
639 | public ExpressionBuilder setValue(Value value) {
|
||
640 | this.value = value;
|
||
641 | return this; |
||
642 | } |
||
643 | |||
644 | @Override
|
||
645 | public String toString() { |
||
646 | return this.value.toString(); |
||
647 | } |
||
648 | |||
649 | @Override
|
||
650 | public void accept(Visitor visitor, VisitorFilter filter) { |
||
651 | this.value.accept(visitor, filter);
|
||
652 | } |
||
653 | |||
654 | @Override
|
||
655 | public boolean has_spatial_functions() { |
||
656 | return this.config.getBoolean(Config.has_spatial_functions); |
||
657 | } |
||
658 | |||
659 | @Override
|
||
660 | public GeometrySupportType geometry_support_type() {
|
||
661 | return (GeometrySupportType) this.config.get(Config.geometry_type_support); |
||
662 | } |
||
663 | |||
664 | @Override
|
||
665 | public String string(String s) { |
||
666 | String quote = this.config.getString(Config.quote_for_strings); |
||
667 | if (s.startsWith(quote)) {
|
||
668 | return s;
|
||
669 | } |
||
670 | return quote + s + quote;
|
||
671 | } |
||
672 | |||
673 | @Override
|
||
674 | public String identifier(String id) { |
||
675 | String quote = this.config.getString(Config.quote_for_identifiers); |
||
676 | if (id.startsWith(quote)) {
|
||
677 | return id;
|
||
678 | } |
||
679 | return quote + id + quote;
|
||
680 | } |
||
681 | |||
682 | @Override
|
||
683 | public String bytearray(byte[] data) { |
||
684 | StringBuilder builder = new StringBuilder(); |
||
685 | builder.append("0x");
|
||
686 | for (byte abyte : data) { |
||
687 | int v = abyte & 0xff; |
||
688 | builder.append(String.format("%02x", v)); |
||
689 | } |
||
690 | return builder.toString();
|
||
691 | } |
||
692 | |||
693 | @Override
|
||
694 | public int getSRSId(IProjection projection) { |
||
695 | String abrev = projection.getAbrev();
|
||
696 | return Integer.valueOf(abrev.split(":")[1]); |
||
697 | } |
||
698 | |||
699 | @Override
|
||
700 | public Constant srs(IProjection projection) {
|
||
701 | return constant(getSRSId(projection));
|
||
702 | } |
||
703 | |||
704 | @Override
|
||
705 | public Variable variable(String name) { |
||
706 | return new VariableBase(name); |
||
707 | } |
||
708 | |||
709 | @Override
|
||
710 | public Variable column(String name) { |
||
711 | return new VariableBase(name); |
||
712 | } |
||
713 | |||
714 | @Override
|
||
715 | public Parameter parameter(Object value) { |
||
716 | return new ParameterBase(value); |
||
717 | } |
||
718 | |||
719 | @Override
|
||
720 | public Constant constant(Object value) { |
||
721 | return new ConstantBase(value); |
||
722 | } |
||
723 | |||
724 | @Override
|
||
725 | public Group group(Value value) { |
||
726 | return new GroupBase(value); |
||
727 | } |
||
728 | |||
729 | @Override
|
||
730 | public GeometryValue geometry(Geometry geom, IProjection projection) {
|
||
731 | return new GeometryValueBase(geom, projection); |
||
732 | } |
||
733 | |||
734 | @Override
|
||
735 | public Custom custom(Object value) { |
||
736 | return new CustomBase(value); |
||
737 | } |
||
738 | |||
739 | public Function function(String name, String format, Value...values) { |
||
740 | FunctionBase func = new FunctionBase(name,format);
|
||
741 | for (Value value : values) {
|
||
742 | func.parameter(value); |
||
743 | } |
||
744 | return func;
|
||
745 | } |
||
746 | |||
747 | public BinaryOperator binaryOperator(String name, String format, Value leftOperand, Value rightOperand) { |
||
748 | BinaryOperator operator = new BinaryOperatorBase(name,format);
|
||
749 | operator.setLeft(leftOperand); |
||
750 | operator.setRight(rightOperand); |
||
751 | return operator;
|
||
752 | } |
||
753 | |||
754 | @Override
|
||
755 | public List<Variable> getVariables() { |
||
756 | final Set<Variable> vars = new HashSet<>(); |
||
757 | this.accept(new Visitor() { |
||
758 | @Override
|
||
759 | public void visit(Visitable value) { |
||
760 | vars.add((Variable)value); |
||
761 | } |
||
762 | }, new ClassVisitorFilter(Variable.class) );
|
||
763 | List<Variable> lvars = new ArrayList<>(vars); |
||
764 | Collections.sort(lvars);
|
||
765 | return lvars;
|
||
766 | } |
||
767 | |||
768 | @Override
|
||
769 | public List<Parameter> getParameters() { |
||
770 | final List<Parameter> params = new ArrayList<>(); |
||
771 | this.accept(new Visitor() { |
||
772 | @Override
|
||
773 | public void visit(Visitable value) { |
||
774 | params.add((Parameter)value); |
||
775 | } |
||
776 | }, new ClassVisitorFilter(Parameter.class) );
|
||
777 | return params;
|
||
778 | } |
||
779 | |||
780 | @Override
|
||
781 | public Function getAsGeometry(Value value) {
|
||
782 | return function("ST_AsBinary", config.getString(Config.ST_AsBinary), value); |
||
783 | } |
||
784 | |||
785 | @Override
|
||
786 | public ExpressionBuilder set(Value value) {
|
||
787 | this.value = value;
|
||
788 | return this; |
||
789 | } |
||
790 | |||
791 | @Override
|
||
792 | public ExpressionBuilder and(Value value) {
|
||
793 | if( this.value == null ) { |
||
794 | return this.set(value); |
||
795 | } |
||
796 | BinaryOperator operator = binaryOperator("AND", config.getString(Config.operator_AND), this.value, value); |
||
797 | this.value = operator;
|
||
798 | return this; |
||
799 | } |
||
800 | |||
801 | @Override
|
||
802 | public ExpressionBuilder or(Value value) {
|
||
803 | if( this.value == null ) { |
||
804 | return this.set(value); |
||
805 | } |
||
806 | BinaryOperator operator = binaryOperator("OR", config.getString(Config.operator_OR), this.value, value); |
||
807 | this.value = operator;
|
||
808 | return this; |
||
809 | } |
||
810 | |||
811 | @Override
|
||
812 | public Function Find_SRID(Value schema, Value table, Value column) {
|
||
813 | return function("Find_SRID", config.getString(Config.Find_SRID), schema, table, column); |
||
814 | } |
||
815 | |||
816 | @Override
|
||
817 | public Function ST_Intersects(Value geom1, Value geom2) {
|
||
818 | return function("ST_Intersects", config.getString(Config.ST_Intersects), geom1, geom2); |
||
819 | } |
||
820 | |||
821 | @Override
|
||
822 | public Function ST_SRID(Value geom) {
|
||
823 | return function("ST_SRID", config.getString(Config.ST_SRID), geom); |
||
824 | } |
||
825 | |||
826 | @Override
|
||
827 | public Function ST_Envelope(Value geom) {
|
||
828 | return function("ST_Envelope", config.getString(Config.ST_Envelope), geom); |
||
829 | } |
||
830 | |||
831 | @Override
|
||
832 | public Function ST_AsText(Value geom) {
|
||
833 | return function("ST_AsText", config.getString(Config.ST_AsText), geom); |
||
834 | } |
||
835 | |||
836 | @Override
|
||
837 | public Function ST_AsBinary(Value geom) {
|
||
838 | return function("ST_AsBinary", config.getString(Config.ST_AsBinary), geom); |
||
839 | } |
||
840 | |||
841 | @Override
|
||
842 | public Function ST_AsEWKB(Value geom) {
|
||
843 | return function("ST_AsEWKB", config.getString(Config.ST_AsEWKB), geom); |
||
844 | } |
||
845 | |||
846 | @Override
|
||
847 | public Function ST_GeomFromText(Value geom, Value crs) {
|
||
848 | return function("ST_GeomFromText", config.getString(Config.ST_GeomFromText), geom, crs); |
||
849 | } |
||
850 | |||
851 | @Override
|
||
852 | public Function ST_GeomFromWKB(Value geom, Value crs) {
|
||
853 | return function("ST_GeomFromWKB", config.getString(Config.ST_GeomFromWKB), geom, crs); |
||
854 | } |
||
855 | |||
856 | @Override
|
||
857 | public Function ST_GeomFromEWKB(Value geom, Value crs) {
|
||
858 | return function("ST_GeomFromEWKB", config.getString(Config.ST_GeomFromEWKB), geom, crs); |
||
859 | } |
||
860 | |||
861 | @Override
|
||
862 | public Function ST_Contains(Value geom1, Value geom2) {
|
||
863 | return function("ST_Contains", config.getString(Config.ST_Contains), geom1, geom2); |
||
864 | } |
||
865 | |||
866 | @Override
|
||
867 | public Function ST_Crosses(Value geom1, Value geom2) {
|
||
868 | return function("ST_Crosses", config.getString(Config.ST_Crosses), geom1, geom2); |
||
869 | } |
||
870 | |||
871 | @Override
|
||
872 | public Function ST_IsClosed(Value geom) {
|
||
873 | return function("ST_IsClosed", config.getString(Config.ST_IsClosed), geom); |
||
874 | } |
||
875 | |||
876 | @Override
|
||
877 | public Function ST_Overlaps(Value geom1, Value geom2) {
|
||
878 | return function("ST_Overlaps", config.getString(Config.ST_Overlaps), geom1, geom2); |
||
879 | } |
||
880 | |||
881 | @Override
|
||
882 | public Function ST_Touches(Value geom1, Value geom2) {
|
||
883 | return function("ST_Touches", config.getString(Config.ST_Touches), geom1, geom2); |
||
884 | } |
||
885 | |||
886 | @Override
|
||
887 | public Function ST_Within(Value geom1, Value geom2) {
|
||
888 | return function("ST_Within", config.getString(Config.ST_Within), geom1, geom2); |
||
889 | } |
||
890 | |||
891 | @Override
|
||
892 | public Function isNull(Value value) {
|
||
893 | return function("IS NULL", config.getString(Config.isNull), value); |
||
894 | } |
||
895 | |||
896 | @Override
|
||
897 | public Function notIsNull(Value value) {
|
||
898 | return function("NOT IS NULL", config.getString(Config.notIsNull), value); |
||
899 | } |
||
900 | |||
901 | @Override
|
||
902 | public Function not(Value value) {
|
||
903 | return function("NOT", config.getString(Config.operator_not), value); |
||
904 | } |
||
905 | |||
906 | @Override
|
||
907 | public Function lcase(Value s) {
|
||
908 | return function("LCASE", config.getString(Config.lcase), s); |
||
909 | } |
||
910 | |||
911 | @Override
|
||
912 | public Function ucase(Value s) {
|
||
913 | return function("UCASE", config.getString(Config.ucase), s); |
||
914 | } |
||
915 | |||
916 | |||
917 | @Override
|
||
918 | public BinaryOperator and(Value leftOperand, Value rightOperand) {
|
||
919 | return binaryOperator("AND", config.getString(Config.operator_AND), leftOperand, rightOperand); |
||
920 | } |
||
921 | |||
922 | @Override
|
||
923 | public BinaryOperator or(Value leftOperand, Value rightOperand) {
|
||
924 | return binaryOperator("OR", config.getString(Config.operator_OR), leftOperand, rightOperand); |
||
925 | } |
||
926 | |||
927 | @Override
|
||
928 | public BinaryOperator eq(Value leftOperand, Value rightOperand) {
|
||
929 | return binaryOperator("=", config.getString(Config.operator_EQ), leftOperand, rightOperand); |
||
930 | } |
||
931 | |||
932 | @Override
|
||
933 | public BinaryOperator ne(Value leftOperand, Value rightOperand) {
|
||
934 | return binaryOperator("<>", config.getString(Config.operator_NE), leftOperand, rightOperand); |
||
935 | } |
||
936 | |||
937 | @Override
|
||
938 | public BinaryOperator gt(Value op1, Value op2) {
|
||
939 | return binaryOperator(">", config.getString(Config.operator_GT), op1, op2); |
||
940 | } |
||
941 | |||
942 | @Override
|
||
943 | public BinaryOperator ge(Value op1, Value op2) {
|
||
944 | return binaryOperator(">=", config.getString(Config.operator_GE), op1, op2); |
||
945 | } |
||
946 | |||
947 | @Override
|
||
948 | public BinaryOperator lt(Value op1, Value op2) {
|
||
949 | return binaryOperator("<", config.getString(Config.operator_LT), op1, op2); |
||
950 | } |
||
951 | |||
952 | @Override
|
||
953 | public BinaryOperator le(Value op1, Value op2) {
|
||
954 | return binaryOperator("<=", config.getString(Config.operator_LE), op1, op2); |
||
955 | } |
||
956 | |||
957 | @Override
|
||
958 | public BinaryOperator like(Value op1, Value op2) {
|
||
959 | return binaryOperator("LIKE", config.getString(Config.operator_LIKE), op1, op2); |
||
960 | } |
||
961 | |||
962 | @Override
|
||
963 | public BinaryOperator ilike(Value op1, Value op2) {
|
||
964 | return binaryOperator("ILIKE", config.getString(Config.operator_ILIKE), op1, op2); |
||
965 | } |
||
966 | |||
967 | @Override
|
||
968 | public BinaryOperator add(Value op1, Value op2) {
|
||
969 | return binaryOperator("ADD", config.getString(Config.operator_add), op1, op2); |
||
970 | } |
||
971 | |||
972 | @Override
|
||
973 | public BinaryOperator subst(Value op1, Value op2) {
|
||
974 | return binaryOperator("SUBST", config.getString(Config.operator_subst), op1, op2); |
||
975 | } |
||
976 | |||
977 | @Override
|
||
978 | public BinaryOperator mult(Value op1, Value op2) {
|
||
979 | return binaryOperator("MULT", config.getString(Config.operator_mult), op1, op2); |
||
980 | } |
||
981 | |||
982 | @Override
|
||
983 | public BinaryOperator div(Value op1, Value op2) {
|
||
984 | return binaryOperator("DIV", config.getString(Config.operator_div), op1, op2); |
||
985 | } |
||
986 | |||
987 | @Override
|
||
988 | public BinaryOperator concat(Value op1, Value op2) {
|
||
989 | return binaryOperator("CONCAT", config.getString(Config.operator_concat), op1, op2); |
||
990 | } |
||
991 | |||
992 | } |