|
1 |
package org.gvsig.fmap.dal.feature.spi;
|
|
2 |
|
|
3 |
import java.text.MessageFormat;
|
|
4 |
import java.util.ArrayList;
|
|
5 |
import java.util.HashMap;
|
|
6 |
import java.util.HashSet;
|
|
7 |
import java.util.List;
|
|
8 |
import java.util.Map;
|
|
9 |
import java.util.Set;
|
|
10 |
import org.apache.commons.lang3.StringUtils;
|
|
11 |
import org.apache.commons.lang3.tuple.ImmutablePair;
|
|
12 |
import org.apache.commons.lang3.tuple.Pair;
|
|
13 |
import org.cresques.cts.IProjection;
|
|
14 |
import org.gvsig.fmap.dal.DataTypes;
|
|
15 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save;
|
|
16 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.AlterTableBuilder;
|
|
17 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.ColumnBuilder;
|
|
18 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.CreateTableBuilder;
|
|
19 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.DeleteBuilder;
|
|
20 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.DropTableBuilder;
|
|
21 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.ExpBuilder;
|
|
22 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.FromBuilder;
|
|
23 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.GeometrySupportType;
|
|
24 |
import static org.gvsig.fmap.dal.feature.SQLBuilder_save.GeometrySupportType.EWKB;
|
|
25 |
import static org.gvsig.fmap.dal.feature.SQLBuilder_save.GeometrySupportType.WKB;
|
|
26 |
import static org.gvsig.fmap.dal.feature.SQLBuilder_save.GeometrySupportType.WKT;
|
|
27 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.GrantBuilder;
|
|
28 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.InsertBuilder;
|
|
29 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.OpBuilder;
|
|
30 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.Parameter;
|
|
31 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.Privilege;
|
|
32 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.SelectBuilder;
|
|
33 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.UpdateBuilder;
|
|
34 |
import org.gvsig.fmap.dal.feature.SQLBuilder_save.UpdateTableStatisticsBuilder;
|
|
35 |
import org.gvsig.fmap.geom.Geometry;
|
|
36 |
import org.gvsig.fmap.geom.primitive.Envelope;
|
|
37 |
import org.gvsig.tools.dataTypes.DataType;
|
|
38 |
import org.slf4j.Logger;
|
|
39 |
import org.slf4j.LoggerFactory;
|
|
40 |
|
|
41 |
public class SQLBuilderBase_save implements SQLBuilder_save {
|
|
42 |
|
|
43 |
protected static final Logger logger = LoggerFactory.getLogger(SQLBuilderBase.class);
|
|
44 |
|
|
45 |
protected SelectBuilder select;
|
|
46 |
protected UpdateBuilder update;
|
|
47 |
protected InsertBuilder insert;
|
|
48 |
protected DeleteBuilder delete;
|
|
49 |
protected AlterTableBuilder alter_table;
|
|
50 |
protected CreateTableBuilder create_table;
|
|
51 |
protected GrantBuilder grant;
|
|
52 |
protected DropTableBuilder drop_table;
|
|
53 |
protected UpdateTableStatisticsBuilder update_table_statistics;
|
|
54 |
protected List<Parameter> parameters;
|
|
55 |
protected SQLBuilderConfig config;
|
|
56 |
|
|
57 |
public class SQLBuilderConfig {
|
|
58 |
|
|
59 |
public boolean allowAutomaticValues;
|
|
60 |
public boolean has_spatial_functions;
|
|
61 |
public String default_schema;
|
|
62 |
public String quote_for_identifiers;
|
|
63 |
public String quote_for_strings;
|
|
64 |
public GeometrySupportType geometry_type_support;
|
|
65 |
public String sql_true;
|
|
66 |
|
|
67 |
public String ST_AsText;
|
|
68 |
public String ST_AsBinary;
|
|
69 |
public String ST_AsEWKB;
|
|
70 |
public String ST_ExtentAggregate;
|
|
71 |
public String ST_UnionAggregate;
|
|
72 |
public String ST_Contains;
|
|
73 |
public String ST_Crosses;
|
|
74 |
public String ST_Disjoint;
|
|
75 |
public String ST_IsClosed;
|
|
76 |
public String ST_Overlaps;
|
|
77 |
public String ST_Touches;
|
|
78 |
public String ST_Within;
|
|
79 |
public String ST_Intersects;
|
|
80 |
public String ST_Envelope;
|
|
81 |
public String ST_GeomFromText;
|
|
82 |
public String ST_GeomFromWKB;
|
|
83 |
public String ST_GeomFromEWKB;
|
|
84 |
public String lcase;
|
|
85 |
public String ucase;
|
|
86 |
public String isNull;
|
|
87 |
public String count;
|
|
88 |
|
|
89 |
public String type_boolean;
|
|
90 |
public String type_byte;
|
|
91 |
public String type_bytearray;
|
|
92 |
public String type_geometry;
|
|
93 |
public String type_char;
|
|
94 |
public String type_date;
|
|
95 |
public String type_double;
|
|
96 |
public String type_numeric_p;
|
|
97 |
public String type_numeric_ps;
|
|
98 |
public String type_bigdecimal;
|
|
99 |
public String type_float;
|
|
100 |
public String type_int;
|
|
101 |
public String type_long;
|
|
102 |
public String type_string;
|
|
103 |
public String type_string_p;
|
|
104 |
public String type_time;
|
|
105 |
public String type_timestamp;
|
|
106 |
public String type_version;
|
|
107 |
public String type_URI;
|
|
108 |
public String type_URL;
|
|
109 |
public String type_FILE;
|
|
110 |
public String type_FOLDER;;
|
|
111 |
|
|
112 |
public String operator_AND;
|
|
113 |
public String operator_OR;
|
|
114 |
public String operator_EQ;
|
|
115 |
public String operator_NE;
|
|
116 |
public String operator_GT;
|
|
117 |
public String operator_GE;
|
|
118 |
public String operator_LT;
|
|
119 |
public String operator_LE;
|
|
120 |
public String operator_LIKE;
|
|
121 |
public String operator_ILIKE;
|
|
122 |
|
|
123 |
public String DELETE_FROM_table_WHERE_expresion;
|
|
124 |
public String DELETE_FROM_table;
|
|
125 |
public String INSERT_INTO_table_columns_VALUES_values;
|
|
126 |
public String UPDATE_TABLE_STATISTICS_table;
|
|
127 |
public String DROP_TABLE_table;
|
|
128 |
public String DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table;
|
|
129 |
public String DELETE_GEOMETRY_COLUMN_FROM_TABLE_table;
|
|
130 |
public String UPDATE_table_SET_columnsAndValues_WHERE_expresion;
|
|
131 |
public String UPDATE_table_SET_columnsAndValues;
|
|
132 |
|
|
133 |
public SQLBuilderConfig() {
|
|
134 |
has_spatial_functions = false;
|
|
135 |
allowAutomaticValues = true;
|
|
136 |
|
|
137 |
sql_true = "(1=1)";
|
|
138 |
default_schema = "public";
|
|
139 |
quote_for_identifiers = "\"";
|
|
140 |
quote_for_strings = "'";
|
|
141 |
geometry_type_support = GeometrySupportType.WKT;
|
|
142 |
|
|
143 |
ST_AsText = "ST_AsText({0})";
|
|
144 |
ST_AsBinary = "ST_AsBinary({0})";
|
|
145 |
ST_AsEWKB = "ST_AsWKB({0})";
|
|
146 |
ST_ExtentAggregate = "ST_Extent({0})";
|
|
147 |
ST_UnionAggregate = "ST_Union({0})";
|
|
148 |
ST_Contains = "ST_Contains(({0}), ({1}))";
|
|
149 |
ST_Crosses = "ST_Crosses(({0}), ({1}))";
|
|
150 |
ST_Disjoint = "ST_Disjoint (({0}), ({1}))";
|
|
151 |
ST_IsClosed = "ST_IsClosed({0})";
|
|
152 |
ST_Overlaps = "ST_Overlaps(({0}), ({1}))";
|
|
153 |
ST_Touches = "ST_Touches(({0}), ({1}))";
|
|
154 |
ST_Within = "ST_Within(({0}), ({1}))";
|
|
155 |
ST_Envelope = "ST_envelope({0})";
|
|
156 |
ST_Intersects = "ST_intersects({0}, {1})";
|
|
157 |
ST_GeomFromText = "ST_GeomFromText({0}, {1})";
|
|
158 |
ST_GeomFromWKB = "ST_GeomFromWKB({0}, {1})";
|
|
159 |
ST_GeomFromEWKB = "ST_GeomFromEWKB({0}, {1})";
|
|
160 |
lcase = "LCASE({0})";
|
|
161 |
ucase = "UCASE({0})";
|
|
162 |
count = "count({0})";
|
|
163 |
isNull = "{0} is null";
|
|
164 |
|
|
165 |
/*
|
|
166 |
* https://www.postgresql.org/docs/9.1/static/datatype.html
|
|
167 |
* http://www.w3schools.com/sql/sql_datatypes.asp
|
|
168 |
*/
|
|
169 |
type_boolean = "BOOLEAN";
|
|
170 |
type_byte = "TINYINT";
|
|
171 |
type_bytearray = "BYTEA";
|
|
172 |
type_geometry = "TEXT";
|
|
173 |
type_char = "CHARACTER(1)";
|
|
174 |
type_date = "DATE";
|
|
175 |
type_double = "DOUBLE PRECISION";
|
|
176 |
type_numeric_p = "NUMERIC({0})";
|
|
177 |
type_numeric_ps = "NUMERIC({0},{1})";
|
|
178 |
type_bigdecimal = "NUMERIC({0},{1})";
|
|
179 |
type_float = "REAL";
|
|
180 |
type_int = "INT";
|
|
181 |
type_long = "BIGINT";
|
|
182 |
type_string = "TEXT";
|
|
183 |
type_string_p = "VARCHAR({0})";
|
|
184 |
type_time = "TIME";
|
|
185 |
type_timestamp = "TIMESTAMP";
|
|
186 |
type_version = "VARCHAR(30)";
|
|
187 |
type_URI = "TEXT";
|
|
188 |
type_URL = "TEXT";
|
|
189 |
type_FILE = "TEXT";
|
|
190 |
type_FOLDER = "TEXT";
|
|
191 |
|
|
192 |
operator_AND = "{0} AND {1}";
|
|
193 |
operator_OR = "{0} OR {1}";
|
|
194 |
operator_EQ = "{0} = {1}";
|
|
195 |
operator_NE = "{0} <> {1}";
|
|
196 |
operator_GT = "{0} > {1}";
|
|
197 |
operator_GE = "{0} >= {1}";
|
|
198 |
operator_LT = "{0} < {1}";
|
|
199 |
operator_LE = "{0} <= {1}";
|
|
200 |
operator_LIKE = "{0} LIKE {1}";
|
|
201 |
operator_ILIKE = "{0} ILIKE {1}";
|
|
202 |
|
|
203 |
DELETE_FROM_table_WHERE_expresion = "DELETE FROM {0} WHERE {1}";
|
|
204 |
DELETE_FROM_table = "DELETE FROM {0}";
|
|
205 |
INSERT_INTO_table_columns_VALUES_values = "INSERT INTO {0} ( {1} ) VALUES ( {2} )";
|
|
206 |
UPDATE_TABLE_STATISTICS_table = "VACUUM ANALYZE {0}";
|
|
207 |
DROP_TABLE_table = "DROP TABLE {0}";
|
|
208 |
DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table = "DELETE FROM GEOMETRY_COLUMNS WHERE f_table_schema = {0} AND f_table_name = {1}";
|
|
209 |
DELETE_GEOMETRY_COLUMN_FROM_TABLE_table = "DELETE FROM GEOMETRY_COLUMNS WHERE f_table_name = {0}";
|
|
210 |
UPDATE_table_SET_columnsAndValues_WHERE_expresion = "UPDATE {0} SET {1} WHERE {2}";
|
|
211 |
UPDATE_table_SET_columnsAndValues = "UPDATE {0} SET {1}";
|
|
212 |
|
|
213 |
}
|
|
214 |
}
|
|
215 |
|
|
216 |
public class ParameterBase implements Parameter {
|
|
217 |
|
|
218 |
protected int type;
|
|
219 |
protected String name;
|
|
220 |
protected IProjection crs;
|
|
221 |
protected Object value;
|
|
222 |
|
|
223 |
public ParameterBase(int type, String name, IProjection crs) {
|
|
224 |
this.type = type;
|
|
225 |
this.name = name;
|
|
226 |
this.crs = crs;
|
|
227 |
this.value = null;
|
|
228 |
}
|
|
229 |
|
|
230 |
public ParameterBase(Object value) {
|
|
231 |
this.type = DataTypes.UNKNOWN;
|
|
232 |
this.name = null;
|
|
233 |
this.crs = null;
|
|
234 |
this.value = value;
|
|
235 |
}
|
|
236 |
|
|
237 |
@Override
|
|
238 |
public int getType() {
|
|
239 |
return this.type;
|
|
240 |
}
|
|
241 |
|
|
242 |
@Override
|
|
243 |
public String getName() {
|
|
244 |
return this.name;
|
|
245 |
}
|
|
246 |
|
|
247 |
@Override
|
|
248 |
public IProjection getCRS() {
|
|
249 |
return this.crs;
|
|
250 |
}
|
|
251 |
|
|
252 |
@Override
|
|
253 |
public boolean isConstant() {
|
|
254 |
return this.name == null;
|
|
255 |
}
|
|
256 |
|
|
257 |
@Override
|
|
258 |
public Object getValue() {
|
|
259 |
return this.value;
|
|
260 |
}
|
|
261 |
}
|
|
262 |
|
|
263 |
public class ParameterValue {
|
|
264 |
|
|
265 |
}
|
|
266 |
|
|
267 |
protected class ColumnInfo {
|
|
268 |
|
|
269 |
public String name;
|
|
270 |
public int type;
|
|
271 |
public int type_p;
|
|
272 |
public int type_s;
|
|
273 |
public boolean isPk;
|
|
274 |
public boolean allowNulls;
|
|
275 |
public boolean isAutomatic;
|
|
276 |
public Object defaultValue;
|
|
277 |
|
|
278 |
public ColumnInfo(String name, int type, Object defaultValue) {
|
|
279 |
this.name = name;
|
|
280 |
this.type = type;
|
|
281 |
this.type_p = -1;
|
|
282 |
this.type_s = -1;
|
|
283 |
this.isPk = false;
|
|
284 |
this.allowNulls = true;
|
|
285 |
this.isAutomatic = false;
|
|
286 |
this.defaultValue = defaultValue;
|
|
287 |
}
|
|
288 |
|
|
289 |
public ColumnInfo(String name, int type, int type_p, int type_s, boolean isPk, boolean allowNulls, boolean isAutomatic, Object defaultValue) {
|
|
290 |
this.name = name;
|
|
291 |
this.type = type;
|
|
292 |
this.type_p = type_p;
|
|
293 |
this.type_s = type_s;
|
|
294 |
this.isPk = isPk;
|
|
295 |
this.allowNulls = allowNulls;
|
|
296 |
this.isAutomatic = isAutomatic;
|
|
297 |
this.defaultValue = defaultValue;
|
|
298 |
}
|
|
299 |
}
|
|
300 |
|
|
301 |
public class OpBuilderBase implements OpBuilder {
|
|
302 |
|
|
303 |
private String operador;
|
|
304 |
private ExpBuilder op1;
|
|
305 |
private ExpBuilder op2;
|
|
306 |
|
|
307 |
public OpBuilderBase(String operador) {
|
|
308 |
this.operador = operador;
|
|
309 |
}
|
|
310 |
|
|
311 |
public OpBuilderBase(String operador, String op1, String op2) {
|
|
312 |
this.operador = operador;
|
|
313 |
this.setop1(op1);
|
|
314 |
this.setop2(op2);
|
|
315 |
}
|
|
316 |
|
|
317 |
@Override
|
|
318 |
public OpBuilder setop1(String op) {
|
|
319 |
this.op1 = createExpBuilder();
|
|
320 |
this.op1.set(op);
|
|
321 |
return this;
|
|
322 |
}
|
|
323 |
|
|
324 |
@Override
|
|
325 |
public OpBuilder setop2(String op) {
|
|
326 |
this.op2 = createExpBuilder();
|
|
327 |
this.op2.set(op);
|
|
328 |
return this;
|
|
329 |
}
|
|
330 |
|
|
331 |
protected OpBuilder add(String operator, ExpBuilder value) {
|
|
332 |
if (this.op2 == null) {
|
|
333 |
this.op2 = value;
|
|
334 |
this.operador = operator;
|
|
335 |
return this;
|
|
336 |
}
|
|
337 |
OpBuilderBase op = (OpBuilderBase) createOpBuilder(operator);
|
|
338 |
op.op1 = this.op2;
|
|
339 |
op.op2 = value;
|
|
340 |
this.op2 = createExpresionBuilder(op);
|
|
341 |
return this;
|
|
342 |
}
|
|
343 |
|
|
344 |
@Override
|
|
345 |
public String toString() {
|
|
346 |
if (this.op2 == null || this.op2.isEmpty() ) {
|
|
347 |
return this.op1.toString();
|
|
348 |
}
|
|
349 |
return MessageFormat.format(this.operador, this.op1.toString(), this.op2.toString());
|
|
350 |
}
|
|
351 |
}
|
|
352 |
|
|
353 |
public class ExpBuilderBase implements ExpBuilder {
|
|
354 |
|
|
355 |
private String value;
|
|
356 |
private OpBuilderBase op;
|
|
357 |
|
|
358 |
public ExpBuilderBase() {
|
|
359 |
}
|
|
360 |
|
|
361 |
public ExpBuilderBase(String value) {
|
|
362 |
this.value = value;
|
|
363 |
}
|
|
364 |
|
|
365 |
public ExpBuilderBase(OpBuilder op) {
|
|
366 |
this.op = (OpBuilderBase) op;
|
|
367 |
}
|
|
368 |
|
|
369 |
@Override
|
|
370 |
public boolean isEmpty() {
|
|
371 |
return ( this.value == null && this.op == null );
|
|
372 |
}
|
|
373 |
|
|
374 |
@Override
|
|
375 |
public ExpBuilder set(String value) {
|
|
376 |
this.value = value;
|
|
377 |
return this;
|
|
378 |
}
|
|
379 |
|
|
380 |
@Override
|
|
381 |
public ExpBuilder set(OpBuilder op) {
|
|
382 |
this.op = (OpBuilderBase) op;
|
|
383 |
return this;
|
|
384 |
}
|
|
385 |
|
|
386 |
@Override
|
|
387 |
public String toString() {
|
|
388 |
if (this.value != null) {
|
|
389 |
return this.value;
|
|
390 |
}
|
|
391 |
return this.op.toString();
|
|
392 |
}
|
|
393 |
|
|
394 |
protected ExpBuilder addOperator(String operator, String value) {
|
|
395 |
if (this.op == null) {
|
|
396 |
if (this.value == null) {
|
|
397 |
this.value = value;
|
|
398 |
} else {
|
|
399 |
OpBuilderBase op = (OpBuilderBase) createOpBuilder(operator);
|
|
400 |
op.setop1(this.value);
|
|
401 |
op.setop2(value);
|
|
402 |
this.value = null;
|
|
403 |
this.op = op;
|
|
404 |
}
|
|
405 |
} else {
|
|
406 |
this.op.add(operator, createExpresionBuilder(value));
|
|
407 |
}
|
|
408 |
return this;
|
|
409 |
}
|
|
410 |
|
|
411 |
@Override
|
|
412 |
public ExpBuilder and(String value) {
|
|
413 |
this.addOperator(config.operator_AND, value);
|
|
414 |
return this;
|
|
415 |
}
|
|
416 |
|
|
417 |
@Override
|
|
418 |
public ExpBuilder and(OpBuilder op) {
|
|
419 |
this.addOperator(config.operator_AND, op.toString());
|
|
420 |
return this;
|
|
421 |
}
|
|
422 |
|
|
423 |
@Override
|
|
424 |
public ExpBuilder or(String value) {
|
|
425 |
this.addOperator(config.operator_OR, value);
|
|
426 |
return this;
|
|
427 |
}
|
|
428 |
|
|
429 |
@Override
|
|
430 |
public ExpBuilder or(OpBuilder op) {
|
|
431 |
this.addOperator(config.operator_OR, op.toString());
|
|
432 |
return this;
|
|
433 |
}
|
|
434 |
}
|
|
435 |
|
|
436 |
public class TableId {
|
|
437 |
|
|
438 |
public String tableName;
|
|
439 |
public String schemaName;
|
|
440 |
private String dbName;
|
|
441 |
|
|
442 |
public TableId(String dbName, String schemaName, String tableName) {
|
|
443 |
this.dbName = dbName;
|
|
444 |
this.tableName = tableName;
|
|
445 |
this.schemaName = schemaName;
|
|
446 |
}
|
|
447 |
|
|
448 |
public TableId(String schemaName, String tableName) {
|
|
449 |
this(null, schemaName, tableName);
|
|
450 |
}
|
|
451 |
|
|
452 |
public TableId(String tableName) {
|
|
453 |
this(null, null, tableName);
|
|
454 |
}
|
|
455 |
|
|
456 |
public String getTableName() {
|
|
457 |
return this.tableName;
|
|
458 |
}
|
|
459 |
|
|
460 |
public String getSchemaName() {
|
|
461 |
return this.schemaName;
|
|
462 |
}
|
|
463 |
|
|
464 |
public boolean hasSchemaName() {
|
|
465 |
return !StringUtils.isEmpty(this.schemaName);
|
|
466 |
}
|
|
467 |
|
|
468 |
public boolean hasDatabaseName() {
|
|
469 |
return !StringUtils.isEmpty(this.dbName);
|
|
470 |
}
|
|
471 |
|
|
472 |
@Override
|
|
473 |
public String toString() {
|
|
474 |
if( this.hasDatabaseName() ) {
|
|
475 |
if( this.hasSchemaName() ) {
|
|
476 |
return identifier(this.dbName) + "." +
|
|
477 |
identifier(this.schemaName) + "." +
|
|
478 |
identifier(this.tableName);
|
|
479 |
}
|
|
480 |
} else {
|
|
481 |
if( this.hasSchemaName() ) {
|
|
482 |
return identifier(this.schemaName) + "." +
|
|
483 |
identifier(this.tableName);
|
|
484 |
}
|
|
485 |
}
|
|
486 |
return identifier(this.tableName);
|
|
487 |
}
|
|
488 |
|
|
489 |
}
|
|
490 |
|
|
491 |
public class FromBuilderBase implements FromBuilder {
|
|
492 |
|
|
493 |
protected TableId tableName= null;
|
|
494 |
private String subquery = null;
|
|
495 |
private String passthrough = null;
|
|
496 |
|
|
497 |
@Override
|
|
498 |
public FromBuilder table(String dbName, String schemaName, String tableName) {
|
|
499 |
this.tableName = new TableId(dbName, schemaName, tableName);
|
|
500 |
return this;
|
|
501 |
}
|
|
502 |
|
|
503 |
@Override
|
|
504 |
public FromBuilder table(String tableName) {
|
|
505 |
this.tableName = new TableId(tableName);
|
|
506 |
return this;
|
|
507 |
}
|
|
508 |
|
|
509 |
@Override
|
|
510 |
public FromBuilder passThrough(String passthrough) {
|
|
511 |
this.passthrough = passthrough;
|
|
512 |
return this;
|
|
513 |
}
|
|
514 |
|
|
515 |
@Override
|
|
516 |
public FromBuilder subquery(String subquery) {
|
|
517 |
this.subquery = subquery;
|
|
518 |
return this;
|
|
519 |
}
|
|
520 |
|
|
521 |
@Override
|
|
522 |
public String toString() {
|
|
523 |
if( ! StringUtils.isEmpty(passthrough) ) {
|
|
524 |
return passthrough;
|
|
525 |
}
|
|
526 |
if( ! StringUtils.isEmpty(subquery) ) {
|
|
527 |
return "( " + this.subquery + ") as _subquery_alias_ ";
|
|
528 |
}
|
|
529 |
return this.tableName.toString();
|
|
530 |
}
|
|
531 |
|
|
532 |
}
|
|
533 |
|
|
534 |
public class ColumnBuilderBase implements ColumnBuilder {
|
|
535 |
|
|
536 |
private String name = null;
|
|
537 |
private String expression = null;
|
|
538 |
private String alias = null;
|
|
539 |
|
|
540 |
@Override
|
|
541 |
public ColumnBuilder name(String name) {
|
|
542 |
String quote = getQuoteForIdentifiers();
|
|
543 |
if (name.startsWith(quote)) {
|
|
544 |
// Remove quotes
|
|
545 |
name = name.substring(1, name.length() - 1);
|
|
546 |
}
|
|
547 |
this.name = name;
|
|
548 |
this.expression = null;
|
|
549 |
return this;
|
|
550 |
}
|
|
551 |
|
|
552 |
@Override
|
|
553 |
public ColumnBuilder expression(String value) {
|
|
554 |
this.expression = value;
|
|
555 |
this.name = null;
|
|
556 |
return this;
|
|
557 |
}
|
|
558 |
|
|
559 |
@Override
|
|
560 |
public ColumnBuilder geometry(String name) {
|
|
561 |
if( config.has_spatial_functions ) {
|
|
562 |
switch( geometry_support_type() ) {
|
|
563 |
case WKB:
|
|
564 |
this.expression = ST_AsBinary(name);
|
|
565 |
break;
|
|
566 |
case EWKB:
|
|
567 |
this.expression = ST_AsEWKB(name);
|
|
568 |
break;
|
|
569 |
case WKT:
|
|
570 |
default:
|
|
571 |
this.expression = ST_AsText(name);
|
|
572 |
break;
|
|
573 |
}
|
|
574 |
this.name = null;
|
|
575 |
} else {
|
|
576 |
this.name = name;
|
|
577 |
this.expression = null;
|
|
578 |
}
|
|
579 |
return this;
|
|
580 |
}
|
|
581 |
|
|
582 |
|
|
583 |
@Override
|
|
584 |
public ColumnBuilder as(String alias) {
|
|
585 |
this.alias = alias;
|
|
586 |
return this;
|
|
587 |
}
|
|
588 |
|
|
589 |
@Override
|
|
590 |
public String getName() {
|
|
591 |
return this.name;
|
|
592 |
}
|
|
593 |
|
|
594 |
@Override
|
|
595 |
public String getAlias() {
|
|
596 |
return this.alias;
|
|
597 |
}
|
|
598 |
|
|
599 |
@Override
|
|
600 |
public String getExpression() {
|
|
601 |
return this.alias;
|
|
602 |
}
|
|
603 |
|
|
604 |
@Override
|
|
605 |
public String toString() {
|
|
606 |
StringBuilder builder = new StringBuilder();
|
|
607 |
if( this.name != null ) {
|
|
608 |
builder.append(identifier(this.name));
|
|
609 |
} else {
|
|
610 |
builder.append(this.expression);
|
|
611 |
}
|
|
612 |
if( this.alias != null ) {
|
|
613 |
builder.append(" AS ");
|
|
614 |
builder.append(identifier(this.alias));
|
|
615 |
}
|
|
616 |
return builder.toString();
|
|
617 |
}
|
|
618 |
}
|
|
619 |
|
|
620 |
public class SelectBuilderBase implements SelectBuilder {
|
|
621 |
|
|
622 |
protected FromBuilder from;
|
|
623 |
protected ExpBuilder where;
|
|
624 |
protected long limit = -1;
|
|
625 |
protected long offset = -1;
|
|
626 |
protected List<ColumnBuilder> columns;
|
|
627 |
protected List<Pair<String,Boolean>> order_by;
|
|
628 |
|
|
629 |
public SelectBuilderBase() {
|
|
630 |
this.columns = new ArrayList<>();
|
|
631 |
}
|
|
632 |
|
|
633 |
@Override
|
|
634 |
public ColumnBuilder column() {
|
|
635 |
ColumnBuilder builder = createColumnBuilder();
|
|
636 |
this.columns.add(builder);
|
|
637 |
return builder;
|
|
638 |
}
|
|
639 |
|
|
640 |
@Override
|
|
641 |
public boolean has_column(String name) {
|
|
642 |
for (ColumnBuilder column : columns) {
|
|
643 |
if( name.equals(column.getName()) ) {
|
|
644 |
return true;
|
|
645 |
}
|
|
646 |
}
|
|
647 |
return false;
|
|
648 |
}
|
|
649 |
|
|
650 |
@Override
|
|
651 |
public FromBuilder from() {
|
|
652 |
if (this.from == null) {
|
|
653 |
this.from = createFromBuilder();
|
|
654 |
}
|
|
655 |
return this.from;
|
|
656 |
}
|
|
657 |
|
|
658 |
@Override
|
|
659 |
public FromBuilder from(String s) {
|
|
660 |
return this.from().table(s);
|
|
661 |
}
|
|
662 |
|
|
663 |
@Override
|
|
664 |
public ExpBuilder where() {
|
|
665 |
if (this.where == null) {
|
|
666 |
this.where = createExpBuilder();
|
|
667 |
}
|
|
668 |
return this.where;
|
|
669 |
}
|
|
670 |
|
|
671 |
@Override
|
|
672 |
public ExpBuilder where(String s) {
|
|
673 |
this.where = createExpBuilder();
|
|
674 |
this.where.set(s);
|
|
675 |
return this.where;
|
|
676 |
}
|
|
677 |
|
|
678 |
@Override
|
|
679 |
public ExpBuilder where(OpBuilder s) {
|
|
680 |
this.where = createExpBuilder();
|
|
681 |
this.where.set(s);
|
|
682 |
return this.where;
|
|
683 |
}
|
|
684 |
|
|
685 |
@Override
|
|
686 |
public SelectBuilder limit(long limit) {
|
|
687 |
this.limit = limit;
|
|
688 |
return this;
|
|
689 |
}
|
|
690 |
|
|
691 |
@Override
|
|
692 |
public SelectBuilder offset(long offset) {
|
|
693 |
this.offset = offset;
|
|
694 |
return this;
|
|
695 |
}
|
|
696 |
|
|
697 |
@Override
|
|
698 |
public SelectBuilder add_order_by(String order) {
|
|
699 |
this.order_by.add(new ImmutablePair(order,null));
|
|
700 |
return this;
|
|
701 |
}
|
|
702 |
|
|
703 |
@Override
|
|
704 |
public SelectBuilder add_order_by(String column, boolean ascending) {
|
|
705 |
this.order_by.add(new ImmutablePair(column,ascending));
|
|
706 |
return this;
|
|
707 |
}
|
|
708 |
|
|
709 |
@Override
|
|
710 |
public boolean has_order_by() {
|
|
711 |
if( this.order_by == null ) {
|
|
712 |
return false;
|
|
713 |
}
|
|
714 |
return !this.order_by.isEmpty();
|
|
715 |
}
|
|
716 |
|
|
717 |
@Override
|
|
718 |
public String toString() {
|
|
719 |
StringBuilder builder = new StringBuilder();
|
|
720 |
|
|
721 |
builder.append("SELECT ");
|
|
722 |
boolean first = true;
|
|
723 |
for (ColumnBuilder column : columns) {
|
|
724 |
if (first) {
|
|
725 |
first = false;
|
|
726 |
} else {
|
|
727 |
builder.append(", ");
|
|
728 |
}
|
|
729 |
builder.append(column.toString());
|
|
730 |
}
|
|
731 |
|
|
732 |
if (this.from != null) {
|
|
733 |
builder.append(" FROM ");
|
|
734 |
builder.append(this.from.toString());
|
|
735 |
}
|
|
736 |
if (this.where != null) {
|
|
737 |
builder.append(" WHERE ");
|
|
738 |
builder.append(this.where.toString());
|
|
739 |
}
|
|
740 |
|
|
741 |
if( this.has_order_by() ) {
|
|
742 |
builder.append(" ORDER BY ");
|
|
743 |
first = true;
|
|
744 |
for (Pair<String,Boolean> item : this.order_by) {
|
|
745 |
if (first) {
|
|
746 |
first = false;
|
|
747 |
} else {
|
|
748 |
builder.append(", ");
|
|
749 |
}
|
|
750 |
builder.append(item.getLeft());
|
|
751 |
if( item.getRight()!=null ) {
|
|
752 |
// Si es null no a?adimos nada ya que puede habernos
|
|
753 |
// llegado ya un valor con el ASC o DESC incluido.
|
|
754 |
if( item.getRight() ) {
|
|
755 |
builder.append(" ASC");
|
|
756 |
} else {
|
|
757 |
builder.append(" DESC");
|
|
758 |
}
|
|
759 |
}
|
|
760 |
}
|
|
761 |
}
|
|
762 |
|
|
763 |
if (this.limit > 0) {
|
|
764 |
builder.append(" LIMIT ");
|
|
765 |
builder.append(this.limit);
|
|
766 |
}
|
|
767 |
if (this.offset > 0) {
|
|
768 |
builder.append(" OFFSET ");
|
|
769 |
builder.append(this.offset);
|
|
770 |
}
|
|
771 |
return builder.toString();
|
|
772 |
|
|
773 |
}
|
|
774 |
}
|
|
775 |
|
|
776 |
public class DropTableBuilderBase implements DropTableBuilder {
|
|
777 |
|
|
778 |
protected TableId table;
|
|
779 |
|
|
780 |
@Override
|
|
781 |
public DropTableBuilderBase table(String dbName, String schemaName, String tableName) {
|
|
782 |
this.table = new TableId(dbName, schemaName, tableName);
|
|
783 |
return this;
|
|
784 |
}
|
|
785 |
|
|
786 |
@Override
|
|
787 |
public DropTableBuilderBase table(String tableName) {
|
|
788 |
this.table = new TableId(tableName);
|
|
789 |
return this;
|
|
790 |
}
|
|
791 |
|
|
792 |
@Override
|
|
793 |
public String toString() {
|
|
794 |
StringBuilder builder = new StringBuilder();
|
|
795 |
boolean first = true;
|
|
796 |
for (String sql : toStrings()) {
|
|
797 |
if( StringUtils.isEmpty(sql) ) {
|
|
798 |
continue;
|
|
799 |
}
|
|
800 |
if (first) {
|
|
801 |
first = false;
|
|
802 |
} else {
|
|
803 |
builder.append("; ");
|
|
804 |
}
|
|
805 |
builder.append(sql);
|
|
806 |
}
|
|
807 |
return builder.toString();
|
|
808 |
}
|
|
809 |
|
|
810 |
@Override
|
|
811 |
public List<String> toStrings() {
|
|
812 |
List<String> sqls = new ArrayList<>();
|
|
813 |
|
|
814 |
sqls.add(
|
|
815 |
MessageFormat.format(
|
|
816 |
config.DROP_TABLE_table,
|
|
817 |
this.table.toString()
|
|
818 |
)
|
|
819 |
);
|
|
820 |
String sql;
|
|
821 |
if (this.table.hasSchemaName()) {
|
|
822 |
sql = MessageFormat.format(
|
|
823 |
config.DELETE_GEOMETRY_COLUMN_FROM_TABLE_schema_table,
|
|
824 |
identifier(this.table.getSchemaName()),
|
|
825 |
identifier(this.table.getTableName())
|
|
826 |
);
|
|
827 |
} else {
|
|
828 |
sql = MessageFormat.format(
|
|
829 |
config.DELETE_GEOMETRY_COLUMN_FROM_TABLE_table,
|
|
830 |
identifier(this.table.getTableName())
|
|
831 |
);
|
|
832 |
}
|
|
833 |
if( !StringUtils.isEmpty(sql) ) {
|
|
834 |
sqls.add(sql);
|
|
835 |
}
|
|
836 |
return sqls;
|
|
837 |
}
|
|
838 |
|
|
839 |
}
|
|
840 |
|
|
841 |
public class GrantBuilderBase implements GrantBuilder {
|
|
842 |
|
|
843 |
protected TableId table;
|
|
844 |
protected Map<String, Set<Privilege>> privilegesByRole;
|
|
845 |
|
|
846 |
@Override
|
|
847 |
public GrantBuilderBase table(String dbName, String schemaName, String tableName) {
|
|
848 |
this.table = new TableId(dbName, schemaName, tableName);
|
|
849 |
return this;
|
|
850 |
}
|
|
851 |
|
|
852 |
@Override
|
|
853 |
public GrantBuilderBase table(String tableName) {
|
|
854 |
this.table = new TableId(tableName);
|
|
855 |
return this;
|
|
856 |
}
|
|
857 |
|
|
858 |
@Override
|
|
859 |
public GrantBuilder add(String roleid, Privilege privilege) {
|
|
860 |
if (this.privilegesByRole.containsKey(roleid)) {
|
|
861 |
this.privilegesByRole.get(roleid).add(privilege);
|
|
862 |
} else {
|
|
863 |
this.privilegesByRole.put(roleid, new HashSet<Privilege>());
|
|
864 |
this.privilegesByRole.get(roleid).add(privilege);
|
|
865 |
}
|
|
866 |
return this;
|
|
867 |
}
|
|
868 |
|
|
869 |
@Override
|
|
870 |
public GrantBuilder select(String roleid) {
|
|
871 |
this.add(roleid, Privilege.SELECT);
|
|
872 |
return this;
|
|
873 |
}
|
|
874 |
|
|
875 |
@Override
|
|
876 |
public GrantBuilder insert(String roleid) {
|
|
877 |
this.add(roleid, Privilege.INSERT);
|
|
878 |
return this;
|
|
879 |
}
|
|
880 |
|
|
881 |
@Override
|
|
882 |
public GrantBuilder delete(String roleid) {
|
|
883 |
this.add(roleid, Privilege.DELETE);
|
|
884 |
return this;
|
|
885 |
}
|
|
886 |
|
|
887 |
@Override
|
|
888 |
public GrantBuilder truncate(String roleid) {
|
|
889 |
this.add(roleid, Privilege.TRUNCATE);
|
|
890 |
return this;
|
|
891 |
}
|
|
892 |
|
|
893 |
@Override
|
|
894 |
public GrantBuilder reference(String roleid) {
|
|
895 |
this.add(roleid, Privilege.REFERENCE);
|
|
896 |
return this;
|
|
897 |
}
|
|
898 |
|
|
899 |
@Override
|
|
900 |
public GrantBuilder trigger(String roleid) {
|
|
901 |
this.add(roleid, Privilege.TRIGGER);
|
|
902 |
return this;
|
|
903 |
}
|
|
904 |
|
|
905 |
@Override
|
|
906 |
public GrantBuilder all(String roleid) {
|
|
907 |
this.add(roleid, Privilege.ALL);
|
|
908 |
return this;
|
|
909 |
}
|
|
910 |
|
|
911 |
@Override
|
|
912 |
public GrantBuilder select() {
|
|
913 |
this.add("PUBLIC", Privilege.SELECT);
|
|
914 |
return this;
|
|
915 |
}
|
|
916 |
|
|
917 |
@Override
|
|
918 |
public GrantBuilder insert() {
|
|
919 |
this.add("PUBLIC", Privilege.INSERT);
|
|
920 |
return this;
|
|
921 |
}
|
|
922 |
|
|
923 |
@Override
|
|
924 |
public GrantBuilder delete() {
|
|
925 |
this.add("PUBLIC", Privilege.DELETE);
|
|
926 |
return this;
|
|
927 |
}
|
|
928 |
|
|
929 |
@Override
|
|
930 |
public GrantBuilder truncate() {
|
|
931 |
this.add("PUBLIC", Privilege.TRUNCATE);
|
|
932 |
return this;
|
|
933 |
}
|
|
934 |
|
|
935 |
@Override
|
|
936 |
public GrantBuilder reference() {
|
|
937 |
this.add("PUBLIC", Privilege.REFERENCE);
|
|
938 |
return this;
|
|
939 |
}
|
|
940 |
|
|
941 |
@Override
|
|
942 |
public GrantBuilder trigger() {
|
|
943 |
this.add("PUBLIC", Privilege.TRIGGER);
|
|
944 |
return this;
|
|
945 |
}
|
|
946 |
|
|
947 |
@Override
|
|
948 |
public GrantBuilder all() {
|
|
949 |
this.add("PUBLIC", Privilege.ALL);
|
|
950 |
return this;
|
|
951 |
}
|
|
952 |
|
|
953 |
@Override
|
|
954 |
public String toString() {
|
|
955 |
StringBuilder builder = new StringBuilder();
|
|
956 |
boolean first = true;
|
|
957 |
for (String sql : toStrings()) {
|
|
958 |
if( StringUtils.isEmpty(sql) ) {
|
|
959 |
continue;
|
|
960 |
}
|
|
961 |
if (first) {
|
|
962 |
first = false;
|
|
963 |
} else {
|
|
964 |
builder.append("; ");
|
|
965 |
}
|
|
966 |
builder.append(sql);
|
|
967 |
}
|
|
968 |
return builder.toString();
|
|
969 |
}
|
|
970 |
|
|
971 |
@Override
|
|
972 |
public List<String> toStrings() {
|
|
973 |
List<String> sqls = new ArrayList<>();
|
|
974 |
if( this.privilegesByRole == null ) {
|
|
975 |
return sqls;
|
|
976 |
}
|
|
977 |
Map<Privilege, String> privileges2SQL = new HashMap<>();
|
|
978 |
privileges2SQL.put(Privilege.ALL, "ALL");
|
|
979 |
privileges2SQL.put(Privilege.SELECT, "SELECT");
|
|
980 |
privileges2SQL.put(Privilege.INSERT, "INSERT");
|
|
981 |
privileges2SQL.put(Privilege.DELETE, "DELETE");
|
|
982 |
privileges2SQL.put(Privilege.REFERENCE, "REFERENCE");
|
|
983 |
privileges2SQL.put(Privilege.TRIGGER, "TRIGGER");
|
|
984 |
privileges2SQL.put(Privilege.TRUNCATE, "TRUNCATE");
|
|
985 |
privileges2SQL.put(Privilege.UPDATE, "UPDATE");
|
|
986 |
|
|
987 |
for (String role : this.privilegesByRole.keySet()) {
|
|
988 |
StringBuilder builder = new StringBuilder();
|
|
989 |
builder.append("GRANT ");
|
|
990 |
boolean first = true;
|
|
991 |
for (Privilege privilege : this.privilegesByRole.get(role)) {
|
|
992 |
if (first) {
|
|
993 |
first = false;
|
|
994 |
} else {
|
|
995 |
builder.append(", ");
|
|
996 |
}
|
|
997 |
builder.append(privileges2SQL.get(privilege));
|
|
998 |
}
|
|
999 |
builder.append(" ON ");
|
|
1000 |
builder.append(this.table);
|
|
1001 |
builder.append(" TO ");
|
|
1002 |
builder.append(role);
|
|
1003 |
sqls.add(builder.toString());
|
|
1004 |
}
|
|
1005 |
return sqls;
|
|
1006 |
}
|
|
1007 |
}
|
|
1008 |
|
|
1009 |
public class UpdateBuilderBase implements UpdateBuilder {
|
|
1010 |
|
|
1011 |
protected ExpBuilder where;
|
|
1012 |
protected List<Pair<String,String>> columns;
|
|
1013 |
protected TableId table;
|
|
1014 |
|
|
1015 |
public UpdateBuilderBase() {
|
|
1016 |
this.columns = new ArrayList<>();
|
|
1017 |
}
|
|
1018 |
|
|
1019 |
@Override
|
|
1020 |
public ExpBuilder where() {
|
|
1021 |
if (this.where == null) {
|
|
1022 |
this.where = createExpBuilder();
|
|
1023 |
}
|
|
1024 |
return this.where;
|
|
1025 |
}
|
|
1026 |
|
|
1027 |
@Override
|
|
1028 |
public UpdateBuilder table(String dbName, String schemaName, String tableName) {
|
|
1029 |
this.table = new TableId(dbName, schemaName, tableName);
|
|
1030 |
return this;
|
|
1031 |
}
|
|
1032 |
|
|
1033 |
@Override
|
|
1034 |
public UpdateBuilder table(String tableName) {
|
|
1035 |
this.table = new TableId(tableName);
|
|
1036 |
return this;
|
|
1037 |
}
|
|
1038 |
|
|
1039 |
@Override
|
|
1040 |
public UpdateBuilder set(String columnName, String value) {
|
|
1041 |
Pair<String,String> pair = new ImmutablePair<>(columnName, value);
|
|
1042 |
this.columns.add(pair);
|
|
1043 |
return this;
|
|
1044 |
}
|
|
1045 |
|
|
1046 |
@Override
|
|
1047 |
public UpdateBuilder setGeometry(String columnName, String value, IProjection crs) {
|
|
1048 |
String g;
|
|
1049 |
switch( geometry_support_type() ) {
|
|
1050 |
case WKB:
|
|
1051 |
g = ST_GeomFromWKB(value, crs(crs));
|
|
1052 |
break;
|
|
1053 |
case EWKB:
|
|
1054 |
g = ST_GeomFromEWKB(value, crs(crs));
|
|
1055 |
break;
|
|
1056 |
case WKT:
|
|
1057 |
default:
|
|
1058 |
g = ST_GeomFromText(value, crs(crs));
|
|
1059 |
break;
|
|
1060 |
}
|
|
1061 |
this.set(identifier(columnName),g);
|
|
1062 |
return this;
|
|
1063 |
}
|
|
1064 |
|
|
1065 |
@Override
|
|
1066 |
public boolean hasWhere() {
|
|
1067 |
return this.where != null;
|
|
1068 |
}
|
|
1069 |
|
|
1070 |
@Override
|
|
1071 |
public String toString() {
|
|
1072 |
/*
|
|
1073 |
* UPDATE [ ONLY ] table [ [ AS ] alias ] SET { column = { expression |
|
|
1074 |
* DEFAULT } | ( column [, ...] ) = ( { expression | DEFAULT } [, ...] )
|
|
1075 |
* } [, ...] [ FROM fromlist ] [ WHERE condition ] [ RETURNING * |
|
|
1076 |
* output_expression [ AS output_name ] [, ...] ]
|
|
1077 |
*/
|
|
1078 |
StringBuilder columnsAndValues = new StringBuilder();
|