Skip to content

Commit 6b5e29a

Browse files
committed
started simple utility function for select statement modification
1 parent 311d636 commit 6b5e29a

File tree

4 files changed

+132
-2
lines changed

4 files changed

+132
-2
lines changed

src/main/java/net/sf/jsqlparser/schema/Column.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public Column(Table table, String columnName) {
4040
this.columnName = columnName;
4141
}
4242

43+
public Column(String columnName) {
44+
this(null, columnName);
45+
}
46+
4347
public String getColumnName() {
4448
return columnName;
4549
}
@@ -61,9 +65,12 @@ public void setTable(Table table) {
6165
*/
6266
public String getWholeColumnName() {
6367

64-
String columnWholeName = null;
65-
String tableWholeName = table.getWholeTableName();
68+
String columnWholeName;
69+
String tableWholeName = null;
6670

71+
if (table != null) {
72+
tableWholeName = table.getWholeTableName();
73+
}
6774
if (tableWholeName != null && tableWholeName.length() != 0) {
6875
columnWholeName = tableWholeName + "." + columnName;
6976
} else {

src/main/java/net/sf/jsqlparser/statement/select/SelectExpressionItem.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public class SelectExpressionItem implements SelectItem {
3232
private Expression expression;
3333
private Alias alias;
3434

35+
public SelectExpressionItem() {
36+
}
37+
38+
public SelectExpressionItem(Expression expression) {
39+
this.expression = expression;
40+
}
41+
3542
public Alias getAlias() {
3643
return alias;
3744
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2014 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
package net.sf.jsqlparser.util;
23+
24+
import net.sf.jsqlparser.expression.Expression;
25+
import net.sf.jsqlparser.statement.select.PlainSelect;
26+
import net.sf.jsqlparser.statement.select.Select;
27+
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
28+
import net.sf.jsqlparser.statement.select.SelectVisitor;
29+
import net.sf.jsqlparser.statement.select.SetOperationList;
30+
import net.sf.jsqlparser.statement.select.WithItem;
31+
32+
/**
33+
* Utility function for select statements.
34+
*
35+
* @author toben
36+
*/
37+
public final class SelectUtils {
38+
39+
private SelectUtils() {
40+
}
41+
42+
/**
43+
* Adds an expression to select statements. E.g. a simple column is an expression.
44+
*
45+
* @param select
46+
* @param expr
47+
*/
48+
public static void addExpression(Select select, final Expression expr) {
49+
select.getSelectBody().accept(new SelectVisitor() {
50+
51+
@Override
52+
public void visit(PlainSelect plainSelect) {
53+
plainSelect.getSelectItems().add(new SelectExpressionItem(expr));
54+
}
55+
56+
@Override
57+
public void visit(SetOperationList setOpList) {
58+
throw new UnsupportedOperationException("Not supported yet.");
59+
}
60+
61+
@Override
62+
public void visit(WithItem withItem) {
63+
throw new UnsupportedOperationException("Not supported yet.");
64+
}
65+
});
66+
}
67+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package net.sf.jsqlparser.util;
2+
3+
import net.sf.jsqlparser.JSQLParserException;
4+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
5+
import net.sf.jsqlparser.schema.Column;
6+
import net.sf.jsqlparser.statement.Statement;
7+
import net.sf.jsqlparser.statement.select.Select;
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 toben
18+
*/
19+
public class SelectUtilsTest {
20+
21+
public SelectUtilsTest() {
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+
/**
41+
* Test of addColumn method, of class SelectUtils.
42+
*/
43+
@Test
44+
public void testAddExpr() throws JSQLParserException {
45+
Select select = (Select) CCJSqlParserUtil.parse("select a from mytable");
46+
SelectUtils.addExpression(select, new Column("b"));
47+
assertEquals("SELECT a, b FROM mytable", select.toString());
48+
}
49+
}

0 commit comments

Comments
 (0)