Revision 44198 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/SQLBuilderBase.java

View differences:

SQLBuilderBase.java
2 2

  
3 3
import java.text.MessageFormat;
4 4
import java.util.ArrayList;
5
import java.util.Collection;
6 5
import java.util.Collections;
7 6
import java.util.HashMap;
8 7
import java.util.HashSet;
......
10 9
import java.util.Map;
11 10
import java.util.Objects;
12 11
import java.util.Set;
13
import org.apache.commons.lang3.ObjectUtils;
14 12
import org.apache.commons.lang3.StringUtils;
15 13
import org.apache.commons.lang3.tuple.ImmutablePair;
16 14
import org.apache.commons.lang3.tuple.Pair;
17 15
import org.cresques.cts.IProjection;
18 16
import org.gvsig.expressionevaluator.ExpressionBuilder;
19
import static org.gvsig.expressionevaluator.ExpressionBuilder.Config.geometry_type_support;
20
import static org.gvsig.expressionevaluator.ExpressionBuilder.Config.has_spatial_functions;
17
import org.gvsig.expressionevaluator.ExpressionBuilder.AbstractValue;
18
import org.gvsig.expressionevaluator.ExpressionBuilder.ClassVisitorFilter;
19
import org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType;
20
import org.gvsig.expressionevaluator.ExpressionBuilder.Parameter;
21
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
22
import org.gvsig.expressionevaluator.ExpressionBuilder.Variable;
23
import org.gvsig.expressionevaluator.ExpressionBuilder.Visitable;
24
import org.gvsig.expressionevaluator.ExpressionBuilder.Visitor;
25
import org.gvsig.expressionevaluator.ExpressionBuilder.VisitorFilter;
21 26
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
27
import org.gvsig.expressionevaluator.Formatter;
22 28
import org.gvsig.fmap.dal.DataStoreParameters;
23 29
import org.gvsig.fmap.dal.DataTypes;
24 30
import org.gvsig.fmap.dal.SQLBuilder;
......
32 38
import org.gvsig.fmap.dal.SQLBuilder.InsertColumnBuilder;
33 39
import org.gvsig.fmap.dal.SQLBuilder.OrderByBuilder;
34 40
import org.gvsig.fmap.dal.SQLBuilder.Privilege;
35
import org.gvsig.fmap.dal.SQLBuilder.SQLConfig;
36 41
import org.gvsig.fmap.dal.SQLBuilder.SelectBuilder;
37 42
import org.gvsig.fmap.dal.SQLBuilder.SelectColumnBuilder;
38 43
import org.gvsig.fmap.dal.SQLBuilder.TableNameBuilder;
......
40 45
import org.gvsig.fmap.dal.SQLBuilder.UpdateColumnBuilder;
41 46
import org.gvsig.fmap.dal.SQLBuilder.UpdateTableStatisticsBuilder;
42 47
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
43
import org.gvsig.fmap.dal.feature.FeatureStore;
44 48
import org.gvsig.fmap.geom.Geometry;
45
import org.gvsig.fmap.geom.primitive.Envelope;
46 49
import org.slf4j.Logger;
47 50
import org.slf4j.LoggerFactory;
48 51

  
52
@SuppressWarnings("UseSpecificCatch")
49 53
public class SQLBuilderBase implements SQLBuilder {
50
    
51
    protected static final Logger logger = LoggerFactory.getLogger(SQLBuilderBase.class);
52
    
54

  
55
    protected static final Logger LOGGER = LoggerFactory.getLogger(SQLBuilderBase.class);
56

  
53 57
    protected SelectBuilder select;
54 58
    protected UpdateBuilder update;
55 59
    protected InsertBuilder insert;
......
60 64
    protected DropTableBuilder drop_table;
61 65
    protected UpdateTableStatisticsBuilder update_table_statistics;
62 66
    protected CreateIndexBuilder create_index;
63
    protected List<Parameter> parameters;
64 67

  
65
    public class ConfigBase implements Config {
68
    protected abstract class AbstractStatementPart extends AbstractValue {
66 69

  
67
        protected Map<String, Object> values;
68

  
69
        public ConfigBase() {
70
            this.values = new HashMap<>();
71

  
72
        }
73

  
74
        @Override
75
        public boolean has_functionality(String functionality) {
76
            Object x = this.values.get(functionality);
77
            if (x == null) {
78
                return false;
79
            }
80
            if (x instanceof CharSequence && StringUtils.isEmpty((CharSequence) x)) {
81
                return false;
82
            }
83
            return true;
84
        }
85

  
86
        @Override
87
        public void remove_functionality(String functionality) {
88
            this.values.remove(functionality);
89
        }
90

  
91
        @Override
92
        public boolean has_spatial_functions() {
93
            return this.getBoolean(has_spatial_functions);
94
        }
95

  
96
        @Override
97
        public GeometrySupportType getGeometryTypeSupport() {
98
            return (GeometrySupportType) this.get(geometry_type_support);
99
        }
100

  
101
        @Override
102
        public boolean getBoolean(String name) {
103
            return (boolean) this.values.get(name);
104
        }
105

  
106
        @Override
107
        public String getString(String name) {
108
            return (String) this.values.get(name);
109
        }
110

  
111
        @Override
112
        public Object get(String name) {
113
            return this.values.get(name);
114
        }
115

  
116
        @Override
117
        public void set(String name, Object value) {
118
            this.values.put(name, value);
119
        }
120 70
    }
121 71

  
122
    public abstract class AbstractValue implements Value {
72
    protected abstract class AbstractStatement extends AbstractStatementPart {
123 73

  
124
        @Override
125
        public void accept(Visitor visitor, VisitorFilter filter) {
126
            if (filter == null || filter.accept(this)) {
127
                visitor.visit(this);
128
            }
129
        }
130

  
131 74
    }
132 75

  
133
    public class ClassVisitorFilter implements VisitorFilter {
134

  
135
        private final Class classFilter;
136

  
137
        public ClassVisitorFilter(Class classFilter) {
138
            this.classFilter = classFilter;
139
        }
140

  
141
        @Override
142
        public boolean accept(Visitable visitable) {
143
            return classFilter.isInstance(visitable);
144
        }
145

  
146
    }
147

  
148
    public class GroupBase extends AbstractValue implements Group {
149

  
150
        protected Value value;
151

  
152
        public GroupBase(Value value) {
153
            this.value = value;
154
        }
155

  
156
        @Override
157
        public Value getValue() {
158
            return value;
159
        }
160

  
161
        @Override
162
        public void accept(Visitor visitor, VisitorFilter filter) {
163
            super.accept(visitor, filter);
164
            this.value.accept(visitor, filter);
165
        }
166

  
167
        @Override
168
        public String toString() {
169
            return MessageFormat.format(config.getString(Config.group), this.value.toString());
170
        }
171
    }
172
    
173 76
    protected class ColumnDescriptorBase implements ColumnDescriptor {
174 77

  
175 78
        private String name;
......
215 118
            this.geom_srsdbcode = null;
216 119
            this._isIndexed = isIndexed;
217 120
        }
218
        
121

  
219 122
        public ColumnDescriptorBase(String name, int geom_type, int geom_subtype, IProjection proj, boolean isIndexed, boolean allowNulls) {
220 123
            this.name = name;
221 124
            this.type = DataTypes.GEOMETRY;
......
227 130
            this.defaultValue = null;
228 131
            this.geom_type = geom_type;
229 132
            this.geom_subtype = geom_subtype;
230
            this.geom_srsdbcode = getSRSId(proj);
133
            this.geom_srsdbcode = srs_id(proj);
231 134
            this._isIndexed = isIndexed;
232 135
        }
233
        
136

  
234 137
        public ColumnDescriptorBase(String name, int geom_type, int geom_subtype, Object srsdbcode, boolean isIndexed, boolean allowNulls) {
235 138
            this.name = name;
236 139
            this.type = DataTypes.GEOMETRY;
......
245 148
            this.geom_srsdbcode = srsdbcode;
246 149
            this._isIndexed = isIndexed;
247 150
        }
248
        
151

  
152
        private ColumnDescriptorBase(FeatureAttributeDescriptor fad) {
153
            this(fad.getName(), fad.getType(), fad.getDefaultValue());
154
            this.type_p = fad.getPrecision();
155
            this.type_s = fad.getSize();
156
            this.isPk = fad.isPrimaryKey();
157
            this._allowNulls = fad.allowNull();
158
            this._isAutomatic = fad.isAutomatic();
159
            this._isIndexed = fad.isIndexed();
160

  
161
            if (fad.getType() == org.gvsig.fmap.geom.DataTypes.GEOMETRY) {
162
                this.geom_type = fad.getGeomType().getType();
163
                this.geom_subtype = fad.getGeomType().getSubType();
164
                this.geom_srsdbcode = fad.getSRS();
165
            }
166
        }
167
    
168

  
249 169
        @Override
250 170
        public String getName() {
251 171
            return this.name;
252 172
        }
253
        
173

  
254 174
        @Override
255 175
        public void setName(String name) {
256 176
            this.name = name;
......
359 279
        @Override
360 280
        public void setGeometrySRSId(Object geom_srsid) {
361 281
            this.geom_srsdbcode = geom_srsid;
362
        }        
282
        }
363 283

  
364 284
        @Override
365 285
        public boolean isGeometry() {
......
375 295
            return this.parameters;
376 296
        }
377 297
    }
298
//
299
//    public class ColumnBase extends AbstractValue implements Column {
300
//
301
//        protected ColumnDescriptor descriptor;
302
//        protected String name;
303
//
304
//        public ColumnBase(ColumnDescriptor descriptor) {
305
//            this.name = descriptor.getName();
306
//            this.descriptor = descriptor;
307
//        }
308
//
309
//        public ColumnBase(String name) {
310
//            this.name = name;
311
//            this.descriptor = null;
312
//        }
313
//
314
//        @Override
315
//        public ColumnDescriptor getDescriptor() {
316
//            return descriptor;
317
//        }
318
//
319
//        @Override
320
//        public String name() {
321
//            return this.name;
322
//        }
323
//
324
//        @Override
325
//        public String toString() {
326
//            return this.toString(formatter());
327
//        }
328
//
329
//        @Override
330
//        public String toString(Formatter<Value> formatter) {
331
//            if (formatter.canApply(this)) {
332
//                return formatter.format(this);
333
//            }
334
//            return as_identifier(this.name);
335
//        }
336
//
337
//        @Override
338
//        public int compareTo(ExpressionBuilder.Variable o) {
339
//            return this.name.compareTo(o.name());
340
//        }
341
//
342
//        @Override
343
//        public boolean equals(Object obj) {
344
//            if (!(obj instanceof ExpressionBuilder.Variable)) {
345
//                return false;
346
//            }
347
//            return this.name.equals(((ExpressionBuilder.Variable) obj).name());
348
//        }
349
//
350
//        @Override
351
//        public int hashCode() {
352
//            int hash = 7;
353
//            hash = 37 * hash + Objects.hashCode(this.name);
354
//            return hash;
355
//        }
356
//
357
//    }
378 358

  
379
    public class VariableBase extends AbstractValue implements Column {
359
    public class TableNameBuilderBase
360
            extends AbstractStatementPart
361
            implements TableNameBuilder {
380 362

  
381
        protected ColumnDescriptor descriptor;
382
        protected String name;
383

  
384

  
385
        public VariableBase(ColumnDescriptor descriptor) {
386
            this.name = descriptor.getName();
387
            this.descriptor = descriptor;
388
        }
389

  
390
        public VariableBase(String name) {
391
            this.name = name;
392
            this.descriptor = null;
393
        }
394

  
395
        @Override
396
        public ColumnDescriptor getDescriptor() {
397
            return descriptor;
398
        }
399

  
400
        @Override
401
        public String getName() {
402
            return this.name;
403
        }
404

  
405
        @Override
406
        public String toString() {
407
            return identifier(this.name);
408
        }
409

  
410
        @Override
411
        public int compareTo(ExpressionBuilder.Variable o) {
412
            return this.name.compareTo(o.getName());
413
        }
414

  
415
        @Override
416
        public boolean equals(Object obj) {
417
            if (!(obj instanceof ExpressionBuilder.Variable)) {
418
                return false;
419
            }
420
            return this.name.equals(((ExpressionBuilder.Variable) obj).getName());
421
        }
422

  
423
        @Override
424
        public int hashCode() {
425
            int hash = 7;
426
            hash = 37 * hash + Objects.hashCode(this.name);
427
            return hash;
428
        }
429

  
430
    }
431

  
432
    
433
    public class ParameterBase extends AbstractValue implements Parameter {
434

  
435
        protected String name;
436
        protected Object value;
437
        protected ParameterType type;
438
        protected Value srs;
439

  
440
        public ParameterBase() {
441
            this.type = ParameterType.Constant;
442
            this.name = null;
443
            this.value = null;
444
        }
445

  
446
        @Override
447
        public void accept(Visitor visitor, VisitorFilter filter) {
448
            super.accept(visitor, filter);
449
            if (this.srs != null) {
450
                this.srs.accept(visitor, filter);
451
            }
452
        }
453

  
454
        @Override
455
        public Parameter as_geometry_variable() {
456
            this.type = ParameterType.Geometry;
457
            if (this.value == null && this.name != null) {
458
                this.value = this.name;
459
            }
460
            return this;
461
        }
462

  
463
        @Override
464
        public Parameter as_constant() {
465
            this.type = ParameterType.Constant;
466
            if (this.value == null && this.name != null) {
467
                this.value = this.name;
468
            }
469
            return this;
470
        }
471

  
472
        @Override
473
        public Parameter as_variable() {
474
            this.type = ParameterType.Variable;
475
            if (this.value != null && this.name == null) {
476
                this.name = (String) this.value;
477
            }
478
            return this;
479
        }
480

  
481
        @Override
482
        public Parameter srs(Value srs) {
483
            this.srs = srs;
484
            if( this.type == ParameterType.Variable ) {
485
                this.type = ParameterType.Geometry;
486
            }
487
            return this;
488
        }
489

  
490
        @Override
491
        public Parameter srs(IProjection srs) {
492
            this.srs = constant(getSRSId(srs));
493
            if( this.type == ParameterType.Variable ) {
494
                this.type = ParameterType.Geometry;
495
            }
496
            return this;
497
        }
498

  
499
        @Override
500
        public String getName() {
501
            switch (this.type) {
502
                case Variable:
503
                case Geometry:
504
                    return this.name;
505
                case Constant:
506
                    if (this.value == null) {
507
                        return null;
508
                    }
509
                    return this.value.toString();
510
                default:
511
                    if (this.name != null) {
512
                        return this.name;
513
                    }
514
                    if (this.value != null) {
515
                        return this.value.toString();
516
                    }
517
                    return null;
518
            }
519
        }
520

  
521
        @Override
522
        public boolean is_constant() {
523
            return this.type == ParameterType.Constant;
524
        }
525

  
526
        @Override
527
        public boolean is_geometry_variable() {
528
            return this.type == ParameterType.Geometry;
529
        }
530

  
531
        @Override
532
        public boolean is_variable() {
533
            return this.type == ParameterType.Variable;
534
        }
535

  
536
        @Override
537
        public Parameter value(Object value) {
538
            this.value = value;
539
            return this;
540
        }
541

  
542
        @Override
543
        public Parameter name(String name) {
544
            this.type = ParameterType.Variable;
545
            this.name = name;
546
            return this;
547
        }
548

  
549
        @Override
550
        public Object getValue() {
551
            try {
552
                switch (this.type) {
553
                    case Constant:
554
                        if (this.value instanceof Geometry) {
555
                            switch (config.getGeometryTypeSupport()) {
556
                                case EWKB:
557
                                    return bytearray(((Geometry) this.value).convertToEWKB());
558
                                case NATIVE:
559
                                case WKB:
560
                                    return bytearray(((Geometry) this.value).convertToWKB());
561
                                case WKT:
562
                                default:
563
                                    return ((Geometry) this.value).convertToWKT();
564
                            }
565
                        } else if (this.value instanceof IProjection) {
566
                            return getSRSId((IProjection) this.value);
567
                        }
568
                        return this.value;
569
                    case Variable:
570
                    case Geometry:
571
                    default:
572
                        return this.value;
573
                }
574
            } catch (Exception ex) {
575
                throw new RuntimeException("Can't get value from parameter.", ex);
576
            }
577
        }
578

  
579
        @Override
580
        public ParameterType getType() {
581
            return this.type;
582
        }
583

  
584
        @Override
585
        public Value getSRS() {
586
            return this.srs;
587
        }
588

  
589
        @Override
590
        public String toString() {
591
            switch (this.type) {
592
                case Constant:
593
                case Variable:
594
                default:
595
                    return "?";
596
                case Geometry:
597
                    switch (config.getGeometryTypeSupport()) {
598
                        case EWKB:
599
                            return MessageFormat.format(
600
                                    config.getString(Config.ST_GeomFromEWKB),
601
                                    "?",
602
                                    String.valueOf(this.srs.toString())
603
                            );
604
                        case WKB:
605
                            return MessageFormat.format(
606
                                    config.getString(Config.ST_GeomFromWKB),
607
                                    "?",
608
                                    String.valueOf(this.srs.toString())
609
                            );
610
                        case WKT:
611
                        default:
612
                            return MessageFormat.format(
613
                                    config.getString(Config.ST_GeomFromText),
614
                                    "?",
615
                                    String.valueOf(this.srs.toString())
616
                            );
617
                    }
618
            }
619
        }
620
    }
621

  
622
    public class ConstantBase extends AbstractValue implements Constant {
623

  
624
        protected Object value;
625

  
626
        public ConstantBase(Object value) {
627
            this.value = value;
628
        }
629

  
630
        @Override
631
        public Object getValue() {
632
            return this.value;
633
        }
634

  
635
        @Override
636
        public String toString() {
637
            if (this.value instanceof String) {
638
                return string((String) this.value);
639
            }
640
            if (this.value instanceof Boolean) {
641
                if (((Boolean) this.value)) {
642
                    return config.getString(Config.constant_true);
643
                } else {
644
                    return config.getString(Config.constant_false);
645
                }
646
            }
647
            return ObjectUtils.toString(this.value, "");
648
        }
649
    }
650

  
651
    public class CustomBase extends AbstractValue implements Custom {
652

  
653
        protected Object value;
654

  
655
        // Esto es para permitir declarar parametros y columnas en una seccion
656
        // custom.
657
        protected List<Value> values;
658

  
659
        public CustomBase(Object value) {
660
            this.value = value;
661
        }
662

  
663
        @Override
664
        public void accept(Visitor visitor, VisitorFilter filter) {
665
            super.accept(visitor, filter);
666
            if (this.values != null) {
667
                for (Value value : values) {
668
                    value.accept(visitor, filter);
669
                }
670
            }
671
        }
672

  
673
        @Override
674
        public Object getValue() {
675
            return this.value;
676
        }
677

  
678
        @Override
679
        public Custom add(Variable variable) {
680
            if (this.values == null) {
681
                this.values = new ArrayList<>();
682
            }
683
            this.values.add(variable);
684
            return this;
685
        }
686

  
687
        @Override
688
        public Custom add(Parameter parameter) {
689
            if (this.values == null) {
690
                this.values = new ArrayList<>();
691
            }
692
            this.values.add(parameter);
693
            return this;
694
        }
695

  
696
        @Override
697
        public String toString() {
698
            return ObjectUtils.toString(this.value, "");
699
        }
700
    }
701

  
702
    public class GeometryValueBase extends AbstractValue implements GeometryValue {
703

  
704
        protected Geometry geometry;
705
        protected IProjection projection;
706

  
707
        public GeometryValueBase(Geometry geometry, IProjection projection) {
708
            this.geometry = geometry;
709
            this.projection = projection;
710
        }
711

  
712
        @Override
713
        public Geometry getGeometry() {
714
            return this.geometry;
715
        }
716

  
717
        @Override
718
        public IProjection getSRS() {
719
            return this.projection;
720
        }
721

  
722
        @Override
723
        public String toString() {
724
            try {
725
                switch (config.getGeometryTypeSupport()) {
726
                    case EWKB:
727
                        return MessageFormat.format(
728
                                config.getString(Config.ST_GeomFromEWKB),
729
                                bytearray(this.geometry.convertToEWKB()),
730
                                String.valueOf(getSRSId(this.projection))
731
                        );
732
                    case WKB:
733
                        return MessageFormat.format(
734
                                config.getString(Config.ST_GeomFromWKB),
735
                                bytearray(this.geometry.convertToWKB()),
736
                                String.valueOf(getSRSId(this.projection))
737
                        );
738
                    case WKT:
739
                    default:
740
                        return MessageFormat.format(
741
                                config.getString(Config.ST_GeomFromText),
742
                                string(this.geometry.convertToWKT()),
743
                                String.valueOf(getSRSId(this.projection))
744
                        );
745
                }
746
            } catch (Exception ex) {
747
                throw new RuntimeException("Can't convert geometry to string.", ex);
748
            }
749
        }
750
    }
751

  
752
    public class FunctionBase extends AbstractValue implements Function {
753

  
754
        protected String name;
755
        protected String format;
756
        protected List<Value> parameters;
757

  
758
        public FunctionBase(String name, String format) {
759
            this.name = name;
760
            this.format = format;
761
        }
762

  
763
        public FunctionBase(String name) {
764
            this(name,null);
765
        }
766
        
767
        @Override
768
        public List<Value> parameters() {
769
            if (this.parameters == null) {
770
                this.parameters = new ArrayList<>();
771
            }
772
            return this.parameters;
773
        }
774

  
775
        @Override
776
        public Function parameter(Value parameter) {
777
            this.parameters().add(parameter);
778
            return this;
779
        }
780

  
781
        @Override
782
        public String getName() {
783
            return this.name;
784
        }
785

  
786
        @Override
787
        public void accept(Visitor visitor, VisitorFilter filter) {
788
            super.accept(visitor, filter);
789
            for (Value value : this.parameters) {
790
                value.accept(visitor, filter);
791
            }
792
        }
793

  
794
        @Override
795
        public String toString() {
796
            if( this.format==null ) {
797
                StringBuilder builder = new StringBuilder();
798
                builder.append(name);
799
                builder.append("(");
800
                if (this.parameters != null && !this.parameters.isEmpty()) {
801
                    boolean first = true;
802
                    for (Value value : this.parameters) {
803
                        if( first ) {
804
                            first=false;
805
                            builder.append(value.toString());
806
                        } else {
807
                            builder.append(", ");
808
                            builder.append(value.toString());
809
                        }
810
                    }
811
                }
812
                builder.append(")");
813
                return builder.toString();
814
            }
815
            if (this.parameters != null && !this.parameters.isEmpty()) {
816
                List<String> values = new ArrayList<>();
817
                for (Value value : this.parameters) {
818
                    values.add(value.toString());
819
                }
820
                return MessageFormat.format(format, values.toArray());
821
            } else {
822
                return this.format;
823
            }
824
        }
825
    }
826

  
827
    public class BinaryOperatorBase extends AbstractValue implements BinaryOperator {
828

  
829
        protected String name;
830
        protected String format;
831
        protected Value left;
832
        protected Value right;
833

  
834
        public BinaryOperatorBase(String name, String format) {
835
            this.name = name;
836
            this.format = format;
837
        }
838

  
839
        @Override
840
        public String getName() {
841
            return this.name;
842
        }
843

  
844
        @Override
845
        public void accept(Visitor visitor, VisitorFilter filter) {
846
            super.accept(visitor, filter);
847
            this.left.accept(visitor, filter);
848
            this.right.accept(visitor, filter);
849
        }
850

  
851
        @Override
852
        public BinaryOperator setLeft(Value operand) {
853
            this.left = operand;
854
            return this;
855
        }
856

  
857
        @Override
858
        public BinaryOperator setRight(Value operand) {
859
            this.right = operand;
860
            return this;
861
        }
862

  
863
        @Override
864
        public Value getLeft() {
865
            return this.left;
866
        }
867

  
868
        @Override
869
        public Value getRight() {
870
            return this.right;
871
        }
872

  
873
        @Override
874
        public String toString() {
875
            return MessageFormat.format(
876
                    format,
877
                    this.left.toString(),
878
                    this.right.toString()
879
            );
880
        }
881
    }
882
    
883
    public class TableNameBuilderBase implements TableNameBuilder {
884

  
885 363
        public String tableName;
886 364
        public String schemaName;
887 365
        private String databaseName;
......
891 369

  
892 370
        @Override
893 371
        public void accept(Visitor visitor, VisitorFilter filter) {
894
            if( filter.accept(this) ) {
372
            if (filter.accept(this)) {
895 373
                visitor.visit(this);
896 374
            }
897 375
        }
......
904 382

  
905 383
        @Override
906 384
        public TableNameBuilder schema(String name) {
907
            if( supportSchemas() ) {
385
            if (support_schemas()) {
908 386
                this.schemaName = name;
909 387
            }
910 388
            return this;
......
930 408
        public String getName() {
931 409
            return this.tableName;
932 410
        }
933
        
411

  
934 412
        @Override
935 413
        public boolean has_schema() {
936
            if( !supportSchemas() ) {
414
            if (!support_schemas()) {
937 415
                return false;
938 416
            }
939 417
            return !StringUtils.isEmpty(this.schemaName);
......
943 421
        public boolean has_database() {
944 422
            return !StringUtils.isEmpty(this.databaseName);
945 423
        }
946
        
424

  
947 425
        @Override
948 426
        public String toString() {
949
            if( this.has_database()) {
950
                if( this.has_schema()) {
951
                    return identifier(this.databaseName) + "." + 
952
                           identifier(this.schemaName) + "." + 
953
                           identifier(this.tableName);
427
            return this.toString(formatter());
428
        }
429

  
430
        @Override
431
        public String toString(Formatter<Value> formatter) {
432
            if (formatter.canApply(this)) {
433
                return formatter.format(this);
434
            }
435
            if (this.has_database()) {
436
                if (this.has_schema()) {
437
                    return as_identifier(this.databaseName) + "."
438
                            + as_identifier(this.schemaName) + "."
439
                            + as_identifier(this.tableName);
954 440
                }
955 441
            } else {
956
                if( this.has_schema()) {
957
                    return identifier(this.schemaName) + "." + 
958
                           identifier(this.tableName);
959
                }                
442
                if (this.has_schema()) {
443
                    return as_identifier(this.schemaName) + "."
444
                            + as_identifier(this.tableName);
445
                }
960 446
            }
961
            return identifier(this.tableName);
447
            return as_identifier(this.tableName);
962 448
        }
963 449

  
964 450
    }
965 451

  
966
    public class CountBuilderBase extends AbstractValue implements CountBuilder {
452
    public class CountBuilderBase
453
            extends AbstractStatementPart
454
            implements CountBuilder {
967 455

  
968 456
        protected Value value;
969 457
        protected boolean distinct;
970
        protected boolean all ;
971
        
458
        protected boolean all;
459

  
972 460
        public CountBuilderBase() {
973 461
            this.value = null;
974 462
            this.distinct = false;
975 463
            this.all = false;
976 464
        }
977
        
465

  
978 466
        @Override
979 467
        public CountBuilder all() {
980 468
            this.all = true;
......
995 483

  
996 484
        @Override
997 485
        public String toString() {
998
            if( this.all ) {
999
                return MessageFormat.format(
1000
                    config.getString(SQLConfig.count),
1001
                    "*"
1002
                );
486
            return this.toString(formatter());
487
        }
488

  
489
        @Override
490
        public String toString(Formatter formatter) {
491
            if (formatter.canApply(this)) {
492
                return formatter.format(this);
1003 493
            }
1004
            if( this.distinct ) {
494
            if (this.all) {
495
                return "COUNT(*)";
496
            }
497
            if (this.distinct) {
1005 498
                return MessageFormat.format(
1006
                    config.getString(SQLConfig.count_distinct),
1007
                    value.toString()
499
                        "COUNT(DISTINCT {0})",
500
                        value.toString(formatter)
1008 501
                );
1009 502
            }
1010 503
            return MessageFormat.format(
1011
                config.getString(SQLConfig.count),
1012
                value.toString()
504
                    "COUNT({0})",
505
                    value.toString(formatter)
1013 506
            );
1014 507
        }
1015
        
1016
        
508

  
1017 509
    }
1018
    
1019
    public class FromBuilderBase implements FromBuilder {
1020 510

  
1021
        protected TableNameBuilder tableName= null;
511
    public class FromBuilderBase
512
            extends AbstractStatementPart
513
            implements FromBuilder {
514

  
515
        protected TableNameBuilder tableName = null;
1022 516
        private String subquery = null;
1023 517
        private String passthrough = null;
1024 518

  
1025 519
        @Override
1026 520
        public TableNameBuilder table() {
1027
            if( tableName == null ) {
521
            if (tableName == null) {
1028 522
                this.tableName = createTableNameBuilder();
1029 523
            }
1030 524
            return this.tableName;
......
1032 526

  
1033 527
        @Override
1034 528
        public void accept(Visitor visitor, VisitorFilter filter) {
1035
            if( filter.accept(this) ) {
529
            if (filter.accept(this)) {
1036 530
                visitor.visit(this);
1037 531
            }
1038
            if( this.tableName != null ) {
532
            if (this.tableName != null) {
1039 533
                this.tableName.accept(visitor, filter);
1040 534
            }
1041 535
        }
......
1051 545
            this.subquery = subquery;
1052 546
            return this;
1053 547
        }
1054
        
548

  
1055 549
        @Override
1056 550
        public String toString() {
1057
            if( ! StringUtils.isEmpty(passthrough) ) {
551
            return this.toString(formatter());
552
        }
553

  
554
        @Override
555
        public String toString(Formatter<Value> formatter) {
556
            if (formatter.canApply(this)) {
557
                return formatter.format(this);
558
            }
559
            if (!StringUtils.isEmpty(passthrough)) {
1058 560
                return passthrough;
1059 561
            }
1060
            if( ! StringUtils.isEmpty(subquery) ) {
562
            if (!StringUtils.isEmpty(subquery)) {
1061 563
                return "( " + this.subquery + ") as _subquery_alias_ ";
1062 564
            }
1063
            return this.tableName.toString();
565
            return this.tableName.toString(formatter);
1064 566
        }
1065 567

  
1066 568
    }
1067 569

  
1068
    public class SelectColumnBuilderBase implements SelectColumnBuilder {
570
    public class SelectColumnBuilderBase
571
            extends AbstractStatementPart
572
            implements SelectColumnBuilder {
1069 573

  
1070 574
        private Variable name = null;
1071 575
        private String alias = null;
1072 576
        private Value value = null;
1073 577
        private boolean asGeometry = false;
1074
        
578

  
1075 579
        @Override
1076 580
        public void accept(Visitor visitor, VisitorFilter filter) {
1077
            if( filter.accept(this) ) {
581
            if (filter.accept(this)) {
1078 582
                visitor.visit(this);
1079 583
            }
1080
            if( this.name != null ) {
584
            if (this.name != null) {
1081 585
                this.name.accept(visitor, filter);
1082 586
            }
1083
            if( this.value != null ) {
587
            if (this.value != null) {
1084 588
                this.value.accept(visitor, filter);
1085 589
            }
1086 590
        }
1087 591

  
1088 592
        @Override
1089 593
        public SelectColumnBuilder name(String name) {
1090
            String quote = config.getString(Config.quote_for_identifiers);
594
            String quote = quote_for_identifiers();
1091 595
            if (name.startsWith(quote)) {
1092 596
                // Remove quotes
1093 597
                name = name.substring(1, name.length() - 1);
1094 598
            }
1095
            this.name = variable(name);
599
            this.name = expression().variable(name);
1096 600
            this.value = null;
1097 601
            this.asGeometry = false;
1098 602
            return this;
......
1101 605
        @Override
1102 606
        public SelectColumnBuilder all() {
1103 607
            this.name = null;
1104
            this.value = custom("*");
608
            this.value = expression().custom("*");
1105 609
            this.asGeometry = false;
1106 610
            return this;
1107 611
        }
1108
        
612

  
1109 613
        @Override
1110 614
        public SelectColumnBuilder as_geometry() {
1111 615
            this.asGeometry = true;
1112 616
            return this;
1113 617
        }
1114
       
618

  
1115 619
        @Override
1116 620
        public SelectColumnBuilder value(Value value) {
1117 621
            this.value = value;
......
1127 631

  
1128 632
        @Override
1129 633
        public String getName() {
1130
            return this.name.getName();
634
            return this.name.name();
1131 635
        }
1132
        
636

  
1133 637
        @Override
1134 638
        public String getAlias() {
1135 639
            return this.alias;
1136 640
        }
1137
        
641

  
1138 642
        @Override
1139 643
        public String getValue() {
1140 644
            return this.alias;
......
1142 646

  
1143 647
        @Override
1144 648
        public String toString() {
649
            return this.toString(formatter());
650
        }
651

  
652
        @Override
653
        public String toString(Formatter<Value> formatter) {
654
            if (formatter.canApply(this)) {
655
                return formatter.format(this);
656
            }
1145 657
            StringBuilder builder = new StringBuilder();
1146
            if( this.asGeometry ) {
1147
                builder.append(getAsGeometry(this.name).toString());
658
            if (this.asGeometry) {
659
                builder.append(expression().ST_AsBinary(this.name).toString(formatter));
1148 660
            } else {
1149
                if( this.name != null ) {
1150
                    builder.append(this.name.toString());
661
                if (this.name != null) {
662
                    builder.append(this.name.toString(formatter));
1151 663
                } else {
1152
                    builder.append(this.value.toString());
664
                    builder.append(this.value.toString(formatter));
1153 665
                }
1154 666
            }
1155
            if( this.alias != null ) {
667
            if (this.alias != null) {
1156 668
                builder.append(" AS ");
1157
                builder.append(identifier(this.alias));
669
                builder.append(as_identifier(this.alias));
1158 670
            }
1159 671
            return builder.toString();
1160 672
        }
1161 673
    }
1162 674

  
1163
    public class OrderByBuilderBase implements OrderByBuilder {
675
    public class OrderByBuilderBase
676
            extends AbstractStatementPart
677
            implements OrderByBuilder {
678

  
1164 679
        protected String value;
1165 680
        protected String custom;
1166 681
        protected boolean ascending;
1167
        
682

  
1168 683
        public OrderByBuilderBase() {
1169 684
            this.ascending = true;
1170 685
        }
1171 686

  
1172 687
        @Override
1173 688
        public void accept(Visitor visitor, VisitorFilter filter) {
1174
            if( filter.accept(this) ) {
689
            if (filter.accept(this)) {
1175 690
                visitor.visit(this);
1176 691
            }
1177 692
        }
......
1208 723

  
1209 724
        @Override
1210 725
        public String toString() {
1211
            if( !StringUtils.isEmpty(this.custom) ) {
726
            return this.toString(formatter());
727
        }
728

  
729
        @Override
730
        public String toString(Formatter<Value> formatter) {
731
            if (formatter.canApply(this)) {
732
                return formatter.format(this);
733
            }
734
            if (!StringUtils.isEmpty(this.custom)) {
1212 735
                return this.custom;
1213 736
            }
1214
            if( this.ascending ) {
1215
                return identifier(this.value) + " ASC";
737
            if (this.ascending) {
738
                return as_identifier(this.value) + " ASC";
1216 739
            }
1217
            return identifier(this.value) + " DESC";
740
            return as_identifier(this.value) + " DESC";
1218 741
        }
1219 742
    }
1220
    
1221
    public class SelectBuilderBase implements SelectBuilder {
1222 743

  
744
    public class SelectBuilderBase
745
            extends AbstractStatement
746
            implements SelectBuilder {
747

  
1223 748
        protected FromBuilder from;
1224 749
        protected ExpressionBuilder where;
1225 750
        protected long limit = -1;
......
1235 760

  
1236 761
        @Override
1237 762
        public void accept(Visitor visitor, VisitorFilter filter) {
1238
            if( filter.accept(this) ) {
763
            if (filter.accept(this)) {
1239 764
                visitor.visit(this);
1240 765
            }
1241 766
            for (SelectColumnBuilder column : columns) {
1242
                column.accept(visitor,filter);
767
                column.accept(visitor, filter);
1243 768
            }
1244
            if( this.has_from() ) {
1245
                this.from.accept(visitor,filter);
769
            if (this.has_from()) {
770
                this.from.accept(visitor, filter);
1246 771
            }
1247
            if( this.has_where() ) {
1248
                this.where.accept(visitor,filter);
772
            if (this.has_where()) {
773
                this.where.accept(visitor, filter);
1249 774
            }
1250
            if( this.has_order_by() ) {
775
            if (this.has_order_by()) {
1251 776
                for (OrderByBuilder order : order_by) {
1252
                    order.accept(visitor,filter);
777
                    order.accept(visitor, filter);
1253 778
                }
1254 779
            }
1255 780
        }
......
1259 784
            this.distinct = true;
1260 785
            return this;
1261 786
        }
1262
        
787

  
1263 788
        @Override
1264 789
        public SelectColumnBuilder column() {
1265 790
            SelectColumnBuilder builder = createSelectColumnBuilder();
......
1270 795
        @Override
1271 796
        public boolean has_column(String name) {
1272 797
            for (SelectColumnBuilder column : columns) {
1273
                if( name.equals(column.getName()) ) {
798
                if (name.equals(column.getName())) {
1274 799
                    return true;
1275 800
                }
1276 801
            }
......
1289 814
        public boolean has_from() {
1290 815
            return this.from != null;
1291 816
        }
1292
        
817

  
1293 818
        @Override
1294 819
        public ExpressionBuilder where() {
1295 820
            if (this.where == null) {
......
1300 825

  
1301 826
        @Override
1302 827
        public boolean has_where() {
1303
            if( this.where == null ) {
828
            if (this.where == null) {
1304 829
                return false;
1305 830
            }
1306
            return this.where.getValue() != null;
831
            return this.where.value() != null;
1307 832
        }
1308
        
833

  
1309 834
        @Override
1310 835
        public SelectBuilder limit(long limit) {
1311 836
            this.limit = limit;
......
1314 839

  
1315 840
        @Override
1316 841
        public SelectBuilder limit(Long limit) {
1317
            if( limit == null ) {
842
            if (limit == null) {
1318 843
                this.limit = 0;
1319 844
            } else {
1320 845
                this.limit = limit;
......
1340 865

  
1341 866
        @Override
1342 867
        public OrderByBuilder order_by() {
1343
            if( this.order_by == null ) {
868
            if (this.order_by == null) {
1344 869
                this.order_by = new ArrayList<>();
1345 870
            }
1346 871
            OrderByBuilder order = createOrderByBuilder();
......
1350 875

  
1351 876
        @Override
1352 877
        public boolean has_order_by() {
1353
            if( this.order_by == null ) {
878
            if (this.order_by == null) {
1354 879
                return false;
1355 880
            }
1356 881
            return !this.order_by.isEmpty();
1357 882
        }
1358
        
883

  
1359 884
        protected boolean isValid(StringBuilder message) {
1360
            if( message == null ) {
885
            if (message == null) {
1361 886
                message = new StringBuilder();
1362 887
            }
1363
            if( this.has_offset() && !this.has_order_by() ) {
888
            if (this.has_offset() && !this.has_order_by()) {
1364 889
                // Algunos gestores de BBDD requieren que se especifique un
1365 890
                // orden para poder usar OFFSET. Como eso parece buena idea para
1366 891
                // asegurar que siempre tengamos los mismo resultados, lo exijimos
......
1370 895
            }
1371 896
            return true;
1372 897
        }
1373
        
898

  
1374 899
        @Override
1375 900
        public String toString() {
901
            return this.toString(formatter());
902
        }
903

  
904
        @Override
905
        public String toString(Formatter<Value> formatter) {
906
            if (formatter.canApply(this)) {
907
                return formatter.format(this);
908
            }
1376 909
            StringBuilder builder = new StringBuilder();
1377
            if( !this.isValid(builder) ) {
910
            if (!this.isValid(builder)) {
1378 911
                throw new IllegalStateException(builder.toString());
1379 912
            }
1380 913
            builder.append("SELECT ");
1381
            if( this.distinct ) {
914
            if (this.distinct) {
1382 915
                builder.append("DISTINCT ");
1383 916
            }
1384 917
            boolean first = true;
......
1388 921
                } else {
1389 922
                    builder.append(", ");
1390 923
                }
1391
                builder.append(column.toString());
924
                builder.append(column.toString(formatter));
1392 925
            }
1393 926

  
1394
            if ( this.has_from() ) {
927
            if (this.has_from()) {
1395 928
                builder.append(" FROM ");
1396
                builder.append(this.from.toString());
929
                builder.append(this.from.toString(formatter));
1397 930
            }
1398
            if ( this.has_where() ) {
931
            if (this.has_where()) {
1399 932
                builder.append(" WHERE ");
1400
                builder.append(this.where.toString());
933
                builder.append(this.where.toString(formatter));
1401 934
            }
1402
            
1403
            if( this.has_order_by() ) {
935

  
936
            if (this.has_order_by()) {
1404 937
                builder.append(" ORDER BY ");
1405 938
                first = true;
1406 939
                for (OrderByBuilder item : this.order_by) {
......
1409 942
                    } else {
1410 943
                        builder.append(", ");
1411 944
                    }
1412
                    builder.append(item.toString());                    
1413
                }   
945
                    builder.append(item.toString(formatter));
946
                }
1414 947
            }
1415
            
1416
            if ( this.has_limit() ) {
948

  
949
            if (this.has_limit()) {
1417 950
                builder.append(" LIMIT ");
1418 951
                builder.append(this.limit);
1419 952
            }
1420
            if ( this.has_offset() ) {
953
            if (this.has_offset()) {
1421 954
                builder.append(" OFFSET ");
1422 955
                builder.append(this.offset);
1423 956
            }
......
1426 959
        }
1427 960
    }
1428 961

  
1429
    public class DropTableBuilderBase implements DropTableBuilder {
962
    public class DropTableBuilderBase
963
            extends AbstractStatement
964
            implements DropTableBuilder {
1430 965

  
1431 966
        protected TableNameBuilder table;
1432 967

  
1433 968
        @Override
1434 969
        public TableNameBuilder table() {
1435
            if( table == null ) {
970
            if (table == null) {
1436 971
                table = createTableNameBuilder();
1437 972
            }
1438 973
            return table;
......
1440 975

  
1441 976
        @Override
1442 977
        public void accept(Visitor visitor, VisitorFilter filter) {
1443
            if( filter.accept(this) ) {
978
            if (filter.accept(this)) {
1444 979
                visitor.visit(this);
1445 980
            }
1446
            this.table.accept(visitor,filter);
981
            this.table.accept(visitor, filter);
1447 982
        }
1448
        
983

  
1449 984
        @Override
1450 985
        public String toString() {
986
            return this.toString(formatter());
987
        }
988

  
989
        @Override
990
        public String toString(Formatter<Value> formatter) {
991
            if (formatter.canApply(this)) {
992
                return formatter.format(this);
993
            }
1451 994
            StringBuilder builder = new StringBuilder();
1452 995
            boolean first = true;
1453
            for (String sql : toStrings()) {
1454
                if( StringUtils.isEmpty(sql) ) {
996
            for (String sql : toStrings(formatter)) {
997
                if (StringUtils.isEmpty(sql)) {
1455 998
                    continue;
1456 999
                }
1457 1000
                if (first) {
......
1466 1009

  
1467 1010
        @Override
1468 1011
        public List<String> toStrings() {
1012
            return this.toStrings(formatter());
1013
        }
1014

  
1015
        @Override
1016
        public List<String> toStrings(Formatter formatter) {
1469 1017
            List<String> sqls = new ArrayList<>();
1470 1018

  
1471 1019
            sqls.add(
1472 1020
                    MessageFormat.format(
1473
                            config.getString(SQLConfig.DROP_TABLE_table),
1474
                            this.table.toString()
1021
                            STMT_DROP_TABLE_table,
1022
                            this.table.toString(formatter)
1475 1023
                    )
1476 1024
            );
1477 1025
            String sql;
1478
            if( config.has_functionality(SQLConfig.DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table) ) {
1026
            if (!StringUtils.isBlank(STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table)) {
1479 1027
                if (this.table.has_schema()) {
1480 1028
                    sql = MessageFormat.format(
1481
                            config.getString(SQLConfig.DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table),
1482
                            string(this.table.getSchema()),
1483
                            string(this.table.getName())
1029
                            STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table,
1030
                            as_string(this.table.getSchema()),
1031
                            as_string(this.table.getName())
1484 1032
                    );
1485 1033
                } else {
1486 1034
                    sql = MessageFormat.format(
1487
                            config.getString(SQLConfig.DELETE_GEOMETRY_COLUMN_FROM_TABLE_table),
1488
                            identifier(this.table.getName())
1035
                            STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_table,
1036
                            as_identifier(this.table.getName())
1489 1037
                    );
1490 1038
                }
1491
                if( !StringUtils.isEmpty(sql) ) {
1039
                if (!StringUtils.isEmpty(sql)) {
1492 1040
                    sqls.add(sql);
1493 1041
                }
1494 1042
            }
......
1496 1044
        }
1497 1045
    }
1498 1046

  
1499
    public class GrantRoleBuilderBase implements GrantRoleBuilder {
1047
    public class GrantRoleBuilderBase
1048
            extends AbstractStatementPart
1049
            implements GrantRoleBuilder {
1050

  
1500 1051
        protected TableNameBuilder table;
1501 1052
        protected String role;
1502 1053
        protected Set<Privilege> privileges;
......
1512 1063
            privileges.add(privilege);
1513 1064
            return this;
1514 1065
        }
1515
        
1066

  
1516 1067
        @Override
1517 1068
        public GrantRoleBuilder select() {
1518
             privileges.add(Privilege.SELECT);
1069
            privileges.add(Privilege.SELECT);
1519 1070
            return this;
1520 1071
        }
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff