Revision 44376 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
SQLBuilderBase.java | ||
---|---|---|
16 | 16 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
17 | 17 |
import org.gvsig.expressionevaluator.ExpressionBuilder.AbstractValue; |
18 | 18 |
import org.gvsig.expressionevaluator.ExpressionBuilder.ClassVisitorFilter; |
19 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.EMPTY_FORMATTER; |
|
19 | 20 |
import org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType; |
20 | 21 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Parameter; |
21 | 22 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
... | ... | |
296 | 297 |
return this.parameters; |
297 | 298 |
} |
298 | 299 |
} |
299 |
// |
|
300 |
// public class ColumnBase extends AbstractValue implements Column { |
|
301 |
// |
|
302 |
// protected ColumnDescriptor descriptor; |
|
303 |
// protected String name; |
|
304 |
// |
|
305 |
// public ColumnBase(ColumnDescriptor descriptor) { |
|
306 |
// this.name = descriptor.getName(); |
|
307 |
// this.descriptor = descriptor; |
|
308 |
// } |
|
309 |
// |
|
310 |
// public ColumnBase(String name) { |
|
311 |
// this.name = name; |
|
312 |
// this.descriptor = null; |
|
313 |
// } |
|
314 |
// |
|
315 |
// @Override |
|
316 |
// public ColumnDescriptor getDescriptor() { |
|
317 |
// return descriptor; |
|
318 |
// } |
|
319 |
// |
|
320 |
// @Override |
|
321 |
// public String name() { |
|
322 |
// return this.name; |
|
323 |
// } |
|
324 |
// |
|
325 |
// @Override |
|
326 |
// public String toString() { |
|
327 |
// return this.toString(formatter()); |
|
328 |
// } |
|
329 |
// |
|
330 |
// @Override |
|
331 |
// public String toString(Formatter<Value> formatter) { |
|
332 |
// if (formatter!=null && formatter.canApply(this)) { |
|
333 |
// return formatter.format(this); |
|
334 |
// } |
|
335 |
// return as_identifier(this.name); |
|
336 |
// } |
|
337 |
// |
|
338 |
// @Override |
|
339 |
// public int compareTo(ExpressionBuilder.Variable o) { |
|
340 |
// return this.name.compareTo(o.name()); |
|
341 |
// } |
|
342 |
// |
|
343 |
// @Override |
|
344 |
// public boolean equals(Object obj) { |
|
345 |
// if (!(obj instanceof ExpressionBuilder.Variable)) { |
|
346 |
// return false; |
|
347 |
// } |
|
348 |
// return this.name.equals(((ExpressionBuilder.Variable) obj).name()); |
|
349 |
// } |
|
350 |
// |
|
351 |
// @Override |
|
352 |
// public int hashCode() { |
|
353 |
// int hash = 7; |
|
354 |
// hash = 37 * hash + Objects.hashCode(this.name); |
|
355 |
// return hash; |
|
356 |
// } |
|
357 |
// |
|
358 |
// } |
|
359 | 300 |
|
301 |
public class ColumnBase extends AbstractValue implements Column { |
|
302 |
|
|
303 |
private String name; |
|
304 |
private TableNameBuilder table; |
|
305 |
|
|
306 |
public ColumnBase(TableNameBuilder table, String name) { |
|
307 |
this.name = name; |
|
308 |
this.table = table; |
|
309 |
} |
|
310 |
|
|
311 |
@Override |
|
312 |
public String name() { |
|
313 |
return this.name; |
|
314 |
} |
|
315 |
|
|
316 |
@Override |
|
317 |
public TableNameBuilder table() { |
|
318 |
return this.table; |
|
319 |
} |
|
320 |
|
|
321 |
@Override |
|
322 |
public TableNameBuilder table(TableNameBuilder table) { |
|
323 |
this.table = table; |
|
324 |
return this.table; |
|
325 |
} |
|
326 |
|
|
327 |
@Override |
|
328 |
public String toString() { |
|
329 |
return this.toString(EMPTY_FORMATTER); |
|
330 |
} |
|
331 |
|
|
332 |
@Override |
|
333 |
public String toString(Formatter<Value> formatter) { |
|
334 |
if( formatter!=null && formatter.canApply(this) ) { |
|
335 |
return formatter.format(this); |
|
336 |
} |
|
337 |
if( this.table==null ) { |
|
338 |
return as_identifier(this.name); |
|
339 |
} |
|
340 |
return this.table.toString(formatter) + "." + as_identifier(this.name); |
|
341 |
} |
|
342 |
|
|
343 |
@Override |
|
344 |
public int compareTo(Variable o) { |
|
345 |
return this.name.compareTo(o.name()); |
|
346 |
} |
|
347 |
|
|
348 |
@Override |
|
349 |
public boolean equals(Object obj) { |
|
350 |
if (!(obj instanceof Variable)) { |
|
351 |
return false; |
|
352 |
} |
|
353 |
return StringUtils.equals(this.toString(), ((Variable) obj).toString()); |
|
354 |
} |
|
355 |
|
|
356 |
@Override |
|
357 |
public int hashCode() { |
|
358 |
int hash = 7; |
|
359 |
hash = 37 * hash + Objects.hashCode(this.toString()); |
|
360 |
return hash; |
|
361 |
} |
|
362 |
} |
|
363 |
|
|
360 | 364 |
public class TableNameBuilderBase |
361 | 365 |
extends AbstractStatementPart |
362 | 366 |
implements TableNameBuilder { |
... | ... | |
468 | 472 |
} |
469 | 473 |
return this.tableName.equals(other.getName()); |
470 | 474 |
} |
471 |
|
|
472 | 475 |
|
476 |
@Override |
|
477 |
public int hashCode() { |
|
478 |
int hash = 7; |
|
479 |
hash = 37 * hash + Objects.hashCode(this.toString()); |
|
480 |
return hash; |
|
481 |
} |
|
482 |
|
|
473 | 483 |
} |
474 | 484 |
|
475 | 485 |
public class CountBuilderBase |
... | ... | |
531 | 541 |
|
532 | 542 |
} |
533 | 543 |
|
544 |
protected class JoinBase |
|
545 |
extends AbstractStatementPart |
|
546 |
implements StatementPart |
|
547 |
{ |
|
548 |
protected String type; |
|
549 |
protected TableNameBuilder table; |
|
550 |
protected Value expression; |
|
551 |
|
|
552 |
public JoinBase(String type, TableNameBuilder table, Value expression) { |
|
553 |
this.type = type; |
|
554 |
this.table = table; |
|
555 |
this.expression = expression; |
|
556 |
} |
|
557 |
|
|
558 |
@Override |
|
559 |
public String toString() { |
|
560 |
return this.toString(formatter()); |
|
561 |
} |
|
562 |
|
|
563 |
@Override |
|
564 |
public String toString(Formatter<Value> formatter) { |
|
565 |
if (formatter!=null && formatter.canApply(this)) { |
|
566 |
return formatter.format(this); |
|
567 |
} |
|
568 |
StringBuilder builder = new StringBuilder(); |
|
569 |
// INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID |
|
570 |
builder.append(this.type.toUpperCase()); |
|
571 |
builder.append(" JOIN "); |
|
572 |
builder.append(this.table.toString(formatter)); |
|
573 |
builder.append(" ON "); |
|
574 |
builder.append(this.expression.toString(formatter)); |
|
575 |
return builder.toString(); |
|
576 |
} |
|
577 |
|
|
578 |
|
|
579 |
} |
|
580 |
|
|
534 | 581 |
public class FromBuilderBase |
535 | 582 |
extends AbstractStatementPart |
536 | 583 |
implements FromBuilder { |
537 | 584 |
|
538 |
protected TableNameBuilder tableName = null; |
|
539 |
private String subquery = null; |
|
540 |
private String passthrough = null; |
|
585 |
protected TableNameBuilder tableName; |
|
586 |
private String subquery; |
|
587 |
private String passthrough; |
|
588 |
private List<JoinBase> joins; |
|
541 | 589 |
|
590 |
public FromBuilderBase() { |
|
591 |
this.tableName = null; |
|
592 |
this.subquery = null; |
|
593 |
this.passthrough = null; |
|
594 |
this.joins = null; |
|
595 |
} |
|
596 |
|
|
542 | 597 |
@Override |
598 |
public FromBuilder left_join(TableNameBuilder table, Value expression) { |
|
599 |
JoinBase join = createJoin("LEFT", table, expression); |
|
600 |
if( this.joins==null ) { |
|
601 |
this.joins = new ArrayList<>(); |
|
602 |
} |
|
603 |
this.joins.add(join); |
|
604 |
return this; |
|
605 |
} |
|
606 |
|
|
607 |
@Override |
|
543 | 608 |
public TableNameBuilder table() { |
544 | 609 |
if (tableName == null) { |
545 | 610 |
this.tableName = createTableNameBuilder(); |
... | ... | |
585 | 650 |
if (!StringUtils.isEmpty(subquery)) { |
586 | 651 |
return "( " + this.subquery + ") as _subquery_alias_ "; |
587 | 652 |
} |
588 |
return this.tableName.toString(formatter); |
|
653 |
if( this.joins==null || this.joins.isEmpty() ) { |
|
654 |
return this.tableName.toString(formatter); |
|
655 |
} |
|
656 |
StringBuilder builder = new StringBuilder(); |
|
657 |
builder.append(this.tableName.toString(formatter)); |
|
658 |
for (JoinBase join : this.joins) { |
|
659 |
builder.append(" "); |
|
660 |
builder.append(join.toString(formatter)); |
|
661 |
} |
|
662 |
return builder.toString(); |
|
589 | 663 |
} |
590 | 664 |
|
591 | 665 |
} |
... | ... | |
598 | 672 |
private String alias = null; |
599 | 673 |
private Value value = null; |
600 | 674 |
private boolean asGeometry = false; |
675 |
private TableNameBuilder table; |
|
601 | 676 |
|
602 | 677 |
@Override |
603 | 678 |
public void accept(Visitor visitor, VisitorFilter filter) { |
... | ... | |
613 | 688 |
} |
614 | 689 |
|
615 | 690 |
@Override |
691 |
public void replace(Value target, Value replacement) { |
|
692 |
if (this.name!=null ) { |
|
693 |
if( this.name == target) { |
|
694 |
this.name = (Variable) replacement; |
|
695 |
} |
|
696 |
} |
|
697 |
if( this.value!=null ) { |
|
698 |
if (this.value == target) { |
|
699 |
this.value = replacement; |
|
700 |
} else { |
|
701 |
this.value.replace(target, replacement); |
|
702 |
} |
|
703 |
} |
|
704 |
} |
|
705 |
|
|
706 |
@Override |
|
616 | 707 |
public SelectColumnBuilder name(String name) { |
708 |
return this.name(null, name); |
|
709 |
} |
|
710 |
|
|
711 |
@Override |
|
712 |
public SelectColumnBuilder name(TableNameBuilder table, String name) { |
|
617 | 713 |
String quote = quote_for_identifiers(); |
618 | 714 |
if (name.startsWith(quote)) { |
619 | 715 |
// Remove quotes |
620 | 716 |
name = name.substring(1, name.length() - 1); |
621 | 717 |
} |
622 | 718 |
this.name = expression().variable(name); |
719 |
this.table = table; |
|
623 | 720 |
this.value = null; |
624 | 721 |
this.asGeometry = false; |
625 | 722 |
return this; |
... | ... | |
682 | 779 |
builder.append(expression().ST_AsBinary(this.name).toString(formatter)); |
683 | 780 |
} else { |
684 | 781 |
if (this.name != null) { |
685 |
builder.append(this.name.toString(formatter)); |
|
782 |
if( this.table==null ) { |
|
783 |
builder.append(this.name.toString(formatter)); |
|
784 |
} else { |
|
785 |
builder.append(this.table.toString(formatter)); |
|
786 |
builder.append("."); |
|
787 |
builder.append(this.name.toString(formatter)); |
|
788 |
} |
|
686 | 789 |
} else { |
687 | 790 |
builder.append(this.value.toString(formatter)); |
688 | 791 |
} |
... | ... | |
815 | 918 |
} |
816 | 919 |
|
817 | 920 |
@Override |
921 |
public void replace(Value target, Value replacement) { |
|
922 |
if( this.columns!=null ) { |
|
923 |
for (int i = 0; i < columns.size(); i++) { |
|
924 |
SelectColumnBuilder column = columns.get(i); |
|
925 |
if( column == target ) { |
|
926 |
columns.set(i, (SelectColumnBuilder) replacement); |
|
927 |
} else { |
|
928 |
column.replace(target, replacement); |
|
929 |
} |
|
930 |
} |
|
931 |
} |
|
932 |
if (this.has_from()) { |
|
933 |
if( this.from == target ) { |
|
934 |
this.from = (FromBuilder) replacement; |
|
935 |
} else { |
|
936 |
this.from.replace(target, replacement); |
|
937 |
} |
|
938 |
} |
|
939 |
if (this.has_where()) { |
|
940 |
if( this.where == target ) { |
|
941 |
this.where = (ExpressionBuilder) replacement; |
|
942 |
} else if( this.where.value() == target ) { |
|
943 |
this.where.value(replacement); |
|
944 |
} else { |
|
945 |
this.where.value().replace(target, replacement); |
|
946 |
} |
|
947 |
} |
|
948 |
if (this.has_order_by()) { |
|
949 |
for (int i = 0; i < order_by.size(); i++) { |
|
950 |
OrderByBuilder order = order_by.get(i); |
|
951 |
if( order == target ) { |
|
952 |
order_by.set(i, (OrderByBuilder) replacement); |
|
953 |
} else { |
|
954 |
order.replace(target, replacement); |
|
955 |
} |
|
956 |
} |
|
957 |
} |
|
958 |
} |
|
959 |
|
|
960 |
@Override |
|
818 | 961 |
public SelectBuilder distinct() { |
819 | 962 |
this.distinct = true; |
820 | 963 |
return this; |
... | ... | |
2760 | 2903 |
} |
2761 | 2904 |
return this.grant; |
2762 | 2905 |
} |
2906 |
|
|
2907 |
@Override |
|
2908 |
public Column column(String name) { |
|
2909 |
ColumnBase col = new ColumnBase(null, name); |
|
2910 |
return col; |
|
2911 |
} |
|
2763 | 2912 |
|
2913 |
@Override |
|
2914 |
public Column column(TableNameBuilder table, String name) { |
|
2915 |
ColumnBase col = new ColumnBase(table, name); |
|
2916 |
return col; |
|
2917 |
} |
|
2918 |
|
|
2919 |
protected JoinBase createJoin(String type, TableNameBuilder table, Value expression) { |
|
2920 |
return new JoinBase(type, table, expression); |
|
2921 |
} |
|
2922 |
|
|
2764 | 2923 |
public void accept(Visitor visitor, VisitorFilter filter) { |
2765 | 2924 |
if (this.select != null) { |
2766 | 2925 |
this.select.accept(visitor, filter); |
Also available in: Unified diff