|
| 1 | +package net.sf.jsqlparser.util; |
| 2 | + |
| 3 | +import java.util.LinkedList; |
| 4 | +import java.util.List; |
| 5 | +import net.sf.jsqlparser.expression.BinaryExpression; |
| 6 | +import net.sf.jsqlparser.statement.select.AllColumns; |
| 7 | +import net.sf.jsqlparser.statement.select.AllTableColumns; |
| 8 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 9 | +import net.sf.jsqlparser.statement.select.SelectExpressionItem; |
| 10 | +import net.sf.jsqlparser.statement.select.SelectItem; |
| 11 | +import net.sf.jsqlparser.statement.select.SelectItemVisitor; |
| 12 | +import net.sf.jsqlparser.statement.select.SelectVisitor; |
| 13 | +import net.sf.jsqlparser.statement.select.SetOperationList; |
| 14 | + |
| 15 | +/** |
| 16 | + * Connect all selected expressions with a binary expression. Out of select a,b from table |
| 17 | + * one gets select a || b as expr from table. The type of binary expression is set by |
| 18 | + * overwriting this class abstract method createBinaryExpression. |
| 19 | + * @author tw |
| 20 | + */ |
| 21 | +public abstract class ConnectExpressionsVisitor implements SelectVisitor, SelectItemVisitor { |
| 22 | + |
| 23 | + private String alias = "expr"; |
| 24 | + private List<SelectExpressionItem> itemsExpr = new LinkedList<SelectExpressionItem>(); |
| 25 | + |
| 26 | + public ConnectExpressionsVisitor() { |
| 27 | + } |
| 28 | + |
| 29 | + public ConnectExpressionsVisitor(String alias) { |
| 30 | + this.alias = alias; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Create instances of this binary expression that connects all selected columns. |
| 35 | + * @return |
| 36 | + */ |
| 37 | + protected abstract BinaryExpression createBinaryExpression(); |
| 38 | + |
| 39 | + @Override |
| 40 | + public void visit(PlainSelect plainSelect) { |
| 41 | + for (SelectItem item : plainSelect.getSelectItems()) { |
| 42 | + item.accept(this); |
| 43 | + } |
| 44 | + |
| 45 | + if (itemsExpr.size() > 1) { |
| 46 | + BinaryExpression binExpr = createBinaryExpression(); |
| 47 | + binExpr.setLeftExpression(itemsExpr.get(0).getExpression()); |
| 48 | + for (int i = 1; i < itemsExpr.size() - 1; i++) { |
| 49 | + binExpr.setRightExpression(itemsExpr.get(i).getExpression()); |
| 50 | + BinaryExpression binExpr2 = createBinaryExpression(); |
| 51 | + binExpr2.setLeftExpression(binExpr); |
| 52 | + binExpr = binExpr2; |
| 53 | + } |
| 54 | + binExpr.setRightExpression(itemsExpr.get(itemsExpr.size() - 1).getExpression()); |
| 55 | + |
| 56 | + SelectExpressionItem sei = new SelectExpressionItem(); |
| 57 | + sei.setExpression(binExpr); |
| 58 | + |
| 59 | + plainSelect.getSelectItems().clear(); |
| 60 | + plainSelect.getSelectItems().add(sei); |
| 61 | + } |
| 62 | + |
| 63 | + ((SelectExpressionItem)plainSelect.getSelectItems().get(0)).setAlias(alias); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void visit(SetOperationList setOpList) { |
| 68 | + for (PlainSelect select : setOpList.getPlainSelects()) { |
| 69 | + select.accept(this); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void visit(AllColumns allColumns) { |
| 75 | + throw new UnsupportedOperationException("Not supported yet."); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void visit(AllTableColumns allTableColumns) { |
| 80 | + throw new UnsupportedOperationException("Not supported yet."); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void visit(SelectExpressionItem selectExpressionItem) { |
| 85 | + itemsExpr.add(selectExpressionItem); |
| 86 | + } |
| 87 | +} |
0 commit comments