Skip to content

Commit 36a6dee

Browse files
#113: added a builder to SqlStatementSelect class (#114)
* #113: added a builder to SqlStatementSelect class
1 parent 254f94c commit 36a6dee

File tree

5 files changed

+136
-15
lines changed

5 files changed

+136
-15
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.exasol</groupId>
66
<artifactId>virtual-schema-common-java</artifactId>
7-
<version>7.4.0</version>
7+
<version>7.5.0</version>
88
<name>Common module of Exasol Virtual Schemas Adapters</name>
99
<description>This is one of the modules of Virtual Schemas Adapters. The libraries provided by this project are the foundation of the adapter development, i.e. adapters must be implemented on top of them.</description>
1010
<url>https://github.com/exasol/virtual-schema-common-java</url>

src/main/java/com/exasol/adapter/request/parser/PushdownSqlParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ private SqlStatementSelect parseSelect(final JsonObject select) {
125125
if (select.containsKey("limit")) {
126126
limit = parseLimit(select.getJsonObject("limit"));
127127
}
128-
return new SqlStatementSelect(table, selectList, whereClause, groupByClause, having, orderBy, limit);
128+
return SqlStatementSelect.builder().selectList(selectList).fromClause(table).whereClause(whereClause)
129+
.groupBy(groupByClause).having(having).orderBy(orderBy).limit(limit).build();
129130
}
130131

131132
private SqlNode parseTable(final JsonObject exp) {

src/main/java/com/exasol/adapter/sql/SqlStatementSelect.java

Lines changed: 117 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@ public class SqlStatementSelect extends SqlStatement {
1414
private final SqlOrderBy orderBy;
1515
private final SqlLimit limit;
1616

17-
public SqlStatementSelect(final SqlNode fromClause, final SqlSelectList selectList, final SqlNode whereClause,
18-
final SqlExpressionList groupBy, final SqlNode having, final SqlOrderBy orderBy, final SqlLimit limit) {
19-
this.fromClause = fromClause;
20-
this.selectList = selectList;
21-
this.whereClause = whereClause;
22-
this.groupBy = groupBy;
23-
this.having = having;
24-
this.orderBy = orderBy;
25-
this.limit = limit;
26-
assert this.fromClause != null;
27-
assert this.selectList != null;
17+
private SqlStatementSelect(final Builder builder) {
18+
this.fromClause = builder.fromClause;
19+
this.selectList = builder.selectList;
20+
this.whereClause = builder.whereClause;
21+
this.groupBy = builder.groupBy;
22+
this.having = builder.having;
23+
this.orderBy = builder.orderBy;
24+
this.limit = builder.limit;
2825
this.fromClause.setParent(this);
2926
this.selectList.setParent(this);
3027

@@ -137,4 +134,112 @@ public SqlNodeType getType() {
137134
public <R> R accept(final SqlNodeVisitor<R> visitor) throws AdapterException {
138135
return visitor.visit(this);
139136
}
140-
}
137+
138+
/**
139+
* Create a new builder for {@link SqlStatementSelect}.
140+
*
141+
* @return builder instance
142+
*/
143+
public static Builder builder() {
144+
return new Builder();
145+
}
146+
147+
/**
148+
* Builder for {@link SqlStatementSelect}.
149+
*/
150+
public static class Builder {
151+
private SqlNode fromClause;
152+
private SqlSelectList selectList;
153+
private SqlNode whereClause;
154+
private SqlExpressionList groupBy;
155+
private SqlNode having;
156+
private SqlOrderBy orderBy;
157+
private SqlLimit limit;
158+
159+
/**
160+
* Set the from clause of the SQL Select Statement.
161+
*
162+
* @param fromClause from clause
163+
* @return builder instance for fluent programming
164+
*/
165+
public Builder fromClause(final SqlNode fromClause) {
166+
this.fromClause = fromClause;
167+
return this;
168+
}
169+
170+
/**
171+
* Set the select list of the SQL Select Statement.
172+
*
173+
* @param selectList select list
174+
* @return builder instance for fluent programming
175+
*/
176+
public Builder selectList(final SqlSelectList selectList) {
177+
this.selectList = selectList;
178+
return this;
179+
}
180+
181+
/**
182+
* Set the where clause of the SQL Select Statement.
183+
*
184+
* @param whereClause where clause
185+
* @return builder instance for fluent programming
186+
*/
187+
public Builder whereClause(final SqlNode whereClause) {
188+
this.whereClause = whereClause;
189+
return this;
190+
}
191+
192+
/**
193+
* Set the group by clause of the SQL Select Statement.
194+
*
195+
* @param groupBy group by clause
196+
* @return builder instance for fluent programming
197+
*/
198+
public Builder groupBy(final SqlExpressionList groupBy) {
199+
this.groupBy = groupBy;
200+
return this;
201+
}
202+
203+
/**
204+
* Set the having clause of the SQL Select Statement.
205+
*
206+
* @param having having clause
207+
* @return builder instance for fluent programming
208+
*/
209+
public Builder having(final SqlNode having) {
210+
this.having = having;
211+
return this;
212+
}
213+
214+
/**
215+
* Set the order by clause of the SQL Select Statement.
216+
*
217+
* @param orderBy order by clause
218+
* @return builder instance for fluent programming
219+
*/
220+
public Builder orderBy(final SqlOrderBy orderBy) {
221+
this.orderBy = orderBy;
222+
return this;
223+
}
224+
225+
/**
226+
* Set the limit clause of the SQL Select Statement.
227+
*
228+
* @param limit limit clause
229+
* @return builder instance for fluent programming
230+
*/
231+
public Builder limit(final SqlLimit limit) {
232+
this.limit = limit;
233+
return this;
234+
}
235+
236+
/**
237+
* Build a new instance of {@link SqlStatementSelect}
238+
*
239+
* @return new instance
240+
*/
241+
public SqlStatementSelect build() {
242+
return new SqlStatementSelect(this);
243+
}
244+
}
245+
}

src/test/java/com/exasol/adapter/sql/SqlNodeTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ private SqlNode getTestSqlNode() {
3636
final SqlOrderBy orderBy = new SqlOrderBy(List.of(new SqlColumn(0, clicksMeta.getColumns().get(0))),
3737
List.of(true), List.of(true));
3838
final SqlLimit limit = new SqlLimit(10);
39-
return new SqlStatementSelect(fromClause, selectList, whereClause, groupBy, having, orderBy, limit);
39+
return SqlStatementSelect.builder().selectList(selectList).fromClause(fromClause).whereClause(whereClause)
40+
.groupBy(groupBy).having(having).orderBy(orderBy).limit(limit).build();
4041
}
4142

4243
private TableMetadata getClicksTableMetadata() {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.exasol.adapter.sql;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.hamcrest.CoreMatchers.instanceOf;
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
8+
class SqlStatementSelectTest {
9+
@Test
10+
void builder() {
11+
final SqlStatementSelect.Builder sqlStatementSelectBuilder = SqlStatementSelect.builder();
12+
assertThat(sqlStatementSelectBuilder, instanceOf(SqlStatementSelect.Builder.class));
13+
}
14+
}

0 commit comments

Comments
 (0)