Skip to content

Commit

Permalink
addJoin introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Jan 21, 2014
1 parent d367559 commit c19f83f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ The generated hierarchy can be navigated using the Visitor Pattern.
## Extensions Version 0.8.7

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

```sql
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/net/sf/jsqlparser/schema/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ public class Table implements FromItem {
private String schemaName;
private String name;
private Alias alias;
private Pivot pivot;
private Pivot pivot;

public Table() {
}

public Table(String name) {
this.name = name;
}

public Table(String schemaName, String name) {
this.schemaName = schemaName;
this.name = name;
Expand Down Expand Up @@ -71,7 +75,7 @@ public void setAlias(Alias alias) {
this.alias = alias;
}

public String getWholeTableName() {
public String getWholeTableName() {

String tableWholeName = null;
if (name == null) {
Expand All @@ -97,19 +101,19 @@ public void accept(IntoTableVisitor intoTableVisitor) {
}

@Override
public Pivot getPivot() {
return pivot;
}
public Pivot getPivot() {
return pivot;
}

@Override
public void setPivot(Pivot pivot) {
this.pivot = pivot;
}
public void setPivot(Pivot pivot) {
this.pivot = pivot;
}

@Override
@Override
public String toString() {
return getWholeTableName() +
((pivot != null) ? " "+pivot : "") +
((alias != null) ? alias.toString() : "");
return getWholeTableName()
+ ((pivot != null) ? " " + pivot : "")
+ ((alias != null) ? alias.toString() : "");
}
}
32 changes: 31 additions & 1 deletion src/main/java/net/sf/jsqlparser/util/SelectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
*/
package net.sf.jsqlparser.util;

import java.util.ArrayList;
import java.util.List;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
Expand All @@ -40,7 +44,8 @@ private SelectUtils() {
}

/**
* Adds an expression to select statements. E.g. a simple column is an expression.
* Adds an expression to select statements. E.g. a simple column is an
* expression.
*
* @param select
* @param expr
Expand All @@ -64,4 +69,29 @@ public void visit(WithItem withItem) {
}
});
}

/**
* Adds a simple join to a select statement. The introduced join is returned for
* more configuration settings on it (e.g. left join, right join).
* @param select
* @param table
* @param onExpression
* @return
*/
public static Join addJoin(Select select, final Table table, final Expression onExpression) {
if (select.getSelectBody() instanceof PlainSelect) {
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
List<Join> joins = plainSelect.getJoins();
if (joins == null) {
joins = new ArrayList<Join>();
plainSelect.setJoins(joins);
}
Join join = new Join();
join.setRightItem(table);
join.setOnExpression(onExpression);
joins.add(join);
return join;
}
throw new UnsupportedOperationException("Not supported yet.");
}
}
14 changes: 14 additions & 0 deletions src/test/java/net/sf/jsqlparser/util/SelectUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.Select;
import org.junit.After;
import org.junit.AfterClass;
Expand Down Expand Up @@ -54,4 +57,15 @@ public void testAddExpr() throws JSQLParserException {

assertEquals("SELECT a, b, 5 + 6 FROM mytable", select.toString());
}

@Test
public void testAddJoin() throws JSQLParserException {
Select select = (Select)CCJSqlParserUtil.parse("select a from mytable");
final EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(new Column("a"));
equalsTo.setRightExpression(new Column("b"));
Join addJoin = SelectUtils.addJoin(select, new Table("mytable2"), equalsTo);
addJoin.setLeft(true);
assertEquals("SELECT a FROM mytable LEFT JOIN mytable2 ON a = b", select.toString());
}
}

0 comments on commit c19f83f

Please sign in to comment.