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 / SQLBuilderBase.java @ 44644
History | View | Annotate | Download (102 KB)
1 |
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.StringUtils; |
13 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
14 |
import org.apache.commons.lang3.tuple.Pair; |
15 |
import org.cresques.cts.IProjection; |
16 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
17 |
import org.gvsig.expressionevaluator.ExpressionBuilder.AbstractValue; |
18 |
import org.gvsig.expressionevaluator.ExpressionBuilder.ClassVisitorFilter; |
19 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.EMPTY_FORMATTER; |
20 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.PARAMETER_TYPE_CONSTANT; |
21 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.PARAMETER_TYPE_VARIABLE; |
22 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Parameter; |
23 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
24 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Variable; |
25 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Visitable; |
26 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Visitor; |
27 |
import org.gvsig.expressionevaluator.ExpressionBuilder.VisitorFilter; |
28 |
import org.gvsig.expressionevaluator.Formatter; |
29 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
30 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilderHelper.GeometrySupportType; |
31 |
import org.gvsig.expressionevaluator.GeometryExpressionEvaluatorLocator; |
32 |
import org.gvsig.fmap.dal.DataStoreParameters; |
33 |
import org.gvsig.fmap.dal.DataTypes; |
34 |
import org.gvsig.fmap.dal.SQLBuilder; |
35 |
import org.gvsig.fmap.dal.SQLBuilder.AlterTableBuilder; |
36 |
import org.gvsig.fmap.dal.SQLBuilder.CreateTableBuilder; |
37 |
import org.gvsig.fmap.dal.SQLBuilder.DeleteBuilder; |
38 |
import org.gvsig.fmap.dal.SQLBuilder.DropTableBuilder; |
39 |
import org.gvsig.fmap.dal.SQLBuilder.FromBuilder; |
40 |
import org.gvsig.fmap.dal.SQLBuilder.GrantBuilder; |
41 |
import org.gvsig.fmap.dal.SQLBuilder.InsertBuilder; |
42 |
import org.gvsig.fmap.dal.SQLBuilder.InsertColumnBuilder; |
43 |
import org.gvsig.fmap.dal.SQLBuilder.OrderByBuilder; |
44 |
import org.gvsig.fmap.dal.SQLBuilder.Privilege; |
45 |
import org.gvsig.fmap.dal.SQLBuilder.SelectBuilder; |
46 |
import org.gvsig.fmap.dal.SQLBuilder.SelectColumnBuilder; |
47 |
import org.gvsig.fmap.dal.SQLBuilder.TableNameBuilder; |
48 |
import org.gvsig.fmap.dal.SQLBuilder.UpdateBuilder; |
49 |
import org.gvsig.fmap.dal.SQLBuilder.UpdateColumnBuilder; |
50 |
import org.gvsig.fmap.dal.SQLBuilder.UpdateTableStatisticsBuilder; |
51 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
52 |
import org.gvsig.fmap.geom.Geometry; |
53 |
import org.slf4j.Logger; |
54 |
import org.slf4j.LoggerFactory; |
55 |
|
56 |
@SuppressWarnings("UseSpecificCatch") |
57 |
public class SQLBuilderBase implements SQLBuilder { |
58 |
|
59 |
protected static final Logger LOGGER = LoggerFactory.getLogger(SQLBuilderBase.class); |
60 |
|
61 |
protected SelectBuilder select;
|
62 |
protected UpdateBuilder update;
|
63 |
protected InsertBuilder insert;
|
64 |
protected DeleteBuilder delete;
|
65 |
protected AlterTableBuilder alter_table;
|
66 |
protected CreateTableBuilder create_table;
|
67 |
protected GrantBuilder grant;
|
68 |
protected DropTableBuilder drop_table;
|
69 |
protected UpdateTableStatisticsBuilder update_table_statistics;
|
70 |
protected CreateIndexBuilder create_index;
|
71 |
protected TableNameBuilder table_name;
|
72 |
|
73 |
protected abstract class AbstractStatementPart extends AbstractValue { |
74 |
|
75 |
} |
76 |
|
77 |
protected abstract class AbstractStatement extends AbstractStatementPart { |
78 |
|
79 |
} |
80 |
|
81 |
protected class ColumnDescriptorBase implements ColumnDescriptor { |
82 |
|
83 |
private String name; |
84 |
private int type; |
85 |
private int type_p; |
86 |
private int type_s; |
87 |
private boolean isPk; |
88 |
private boolean _allowNulls; |
89 |
private boolean _isAutomatic; |
90 |
private Object defaultValue; |
91 |
private int geom_type; |
92 |
private int geom_subtype; |
93 |
private Object geom_srsdbcode; |
94 |
private boolean _isIndexed; |
95 |
private DataStoreParameters parameters = null; |
96 |
|
97 |
public ColumnDescriptorBase(String name, int type, Object defaultValue) { |
98 |
this.name = name;
|
99 |
this.type = type;
|
100 |
this.type_p = -1; |
101 |
this.type_s = -1; |
102 |
this.isPk = false; |
103 |
this._allowNulls = true; |
104 |
this._isAutomatic = false; |
105 |
this.defaultValue = defaultValue;
|
106 |
this.geom_type = Geometry.TYPES.GEOMETRY;
|
107 |
this.geom_subtype = Geometry.SUBTYPES.GEOM2D;
|
108 |
this.geom_srsdbcode = null; |
109 |
this._isIndexed = false; |
110 |
} |
111 |
|
112 |
public ColumnDescriptorBase(String name, int type, int type_p, int type_s, boolean isPk, boolean isIndexed, boolean allowNulls, boolean isAutomatic, Object defaultValue) { |
113 |
this.name = name;
|
114 |
this.type = type;
|
115 |
this.type_p = type_p;
|
116 |
this.type_s = type_s;
|
117 |
this.isPk = isPk;
|
118 |
this._allowNulls = allowNulls;
|
119 |
this._isAutomatic = isAutomatic;
|
120 |
this.defaultValue = defaultValue;
|
121 |
this.geom_type = Geometry.TYPES.GEOMETRY;
|
122 |
this.geom_subtype = Geometry.SUBTYPES.GEOM2D;
|
123 |
this.geom_srsdbcode = null; |
124 |
this._isIndexed = isIndexed;
|
125 |
} |
126 |
|
127 |
public ColumnDescriptorBase(String name, int geom_type, int geom_subtype, IProjection proj, boolean isIndexed, boolean allowNulls) { |
128 |
this.name = name;
|
129 |
this.type = DataTypes.GEOMETRY;
|
130 |
this.type_p = 0; |
131 |
this.type_s = 0; |
132 |
this.isPk = false; |
133 |
this._allowNulls = allowNulls;
|
134 |
this._isAutomatic = false; |
135 |
this.defaultValue = null; |
136 |
this.geom_type = geom_type;
|
137 |
this.geom_subtype = geom_subtype;
|
138 |
this.geom_srsdbcode = srs_id(proj);
|
139 |
this._isIndexed = isIndexed;
|
140 |
} |
141 |
|
142 |
public ColumnDescriptorBase(String name, int geom_type, int geom_subtype, Object srsdbcode, boolean isIndexed, boolean allowNulls) { |
143 |
this.name = name;
|
144 |
this.type = DataTypes.GEOMETRY;
|
145 |
this.type_p = 0; |
146 |
this.type_s = 0; |
147 |
this.isPk = false; |
148 |
this._allowNulls = allowNulls;
|
149 |
this._isAutomatic = false; |
150 |
this.defaultValue = null; |
151 |
this.geom_type = geom_type;
|
152 |
this.geom_subtype = geom_subtype;
|
153 |
this.geom_srsdbcode = srsdbcode;
|
154 |
this._isIndexed = isIndexed;
|
155 |
} |
156 |
|
157 |
private ColumnDescriptorBase(FeatureAttributeDescriptor fad) {
|
158 |
this(fad.getName(), fad.getType(), fad.getDefaultValue());
|
159 |
this.type_p = fad.getPrecision();
|
160 |
this.type_s = fad.getSize();
|
161 |
this.isPk = fad.isPrimaryKey();
|
162 |
this._allowNulls = fad.allowNull();
|
163 |
this._isAutomatic = fad.isAutomatic();
|
164 |
this._isIndexed = fad.isIndexed();
|
165 |
|
166 |
if (fad.getType() == org.gvsig.fmap.geom.DataTypes.GEOMETRY) {
|
167 |
this.geom_type = fad.getGeomType().getType();
|
168 |
this.geom_subtype = fad.getGeomType().getSubType();
|
169 |
this.geom_srsdbcode = fad.getSRS();
|
170 |
} |
171 |
} |
172 |
|
173 |
|
174 |
@Override
|
175 |
public String getName() { |
176 |
return this.name; |
177 |
} |
178 |
|
179 |
@Override
|
180 |
public void setName(String name) { |
181 |
this.name = name;
|
182 |
} |
183 |
|
184 |
@Override
|
185 |
public int getType() { |
186 |
return this.type; |
187 |
} |
188 |
|
189 |
@Override
|
190 |
public void setType(int type) { |
191 |
this.type = type;
|
192 |
} |
193 |
|
194 |
@Override
|
195 |
public int getPrecision() { |
196 |
return type_p;
|
197 |
} |
198 |
|
199 |
@Override
|
200 |
public void setPrecision(int precision) { |
201 |
this.type_p = precision;
|
202 |
} |
203 |
|
204 |
@Override
|
205 |
public int getSize() { |
206 |
return type_s;
|
207 |
} |
208 |
|
209 |
@Override
|
210 |
public void setSize(int size) { |
211 |
this.type_s = size;
|
212 |
} |
213 |
|
214 |
@Override
|
215 |
public boolean isPrimaryKey() { |
216 |
return isPk;
|
217 |
} |
218 |
|
219 |
@Override
|
220 |
public void setIsPrimaryKey(boolean isPk) { |
221 |
this.isPk = isPk;
|
222 |
} |
223 |
|
224 |
@Override
|
225 |
public boolean allowNulls() { |
226 |
return _allowNulls;
|
227 |
} |
228 |
|
229 |
@Override
|
230 |
public void setAllowNulls(boolean allowNulls) { |
231 |
this._allowNulls = allowNulls;
|
232 |
} |
233 |
|
234 |
@Override
|
235 |
public boolean isAutomatic() { |
236 |
return _isAutomatic;
|
237 |
} |
238 |
|
239 |
@Override
|
240 |
public boolean isIndexed() { |
241 |
return _isIndexed;
|
242 |
} |
243 |
|
244 |
@Override
|
245 |
public void setIsAutomatic(boolean isAutomatic) { |
246 |
this._isAutomatic = isAutomatic;
|
247 |
} |
248 |
|
249 |
@Override
|
250 |
public Object getDefaultValue() { |
251 |
return defaultValue;
|
252 |
} |
253 |
|
254 |
@Override
|
255 |
public void setDefaultValue(Object defaultValue) { |
256 |
this.defaultValue = defaultValue;
|
257 |
} |
258 |
|
259 |
@Override
|
260 |
public int getGeometryType() { |
261 |
return geom_type;
|
262 |
} |
263 |
|
264 |
@Override
|
265 |
public void setGeometryType(int geom_type) { |
266 |
this.geom_type = geom_type;
|
267 |
} |
268 |
|
269 |
@Override
|
270 |
public int getGeometrySubtype() { |
271 |
return geom_subtype;
|
272 |
} |
273 |
|
274 |
@Override
|
275 |
public void setGeometrySubtype(int geom_subtype) { |
276 |
this.geom_subtype = geom_subtype;
|
277 |
} |
278 |
|
279 |
@Override
|
280 |
public Object getGeometrySRSId() { |
281 |
return geom_srsdbcode;
|
282 |
} |
283 |
|
284 |
@Override
|
285 |
public void setGeometrySRSId(Object geom_srsid) { |
286 |
this.geom_srsdbcode = geom_srsid;
|
287 |
} |
288 |
|
289 |
@Override
|
290 |
public boolean isGeometry() { |
291 |
return this.type == DataTypes.GEOMETRY; |
292 |
} |
293 |
|
294 |
private void setStoreParameters(DataStoreParameters parameters) { |
295 |
this.parameters = parameters;
|
296 |
} |
297 |
|
298 |
@Override
|
299 |
public DataStoreParameters getStoreParameters() {
|
300 |
return this.parameters; |
301 |
} |
302 |
} |
303 |
|
304 |
public class ColumnBase extends AbstractValue implements Column { |
305 |
|
306 |
private final String name; |
307 |
private TableNameBuilder table;
|
308 |
|
309 |
public ColumnBase(TableNameBuilder table, String name) { |
310 |
this.name = name;
|
311 |
this.table = table;
|
312 |
} |
313 |
|
314 |
@Override
|
315 |
public String name() { |
316 |
return this.name; |
317 |
} |
318 |
|
319 |
@Override
|
320 |
public TableNameBuilder table() {
|
321 |
return this.table; |
322 |
} |
323 |
|
324 |
@Override
|
325 |
public TableNameBuilder table(TableNameBuilder table) {
|
326 |
this.table = table;
|
327 |
return this.table; |
328 |
} |
329 |
|
330 |
@Override
|
331 |
public String toString() { |
332 |
return this.toString(EMPTY_FORMATTER); |
333 |
} |
334 |
|
335 |
@Override
|
336 |
public String toString(Formatter<Value> formatter) { |
337 |
if( formatter!=null && formatter.canApply(this) ) { |
338 |
return formatter.format(this); |
339 |
} |
340 |
if( this.table==null ) { |
341 |
return as_identifier(this.name); |
342 |
} |
343 |
return this.table.toString(formatter) + "." + as_identifier(this.name); |
344 |
} |
345 |
|
346 |
@Override
|
347 |
public int compareTo(Variable o) { |
348 |
return this.name.compareTo(o.name()); |
349 |
} |
350 |
|
351 |
@Override
|
352 |
public boolean equals(Object obj) { |
353 |
if (!(obj instanceof Variable)) { |
354 |
return false; |
355 |
} |
356 |
return StringUtils.equals(this.toString(), ((Variable) obj).toString()); |
357 |
} |
358 |
|
359 |
@Override
|
360 |
public int hashCode() { |
361 |
int hash = 7; |
362 |
hash = 37 * hash + Objects.hashCode(this.toString()); |
363 |
return hash;
|
364 |
} |
365 |
} |
366 |
|
367 |
public class TableNameBuilderBase |
368 |
extends AbstractStatementPart
|
369 |
implements TableNameBuilder {
|
370 |
|
371 |
public String tableName; |
372 |
public String schemaName; |
373 |
private String databaseName; |
374 |
|
375 |
public TableNameBuilderBase() {
|
376 |
} |
377 |
|
378 |
@Override
|
379 |
public void accept(Visitor visitor, VisitorFilter filter) { |
380 |
if (filter.accept(this)) { |
381 |
visitor.visit(this);
|
382 |
} |
383 |
} |
384 |
|
385 |
@Override
|
386 |
public TableNameBuilder database(String name) { |
387 |
this.databaseName = name;
|
388 |
return this; |
389 |
} |
390 |
|
391 |
@Override
|
392 |
public TableNameBuilder schema(String name) { |
393 |
if (support_schemas()) {
|
394 |
this.schemaName = name;
|
395 |
} |
396 |
return this; |
397 |
} |
398 |
|
399 |
@Override
|
400 |
public TableNameBuilder name(String name) { |
401 |
this.tableName = name;
|
402 |
return this; |
403 |
} |
404 |
|
405 |
@Override
|
406 |
public String getDatabase() { |
407 |
return this.databaseName; |
408 |
} |
409 |
|
410 |
@Override
|
411 |
public String getSchema() { |
412 |
return this.schemaName; |
413 |
} |
414 |
|
415 |
@Override
|
416 |
public String getName() { |
417 |
return this.tableName; |
418 |
} |
419 |
|
420 |
@Override
|
421 |
public boolean has_schema() { |
422 |
if (!support_schemas()) {
|
423 |
return false; |
424 |
} |
425 |
return !StringUtils.isEmpty(this.schemaName); |
426 |
} |
427 |
|
428 |
@Override
|
429 |
public boolean has_database() { |
430 |
return !StringUtils.isEmpty(this.databaseName); |
431 |
} |
432 |
|
433 |
@Override
|
434 |
public String toString() { |
435 |
return this.toString(formatter()); |
436 |
} |
437 |
|
438 |
@Override
|
439 |
public String toString(Formatter<Value> formatter) { |
440 |
if (formatter!=null && formatter.canApply(this)) { |
441 |
return formatter.format(this); |
442 |
} |
443 |
if (this.has_database()) { |
444 |
if (this.has_schema()) { |
445 |
return as_identifier(this.databaseName) + "." |
446 |
+ as_identifier(this.schemaName) + "." |
447 |
+ as_identifier(this.tableName);
|
448 |
} |
449 |
} else {
|
450 |
if (this.has_schema()) { |
451 |
return as_identifier(this.schemaName) + "." |
452 |
+ as_identifier(this.tableName);
|
453 |
} |
454 |
} |
455 |
return as_identifier(this.tableName); |
456 |
} |
457 |
|
458 |
@Override
|
459 |
public boolean equals(Object obj) { |
460 |
if( obj==null || !(obj instanceof TableNameBuilder) ) { |
461 |
return false; |
462 |
} |
463 |
TableNameBuilder other = (TableNameBuilder) obj; |
464 |
if (this.has_database()) { |
465 |
if (this.has_schema()) { |
466 |
return this.databaseName.equals(other.getDatabase()) && |
467 |
this.schemaName.equals(other.getSchema()) &&
|
468 |
this.tableName.equals(other.getName());
|
469 |
} |
470 |
} else {
|
471 |
if (this.has_schema()) { |
472 |
return this.schemaName.equals(other.getSchema()) && |
473 |
this.tableName.equals(other.getName());
|
474 |
} |
475 |
} |
476 |
return this.tableName.equals(other.getName()); |
477 |
} |
478 |
|
479 |
@Override
|
480 |
public int hashCode() { |
481 |
int hash = 7; |
482 |
hash = 37 * hash + Objects.hashCode(this.toString()); |
483 |
return hash;
|
484 |
} |
485 |
|
486 |
} |
487 |
|
488 |
public class CountBuilderBase |
489 |
extends AbstractStatementPart
|
490 |
implements CountBuilder {
|
491 |
|
492 |
protected Value value;
|
493 |
protected boolean distinct; |
494 |
protected boolean all; |
495 |
|
496 |
public CountBuilderBase() {
|
497 |
this.value = null; |
498 |
this.distinct = false; |
499 |
this.all = false; |
500 |
} |
501 |
|
502 |
@Override
|
503 |
public CountBuilder all() {
|
504 |
this.all = true; |
505 |
return this; |
506 |
} |
507 |
|
508 |
@Override
|
509 |
public CountBuilder column(Value value) {
|
510 |
this.value = value;
|
511 |
return this; |
512 |
} |
513 |
|
514 |
@Override
|
515 |
public CountBuilder distinct() {
|
516 |
this.distinct = true; |
517 |
return this; |
518 |
} |
519 |
|
520 |
@Override
|
521 |
public String toString() { |
522 |
return this.toString(formatter()); |
523 |
} |
524 |
|
525 |
@Override
|
526 |
public String toString(Formatter formatter) { |
527 |
if (formatter!=null && formatter.canApply(this)) { |
528 |
return formatter.format(this); |
529 |
} |
530 |
if (this.all) { |
531 |
return "COUNT(*)"; |
532 |
} |
533 |
if (this.distinct) { |
534 |
return MessageFormat.format( |
535 |
"COUNT(DISTINCT {0})",
|
536 |
value.toString(formatter) |
537 |
); |
538 |
} |
539 |
return MessageFormat.format( |
540 |
"COUNT({0})",
|
541 |
value.toString(formatter) |
542 |
); |
543 |
} |
544 |
|
545 |
} |
546 |
|
547 |
protected class JoinBase |
548 |
extends AbstractStatementPart
|
549 |
implements StatementPart
|
550 |
{ |
551 |
protected String type; |
552 |
protected TableNameBuilder table;
|
553 |
protected Value expression;
|
554 |
|
555 |
public JoinBase(String type, TableNameBuilder table, Value expression) { |
556 |
this.type = type;
|
557 |
this.table = table;
|
558 |
this.expression = expression;
|
559 |
} |
560 |
|
561 |
@Override
|
562 |
public String toString() { |
563 |
return this.toString(formatter()); |
564 |
} |
565 |
|
566 |
@Override
|
567 |
public String toString(Formatter<Value> formatter) { |
568 |
if (formatter!=null && formatter.canApply(this)) { |
569 |
return formatter.format(this); |
570 |
} |
571 |
StringBuilder builder = new StringBuilder(); |
572 |
// INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
|
573 |
builder.append(this.type.toUpperCase());
|
574 |
builder.append(" JOIN ");
|
575 |
builder.append(this.table.toString(formatter));
|
576 |
builder.append(" ON ");
|
577 |
builder.append(this.expression.toString(formatter));
|
578 |
return builder.toString();
|
579 |
} |
580 |
|
581 |
|
582 |
} |
583 |
|
584 |
public class FromBuilderBase |
585 |
extends AbstractStatementPart
|
586 |
implements FromBuilder {
|
587 |
|
588 |
protected TableNameBuilder tableName;
|
589 |
private String subquery; |
590 |
private String passthrough; |
591 |
private List<JoinBase> joins; |
592 |
|
593 |
public FromBuilderBase() {
|
594 |
this.tableName = null; |
595 |
this.subquery = null; |
596 |
this.passthrough = null; |
597 |
this.joins = null; |
598 |
} |
599 |
|
600 |
@Override
|
601 |
public FromBuilder left_join(TableNameBuilder table, Value expression) {
|
602 |
JoinBase join = createJoin("LEFT", table, expression);
|
603 |
if( this.joins==null ) { |
604 |
this.joins = new ArrayList<>(); |
605 |
} |
606 |
this.joins.add(join);
|
607 |
return this; |
608 |
} |
609 |
|
610 |
@Override
|
611 |
public TableNameBuilder table() {
|
612 |
if (tableName == null) { |
613 |
this.tableName = createTableNameBuilder();
|
614 |
} |
615 |
return this.tableName; |
616 |
} |
617 |
|
618 |
@Override
|
619 |
public void accept(Visitor visitor, VisitorFilter filter) { |
620 |
if (filter.accept(this)) { |
621 |
visitor.visit(this);
|
622 |
} |
623 |
if (this.tableName != null) { |
624 |
this.tableName.accept(visitor, filter);
|
625 |
} |
626 |
} |
627 |
|
628 |
@Override
|
629 |
public FromBuilder custom(String passthrough) { |
630 |
this.passthrough = passthrough;
|
631 |
return this; |
632 |
} |
633 |
|
634 |
@Override
|
635 |
public FromBuilder subquery(String subquery) { |
636 |
this.subquery = subquery;
|
637 |
return this; |
638 |
} |
639 |
|
640 |
@Override
|
641 |
public String toString() { |
642 |
return this.toString(formatter()); |
643 |
} |
644 |
|
645 |
@Override
|
646 |
public String toString(Formatter<Value> formatter) { |
647 |
if (formatter!=null && formatter.canApply(this)) { |
648 |
return formatter.format(this); |
649 |
} |
650 |
if (!StringUtils.isEmpty(passthrough)) {
|
651 |
return passthrough;
|
652 |
} |
653 |
if (!StringUtils.isEmpty(subquery)) {
|
654 |
return "( " + this.subquery + ") as _subquery_alias_ "; |
655 |
} |
656 |
if( this.joins==null || this.joins.isEmpty() ) { |
657 |
return this.tableName.toString(formatter); |
658 |
} |
659 |
StringBuilder builder = new StringBuilder(); |
660 |
builder.append(this.tableName.toString(formatter));
|
661 |
for (JoinBase join : this.joins) { |
662 |
builder.append(" ");
|
663 |
builder.append(join.toString(formatter)); |
664 |
} |
665 |
return builder.toString();
|
666 |
} |
667 |
|
668 |
} |
669 |
|
670 |
public class SelectColumnBuilderBase |
671 |
extends AbstractStatementPart
|
672 |
implements SelectColumnBuilder {
|
673 |
|
674 |
private Variable name = null; |
675 |
private String alias = null; |
676 |
private Value value = null; |
677 |
private boolean asGeometry = false; |
678 |
private TableNameBuilder table;
|
679 |
|
680 |
@Override
|
681 |
public void accept(Visitor visitor, VisitorFilter filter) { |
682 |
if (filter.accept(this)) { |
683 |
visitor.visit(this);
|
684 |
} |
685 |
if (this.name != null) { |
686 |
this.name.accept(visitor, filter);
|
687 |
} |
688 |
if (this.value != null) { |
689 |
this.value.accept(visitor, filter);
|
690 |
} |
691 |
} |
692 |
|
693 |
@Override
|
694 |
public void replace(Value target, Value replacement) { |
695 |
if (this.name!=null ) { |
696 |
if( this.name == target) { |
697 |
this.name = (Variable) replacement;
|
698 |
} |
699 |
} |
700 |
if( this.value!=null ) { |
701 |
if (this.value == target) { |
702 |
this.value = replacement;
|
703 |
} else {
|
704 |
this.value.replace(target, replacement);
|
705 |
} |
706 |
} |
707 |
} |
708 |
|
709 |
@Override
|
710 |
public SelectColumnBuilder name(String name) { |
711 |
return this.name(null, name); |
712 |
} |
713 |
|
714 |
@Override
|
715 |
public SelectColumnBuilder name(TableNameBuilder table, String name) { |
716 |
String quote = quote_for_identifiers();
|
717 |
if (name.startsWith(quote)) {
|
718 |
// Remove quotes
|
719 |
name = name.substring(1, name.length() - 1); |
720 |
} |
721 |
this.name = expression().variable(name);
|
722 |
this.table = table;
|
723 |
this.value = null; |
724 |
this.asGeometry = false; |
725 |
return this; |
726 |
} |
727 |
|
728 |
@Override
|
729 |
public SelectColumnBuilder all() {
|
730 |
this.name = null; |
731 |
this.value = expression().custom("*"); |
732 |
this.asGeometry = false; |
733 |
return this; |
734 |
} |
735 |
|
736 |
@Override
|
737 |
public SelectColumnBuilder as_geometry() {
|
738 |
this.asGeometry = true; |
739 |
return this; |
740 |
} |
741 |
|
742 |
@Override
|
743 |
public SelectColumnBuilder value(Value value) {
|
744 |
this.value = value;
|
745 |
this.name = null; |
746 |
return this; |
747 |
} |
748 |
|
749 |
@Override
|
750 |
public SelectColumnBuilder as(String alias) { |
751 |
this.alias = alias;
|
752 |
return this; |
753 |
} |
754 |
|
755 |
@Override
|
756 |
public String getName() { |
757 |
return this.name.name(); |
758 |
} |
759 |
|
760 |
@Override
|
761 |
public String getAlias() { |
762 |
return this.alias; |
763 |
} |
764 |
|
765 |
@Override
|
766 |
public String getValue() { |
767 |
return this.alias; |
768 |
} |
769 |
|
770 |
@Override
|
771 |
public String toString() { |
772 |
return this.toString(formatter()); |
773 |
} |
774 |
|
775 |
@Override
|
776 |
public String toString(Formatter<Value> formatter) { |
777 |
if (formatter!=null && formatter.canApply(this)) { |
778 |
return formatter.format(this); |
779 |
} |
780 |
StringBuilder builder = new StringBuilder(); |
781 |
if (this.asGeometry) { |
782 |
builder.append(expression().ST_AsBinary(this.name).toString(formatter));
|
783 |
} else {
|
784 |
if (this.name != null) { |
785 |
if( this.table==null ) { |
786 |
builder.append(this.name.toString(formatter));
|
787 |
} else {
|
788 |
builder.append(this.table.toString(formatter));
|
789 |
builder.append(".");
|
790 |
builder.append(this.name.toString(formatter));
|
791 |
} |
792 |
} else {
|
793 |
builder.append(this.value.toString(formatter));
|
794 |
} |
795 |
} |
796 |
if (this.alias != null) { |
797 |
builder.append(" AS ");
|
798 |
builder.append(as_identifier(this.alias));
|
799 |
} |
800 |
return builder.toString();
|
801 |
} |
802 |
} |
803 |
|
804 |
public class OrderByBuilderBase |
805 |
extends AbstractStatementPart
|
806 |
implements OrderByBuilder {
|
807 |
|
808 |
protected String value; |
809 |
protected String custom; |
810 |
protected boolean ascending; |
811 |
|
812 |
public OrderByBuilderBase() {
|
813 |
this.ascending = true; |
814 |
} |
815 |
|
816 |
@Override
|
817 |
public void accept(Visitor visitor, VisitorFilter filter) { |
818 |
if (filter.accept(this)) { |
819 |
visitor.visit(this);
|
820 |
} |
821 |
} |
822 |
|
823 |
@Override
|
824 |
public OrderByBuilder column(String name) { |
825 |
this.value = name;
|
826 |
return this; |
827 |
} |
828 |
|
829 |
@Override
|
830 |
public OrderByBuilder custom(String order) { |
831 |
this.custom = order;
|
832 |
return this; |
833 |
} |
834 |
|
835 |
@Override
|
836 |
public OrderByBuilder ascending() {
|
837 |
this.ascending = true; |
838 |
return this; |
839 |
} |
840 |
|
841 |
@Override
|
842 |
public OrderByBuilder ascending(boolean asc) { |
843 |
this.ascending = asc;
|
844 |
return this; |
845 |
} |
846 |
|
847 |
@Override
|
848 |
public OrderByBuilder descending() {
|
849 |
this.ascending = false; |
850 |
return this; |
851 |
} |
852 |
|
853 |
@Override
|
854 |
public String toString() { |
855 |
return this.toString(formatter()); |
856 |
} |
857 |
|
858 |
@Override
|
859 |
public String toString(Formatter<Value> formatter) { |
860 |
if (formatter!=null && formatter.canApply(this)) { |
861 |
return formatter.format(this); |
862 |
} |
863 |
if (!StringUtils.isEmpty(this.custom)) { |
864 |
return this.custom; |
865 |
} |
866 |
if (this.ascending) { |
867 |
return as_identifier(this.value) + " ASC"; |
868 |
} |
869 |
return as_identifier(this.value) + " DESC"; |
870 |
} |
871 |
} |
872 |
|
873 |
public class SelectBuilderBase |
874 |
extends AbstractStatement
|
875 |
implements SelectBuilder {
|
876 |
|
877 |
protected FromBuilder from;
|
878 |
protected GeometryExpressionBuilder where;
|
879 |
protected long limit = -1; |
880 |
protected long offset = -1; |
881 |
protected List<SelectColumnBuilder> columns; |
882 |
protected List<OrderByBuilder> order_by; |
883 |
protected boolean distinct; |
884 |
protected List<Variable> groupColumn; |
885 |
|
886 |
public SelectBuilderBase() {
|
887 |
this.columns = new ArrayList<>(); |
888 |
this.distinct = false; |
889 |
} |
890 |
|
891 |
@Override
|
892 |
public SelectBuilder group_by(Variable... columns) {
|
893 |
if( this.groupColumn==null ) { |
894 |
this.groupColumn = new ArrayList<>(); |
895 |
} |
896 |
for (Variable column : columns) {
|
897 |
this.groupColumn.add(column);
|
898 |
} |
899 |
return this; |
900 |
} |
901 |
|
902 |
@Override
|
903 |
public void accept(Visitor visitor, VisitorFilter filter) { |
904 |
if (filter.accept(this)) { |
905 |
visitor.visit(this);
|
906 |
} |
907 |
for (SelectColumnBuilder column : columns) {
|
908 |
column.accept(visitor, filter); |
909 |
} |
910 |
if (this.has_from()) { |
911 |
this.from.accept(visitor, filter);
|
912 |
} |
913 |
if (this.has_where()) { |
914 |
this.where.accept(visitor, filter);
|
915 |
} |
916 |
if (this.has_order_by()) { |
917 |
for (OrderByBuilder order : order_by) {
|
918 |
order.accept(visitor, filter); |
919 |
} |
920 |
} |
921 |
} |
922 |
|
923 |
@Override
|
924 |
public void replace(Value target, Value replacement) { |
925 |
if( this.columns!=null ) { |
926 |
for (int i = 0; i < columns.size(); i++) { |
927 |
SelectColumnBuilder column = columns.get(i); |
928 |
if( column == target ) {
|
929 |
columns.set(i, (SelectColumnBuilder) replacement); |
930 |
} else {
|
931 |
column.replace(target, replacement); |
932 |
} |
933 |
} |
934 |
} |
935 |
if (this.has_from()) { |
936 |
if( this.from == target ) { |
937 |
this.from = (FromBuilder) replacement;
|
938 |
} else {
|
939 |
this.from.replace(target, replacement);
|
940 |
} |
941 |
} |
942 |
if (this.has_where()) { |
943 |
if( this.where == target ) { |
944 |
this.where = (GeometryExpressionBuilder) replacement;
|
945 |
} else if( this.where.value() == target ) { |
946 |
this.where.value(replacement);
|
947 |
} else {
|
948 |
this.where.value().replace(target, replacement);
|
949 |
} |
950 |
} |
951 |
if (this.has_order_by()) { |
952 |
for (int i = 0; i < order_by.size(); i++) { |
953 |
OrderByBuilder order = order_by.get(i); |
954 |
if( order == target ) {
|
955 |
order_by.set(i, (OrderByBuilder) replacement); |
956 |
} else {
|
957 |
order.replace(target, replacement); |
958 |
} |
959 |
} |
960 |
} |
961 |
} |
962 |
|
963 |
@Override
|
964 |
public SelectBuilder distinct() {
|
965 |
this.distinct = true; |
966 |
return this; |
967 |
} |
968 |
|
969 |
@Override
|
970 |
public SelectColumnBuilder column() {
|
971 |
SelectColumnBuilder builder = createSelectColumnBuilder(); |
972 |
this.columns.add(builder);
|
973 |
return builder;
|
974 |
} |
975 |
|
976 |
@Override
|
977 |
public SelectBuilder remove_all_columns() {
|
978 |
this.columns = new ArrayList<>(); |
979 |
return this; |
980 |
} |
981 |
|
982 |
@Override
|
983 |
public boolean has_column(String name) { |
984 |
for (SelectColumnBuilder column : columns) {
|
985 |
if (name.equals(column.getName())) {
|
986 |
return true; |
987 |
} |
988 |
} |
989 |
return false; |
990 |
} |
991 |
|
992 |
@Override
|
993 |
public FromBuilder from() {
|
994 |
if (this.from == null) { |
995 |
this.from = createFromBuilder();
|
996 |
} |
997 |
return this.from; |
998 |
} |
999 |
|
1000 |
@Override
|
1001 |
public boolean has_from() { |
1002 |
return this.from != null; |
1003 |
} |
1004 |
|
1005 |
@Override
|
1006 |
public GeometryExpressionBuilder where() {
|
1007 |
if (this.where == null) { |
1008 |
this.where = createExpressionBuilder();
|
1009 |
} |
1010 |
return this.where; |
1011 |
} |
1012 |
|
1013 |
@Override
|
1014 |
public boolean has_where() { |
1015 |
if (this.where == null) { |
1016 |
return false; |
1017 |
} |
1018 |
return this.where.value() != null; |
1019 |
} |
1020 |
|
1021 |
@Override
|
1022 |
public SelectBuilder limit(long limit) { |
1023 |
this.limit = limit;
|
1024 |
return this; |
1025 |
} |
1026 |
|
1027 |
@Override
|
1028 |
public SelectBuilder limit(Long limit) { |
1029 |
if (limit == null) { |
1030 |
this.limit = 0; |
1031 |
} else {
|
1032 |
this.limit = limit;
|
1033 |
} |
1034 |
return this; |
1035 |
} |
1036 |
|
1037 |
@Override
|
1038 |
public boolean has_limit() { |
1039 |
return this.limit > 0; |
1040 |
} |
1041 |
|
1042 |
@Override
|
1043 |
public SelectBuilder offset(long offset) { |
1044 |
this.offset = offset;
|
1045 |
return this; |
1046 |
} |
1047 |
|
1048 |
@Override
|
1049 |
public boolean has_offset() { |
1050 |
return this.offset > 0; |
1051 |
} |
1052 |
|
1053 |
@Override
|
1054 |
public OrderByBuilder order_by() {
|
1055 |
if (this.order_by == null) { |
1056 |
this.order_by = new ArrayList<>(); |
1057 |
} |
1058 |
OrderByBuilder order = createOrderByBuilder(); |
1059 |
this.order_by.add(order);
|
1060 |
return order;
|
1061 |
} |
1062 |
|
1063 |
@Override
|
1064 |
public boolean has_order_by() { |
1065 |
if (this.order_by == null) { |
1066 |
return false; |
1067 |
} |
1068 |
return !this.order_by.isEmpty(); |
1069 |
} |
1070 |
|
1071 |
@Override
|
1072 |
public boolean has_group_by() { |
1073 |
if (this.groupColumn == null) { |
1074 |
return false; |
1075 |
} |
1076 |
return !this.groupColumn.isEmpty(); |
1077 |
} |
1078 |
|
1079 |
protected boolean isValid(StringBuilder message) { |
1080 |
if (message == null) { |
1081 |
message = new StringBuilder(); |
1082 |
} |
1083 |
if (this.has_offset() && !this.has_order_by()) { |
1084 |
// Algunos gestores de BBDD requieren que se especifique un
|
1085 |
// orden para poder usar OFFSET. Como eso parece buena idea para
|
1086 |
// asegurar que siempre tengamos los mismo resultados, lo exijimos
|
1087 |
// siempre.
|
1088 |
message.append("Can't use OFFSET without an ORDER BY.");
|
1089 |
return false; |
1090 |
} |
1091 |
return true; |
1092 |
} |
1093 |
|
1094 |
@Override
|
1095 |
public String toString() { |
1096 |
return this.toString(formatter()); |
1097 |
} |
1098 |
|
1099 |
@Override
|
1100 |
public String toString(Formatter<Value> formatter) { |
1101 |
if (formatter!=null && formatter.canApply(this)) { |
1102 |
return formatter.format(this); |
1103 |
} |
1104 |
StringBuilder builder = new StringBuilder(); |
1105 |
if (!this.isValid(builder)) { |
1106 |
throw new IllegalStateException(builder.toString()); |
1107 |
} |
1108 |
builder.append("SELECT ");
|
1109 |
if (this.distinct) { |
1110 |
builder.append("DISTINCT ");
|
1111 |
} |
1112 |
boolean first = true; |
1113 |
for (SelectColumnBuilder column : columns) {
|
1114 |
if (first) {
|
1115 |
first = false;
|
1116 |
} else {
|
1117 |
builder.append(", ");
|
1118 |
} |
1119 |
builder.append(column.toString(formatter)); |
1120 |
} |
1121 |
|
1122 |
if (this.has_from()) { |
1123 |
builder.append(" FROM ");
|
1124 |
builder.append(this.from.toString(formatter));
|
1125 |
} |
1126 |
if( this.has_group_by() ) { |
1127 |
builder.append(" GROUP BY ");
|
1128 |
builder.append(this.groupColumn.get(0).toString(formatter)); |
1129 |
for (int i = 1; i < groupColumn.size(); i++) { |
1130 |
builder.append(", ");
|
1131 |
builder.append(this.groupColumn.get(i).toString(formatter));
|
1132 |
} |
1133 |
} |
1134 |
if (this.has_where()) { |
1135 |
builder.append(" WHERE ");
|
1136 |
builder.append(this.where.toString(formatter));
|
1137 |
} |
1138 |
|
1139 |
if (this.has_order_by()) { |
1140 |
builder.append(" ORDER BY ");
|
1141 |
first = true;
|
1142 |
for (OrderByBuilder item : this.order_by) { |
1143 |
if (first) {
|
1144 |
first = false;
|
1145 |
} else {
|
1146 |
builder.append(", ");
|
1147 |
} |
1148 |
builder.append(item.toString(formatter)); |
1149 |
} |
1150 |
} |
1151 |
|
1152 |
if (this.has_limit()) { |
1153 |
builder.append(" LIMIT ");
|
1154 |
builder.append(this.limit);
|
1155 |
} |
1156 |
if (this.has_offset()) { |
1157 |
builder.append(" OFFSET ");
|
1158 |
builder.append(this.offset);
|
1159 |
} |
1160 |
return builder.toString();
|
1161 |
|
1162 |
} |
1163 |
} |
1164 |
|
1165 |
public class DropTableBuilderBase |
1166 |
extends AbstractStatement
|
1167 |
implements DropTableBuilder {
|
1168 |
|
1169 |
protected TableNameBuilder table;
|
1170 |
|
1171 |
@Override
|
1172 |
public TableNameBuilder table() {
|
1173 |
if (table == null) { |
1174 |
table = createTableNameBuilder(); |
1175 |
} |
1176 |
return table;
|
1177 |
} |
1178 |
|
1179 |
@Override
|
1180 |
public void accept(Visitor visitor, VisitorFilter filter) { |
1181 |
if (filter.accept(this)) { |
1182 |
visitor.visit(this);
|
1183 |
} |
1184 |
this.table.accept(visitor, filter);
|
1185 |
} |
1186 |
|
1187 |
@Override
|
1188 |
public String toString() { |
1189 |
return this.toString(formatter()); |
1190 |
} |
1191 |
|
1192 |
@Override
|
1193 |
public String toString(Formatter<Value> formatter) { |
1194 |
if (formatter!=null && formatter.canApply(this)) { |
1195 |
return formatter.format(this); |
1196 |
} |
1197 |
StringBuilder builder = new StringBuilder(); |
1198 |
boolean first = true; |
1199 |
for (String sql : toStrings(formatter)) { |
1200 |
if (StringUtils.isEmpty(sql)) {
|
1201 |
continue;
|
1202 |
} |
1203 |
if (first) {
|
1204 |
first = false;
|
1205 |
} else {
|
1206 |
builder.append("; ");
|
1207 |
} |
1208 |
builder.append(sql); |
1209 |
} |
1210 |
return builder.toString();
|
1211 |
} |
1212 |
|
1213 |
@Override
|
1214 |
public List<String> toStrings() { |
1215 |
return this.toStrings(formatter()); |
1216 |
} |
1217 |
|
1218 |
@Override
|
1219 |
public List<String> toStrings(Formatter formatter) { |
1220 |
List<String> sqls = new ArrayList<>(); |
1221 |
|
1222 |
sqls.add( |
1223 |
MessageFormat.format(
|
1224 |
STMT_DROP_TABLE_table, |
1225 |
this.table.toString(formatter)
|
1226 |
) |
1227 |
); |
1228 |
String sql;
|
1229 |
if (!StringUtils.isBlank(STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table)) {
|
1230 |
if (this.table.has_schema()) { |
1231 |
sql = MessageFormat.format(
|
1232 |
STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table, |
1233 |
as_string(this.table.getSchema()),
|
1234 |
as_string(this.table.getName())
|
1235 |
); |
1236 |
} else {
|
1237 |
sql = MessageFormat.format(
|
1238 |
STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_table, |
1239 |
as_identifier(this.table.getName())
|
1240 |
); |
1241 |
} |
1242 |
if (!StringUtils.isEmpty(sql)) {
|
1243 |
sqls.add(sql); |
1244 |
} |
1245 |
} |
1246 |
return sqls;
|
1247 |
} |
1248 |
} |
1249 |
|
1250 |
public class GrantRoleBuilderBase |
1251 |
extends AbstractStatementPart
|
1252 |
implements GrantRoleBuilder {
|
1253 |
|
1254 |
protected TableNameBuilder table;
|
1255 |
protected String role; |
1256 |
protected Set<Privilege> privileges; |
1257 |
|
1258 |
public GrantRoleBuilderBase(TableNameBuilder table, String role) { |
1259 |
this.table = table;
|
1260 |
this.role = role;
|
1261 |
this.privileges = new HashSet<>(); |
1262 |
} |
1263 |
|
1264 |
@Override
|
1265 |
public GrantRoleBuilder privilege(Privilege privilege) {
|
1266 |
privileges.add(privilege); |
1267 |
return this; |
1268 |
} |
1269 |
|
1270 |
@Override
|
1271 |
public GrantRoleBuilder select() {
|
1272 |
privileges.add(Privilege.SELECT); |
1273 |
return this; |
1274 |
} |
1275 |
|
1276 |
@Override
|
1277 |
public GrantRoleBuilder update() {
|
1278 |
privileges.add(Privilege.UPDATE); |
1279 |
return this; |
1280 |
} |
1281 |
|
1282 |
@Override
|
1283 |
public GrantRoleBuilder insert() {
|
1284 |
privileges.add(Privilege.INSERT); |
1285 |
return this; |
1286 |
} |
1287 |
|
1288 |
@Override
|
1289 |
public GrantRoleBuilder delete() {
|
1290 |
privileges.add(Privilege.DELETE); |
1291 |
return this; |
1292 |
} |
1293 |
|
1294 |
@Override
|
1295 |
public GrantRoleBuilder truncate() {
|
1296 |
privileges.add(Privilege.TRUNCATE); |
1297 |
return this; |
1298 |
} |
1299 |
|
1300 |
@Override
|
1301 |
public GrantRoleBuilder reference() {
|
1302 |
privileges.add(Privilege.REFERENCE); |
1303 |
return this; |
1304 |
} |
1305 |
|
1306 |
@Override
|
1307 |
public GrantRoleBuilder trigger() {
|
1308 |
privileges.add(Privilege.TRIGGER); |
1309 |
return this; |
1310 |
} |
1311 |
|
1312 |
@Override
|
1313 |
public GrantRoleBuilder all() {
|
1314 |
privileges.add(Privilege.ALL); |
1315 |
return this; |
1316 |
} |
1317 |
|
1318 |
protected String getPrivilegeName(Privilege privilege) { |
1319 |
switch (privilege) {
|
1320 |
case DELETE:
|
1321 |
return "DELETE"; |
1322 |
case INSERT:
|
1323 |
return "INSERT"; |
1324 |
case REFERENCE:
|
1325 |
return "REFERENCE"; |
1326 |
case SELECT:
|
1327 |
return "SELECT"; |
1328 |
case TRIGGER:
|
1329 |
return "TRIGGER"; |
1330 |
case TRUNCATE:
|
1331 |
return "TRUNCATE"; |
1332 |
case UPDATE:
|
1333 |
return "UPDATE"; |
1334 |
case ALL:
|
1335 |
default:
|
1336 |
return "ALL"; |
1337 |
} |
1338 |
} |
1339 |
|
1340 |
@Override
|
1341 |
public String toString() { |
1342 |
return this.toString(formatter()); |
1343 |
} |
1344 |
|
1345 |
@Override
|
1346 |
public String toString(Formatter<Value> formatter) { |
1347 |
if (formatter!=null && formatter.canApply(this)) { |
1348 |
return formatter.format(this); |
1349 |
} |
1350 |
StringBuilder builder = new StringBuilder(); |
1351 |
boolean first = true; |
1352 |
for (Privilege privilege : privileges) {
|
1353 |
if (first) {
|
1354 |
first = false;
|
1355 |
} else {
|
1356 |
builder.append(", ");
|
1357 |
} |
1358 |
builder.append(this.getPrivilegeName(privilege));
|
1359 |
} |
1360 |
String sql = MessageFormat.format( |
1361 |
STMT_GRANT_privileges_ON_table_TO_role, |
1362 |
builder.toString(), |
1363 |
table.toString(formatter), |
1364 |
role |
1365 |
); |
1366 |
return sql;
|
1367 |
} |
1368 |
} |
1369 |
|
1370 |
public class GrantBuilderBase |
1371 |
extends AbstractStatement
|
1372 |
implements GrantBuilder {
|
1373 |
|
1374 |
protected TableNameBuilder table;
|
1375 |
protected Map<String, GrantRoleBuilder> roles; |
1376 |
|
1377 |
public GrantBuilderBase() {
|
1378 |
this.roles = new HashMap<>(); |
1379 |
} |
1380 |
|
1381 |
@Override
|
1382 |
public TableNameBuilder table() {
|
1383 |
if (table == null) { |
1384 |
table = createTableNameBuilder(); |
1385 |
} |
1386 |
return table;
|
1387 |
} |
1388 |
|
1389 |
@Override
|
1390 |
public void accept(Visitor visitor, VisitorFilter filter) { |
1391 |
if (filter.accept(this)) { |
1392 |
visitor.visit(this);
|
1393 |
} |
1394 |
if (this.table != null) { |
1395 |
this.table.accept(visitor, filter);
|
1396 |
} |
1397 |
} |
1398 |
|
1399 |
@Override
|
1400 |
public GrantRoleBuilder role(String role) { |
1401 |
GrantRoleBuilder roleBuilder = this.roles.get(role);
|
1402 |
if (roleBuilder == null) { |
1403 |
roleBuilder = createGrantRoleBuilder(this.table(), role);
|
1404 |
this.roles.put(role, roleBuilder);
|
1405 |
} |
1406 |
return roleBuilder;
|
1407 |
} |
1408 |
|
1409 |
@Override
|
1410 |
public String toString() { |
1411 |
return this.toString(formatter()); |
1412 |
} |
1413 |
|
1414 |
@Override
|
1415 |
public String toString(Formatter<Value> formatter) { |
1416 |
if (formatter!=null && formatter.canApply(this)) { |
1417 |
return formatter.format(this); |
1418 |
} |
1419 |
StringBuilder builder = new StringBuilder(); |
1420 |
boolean first = true; |
1421 |
for (String sql : toStrings(formatter)) { |
1422 |
if (StringUtils.isEmpty(sql)) {
|
1423 |
continue;
|
1424 |
} |
1425 |
if (first) {
|
1426 |
first = false;
|
1427 |
} else {
|
1428 |
builder.append("; ");
|
1429 |
} |
1430 |
builder.append(sql); |
1431 |
} |
1432 |
return builder.toString();
|
1433 |
} |
1434 |
|
1435 |
@Override
|
1436 |
public List<String> toStrings() { |
1437 |
return this.toStrings(formatter()); |
1438 |
} |
1439 |
|
1440 |
@Override
|
1441 |
public List<String> toStrings(Formatter formatter) { |
1442 |
List<String> sqls = new ArrayList<>(); |
1443 |
for (GrantRoleBuilder role : roles.values()) {
|
1444 |
sqls.add(role.toString(formatter)); |
1445 |
} |
1446 |
return sqls;
|
1447 |
} |
1448 |
} |
1449 |
|
1450 |
public class UpdateColumnBuilderBase |
1451 |
extends InsertColumnBuilderBase
|
1452 |
implements UpdateColumnBuilder {
|
1453 |
|
1454 |
public UpdateColumnBuilderBase() {
|
1455 |
super();
|
1456 |
} |
1457 |
|
1458 |
@Override
|
1459 |
public UpdateColumnBuilder name(String name) { |
1460 |
return (UpdateColumnBuilder) super.name(name); |
1461 |
} |
1462 |
|
1463 |
@Override
|
1464 |
public UpdateColumnBuilder with_value(Value value) {
|
1465 |
return (UpdateColumnBuilder) super.with_value(value); |
1466 |
} |
1467 |
|
1468 |
} |
1469 |
|
1470 |
public class UpdateBuilderBase |
1471 |
extends AbstractStatement
|
1472 |
implements UpdateBuilder {
|
1473 |
|
1474 |
protected GeometryExpressionBuilder where;
|
1475 |
protected List<UpdateColumnBuilder> columns; |
1476 |
protected TableNameBuilder table;
|
1477 |
|
1478 |
public UpdateBuilderBase() {
|
1479 |
this.columns = new ArrayList<>(); |
1480 |
} |
1481 |
|
1482 |
@Override
|
1483 |
public void accept(Visitor visitor, VisitorFilter filter) { |
1484 |
if (filter.accept(this)) { |
1485 |
visitor.visit(this);
|
1486 |
} |
1487 |
if (this.table != null) { |
1488 |
this.table.accept(visitor, filter);
|
1489 |
} |
1490 |
for (UpdateColumnBuilder column : columns) {
|
1491 |
column.accept(visitor, filter); |
1492 |
} |
1493 |
if (this.has_where()) { |
1494 |
this.where.accept(visitor, filter);
|
1495 |
} |
1496 |
} |
1497 |
|
1498 |
@Override
|
1499 |
public GeometryExpressionBuilder where() {
|
1500 |
if (this.where == null) { |
1501 |
this.where = createExpressionBuilder();
|
1502 |
} |
1503 |
return this.where; |
1504 |
} |
1505 |
|
1506 |
@Override
|
1507 |
public TableNameBuilder table() {
|
1508 |
if (table == null) { |
1509 |
table = createTableNameBuilder(); |
1510 |
} |
1511 |
return table;
|
1512 |
} |
1513 |
|
1514 |
@Override
|
1515 |
public UpdateColumnBuilder column() {
|
1516 |
UpdateColumnBuilder column = createUpdateColumnBuilder(); |
1517 |
this.columns.add(column);
|
1518 |
return column;
|
1519 |
} |
1520 |
|
1521 |
@Override
|
1522 |
public boolean has_where() { |
1523 |
return this.where != null; |
1524 |
} |
1525 |
|
1526 |
@Override
|
1527 |
public String toString() { |
1528 |
return this.toString(formatter()); |
1529 |
} |
1530 |
|
1531 |
@Override
|
1532 |
public String toString(Formatter<Value> formatter) { |
1533 |
if (formatter!=null && formatter.canApply(this)) { |
1534 |
return formatter.format(this); |
1535 |
} |
1536 |
/*
|
1537 |
* UPDATE [ ONLY ] table [ [ AS ] alias ] SET { column = { expression |
|
1538 |
* DEFAULT } | ( column [, ...] ) = ( { expression | DEFAULT } [, ...] )
|
1539 |
* } [, ...] [ FROM fromlist ] [ WHERE condition ] [ RETURNING * |
|
1540 |
* output_expression [ AS output_name ] [, ...] ]
|
1541 |
*/
|
1542 |
StringBuilder columnsAndValues = new StringBuilder(); |
1543 |
|
1544 |
boolean first = true; |
1545 |
for (UpdateColumnBuilder column : columns) {
|
1546 |
if (first) {
|
1547 |
first = false;
|
1548 |
} else {
|
1549 |
columnsAndValues.append(", ");
|
1550 |
} |
1551 |
columnsAndValues.append(as_identifier(column.getName())); |
1552 |
columnsAndValues.append(" = ");
|
1553 |
columnsAndValues.append(column.getValue().toString(formatter)); |
1554 |
} |
1555 |
|
1556 |
String sql;
|
1557 |
if (this.has_where()) { |
1558 |
sql = MessageFormat.format(
|
1559 |
STMT_UPDATE_table_SET_columnsAndValues_WHERE_expresion, |
1560 |
this.table.toString(formatter),
|
1561 |
columnsAndValues.toString(), |
1562 |
this.where.toString(formatter)
|
1563 |
); |
1564 |
} else {
|
1565 |
sql = MessageFormat.format(
|
1566 |
STMT_UPDATE_table_SET_columnsAndValues, |
1567 |
this.table.toString(formatter),
|
1568 |
columnsAndValues.toString() |
1569 |
); |
1570 |
} |
1571 |
return sql;
|
1572 |
} |
1573 |
} |
1574 |
|
1575 |
public class DeleteBuilderBase |
1576 |
extends AbstractStatement
|
1577 |
implements DeleteBuilder {
|
1578 |
|
1579 |
protected GeometryExpressionBuilder where;
|
1580 |
protected TableNameBuilder table;
|
1581 |
|
1582 |
public DeleteBuilderBase() {
|
1583 |
} |
1584 |
|
1585 |
@Override
|
1586 |
public void accept(Visitor visitor, VisitorFilter filter) { |
1587 |
if (filter.accept(this)) { |
1588 |
visitor.visit(this);
|
1589 |
} |
1590 |
if (this.table != null) { |
1591 |
this.table.accept(visitor, filter);
|
1592 |
} |
1593 |
if (this.has_where()) { |
1594 |
this.where.accept(visitor, filter);
|
1595 |
} |
1596 |
} |
1597 |
|
1598 |
@Override
|
1599 |
public GeometryExpressionBuilder where() {
|
1600 |
if (this.where == null) { |
1601 |
this.where = createExpressionBuilder();
|
1602 |
} |
1603 |
return this.where; |
1604 |
} |
1605 |
|
1606 |
@Override
|
1607 |
public TableNameBuilder table() {
|
1608 |
if (table == null) { |
1609 |
table = createTableNameBuilder(); |
1610 |
} |
1611 |
return table;
|
1612 |
} |
1613 |
|
1614 |
@Override
|
1615 |
public boolean has_where() { |
1616 |
return this.where != null; |
1617 |
} |
1618 |
|
1619 |
@Override
|
1620 |
public String toString() { |
1621 |
return this.toString(formatter()); |
1622 |
} |
1623 |
|
1624 |
@Override
|
1625 |
public String toString(Formatter<Value> formatter) { |
1626 |
if (formatter!=null && formatter.canApply(this)) { |
1627 |
return formatter.format(this); |
1628 |
} |
1629 |
/*
|
1630 |
* DELETE FROM table_name
|
1631 |
* WHERE some_column=some_value;
|
1632 |
*/
|
1633 |
String sql;
|
1634 |
if (this.has_where()) { |
1635 |
sql = MessageFormat.format(
|
1636 |
STMT_DELETE_FROM_table_WHERE_expresion, |
1637 |
this.table.toString(formatter),
|
1638 |
this.where.toString(formatter)
|
1639 |
); |
1640 |
} else {
|
1641 |
sql = MessageFormat.format(
|
1642 |
STMT_DELETE_FROM_table, |
1643 |
this.table.toString(formatter)
|
1644 |
); |
1645 |
} |
1646 |
return sql;
|
1647 |
} |
1648 |
} |
1649 |
|
1650 |
public class CreateIndexBuilderBase |
1651 |
extends AbstractStatement
|
1652 |
implements CreateIndexBuilder {
|
1653 |
|
1654 |
protected boolean ifNotExist = false; |
1655 |
protected boolean isUnique = false; |
1656 |
protected String indexName; |
1657 |
protected boolean isSpatial = false; |
1658 |
protected TableNameBuilder table;
|
1659 |
protected final List<String> columns; |
1660 |
|
1661 |
public CreateIndexBuilderBase() {
|
1662 |
this.columns = new ArrayList<>(); |
1663 |
} |
1664 |
|
1665 |
@Override
|
1666 |
public CreateIndexBuilder unique() {
|
1667 |
this.isUnique = true; |
1668 |
return this; |
1669 |
} |
1670 |
|
1671 |
@Override
|
1672 |
public CreateIndexBuilder if_not_exist() {
|
1673 |
this.ifNotExist = true; |
1674 |
return this; |
1675 |
} |
1676 |
|
1677 |
@Override
|
1678 |
public CreateIndexBuilder name(String name) { |
1679 |
this.indexName = name;
|
1680 |
return this; |
1681 |
} |
1682 |
|
1683 |
@Override
|
1684 |
public CreateIndexBuilder spatial() {
|
1685 |
this.isSpatial = true; |
1686 |
return this; |
1687 |
} |
1688 |
|
1689 |
@Override
|
1690 |
public CreateIndexBuilder column(String name) { |
1691 |
this.columns.add(name);
|
1692 |
return this; |
1693 |
} |
1694 |
|
1695 |
@Override
|
1696 |
public TableNameBuilder table() {
|
1697 |
if (table == null) { |
1698 |
table = createTableNameBuilder(); |
1699 |
} |
1700 |
return table;
|
1701 |
} |
1702 |
|
1703 |
@Override
|
1704 |
public void accept(Visitor visitor, VisitorFilter filter) { |
1705 |
if (filter.accept(this)) { |
1706 |
visitor.visit(this);
|
1707 |
} |
1708 |
if (this.table != null) { |
1709 |
this.table.accept(visitor, filter);
|
1710 |
} |
1711 |
} |
1712 |
|
1713 |
@Override
|
1714 |
public String toString() { |
1715 |
return this.toString(formatter()); |
1716 |
} |
1717 |
|
1718 |
@Override
|
1719 |
public String toString(Formatter<Value> formatter) { |
1720 |
if (formatter!=null && formatter.canApply(this)) { |
1721 |
return formatter.format(this); |
1722 |
} |
1723 |
StringBuilder builder = new StringBuilder(); |
1724 |
boolean first = true; |
1725 |
for (String sql : toStrings(formatter)) { |
1726 |
if (StringUtils.isEmpty(sql)) {
|
1727 |
continue;
|
1728 |
} |
1729 |
if (first) {
|
1730 |
first = false;
|
1731 |
} else {
|
1732 |
builder.append("; ");
|
1733 |
} |
1734 |
builder.append(sql); |
1735 |
} |
1736 |
return builder.toString();
|
1737 |
} |
1738 |
|
1739 |
@Override
|
1740 |
public List<String> toStrings() { |
1741 |
return this.toStrings(formatter()); |
1742 |
} |
1743 |
|
1744 |
@Override
|
1745 |
public List<String> toStrings(Formatter formatter) { |
1746 |
StringBuilder builder = new StringBuilder(); |
1747 |
builder.append("CREATE ");
|
1748 |
if (this.isUnique) { |
1749 |
builder.append("UNIQUE ");
|
1750 |
} |
1751 |
builder.append("INDEX ");
|
1752 |
if (this.ifNotExist) { |
1753 |
builder.append("IF NOT EXISTS ");
|
1754 |
} |
1755 |
builder.append(as_identifier(this.indexName));
|
1756 |
builder.append(" ON ");
|
1757 |
builder.append(this.table.toString(formatter));
|
1758 |
if (this.isSpatial) { |
1759 |
builder.append(" USING GIST ");
|
1760 |
} |
1761 |
builder.append(" ( ");
|
1762 |
boolean is_first_column = true; |
1763 |
for (String column : this.columns) { |
1764 |
if (is_first_column) {
|
1765 |
is_first_column = false;
|
1766 |
} else {
|
1767 |
builder.append(", ");
|
1768 |
} |
1769 |
builder.append(column); |
1770 |
} |
1771 |
builder.append(" )");
|
1772 |
|
1773 |
List<String> sqls = new ArrayList<>(); |
1774 |
sqls.add(builder.toString()); |
1775 |
return sqls;
|
1776 |
} |
1777 |
|
1778 |
} |
1779 |
|
1780 |
public class AlterTableBuilderBase |
1781 |
extends AbstractStatement
|
1782 |
implements AlterTableBuilder {
|
1783 |
|
1784 |
protected TableNameBuilder table;
|
1785 |
protected List<String> drops; |
1786 |
protected List<ColumnDescriptor> adds; |
1787 |
protected List<ColumnDescriptor> alters; |
1788 |
protected List<Pair<String, String>> renames; |
1789 |
|
1790 |
public AlterTableBuilderBase() {
|
1791 |
this.drops = new ArrayList<>(); |
1792 |
this.adds = new ArrayList<>(); |
1793 |
this.alters = new ArrayList<>(); |
1794 |
this.renames = new ArrayList<>(); |
1795 |
} |
1796 |
|
1797 |
@Override
|
1798 |
public boolean isEmpty() { |
1799 |
return this.drops.isEmpty() |
1800 |
&& this.adds.isEmpty()
|
1801 |
&& this.alters.isEmpty()
|
1802 |
&& this.renames.isEmpty();
|
1803 |
} |
1804 |
|
1805 |
@Override
|
1806 |
public void accept(Visitor visitor, VisitorFilter filter) { |
1807 |
if (filter.accept(this)) { |
1808 |
visitor.visit(this);
|
1809 |
} |
1810 |
if (this.table != null) { |
1811 |
this.table.accept(visitor, filter);
|
1812 |
} |
1813 |
} |
1814 |
|
1815 |
@Override
|
1816 |
public TableNameBuilder table() {
|
1817 |
if (table == null) { |
1818 |
table = createTableNameBuilder(); |
1819 |
} |
1820 |
return table;
|
1821 |
} |
1822 |
|
1823 |
@Override
|
1824 |
public AlterTableBuilder drop_column(String columnName) { |
1825 |
this.drops.add(columnName);
|
1826 |
return this; |
1827 |
} |
1828 |
|
1829 |
@Override
|
1830 |
public AlterTableBuilder add_column(FeatureAttributeDescriptor fad) {
|
1831 |
this.adds.add(new ColumnDescriptorBase(fad)); |
1832 |
return this; |
1833 |
} |
1834 |
|
1835 |
@Override
|
1836 |
public AlterTableBuilder add_column(String columnName, int type, int type_p, int type_s, boolean isPk, boolean isIndexed, boolean allowNulls, boolean isAutomatic, Object defaultValue) { |
1837 |
if (isPk || isAutomatic) {
|
1838 |
allowNulls = false;
|
1839 |
} |
1840 |
this.adds.add(new ColumnDescriptorBase(columnName, type, type_p, type_s, isPk, isIndexed, allowNulls, isAutomatic, defaultValue)); |
1841 |
return this; |
1842 |
} |
1843 |
|
1844 |
@Override
|
1845 |
public AlterTableBuilder add_geometry_column(String columnName, int type, int subtype, IProjection proj, boolean isIndexed, boolean allowNulls) { |
1846 |
if (StringUtils.isEmpty(columnName)) {
|
1847 |
throw new IllegalArgumentException("Argument 'columnName' can't be empty."); |
1848 |
} |
1849 |
this.adds.add(new ColumnDescriptorBase(columnName, type, subtype, proj, isIndexed, allowNulls)); |
1850 |
return this; |
1851 |
} |
1852 |
|
1853 |
@Override
|
1854 |
public AlterTableBuilder add_geometry_column(String columnName, int type, int subtype, Object srsdbcode, boolean isIndexed, boolean allowNulls) { |
1855 |
if (StringUtils.isEmpty(columnName)) {
|
1856 |
throw new IllegalArgumentException("Argument 'columnName' can't be empty."); |
1857 |
} |
1858 |
this.adds.add(new ColumnDescriptorBase(columnName, type, subtype, srsdbcode, isIndexed, allowNulls)); |
1859 |
return this; |
1860 |
} |
1861 |
|
1862 |
@Override
|
1863 |
public AlterTableBuilder alter_column(FeatureAttributeDescriptor fad) {
|
1864 |
this.alters.add(new ColumnDescriptorBase(fad)); |
1865 |
return this; |
1866 |
} |
1867 |
|
1868 |
@Override
|
1869 |
public AlterTableBuilder alter_column(String columnName, int type, int type_p, int type_s, boolean isPk, boolean isIndexed, boolean allowNulls, boolean isAutomatic, Object defaultValue) { |
1870 |
if (isPk || isAutomatic) {
|
1871 |
allowNulls = false;
|
1872 |
} |
1873 |
this.alters.add(new ColumnDescriptorBase(columnName, type, type_p, type_s, isPk, isIndexed, allowNulls, isAutomatic, defaultValue)); |
1874 |
return this; |
1875 |
} |
1876 |
|
1877 |
@Override
|
1878 |
public AlterTableBuilder alter_geometry_column(String columnName, int type, int subtype, IProjection proj, boolean isIndexed, boolean allowNulls) { |
1879 |
if (StringUtils.isEmpty(columnName)) {
|
1880 |
throw new IllegalArgumentException("Argument 'columnName' can't be empty."); |
1881 |
} |
1882 |
this.alters.add(new ColumnDescriptorBase(columnName, type, subtype, proj, isIndexed, allowNulls)); |
1883 |
return this; |
1884 |
} |
1885 |
|
1886 |
@Override
|
1887 |
public AlterTableBuilder alter_geometry_column(String columnName, int type, int subtype, Object srsdbcode, boolean isIndexed, boolean allowNulls) { |
1888 |
if (StringUtils.isEmpty(columnName)) {
|
1889 |
throw new IllegalArgumentException("Argument 'columnName' can't be empty."); |
1890 |
} |
1891 |
this.alters.add(new ColumnDescriptorBase(columnName, type, subtype, srsdbcode, isIndexed, allowNulls)); |
1892 |
return this; |
1893 |
} |
1894 |
|
1895 |
@Override
|
1896 |
public AlterTableBuilder rename_column(String source, String target) { |
1897 |
this.renames.add(new ImmutablePair(source, target)); |
1898 |
return this; |
1899 |
} |
1900 |
|
1901 |
@Override
|
1902 |
public String toString() { |
1903 |
return this.toString(formatter()); |
1904 |
} |
1905 |
|
1906 |
@Override
|
1907 |
public String toString(Formatter<Value> formatter) { |
1908 |
if (formatter!=null && formatter.canApply(this)) { |
1909 |
return formatter.format(this); |
1910 |
} |
1911 |
StringBuilder builder = new StringBuilder(); |
1912 |
boolean first = true; |
1913 |
for (String sql : toStrings(formatter)) { |
1914 |
if (StringUtils.isEmpty(sql)) {
|
1915 |
continue;
|
1916 |
} |
1917 |
if (first) {
|
1918 |
first = false;
|
1919 |
} else {
|
1920 |
builder.append("; ");
|
1921 |
} |
1922 |
builder.append(sql); |
1923 |
} |
1924 |
return builder.toString();
|
1925 |
} |
1926 |
|
1927 |
@Override
|
1928 |
public List<String> toStrings() { |
1929 |
return this.toStrings(formatter()); |
1930 |
} |
1931 |
|
1932 |
@Override
|
1933 |
public List<String> toStrings(Formatter formatter) { |
1934 |
List<String> sqls = new ArrayList<>(); |
1935 |
if (this.isEmpty()) { |
1936 |
return sqls;
|
1937 |
} |
1938 |
for (String column : drops) { |
1939 |
StringBuilder builder = new StringBuilder(); |
1940 |
builder.append("ALTER TABLE ");
|
1941 |
builder.append(this.table.toString(formatter));
|
1942 |
builder.append(" DROP COLUMN IF EXISTS ");
|
1943 |
builder.append(as_identifier(column)); |
1944 |
sqls.add(builder.toString()); |
1945 |
} |
1946 |
for (ColumnDescriptor column : adds) {
|
1947 |
StringBuilder builder = new StringBuilder(); |
1948 |
builder.append("ALTER TABLE ");
|
1949 |
builder.append(this.table.toString(formatter));
|
1950 |
builder.append(" ADD COLUMN ");
|
1951 |
builder.append(as_identifier(column.getName())); |
1952 |
builder.append(" ");
|
1953 |
if (column.getType() == DataTypes.INT && column.isAutomatic()) {
|
1954 |
builder.append(" SERIAL");
|
1955 |
} else {
|
1956 |
builder.append( |
1957 |
sqltype( |
1958 |
column.getType(), |
1959 |
column.getPrecision(), |
1960 |
column.getSize(), |
1961 |
column.getGeometryType(), |
1962 |
column.getGeometrySubtype() |
1963 |
) |
1964 |
); |
1965 |
} |
1966 |
if (column.getDefaultValue() == null) { |
1967 |
if (column.allowNulls()) {
|
1968 |
builder.append(" DEFAULT NULL");
|
1969 |
} |
1970 |
} else {
|
1971 |
builder.append(" DEFAULT '");
|
1972 |
builder.append(Objects.toString(column.getDefaultValue(),""));
|
1973 |
builder.append("'");
|
1974 |
} |
1975 |
if (column.allowNulls()) {
|
1976 |
builder.append(" NULL");
|
1977 |
} else {
|
1978 |
builder.append(" NOT NULL");
|
1979 |
} |
1980 |
if (column.isPrimaryKey()) {
|
1981 |
builder.append(" PRIMARY KEY");
|
1982 |
} |
1983 |
sqls.add(builder.toString()); |
1984 |
} |
1985 |
for (ColumnDescriptor column : alters) {
|
1986 |
StringBuilder builder = new StringBuilder(); |
1987 |
builder.append("ALTER TABLE ");
|
1988 |
builder.append(this.table.toString(formatter));
|
1989 |
builder.append(" ALTER COLUMN ");
|
1990 |
builder.append(as_identifier(column.getName())); |
1991 |
builder.append(" SET DATA TYPE ");
|
1992 |
if (column.getType() == DataTypes.INT && column.isAutomatic()) {
|
1993 |
builder.append(" SERIAL");
|
1994 |
} else {
|
1995 |
builder.append( |
1996 |
sqltype( |
1997 |
column.getType(), |
1998 |
column.getPrecision(), |
1999 |
column.getSize(), |
2000 |
column.getGeometryType(), |
2001 |
column.getGeometrySubtype() |
2002 |
) |
2003 |
); |
2004 |
} |
2005 |
if (column.getDefaultValue() == null) { |
2006 |
if (column.allowNulls()) {
|
2007 |
builder.append(" DEFAULT NULL");
|
2008 |
} else {
|
2009 |
builder.append(" DROP DEFAULT");
|
2010 |
} |
2011 |
} else {
|
2012 |
builder.append(" DEFAULT '");
|
2013 |
builder.append(column.getDefaultValue().toString()); |
2014 |
builder.append("'");
|
2015 |
} |
2016 |
sqls.add(builder.toString()); |
2017 |
} |
2018 |
for (Pair<String, String> pair : renames) { |
2019 |
StringBuilder builder = new StringBuilder(); |
2020 |
builder.append("ALTER TABLE ");
|
2021 |
builder.append(this.table.toString(formatter));
|
2022 |
builder.append(" RENAME COLUMN ");
|
2023 |
builder.append(as_identifier(pair.getLeft())); |
2024 |
builder.append(" TO ");
|
2025 |
builder.append(as_identifier(pair.getRight())); |
2026 |
sqls.add(builder.toString()); |
2027 |
} |
2028 |
return sqls;
|
2029 |
} |
2030 |
|
2031 |
} |
2032 |
|
2033 |
public class CreateTableBuilderBase |
2034 |
extends AbstractStatement
|
2035 |
implements CreateTableBuilder {
|
2036 |
|
2037 |
protected TableNameBuilder table;
|
2038 |
protected List<ColumnDescriptor> columns; |
2039 |
|
2040 |
public CreateTableBuilderBase() {
|
2041 |
this.columns = new ArrayList<>(); |
2042 |
} |
2043 |
|
2044 |
@Override
|
2045 |
public void accept(Visitor visitor, VisitorFilter filter) { |
2046 |
if (filter.accept(this)) { |
2047 |
visitor.visit(this);
|
2048 |
} |
2049 |
if (this.table != null) { |
2050 |
this.table.accept(visitor, filter);
|
2051 |
} |
2052 |
} |
2053 |
|
2054 |
@Override
|
2055 |
public TableNameBuilder table() {
|
2056 |
if (table == null) { |
2057 |
table = createTableNameBuilder(); |
2058 |
} |
2059 |
return table;
|
2060 |
} |
2061 |
|
2062 |
@Override
|
2063 |
public CreateTableBuilderBase add_column(FeatureAttributeDescriptor fad) {
|
2064 |
this.columns.add(new ColumnDescriptorBase(fad)); |
2065 |
return this; |
2066 |
} |
2067 |
|
2068 |
@Override
|
2069 |
public CreateTableBuilderBase add_column(String columnName, int type, int type_p, int type_s, boolean isPk, boolean isIndexed, boolean allowNulls, boolean isAutomatic, Object defaultValue) { |
2070 |
if (StringUtils.isEmpty(columnName)) {
|
2071 |
throw new IllegalArgumentException("Argument 'columnName' can't be empty."); |
2072 |
} |
2073 |
if (isPk || isAutomatic) {
|
2074 |
allowNulls = false;
|
2075 |
} |
2076 |
this.columns.add(new ColumnDescriptorBase(columnName, type, type_p, type_s, isPk, isIndexed, allowNulls, isAutomatic, defaultValue)); |
2077 |
return this; |
2078 |
} |
2079 |
|
2080 |
@Override
|
2081 |
public CreateTableBuilder add_geometry_column(String columnName, int type, int subtype, IProjection proj, boolean isIndexed, boolean allowNulls) { |
2082 |
if (StringUtils.isEmpty(columnName)) {
|
2083 |
throw new IllegalArgumentException("Argument 'columnName' can't be empty."); |
2084 |
} |
2085 |
this.columns.add(new ColumnDescriptorBase(columnName, type, subtype, proj, isIndexed, allowNulls)); |
2086 |
return this; |
2087 |
} |
2088 |
|
2089 |
@Override
|
2090 |
public CreateTableBuilder add_geometry_column(String columnName, int type, int subtype, Object srsdbcode, boolean isIndexed, boolean allowNulls) { |
2091 |
if (StringUtils.isEmpty(columnName)) {
|
2092 |
throw new IllegalArgumentException("Argument 'columnName' can't be empty."); |
2093 |
} |
2094 |
this.columns.add(new ColumnDescriptorBase(columnName, type, subtype, srsdbcode, isIndexed, allowNulls)); |
2095 |
return this; |
2096 |
} |
2097 |
|
2098 |
@Override
|
2099 |
public ColumnDescriptor getColumnDescriptor(String columnName) { |
2100 |
if (StringUtils.isEmpty(columnName)) {
|
2101 |
return null; |
2102 |
} |
2103 |
for (ColumnDescriptor column : columns) {
|
2104 |
if (columnName.equals(column.getName())) {
|
2105 |
return column;
|
2106 |
} |
2107 |
} |
2108 |
return null; |
2109 |
} |
2110 |
|
2111 |
@Override
|
2112 |
public String toString() { |
2113 |
return this.toString(formatter()); |
2114 |
} |
2115 |
|
2116 |
@Override
|
2117 |
public String toString(Formatter<Value> formatter) { |
2118 |
if (formatter!=null && formatter.canApply(this)) { |
2119 |
return formatter.format(this); |
2120 |
} |
2121 |
StringBuilder builder = new StringBuilder(); |
2122 |
boolean first = true; |
2123 |
for (String sql : toStrings(formatter)) { |
2124 |
if (StringUtils.isEmpty(sql)) {
|
2125 |
continue;
|
2126 |
} |
2127 |
if (first) {
|
2128 |
first = false;
|
2129 |
} else {
|
2130 |
builder.append("; ");
|
2131 |
} |
2132 |
builder.append(sql); |
2133 |
} |
2134 |
return builder.toString();
|
2135 |
} |
2136 |
|
2137 |
@Override
|
2138 |
public List<String> toStrings() { |
2139 |
return this.toStrings(formatter()); |
2140 |
} |
2141 |
|
2142 |
@Override
|
2143 |
public List<String> toStrings(Formatter formatter) { |
2144 |
List<String> sqls = new ArrayList<>(); |
2145 |
/**
|
2146 |
* CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE
|
2147 |
* table_name ( { column_name data_type [ DEFAULT default_expr ] [
|
2148 |
* column_constraint [ ... ] ] | table_constraint | LIKE
|
2149 |
* parent_table [ { INCLUDING | EXCLUDING } DEFAULTS ] } [, ... ] )
|
2150 |
* [ INHERITS ( parent_table [, ... ] ) ] [ WITH OIDS | WITHOUT OIDS
|
2151 |
* ] [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
|
2152 |
*
|
2153 |
* where column_constraint is:
|
2154 |
*
|
2155 |
* [ CONSTRAINT constraint_name ] { NOT NULL | NULL | UNIQUE |
|
2156 |
* PRIMARY KEY | CHECK (expression) | REFERENCES reftable [ (
|
2157 |
* refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON
|
2158 |
* DELETE action ] [ ON UPDATE action ] } [ DEFERRABLE | NOT
|
2159 |
* DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
|
2160 |
*
|
2161 |
* and table_constraint is:
|
2162 |
*
|
2163 |
* [ CONSTRAINT constraint_name ] { UNIQUE ( column_name [, ... ] )
|
2164 |
* | PRIMARY KEY ( column_name [, ... ] ) | CHECK ( expression ) |
|
2165 |
* FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ (
|
2166 |
* refcolumn [, ... ] ) ] [ MATCH FULL | MATCH PARTIAL | MATCH
|
2167 |
* SIMPLE ] [ ON DELETE action ] [ ON UPDATE action ] } [ DEFERRABLE
|
2168 |
* | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
|
2169 |
*/
|
2170 |
StringBuilder builder = new StringBuilder(); |
2171 |
|
2172 |
builder.append("CREATE TABLE ");
|
2173 |
builder.append(this.table.toString(formatter));
|
2174 |
builder.append(" (");
|
2175 |
boolean first = true; |
2176 |
for (ColumnDescriptor column : columns) {
|
2177 |
if (first) {
|
2178 |
first = false;
|
2179 |
} else {
|
2180 |
builder.append(", ");
|
2181 |
} |
2182 |
builder.append(as_identifier(column.getName())); |
2183 |
builder.append(" ");
|
2184 |
if (column.isAutomatic() && column.getType() == DataTypes.INT) {
|
2185 |
builder.append("SERIAL");
|
2186 |
} else if (column.isAutomatic() && column.getType() == DataTypes.LONG) { |
2187 |
builder.append("BIGSERIAL");
|
2188 |
} else {
|
2189 |
builder.append(sqltype( |
2190 |
column.getType(), |
2191 |
column.getPrecision(), |
2192 |
column.getSize(), |
2193 |
column.getGeometryType(), |
2194 |
column.getGeometrySubtype() |
2195 |
) |
2196 |
); |
2197 |
} |
2198 |
if (column.getDefaultValue() == null) { |
2199 |
if (column.allowNulls()) {
|
2200 |
builder.append(" DEFAULT NULL");
|
2201 |
} |
2202 |
} else {
|
2203 |
builder.append(" DEFAULT '");
|
2204 |
builder.append(Objects.toString(column.getDefaultValue(),""));
|
2205 |
builder.append("'");
|
2206 |
} |
2207 |
if (column.allowNulls()) {
|
2208 |
builder.append(" NULL");
|
2209 |
} else {
|
2210 |
builder.append(" NOT NULL");
|
2211 |
} |
2212 |
if (column.isPrimaryKey()) {
|
2213 |
builder.append(" PRIMARY KEY");
|
2214 |
} |
2215 |
} |
2216 |
builder.append(" )");
|
2217 |
sqls.add(builder.toString()); |
2218 |
return sqls;
|
2219 |
} |
2220 |
} |
2221 |
|
2222 |
public class InsertColumnBuilderBase |
2223 |
extends AbstractStatement
|
2224 |
implements InsertColumnBuilder {
|
2225 |
|
2226 |
protected Variable name;
|
2227 |
protected Value value;
|
2228 |
|
2229 |
public InsertColumnBuilderBase() {
|
2230 |
} |
2231 |
|
2232 |
@Override
|
2233 |
public void accept(Visitor visitor, VisitorFilter filter) { |
2234 |
if (filter.accept(this)) { |
2235 |
visitor.visit(this);
|
2236 |
} |
2237 |
if (this.name != null) { |
2238 |
this.name.accept(visitor, filter);
|
2239 |
} |
2240 |
if (this.value != null) { |
2241 |
this.value.accept(visitor, filter);
|
2242 |
} |
2243 |
} |
2244 |
|
2245 |
@Override
|
2246 |
public InsertColumnBuilder name(String name) { |
2247 |
this.name = expression().variable(name);
|
2248 |
return this; |
2249 |
} |
2250 |
|
2251 |
@Override
|
2252 |
public InsertColumnBuilder with_value(Value value) {
|
2253 |
this.value = value;
|
2254 |
return this; |
2255 |
} |
2256 |
|
2257 |
@Override
|
2258 |
public String getName() { |
2259 |
return this.name.name(); |
2260 |
} |
2261 |
|
2262 |
@Override
|
2263 |
public Value getValue() {
|
2264 |
return this.value; |
2265 |
} |
2266 |
|
2267 |
@Override
|
2268 |
public String toString() { |
2269 |
return this.toString(formatter()); |
2270 |
} |
2271 |
|
2272 |
@Override
|
2273 |
public String toString(Formatter<Value> formatter) { |
2274 |
if (formatter!=null && formatter.canApply(this)) { |
2275 |
return formatter.format(this); |
2276 |
} |
2277 |
return this.value.toString(formatter); |
2278 |
} |
2279 |
} |
2280 |
|
2281 |
public class InsertBuilderBase |
2282 |
extends AbstractStatement
|
2283 |
implements InsertBuilder {
|
2284 |
|
2285 |
protected List<InsertColumnBuilder> columns; |
2286 |
protected TableNameBuilder table;
|
2287 |
|
2288 |
public InsertBuilderBase() {
|
2289 |
this.columns = new ArrayList<>(); |
2290 |
} |
2291 |
|
2292 |
@Override
|
2293 |
public void accept(Visitor visitor, VisitorFilter filter) { |
2294 |
if (filter.accept(this)) { |
2295 |
visitor.visit(this);
|
2296 |
} |
2297 |
if (this.table != null) { |
2298 |
this.table.accept(visitor, filter);
|
2299 |
} |
2300 |
for (InsertColumnBuilder column : columns) {
|
2301 |
column.accept(visitor, filter); |
2302 |
} |
2303 |
} |
2304 |
|
2305 |
@Override
|
2306 |
public TableNameBuilder table() {
|
2307 |
if (table == null) { |
2308 |
table = createTableNameBuilder(); |
2309 |
} |
2310 |
return table;
|
2311 |
} |
2312 |
|
2313 |
@Override
|
2314 |
public InsertColumnBuilder column() {
|
2315 |
InsertColumnBuilder column = createInsertColumnBuilder(); |
2316 |
this.columns.add(column);
|
2317 |
return column;
|
2318 |
} |
2319 |
|
2320 |
@Override
|
2321 |
public String toString() { |
2322 |
return this.toString(formatter()); |
2323 |
} |
2324 |
|
2325 |
@Override
|
2326 |
public String toString(Formatter<Value> formatter) { |
2327 |
if (formatter!=null && formatter.canApply(this)) { |
2328 |
return formatter.format(this); |
2329 |
} |
2330 |
/*
|
2331 |
* INSERT INTO table [ ( column [, ...] ) ] { DEFAULT VALUES | VALUES (
|
2332 |
* { expression | DEFAULT } [, ...] ) [, ...] | query } [ RETURNING * |
|
2333 |
* output_expression [ AS output_name ] [, ...] ]
|
2334 |
*/
|
2335 |
StringBuilder builderColumns = new StringBuilder(); |
2336 |
StringBuilder builderValues = new StringBuilder(); |
2337 |
|
2338 |
boolean first = true; |
2339 |
for (InsertColumnBuilder column : columns) {
|
2340 |
if (first) {
|
2341 |
first = false;
|
2342 |
} else {
|
2343 |
builderColumns.append(", ");
|
2344 |
} |
2345 |
builderColumns.append(as_identifier(column.getName())); |
2346 |
} |
2347 |
first = true;
|
2348 |
for (InsertColumnBuilder column : columns) {
|
2349 |
if (first) {
|
2350 |
first = false;
|
2351 |
} else {
|
2352 |
builderValues.append(", ");
|
2353 |
} |
2354 |
builderValues.append(column.toString(formatter)); |
2355 |
} |
2356 |
|
2357 |
String sql = MessageFormat.format( |
2358 |
STMT_INSERT_INTO_table_columns_VALUES_values, |
2359 |
this.table.toString(formatter),
|
2360 |
builderColumns.toString(), |
2361 |
builderValues.toString() |
2362 |
); |
2363 |
return sql;
|
2364 |
|
2365 |
} |
2366 |
} |
2367 |
|
2368 |
public class UpdateTableStatisticsBuilderBase |
2369 |
extends AbstractStatement
|
2370 |
implements UpdateTableStatisticsBuilder {
|
2371 |
|
2372 |
protected TableNameBuilder table;
|
2373 |
|
2374 |
@Override
|
2375 |
public void accept(Visitor visitor, VisitorFilter filter) { |
2376 |
if (filter.accept(this)) { |
2377 |
visitor.visit(this);
|
2378 |
} |
2379 |
if (this.table != null) { |
2380 |
this.table.accept(visitor, filter);
|
2381 |
} |
2382 |
} |
2383 |
|
2384 |
@Override
|
2385 |
public TableNameBuilder table() {
|
2386 |
if (table == null) { |
2387 |
table = createTableNameBuilder(); |
2388 |
} |
2389 |
return table;
|
2390 |
} |
2391 |
|
2392 |
@Override
|
2393 |
public String toString() { |
2394 |
return this.toString(formatter()); |
2395 |
} |
2396 |
|
2397 |
@Override
|
2398 |
public String toString(Formatter<Value> formatter) { |
2399 |
if (formatter!=null && formatter.canApply(this)) { |
2400 |
return formatter.format(this); |
2401 |
} |
2402 |
StringBuilder builder = new StringBuilder(); |
2403 |
boolean first = true; |
2404 |
for (String sql : toStrings(formatter)) { |
2405 |
if (StringUtils.isEmpty(sql)) {
|
2406 |
continue;
|
2407 |
} |
2408 |
if (first) {
|
2409 |
first = false;
|
2410 |
} else {
|
2411 |
builder.append("; ");
|
2412 |
} |
2413 |
builder.append(sql); |
2414 |
} |
2415 |
return builder.toString();
|
2416 |
} |
2417 |
|
2418 |
@Override
|
2419 |
public List<String> toStrings() { |
2420 |
return this.toStrings(formatter()); |
2421 |
} |
2422 |
|
2423 |
@Override
|
2424 |
public List<String> toStrings(Formatter formatter) { |
2425 |
List<String> sqls = new ArrayList<>(); |
2426 |
|
2427 |
if (!StringUtils.isBlank(STMT_UPDATE_TABLE_STATISTICS_table)) {
|
2428 |
String sql = MessageFormat.format( |
2429 |
STMT_UPDATE_TABLE_STATISTICS_table, |
2430 |
table.toString(formatter) |
2431 |
); |
2432 |
if (!StringUtils.isEmpty(sql)) {
|
2433 |
sqls.add(sql); |
2434 |
} |
2435 |
} |
2436 |
return sqls;
|
2437 |
} |
2438 |
} |
2439 |
|
2440 |
protected GeometryExpressionBuilder expressionBuilder;
|
2441 |
|
2442 |
protected String defaultSchema; |
2443 |
protected boolean supportSchemas; |
2444 |
protected boolean hasSpatialFunctions; |
2445 |
protected GeometrySupportType geometrySupportType;
|
2446 |
protected boolean allowAutomaticValues; |
2447 |
|
2448 |
private static Map<Pair<Integer, Integer>, String> sqlgeometrytypes = null; |
2449 |
|
2450 |
protected String constant_true = "(1=1)"; |
2451 |
protected String constant_false = "(1<>1)"; |
2452 |
|
2453 |
protected String type_boolean = "BOOLEAN"; |
2454 |
protected String type_byte = "TINYINT"; |
2455 |
protected String type_bytearray = "BYTEA"; |
2456 |
protected String type_geometry = "TEXT"; |
2457 |
protected String type_char = "CHARACTER(1)"; |
2458 |
protected String type_date = "DATE"; |
2459 |
protected String type_double = "DOUBLE PRECISION"; //float con 53 bits de mantisa, float(54) |
2460 |
protected String type_numeric_p = "NUMERIC({0,Number,#######})"; |
2461 |
protected String type_numeric_ps = "NUMERIC({0,Number,#######},{1,Number,#######})"; |
2462 |
protected String type_bigdecimal = "NUMERIC({0,Number,#######},{1,Number,#######})"; |
2463 |
protected String type_float = "REAL"; //float con 24 bits de mantisa, float(24) |
2464 |
protected String type_int = "INT"; |
2465 |
protected String type_long = "BIGINT"; |
2466 |
protected String type_string = "TEXT"; |
2467 |
protected String type_string_p = "VARCHAR({0,Number,#######})"; |
2468 |
protected String type_time = "TIME"; |
2469 |
protected String type_timestamp = "TIMESTAMP"; |
2470 |
protected String type_version = "VARCHAR(30)"; |
2471 |
protected String type_URI = "TEXT"; |
2472 |
protected String type_URL = "TEXT"; |
2473 |
protected String type_FILE = "TEXT"; |
2474 |
protected String type_FOLDER = "TEXT"; |
2475 |
|
2476 |
protected String STMT_DELETE_FROM_table_WHERE_expresion = "DELETE FROM {0} WHERE {1}"; |
2477 |
protected String STMT_DELETE_FROM_table = "DELETE FROM {0}"; |
2478 |
protected String STMT_INSERT_INTO_table_columns_VALUES_values = "INSERT INTO {0} ( {1} ) VALUES ( {2} )"; |
2479 |
protected String STMT_UPDATE_TABLE_STATISTICS_table = "VACUUM ANALYZE {0}"; |
2480 |
protected String STMT_DROP_TABLE_table = "DROP TABLE {0}"; |
2481 |
protected String STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table = "DELETE FROM GEOMETRY_COLUMNS WHERE f_table_schema = {0} AND f_table_name = {1}"; |
2482 |
protected String STMT_DELETE_GEOMETRY_COLUMN_FROM_TABLE_table = "DELETE FROM GEOMETRY_COLUMNS WHERE f_table_name = {0}"; |
2483 |
protected String STMT_UPDATE_table_SET_columnsAndValues_WHERE_expresion = "UPDATE {0} SET {1} WHERE {2}"; |
2484 |
protected String STMT_UPDATE_table_SET_columnsAndValues = "UPDATE {0} SET {1}"; |
2485 |
protected String STMT_GRANT_privileges_ON_table_TO_role = "GRANT {0} ON {1} TO {2}"; |
2486 |
|
2487 |
public SQLBuilderBase() {
|
2488 |
this.expressionBuilder = GeometryExpressionEvaluatorLocator.getManager().createExpressionBuilder();
|
2489 |
|
2490 |
this.hasSpatialFunctions = false; |
2491 |
this.supportSchemas = true; |
2492 |
this.geometrySupportType = GeometrySupportType.WKT;
|
2493 |
|
2494 |
this.defaultSchema = "public"; |
2495 |
this.allowAutomaticValues = true; |
2496 |
|
2497 |
} |
2498 |
|
2499 |
@Override
|
2500 |
public void setProperties(Class filter, final Object... values) { |
2501 |
this.accept(new Visitor() { |
2502 |
@Override
|
2503 |
public void visit(Visitable v) { |
2504 |
for (int i = 0; i < values.length; i+=2) { |
2505 |
((Value)v).setProperty((String) values[i], values[i+1]); |
2506 |
} |
2507 |
} |
2508 |
}, new ClassVisitorFilter(filter) );
|
2509 |
} |
2510 |
|
2511 |
public String quote_for_identifiers() { |
2512 |
return "\""; |
2513 |
} |
2514 |
|
2515 |
public String quote_for_strings() { |
2516 |
return "'"; |
2517 |
} |
2518 |
|
2519 |
@Override
|
2520 |
public String as_identifier(String id) { |
2521 |
String quote = this.quote_for_identifiers(); |
2522 |
// No se porque no esta disponible wrapIfMissing
|
2523 |
// return StringUtils.wrapIfMissing(id,quote);
|
2524 |
if (id.startsWith(quote)) {
|
2525 |
return id;
|
2526 |
} |
2527 |
return quote + id + quote;
|
2528 |
|
2529 |
} |
2530 |
|
2531 |
@Override
|
2532 |
public String as_string(String s) { |
2533 |
String quote = this.quote_for_strings(); |
2534 |
// No se porque no esta disponible wrapIfMissing
|
2535 |
// return StringUtils.wrapIfMissing(id,quote);
|
2536 |
if (s.startsWith(quote)) {
|
2537 |
return s;
|
2538 |
} |
2539 |
return quote + s + quote;
|
2540 |
|
2541 |
} |
2542 |
|
2543 |
@Override
|
2544 |
public String as_string(byte[] data) { |
2545 |
return this.expressionBuilder.bytearray_0x(data); |
2546 |
// return this.expressionBuilder.bytearray_hex(data);
|
2547 |
// return this.expressionBuilder.bytearray_x(data);
|
2548 |
} |
2549 |
|
2550 |
@Override
|
2551 |
public String as_string(boolean value) { |
2552 |
return value? "TRUE" : "FALSE"; |
2553 |
} |
2554 |
|
2555 |
@Override
|
2556 |
public String as_string(Number value) { |
2557 |
return Objects.toString(value);
|
2558 |
} |
2559 |
|
2560 |
@Override
|
2561 |
public String as_string(Object value) { |
2562 |
if( value == null ) { |
2563 |
return "NULL"; |
2564 |
} |
2565 |
if( value instanceof CharSequence ) { |
2566 |
return as_string(value.toString());
|
2567 |
} |
2568 |
if( value instanceof Number ) { |
2569 |
return as_string((Number)value); |
2570 |
} |
2571 |
if( value instanceof Boolean ) { |
2572 |
return as_string((boolean)value); |
2573 |
} |
2574 |
if( value instanceof byte[] ) { |
2575 |
return as_string((byte[])value); |
2576 |
} |
2577 |
throw new IllegalArgumentException("Can't support type of value '"+value.getClass().getName()+"'."); |
2578 |
} |
2579 |
|
2580 |
@Override
|
2581 |
public GeometryExpressionBuilder expression() {
|
2582 |
return this.expressionBuilder; |
2583 |
} |
2584 |
|
2585 |
@Override
|
2586 |
public boolean has_spatial_functions() { |
2587 |
return this.hasSpatialFunctions; |
2588 |
} |
2589 |
|
2590 |
@Override
|
2591 |
public GeometrySupportType geometry_support_type() {
|
2592 |
return this.geometrySupportType; |
2593 |
} |
2594 |
|
2595 |
protected GeometryExpressionBuilder createExpressionBuilder() {
|
2596 |
return GeometryExpressionEvaluatorLocator.getManager().createExpressionBuilder();
|
2597 |
} |
2598 |
|
2599 |
@Override
|
2600 |
public Object srs_id(IProjection projection) { |
2601 |
String abrev = projection.getAbrev();
|
2602 |
return abrev.split(":")[1].trim(); |
2603 |
} |
2604 |
|
2605 |
@Override
|
2606 |
public String default_schema() { |
2607 |
return this.defaultSchema; |
2608 |
} |
2609 |
|
2610 |
@Override
|
2611 |
public boolean support_schemas() { |
2612 |
return this.supportSchemas; |
2613 |
} |
2614 |
|
2615 |
@Override
|
2616 |
public String sqltype(int type, int p, int s, int geomType, int geomSubtype) { |
2617 |
switch (type) {
|
2618 |
case DataTypes.BOOLEAN:
|
2619 |
return type_boolean;
|
2620 |
case DataTypes.BYTE:
|
2621 |
return type_byte;
|
2622 |
case DataTypes.BYTEARRAY:
|
2623 |
return type_bytearray;
|
2624 |
case DataTypes.GEOMETRY:
|
2625 |
return type_geometry;
|
2626 |
case DataTypes.CHAR:
|
2627 |
return type_char;
|
2628 |
case DataTypes.DATE:
|
2629 |
return type_date;
|
2630 |
case DataTypes.DOUBLE:
|
2631 |
// FIXME: Si cargamos la capa "country" al exportarla a
|
2632 |
// SQLServer falla por:
|
2633 |
// Error de desbordamiento aritm?tico al convertir float al tipo de datos numeric.
|
2634 |
// Al parecer la capa declara la columna sqkm_ctry como Numeric(12,6) y para
|
2635 |
// Algeria intenta asignarle un valor de 2320972.0 y falla.
|
2636 |
// Habria que repasar el proveedor de shape.
|
2637 |
|
2638 |
// if (p > 1) {
|
2639 |
// if (s < 0) {
|
2640 |
// return MessageFormat.format(config.getString(SQLConfig.type_numeric_p), p);
|
2641 |
// }
|
2642 |
// return MessageFormat.format(config.getString(SQLConfig.type_numeric_ps), p,s);
|
2643 |
// }
|
2644 |
return MessageFormat.format(type_double, p, s); |
2645 |
case DataTypes.BIGDECIMAL:
|
2646 |
if (p < 1) { |
2647 |
p = 20;
|
2648 |
} |
2649 |
if (s < 0) { |
2650 |
s = 10;
|
2651 |
} |
2652 |
return MessageFormat.format(type_bigdecimal, p, s); |
2653 |
case DataTypes.FLOAT:
|
2654 |
return MessageFormat.format(type_float, p, s); |
2655 |
case DataTypes.INT:
|
2656 |
return MessageFormat.format(type_int, p, s); |
2657 |
case DataTypes.LONG:
|
2658 |
return MessageFormat.format(type_long, p, s); |
2659 |
case DataTypes.STRING:
|
2660 |
if (p < 0) { |
2661 |
return type_string;
|
2662 |
} else if (p < 4096) { |
2663 |
return MessageFormat.format(type_string_p, p); |
2664 |
} |
2665 |
return type_string;
|
2666 |
case DataTypes.TIME:
|
2667 |
return type_time;
|
2668 |
case DataTypes.TIMESTAMP:
|
2669 |
return type_timestamp;
|
2670 |
case DataTypes.VERSION:
|
2671 |
return type_version;
|
2672 |
case DataTypes.URI:
|
2673 |
return type_URI;
|
2674 |
case DataTypes.URL:
|
2675 |
return type_URL;
|
2676 |
case DataTypes.FILE:
|
2677 |
return type_FILE;
|
2678 |
case DataTypes.FOLDER:
|
2679 |
return type_FOLDER;
|
2680 |
default:
|
2681 |
return null; |
2682 |
} |
2683 |
} |
2684 |
|
2685 |
@Override
|
2686 |
public Object sqlgeometrytype(int type, int subtype) { |
2687 |
// Devuelve un Object por que algunos gestores de BBDD utilizan
|
2688 |
// identificadores numericos para el tipo y otros strings.
|
2689 |
// Por defecto vamos a devolver strings.
|
2690 |
if (sqlgeometrytypes == null) { |
2691 |
sqlgeometrytypes = new HashMap<>(); |
2692 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D), "POINT"); |
2693 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM3D), "POINTZ"); |
2694 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2DM), "POINTM"); |
2695 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM3DM), "POINTZM"); |
2696 |
|
2697 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.LINE, Geometry.SUBTYPES.GEOM2D), "LINESTRING"); |
2698 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.LINE, Geometry.SUBTYPES.GEOM3D), "LINESTRINGZ"); |
2699 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.LINE, Geometry.SUBTYPES.GEOM2DM), "LINESTRINGM"); |
2700 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.LINE, Geometry.SUBTYPES.GEOM3DM), "LINESTRINGZM"); |
2701 |
|
2702 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.POLYGON, Geometry.SUBTYPES.GEOM2D), "POLYGON"); |
2703 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.POLYGON, Geometry.SUBTYPES.GEOM3D), "POLYGONZ"); |
2704 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.POLYGON, Geometry.SUBTYPES.GEOM2DM), "POLYGONM"); |
2705 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.POLYGON, Geometry.SUBTYPES.GEOM3DM), "POLYGONZM"); |
2706 |
|
2707 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTIPOINT, Geometry.SUBTYPES.GEOM2D), "MULTIPOINT"); |
2708 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTIPOINT, Geometry.SUBTYPES.GEOM3D), "MULTIPOINTZ"); |
2709 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTIPOINT, Geometry.SUBTYPES.GEOM2DM), "MULTIPOINTM"); |
2710 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTIPOINT, Geometry.SUBTYPES.GEOM3DM), "MULTIPOINTZM"); |
2711 |
|
2712 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTILINE, Geometry.SUBTYPES.GEOM2D), "MULTILINESTRING"); |
2713 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTILINE, Geometry.SUBTYPES.GEOM3D), "MULTILINESTRINGZ"); |
2714 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTILINE, Geometry.SUBTYPES.GEOM2DM), "MULTILINESTRINGM"); |
2715 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTILINE, Geometry.SUBTYPES.GEOM3DM), "MULTILINESTRINGZM"); |
2716 |
|
2717 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTICURVE, Geometry.SUBTYPES.GEOM2D), "MULTILINESTRING"); |
2718 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTICURVE, Geometry.SUBTYPES.GEOM3D), "MULTILINESTRINGZ"); |
2719 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTICURVE, Geometry.SUBTYPES.GEOM2DM), "MULTILINESTRINGM"); |
2720 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTICURVE, Geometry.SUBTYPES.GEOM3DM), "MULTILINESTRINGZM"); |
2721 |
|
2722 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTIPOLYGON, Geometry.SUBTYPES.GEOM2D), "MULTIPOLYGON"); |
2723 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTIPOLYGON, Geometry.SUBTYPES.GEOM3D), "MULTIPOLYGONZ"); |
2724 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTIPOLYGON, Geometry.SUBTYPES.GEOM2DM), "MULTIPOLYGONM"); |
2725 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTIPOLYGON, Geometry.SUBTYPES.GEOM3DM), "MULTIPOLYGONZM"); |
2726 |
|
2727 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTISURFACE, Geometry.SUBTYPES.GEOM2D), "MULTIPOLYGON"); |
2728 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTISURFACE, Geometry.SUBTYPES.GEOM3D), "MULTIPOLYGONZ"); |
2729 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTISURFACE, Geometry.SUBTYPES.GEOM2DM), "MULTIPOLYGONM"); |
2730 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.MULTISURFACE, Geometry.SUBTYPES.GEOM3DM), "MULTIPOLYGONZM"); |
2731 |
|
2732 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM2D), "GEOMETRY"); |
2733 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM3D), "GEOMETRYZ"); |
2734 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM2DM), "GEOMETRYM"); |
2735 |
sqlgeometrytypes.put(new ImmutablePair<>(Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM3DM), "GEOMETRYZM"); |
2736 |
} |
2737 |
return sqlgeometrytypes.get(new ImmutablePair<>(type, subtype)); |
2738 |
} |
2739 |
|
2740 |
@Override
|
2741 |
public Object sqlgeometrydimension(int type, int subtype) { |
2742 |
// Devuelve un Object por que algunos gestortes de BBDD utilizan
|
2743 |
// identificadores numericos para las dimensiones y otros strings.
|
2744 |
// Por defecto vamos a devolver enteros.
|
2745 |
switch (subtype) {
|
2746 |
case Geometry.SUBTYPES.GEOM3D:
|
2747 |
return 3; |
2748 |
case Geometry.SUBTYPES.GEOM2DM:
|
2749 |
return 3; |
2750 |
case Geometry.SUBTYPES.GEOM3DM:
|
2751 |
return 4; |
2752 |
case Geometry.SUBTYPES.GEOM2D:
|
2753 |
default:
|
2754 |
return 2; |
2755 |
} |
2756 |
} |
2757 |
|
2758 |
@Override
|
2759 |
public TableNameBuilder createTableNameBuilder() {
|
2760 |
return new TableNameBuilderBase(); |
2761 |
} |
2762 |
|
2763 |
protected SelectColumnBuilder createSelectColumnBuilder() {
|
2764 |
return new SelectColumnBuilderBase(); |
2765 |
} |
2766 |
|
2767 |
protected UpdateColumnBuilder createUpdateColumnBuilder() {
|
2768 |
return new UpdateColumnBuilderBase(); |
2769 |
} |
2770 |
|
2771 |
protected InsertColumnBuilder createInsertColumnBuilder() {
|
2772 |
return new InsertColumnBuilderBase(); |
2773 |
} |
2774 |
|
2775 |
protected OrderByBuilder createOrderByBuilder() {
|
2776 |
return new OrderByBuilderBase(); |
2777 |
} |
2778 |
|
2779 |
protected FromBuilder createFromBuilder() {
|
2780 |
return new FromBuilderBase(); |
2781 |
} |
2782 |
|
2783 |
protected SelectBuilder createSelectBuilder() {
|
2784 |
return new SelectBuilderBase(); |
2785 |
} |
2786 |
|
2787 |
protected UpdateBuilder createUpdateBuilder() {
|
2788 |
return new UpdateBuilderBase(); |
2789 |
} |
2790 |
|
2791 |
protected DeleteBuilder createDeleteBuilder() {
|
2792 |
return new DeleteBuilderBase(); |
2793 |
} |
2794 |
|
2795 |
protected GrantBuilder createGrantBuilder() {
|
2796 |
return new GrantBuilderBase(); |
2797 |
} |
2798 |
|
2799 |
protected GrantRoleBuilder createGrantRoleBuilder(TableNameBuilder table, String role) { |
2800 |
return new GrantRoleBuilderBase(table, role); |
2801 |
} |
2802 |
|
2803 |
protected DropTableBuilder createDropTableBuilder() {
|
2804 |
return new DropTableBuilderBase(); |
2805 |
} |
2806 |
|
2807 |
protected CreateTableBuilder createCreateTableBuilder() {
|
2808 |
return new CreateTableBuilderBase(); |
2809 |
} |
2810 |
|
2811 |
protected AlterTableBuilder createAlterTableBuilder() {
|
2812 |
return new AlterTableBuilderBase(); |
2813 |
} |
2814 |
|
2815 |
protected InsertBuilder createInsertBuilder() {
|
2816 |
return new InsertBuilderBase(); |
2817 |
} |
2818 |
|
2819 |
protected UpdateTableStatisticsBuilder createUpdateTableStatisticsBuilder() {
|
2820 |
return new UpdateTableStatisticsBuilderBase(); |
2821 |
} |
2822 |
|
2823 |
protected CreateIndexBuilder createCreateIndexBuilder() {
|
2824 |
return new CreateIndexBuilderBase(); |
2825 |
} |
2826 |
|
2827 |
@Override
|
2828 |
public SelectBuilder select() {
|
2829 |
if (this.select == null) { |
2830 |
this.select = this.createSelectBuilder(); |
2831 |
} |
2832 |
return this.select; |
2833 |
} |
2834 |
|
2835 |
@Override
|
2836 |
public UpdateBuilder update() {
|
2837 |
if (this.update == null) { |
2838 |
this.update = this.createUpdateBuilder(); |
2839 |
} |
2840 |
return this.update; |
2841 |
} |
2842 |
|
2843 |
@Override
|
2844 |
public UpdateTableStatisticsBuilder update_table_statistics() {
|
2845 |
if (this.update_table_statistics == null) { |
2846 |
this.update_table_statistics = this.createUpdateTableStatisticsBuilder(); |
2847 |
} |
2848 |
return this.update_table_statistics; |
2849 |
} |
2850 |
|
2851 |
@Override
|
2852 |
public DropTableBuilder drop_table() {
|
2853 |
if (this.drop_table == null) { |
2854 |
this.drop_table = this.createDropTableBuilder(); |
2855 |
} |
2856 |
return this.drop_table; |
2857 |
} |
2858 |
|
2859 |
@Override
|
2860 |
public CreateIndexBuilder create_index() {
|
2861 |
if (this.create_index == null) { |
2862 |
this.create_index = this.createCreateIndexBuilder(); |
2863 |
} |
2864 |
return this.create_index; |
2865 |
} |
2866 |
|
2867 |
@Override
|
2868 |
public DeleteBuilder delete() {
|
2869 |
if (this.delete == null) { |
2870 |
this.delete = this.createDeleteBuilder(); |
2871 |
} |
2872 |
return this.delete; |
2873 |
} |
2874 |
|
2875 |
@Override
|
2876 |
public InsertBuilder insert() {
|
2877 |
if (this.insert == null) { |
2878 |
this.insert = this.createInsertBuilder(); |
2879 |
} |
2880 |
return this.insert; |
2881 |
} |
2882 |
|
2883 |
@Override
|
2884 |
public TableNameBuilder table_name() {
|
2885 |
if (this.table_name == null) { |
2886 |
this.table_name = this.createTableNameBuilder(); |
2887 |
} |
2888 |
return this.table_name; |
2889 |
} |
2890 |
|
2891 |
|
2892 |
@Override
|
2893 |
public AlterTableBuilder alter_table() {
|
2894 |
if (this.alter_table == null) { |
2895 |
this.alter_table = this.createAlterTableBuilder(); |
2896 |
} |
2897 |
return this.alter_table; |
2898 |
} |
2899 |
|
2900 |
@Override
|
2901 |
public CreateTableBuilder create_table() {
|
2902 |
if (this.create_table == null) { |
2903 |
this.create_table = this.createCreateTableBuilder(); |
2904 |
} |
2905 |
return this.create_table; |
2906 |
} |
2907 |
|
2908 |
@Override
|
2909 |
public GrantBuilder grant() {
|
2910 |
if (this.grant == null) { |
2911 |
this.grant = this.createGrantBuilder(); |
2912 |
} |
2913 |
return this.grant; |
2914 |
} |
2915 |
|
2916 |
@Override
|
2917 |
public Column column(String name) { |
2918 |
ColumnBase col = new ColumnBase(null, name); |
2919 |
return col;
|
2920 |
} |
2921 |
|
2922 |
@Override
|
2923 |
public Column column(TableNameBuilder table, String name) { |
2924 |
ColumnBase col = new ColumnBase(table, name);
|
2925 |
return col;
|
2926 |
} |
2927 |
|
2928 |
protected JoinBase createJoin(String type, TableNameBuilder table, Value expression) { |
2929 |
return new JoinBase(type, table, expression); |
2930 |
} |
2931 |
|
2932 |
public void accept(Visitor visitor, VisitorFilter filter) { |
2933 |
if (this.select != null) { |
2934 |
this.select.accept(visitor, filter);
|
2935 |
} |
2936 |
if (this.update != null) { |
2937 |
this.update.accept(visitor, filter);
|
2938 |
} |
2939 |
if (this.insert != null) { |
2940 |
this.insert.accept(visitor, filter);
|
2941 |
} |
2942 |
if (this.delete != null) { |
2943 |
this.delete.accept(visitor, filter);
|
2944 |
} |
2945 |
if (this.alter_table != null) { |
2946 |
this.alter_table.accept(visitor, filter);
|
2947 |
} |
2948 |
if (this.create_table != null) { |
2949 |
this.create_table.accept(visitor, filter);
|
2950 |
} |
2951 |
if (this.drop_table != null) { |
2952 |
this.drop_table.accept(visitor, filter);
|
2953 |
} |
2954 |
if (this.table_name != null) { |
2955 |
this.table_name.accept(visitor, filter);
|
2956 |
} |
2957 |
} |
2958 |
|
2959 |
protected Formatter formatter() { |
2960 |
return ExpressionBuilder.EMPTY_FORMATTER;
|
2961 |
} |
2962 |
|
2963 |
@Override
|
2964 |
public String toString() { |
2965 |
return this.toString(formatter()); |
2966 |
} |
2967 |
|
2968 |
@Override
|
2969 |
public String toString(Formatter formatter) { |
2970 |
if (this.select != null) { |
2971 |
return this.select.toString(formatter); |
2972 |
} |
2973 |
if (this.update != null) { |
2974 |
return this.update.toString(formatter); |
2975 |
} |
2976 |
if (this.insert != null) { |
2977 |
return this.insert.toString(formatter); |
2978 |
} |
2979 |
if (this.delete != null) { |
2980 |
return this.delete.toString(formatter); |
2981 |
} |
2982 |
if (this.alter_table != null) { |
2983 |
return this.alter_table.toString(formatter); |
2984 |
} |
2985 |
if (this.create_table != null) { |
2986 |
return this.create_table.toString(formatter); |
2987 |
} |
2988 |
if (this.drop_table != null) { |
2989 |
return this.drop_table.toString(formatter); |
2990 |
} |
2991 |
if (this.update_table_statistics != null) { |
2992 |
return this.update_table_statistics.toString(formatter); |
2993 |
} |
2994 |
if (this.table_name != null) { |
2995 |
return this.table_name.toString(formatter); |
2996 |
} |
2997 |
return ""; |
2998 |
} |
2999 |
|
3000 |
@Override
|
3001 |
public CountBuilder count() {
|
3002 |
return new CountBuilderBase(); |
3003 |
} |
3004 |
|
3005 |
@Override
|
3006 |
public List<Parameter> parameters() { |
3007 |
final List<Parameter> params = new ArrayList<>(); |
3008 |
this.accept(new Visitor() { |
3009 |
@Override
|
3010 |
public void visit(Visitable value) { |
3011 |
params.add((Parameter) value); |
3012 |
} |
3013 |
}, new ClassVisitorFilter(Parameter.class));
|
3014 |
return params;
|
3015 |
} |
3016 |
|
3017 |
@Override
|
3018 |
public List<Variable> variables() { |
3019 |
final List<Variable> vars = new ArrayList<>(); |
3020 |
this.accept(new Visitor() { |
3021 |
@Override
|
3022 |
public void visit(Visitable value) { |
3023 |
if (!vars.contains((Variable) value)) {
|
3024 |
vars.add((Variable) value); |
3025 |
} |
3026 |
} |
3027 |
}, new ClassVisitorFilter(Variable.class));
|
3028 |
return vars;
|
3029 |
} |
3030 |
|
3031 |
@Override
|
3032 |
public List<String> parameters_names() { |
3033 |
List<String> params = new ArrayList<>(); |
3034 |
for (Parameter param : parameters()) {
|
3035 |
String s;
|
3036 |
switch (param.type()) {
|
3037 |
case PARAMETER_TYPE_CONSTANT:
|
3038 |
Object theValue = param.value();
|
3039 |
if (theValue == null) { |
3040 |
s = "null";
|
3041 |
} else if (theValue instanceof String) { |
3042 |
s = "'" + (String) theValue + "'"; |
3043 |
} else {
|
3044 |
s = theValue.toString(); |
3045 |
} |
3046 |
break;
|
3047 |
case PARAMETER_TYPE_VARIABLE:
|
3048 |
default:
|
3049 |
s = "\"" + param.name() + "\""; |
3050 |
} |
3051 |
params.add(s); |
3052 |
} |
3053 |
return params;
|
3054 |
} |
3055 |
|
3056 |
@Override
|
3057 |
public List<String> variables_names() { |
3058 |
List<String> vars = new ArrayList<>(); |
3059 |
for (Variable var : this.variables()) { |
3060 |
vars.add(var.name()); |
3061 |
} |
3062 |
Collections.sort(vars);
|
3063 |
return vars;
|
3064 |
} |
3065 |
} |