Skip to content

Commit 2ee7aa9

Browse files
committed
- Tool alias adder implemented
1 parent 147e8b7 commit 2ee7aa9

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package net.sf.jsqlparser.util;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
import net.sf.jsqlparser.statement.select.AllColumns;
6+
import net.sf.jsqlparser.statement.select.AllTableColumns;
7+
import net.sf.jsqlparser.statement.select.PlainSelect;
8+
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
9+
import net.sf.jsqlparser.statement.select.SelectItem;
10+
import net.sf.jsqlparser.statement.select.SelectItemVisitor;
11+
import net.sf.jsqlparser.statement.select.SelectVisitor;
12+
import net.sf.jsqlparser.statement.select.SetOperationList;
13+
14+
/**
15+
* Add aliases to every column and expression selected by a
16+
* select - statement. Existing aliases are recognized and
17+
* preserved. This class standard uses a prefix of A and a counter
18+
* to generate new aliases (e.g. A1, A5, ...). This behaviour
19+
* can be altered.
20+
*
21+
* @author tw
22+
*/
23+
public class AddAliasesVisitor implements SelectVisitor, SelectItemVisitor {
24+
25+
private List<String> aliases = new LinkedList<String>();
26+
private boolean firstRun = true;
27+
private int counter = 0;
28+
private String prefix = "A";
29+
30+
@Override
31+
public void visit(PlainSelect plainSelect) {
32+
firstRun = true;
33+
counter = 0;
34+
aliases.clear();
35+
for (SelectItem item : plainSelect.getSelectItems()) {
36+
item.accept(this);
37+
}
38+
firstRun = false;
39+
for (SelectItem item : plainSelect.getSelectItems()) {
40+
item.accept(this);
41+
}
42+
}
43+
44+
@Override
45+
public void visit(SetOperationList setOpList) {
46+
for (PlainSelect select : setOpList.getPlainSelects()) {
47+
select.accept(this);
48+
}
49+
}
50+
51+
@Override
52+
public void visit(AllColumns allColumns) {
53+
throw new UnsupportedOperationException("Not supported yet.");
54+
}
55+
56+
@Override
57+
public void visit(AllTableColumns allTableColumns) {
58+
throw new UnsupportedOperationException("Not supported yet.");
59+
}
60+
61+
@Override
62+
public void visit(SelectExpressionItem selectExpressionItem) {
63+
if (firstRun) {
64+
if (selectExpressionItem.getAlias() != null) {
65+
aliases.add(selectExpressionItem.getAlias().toUpperCase());
66+
}
67+
} else {
68+
if (selectExpressionItem.getAlias() == null) {
69+
70+
while (true) {
71+
String alias = getNextAlias().toUpperCase();
72+
if (!aliases.contains(alias)) {
73+
aliases.add(alias);
74+
selectExpressionItem.setAlias(alias);
75+
break;
76+
}
77+
}
78+
}
79+
}
80+
}
81+
82+
/**
83+
* Calculate next alias name to use.
84+
* @return
85+
*/
86+
protected String getNextAlias() {
87+
counter++;
88+
return prefix + counter;
89+
}
90+
91+
/**
92+
* Set alias prefix.
93+
*
94+
* @param prefix
95+
*/
96+
public void setPrefix(String prefix) {
97+
this.prefix = prefix;
98+
}
99+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package net.sf.jsqlparser.util;
2+
3+
import java.io.StringReader;
4+
import net.sf.jsqlparser.JSQLParserException;
5+
import net.sf.jsqlparser.parser.CCJSqlParserManager;
6+
import net.sf.jsqlparser.statement.select.Select;
7+
import net.sf.jsqlparser.statement.select.SetOperationList;
8+
import org.junit.After;
9+
import org.junit.AfterClass;
10+
import org.junit.Before;
11+
import org.junit.BeforeClass;
12+
import org.junit.Test;
13+
import static org.junit.Assert.*;
14+
15+
/**
16+
*
17+
* @author tw
18+
*/
19+
public class AddAliasesVisitorTest {
20+
21+
public AddAliasesVisitorTest() {
22+
}
23+
24+
@BeforeClass
25+
public static void setUpClass() {
26+
}
27+
28+
@AfterClass
29+
public static void tearDownClass() {
30+
}
31+
32+
@Before
33+
public void setUp() {
34+
}
35+
36+
@After
37+
public void tearDown() {
38+
}
39+
40+
CCJSqlParserManager parserManager = new CCJSqlParserManager();
41+
42+
/**
43+
* Test of visit method, of class AddAliasesVisitor.
44+
*/
45+
@Test
46+
public void testVisit_PlainSelect() throws JSQLParserException {
47+
String sql = "select a,b,c from test";
48+
Select select = (Select) parserManager.parse( new StringReader(sql));
49+
final AddAliasesVisitor instance = new AddAliasesVisitor();
50+
select.getSelectBody().accept(instance);
51+
52+
assertEquals("SELECT a AS A1, b AS A2, c AS A3 FROM test", select.toString());
53+
}
54+
55+
@Test
56+
public void testVisit_PlainSelect_duplicates() throws JSQLParserException {
57+
String sql = "select a,b as a1,c from test";
58+
Select select = (Select) parserManager.parse( new StringReader(sql));
59+
final AddAliasesVisitor instance = new AddAliasesVisitor();
60+
select.getSelectBody().accept(instance);
61+
62+
assertEquals("SELECT a AS A2, b AS a1, c AS A3 FROM test", select.toString());
63+
}
64+
65+
@Test
66+
public void testVisit_PlainSelect_expression() throws JSQLParserException {
67+
String sql = "select 3+4 from test";
68+
Select select = (Select) parserManager.parse( new StringReader(sql));
69+
final AddAliasesVisitor instance = new AddAliasesVisitor();
70+
select.getSelectBody().accept(instance);
71+
72+
assertEquals("SELECT 3 + 4 AS A1 FROM test", select.toString());
73+
}
74+
75+
/**
76+
* Test of visit method, of class AddAliasesVisitor.
77+
*/
78+
@Test
79+
public void testVisit_SetOperationList() throws JSQLParserException {
80+
String sql = "select 3+4 from test union select 7+8 from test2";
81+
Select setOpList = (Select) parserManager.parse( new StringReader(sql));
82+
final AddAliasesVisitor instance = new AddAliasesVisitor();
83+
setOpList.getSelectBody().accept(instance);
84+
85+
assertEquals("(SELECT 3 + 4 AS A1 FROM test) UNION (SELECT 7 + 8 AS A1 FROM test2)", setOpList.toString());
86+
}
87+
}

0 commit comments

Comments
 (0)