Skip to content

Commit

Permalink
#77: fixed review findings
Browse files Browse the repository at this point in the history
  • Loading branch information
AnastasiiaSergienko committed Jul 3, 2020
1 parent b1b351f commit f88698c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion doc/changes/changes-4.0.0.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SQL Statement Builder 4.0.0, released 2020-07-02
# SQL Statement Builder 4.0.0, released 2020-07-03

## Bug Fixes

Expand Down
17 changes: 10 additions & 7 deletions src/main/java/com/exasol/sql/dql/select/FromClause.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,24 @@ public FromClause fullOuterJoin(final String name, final String specification) {
* @return {@code FROM} clause
*/
public FromClause select(final Select select) {
this.innerSelect = select;
this.subSelect = select;
return this;
}

/**
* Check if the {@link FromClause} contains an inner select statement.
* Check if the {@link FromClause} contains a sub-select statement.
*
* @return true if the {@link FromClause} contains an inner select statement
* @return true if the {@link FromClause} contains a sub-select statement
*/
public boolean hasInnerSelect() {
return innerSelect != null;
public boolean hasSubSelect() {
return this.subSelect != null;
}

@Override
public void accept(final SelectVisitor visitor) {
visitor.visit(this);
if (hasInnerSelect()) {
this.innerSelect.accept(visitor);
if (hasSubSelect()) {
this.subSelect.accept(visitor);
}
for (final Table table : this.tables) {
table.accept(visitor);
Expand All @@ -201,6 +201,9 @@ public void accept(final SelectVisitor visitor) {
join.accept(visitor);
}
for (final ValueTable valueTable : this.valueTables) {
if (hasSubSelect()) {
throw new IllegalArgumentException("SELECT statement cannot combine sub-select and value table.");
}
valueTable.accept(visitor);
}
visitor.leave(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public void visit(final DerivedColumn derivedColumn) {
@Override
public void visit(final FromClause fromClause) {
appendKeyWord(" FROM ");
if (fromClause.hasInnerSelect()) {
if (fromClause.hasSubSelect()) {
startParenthesis();
}
setLastVisited(fromClause);
}

@Override
public void leave(FromClause fromClause) {
if (fromClause.hasInnerSelect()) {
public void leave(final FromClause fromClause) {
if (fromClause.hasSubSelect()) {
endParenthesis();
}
setLastVisited(fromClause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import static com.exasol.hamcrest.SqlFragmentRenderResultMatcher.rendersWithConfigTo;
import static com.exasol.sql.expression.BooleanTerm.eq;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThrows;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.exasol.sql.StatementFactory;
import com.exasol.sql.ValueTable;
import com.exasol.sql.dql.select.Select;
import com.exasol.sql.expression.*;
import com.exasol.sql.rendering.StringRendererConfig;
Expand Down Expand Up @@ -104,10 +107,23 @@ void testQuotedIdentifiers() {
}

@Test
void testSelectFromSelect() {
void testSelectFromSubSelect() {
final Select innerSelect = StatementFactory.getInstance().select();
innerSelect.all().from().table("t");
this.select.all().from().select(innerSelect);
assertThat(this.select, rendersTo("SELECT * FROM (SELECT * FROM t)"));
}

@Test
void testSelectFromSubSelectInvalid() {
final Select innerSelect = StatementFactory.getInstance().select();
innerSelect.all().from().table("t");
final ValueTable values = new ValueTable(this.select);
this.select.all().from().select(innerSelect).valueTable(values);
final SelectRenderer renderer = SelectRenderer.create();
final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> this.select.accept(renderer));
assertThat(exception.getMessage(),
containsString("SELECT statement cannot combine sub-select and value table"));
}
}

0 comments on commit f88698c

Please sign in to comment.