-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Tool expression connector included
- Loading branch information
Showing
2 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/main/java/net/sf/jsqlparser/util/ConnectExpressionsVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package net.sf.jsqlparser.util; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import net.sf.jsqlparser.expression.BinaryExpression; | ||
import net.sf.jsqlparser.statement.select.AllColumns; | ||
import net.sf.jsqlparser.statement.select.AllTableColumns; | ||
import net.sf.jsqlparser.statement.select.PlainSelect; | ||
import net.sf.jsqlparser.statement.select.SelectExpressionItem; | ||
import net.sf.jsqlparser.statement.select.SelectItem; | ||
import net.sf.jsqlparser.statement.select.SelectItemVisitor; | ||
import net.sf.jsqlparser.statement.select.SelectVisitor; | ||
import net.sf.jsqlparser.statement.select.SetOperationList; | ||
|
||
/** | ||
* Connect all selected expressions with a binary expression. Out of select a,b from table | ||
* one gets select a || b as expr from table. The type of binary expression is set by | ||
* overwriting this class abstract method createBinaryExpression. | ||
* @author tw | ||
*/ | ||
public abstract class ConnectExpressionsVisitor implements SelectVisitor, SelectItemVisitor { | ||
|
||
private String alias = "expr"; | ||
private List<SelectExpressionItem> itemsExpr = new LinkedList<SelectExpressionItem>(); | ||
|
||
public ConnectExpressionsVisitor() { | ||
} | ||
|
||
public ConnectExpressionsVisitor(String alias) { | ||
this.alias = alias; | ||
} | ||
|
||
/** | ||
* Create instances of this binary expression that connects all selected columns. | ||
* @return | ||
*/ | ||
protected abstract BinaryExpression createBinaryExpression(); | ||
|
||
@Override | ||
public void visit(PlainSelect plainSelect) { | ||
for (SelectItem item : plainSelect.getSelectItems()) { | ||
item.accept(this); | ||
} | ||
|
||
if (itemsExpr.size() > 1) { | ||
BinaryExpression binExpr = createBinaryExpression(); | ||
binExpr.setLeftExpression(itemsExpr.get(0).getExpression()); | ||
for (int i = 1; i < itemsExpr.size() - 1; i++) { | ||
binExpr.setRightExpression(itemsExpr.get(i).getExpression()); | ||
BinaryExpression binExpr2 = createBinaryExpression(); | ||
binExpr2.setLeftExpression(binExpr); | ||
binExpr = binExpr2; | ||
} | ||
binExpr.setRightExpression(itemsExpr.get(itemsExpr.size() - 1).getExpression()); | ||
|
||
SelectExpressionItem sei = new SelectExpressionItem(); | ||
sei.setExpression(binExpr); | ||
|
||
plainSelect.getSelectItems().clear(); | ||
plainSelect.getSelectItems().add(sei); | ||
} | ||
|
||
((SelectExpressionItem)plainSelect.getSelectItems().get(0)).setAlias(alias); | ||
} | ||
|
||
@Override | ||
public void visit(SetOperationList setOpList) { | ||
for (PlainSelect select : setOpList.getPlainSelects()) { | ||
select.accept(this); | ||
} | ||
} | ||
|
||
@Override | ||
public void visit(AllColumns allColumns) { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public void visit(AllTableColumns allTableColumns) { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public void visit(SelectExpressionItem selectExpressionItem) { | ||
itemsExpr.add(selectExpressionItem); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/test/java/net/sf/jsqlparser/util/ConnectExpressionsVisitorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package net.sf.jsqlparser.util; | ||
|
||
import java.io.StringReader; | ||
import net.sf.jsqlparser.JSQLParserException; | ||
import net.sf.jsqlparser.expression.BinaryExpression; | ||
import net.sf.jsqlparser.expression.operators.arithmetic.Addition; | ||
import net.sf.jsqlparser.expression.operators.arithmetic.Concat; | ||
import net.sf.jsqlparser.parser.CCJSqlParserManager; | ||
import net.sf.jsqlparser.statement.select.Select; | ||
import org.junit.After; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* | ||
* @author tw | ||
*/ | ||
public class ConnectExpressionsVisitorTest { | ||
|
||
public ConnectExpressionsVisitorTest() { | ||
} | ||
|
||
@BeforeClass | ||
public static void setUpClass() { | ||
} | ||
|
||
@AfterClass | ||
public static void tearDownClass() { | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
} | ||
|
||
CCJSqlParserManager parserManager = new CCJSqlParserManager(); | ||
|
||
@Test | ||
public void testVisit_PlainSelect_concat() throws JSQLParserException { | ||
String sql = "select a,b,c from test"; | ||
Select select = (Select) parserManager.parse( new StringReader(sql)); | ||
ConnectExpressionsVisitor instance = new ConnectExpressionsVisitor() { | ||
|
||
@Override | ||
protected BinaryExpression createBinaryExpression() { | ||
return new Concat(); | ||
} | ||
}; | ||
select.getSelectBody().accept(instance); | ||
|
||
assertEquals("SELECT a || b || c AS expr FROM test", select.toString()); | ||
} | ||
|
||
@Test | ||
public void testVisit_PlainSelect_addition() throws JSQLParserException { | ||
String sql = "select a,b,c from test"; | ||
Select select = (Select) parserManager.parse( new StringReader(sql)); | ||
ConnectExpressionsVisitor instance = new ConnectExpressionsVisitor("testexpr") { | ||
|
||
@Override | ||
protected BinaryExpression createBinaryExpression() { | ||
return new Addition(); | ||
} | ||
}; | ||
select.getSelectBody().accept(instance); | ||
|
||
assertEquals("SELECT a + b + c AS testexpr FROM test", select.toString()); | ||
} | ||
} |