Skip to content

Commit c19f83f

Browse files
committed
addJoin introduced
1 parent d367559 commit c19f83f

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ The generated hierarchy can be navigated using the Visitor Pattern.
2525
## Extensions Version 0.8.7
2626

2727
* Startet a simple utility class **SelectUtils** to collect basic **select** modification tools.
28+
** addExpression adds a new expression to the select list.
29+
** addJoin adds a new join to the select.
2830
* Added support for optional " AS " for aliases.
2931

3032
```sql

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ public class Table implements FromItem {
3535
private String schemaName;
3636
private String name;
3737
private Alias alias;
38-
private Pivot pivot;
38+
private Pivot pivot;
3939

4040
public Table() {
4141
}
4242

43+
public Table(String name) {
44+
this.name = name;
45+
}
46+
4347
public Table(String schemaName, String name) {
4448
this.schemaName = schemaName;
4549
this.name = name;
@@ -71,7 +75,7 @@ public void setAlias(Alias alias) {
7175
this.alias = alias;
7276
}
7377

74-
public String getWholeTableName() {
78+
public String getWholeTableName() {
7579

7680
String tableWholeName = null;
7781
if (name == null) {
@@ -97,19 +101,19 @@ public void accept(IntoTableVisitor intoTableVisitor) {
97101
}
98102

99103
@Override
100-
public Pivot getPivot() {
101-
return pivot;
102-
}
104+
public Pivot getPivot() {
105+
return pivot;
106+
}
103107

104108
@Override
105-
public void setPivot(Pivot pivot) {
106-
this.pivot = pivot;
107-
}
109+
public void setPivot(Pivot pivot) {
110+
this.pivot = pivot;
111+
}
108112

109-
@Override
113+
@Override
110114
public String toString() {
111-
return getWholeTableName() +
112-
((pivot != null) ? " "+pivot : "") +
113-
((alias != null) ? alias.toString() : "");
115+
return getWholeTableName()
116+
+ ((pivot != null) ? " " + pivot : "")
117+
+ ((alias != null) ? alias.toString() : "");
114118
}
115119
}

src/main/java/net/sf/jsqlparser/util/SelectUtils.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
*/
2222
package net.sf.jsqlparser.util;
2323

24+
import java.util.ArrayList;
25+
import java.util.List;
2426
import net.sf.jsqlparser.expression.Expression;
27+
import net.sf.jsqlparser.schema.Table;
28+
import net.sf.jsqlparser.statement.select.Join;
2529
import net.sf.jsqlparser.statement.select.PlainSelect;
2630
import net.sf.jsqlparser.statement.select.Select;
2731
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
@@ -40,7 +44,8 @@ private SelectUtils() {
4044
}
4145

4246
/**
43-
* Adds an expression to select statements. E.g. a simple column is an expression.
47+
* Adds an expression to select statements. E.g. a simple column is an
48+
* expression.
4449
*
4550
* @param select
4651
* @param expr
@@ -64,4 +69,29 @@ public void visit(WithItem withItem) {
6469
}
6570
});
6671
}
72+
73+
/**
74+
* Adds a simple join to a select statement. The introduced join is returned for
75+
* more configuration settings on it (e.g. left join, right join).
76+
* @param select
77+
* @param table
78+
* @param onExpression
79+
* @return
80+
*/
81+
public static Join addJoin(Select select, final Table table, final Expression onExpression) {
82+
if (select.getSelectBody() instanceof PlainSelect) {
83+
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
84+
List<Join> joins = plainSelect.getJoins();
85+
if (joins == null) {
86+
joins = new ArrayList<Join>();
87+
plainSelect.setJoins(joins);
88+
}
89+
Join join = new Join();
90+
join.setRightItem(table);
91+
join.setOnExpression(onExpression);
92+
joins.add(join);
93+
return join;
94+
}
95+
throw new UnsupportedOperationException("Not supported yet.");
96+
}
6797
}

src/test/java/net/sf/jsqlparser/util/SelectUtilsTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import net.sf.jsqlparser.JSQLParserException;
44
import net.sf.jsqlparser.expression.LongValue;
55
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
6+
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
67
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
78
import net.sf.jsqlparser.schema.Column;
9+
import net.sf.jsqlparser.schema.Table;
10+
import net.sf.jsqlparser.statement.select.Join;
811
import net.sf.jsqlparser.statement.select.Select;
912
import org.junit.After;
1013
import org.junit.AfterClass;
@@ -54,4 +57,15 @@ public void testAddExpr() throws JSQLParserException {
5457

5558
assertEquals("SELECT a, b, 5 + 6 FROM mytable", select.toString());
5659
}
60+
61+
@Test
62+
public void testAddJoin() throws JSQLParserException {
63+
Select select = (Select)CCJSqlParserUtil.parse("select a from mytable");
64+
final EqualsTo equalsTo = new EqualsTo();
65+
equalsTo.setLeftExpression(new Column("a"));
66+
equalsTo.setRightExpression(new Column("b"));
67+
Join addJoin = SelectUtils.addJoin(select, new Table("mytable2"), equalsTo);
68+
addJoin.setLeft(true);
69+
assertEquals("SELECT a FROM mytable LEFT JOIN mytable2 ON a = b", select.toString());
70+
}
5771
}

0 commit comments

Comments
 (0)