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