Skip to content

Commit 6cff161

Browse files
authored
Fluent builder api #1004 (#1014)
* #1004 * create(...) methods * chaining - methods returning "this" * overwrite chaining - methods of abstract parents/interfaces for returning concrete type * add<Name> methods on collection-fields with varargs-parameter * add public T get<Name>(Class<T>) - casting and returning an inner interface-type * 1004 add chaining - methods returning "this" * #1004 add chaining - methods returning "this" * * add<Name> methods on collection-fields with varargs-parameter * add<Name> methods on collection-fields with collection-parameter #1004 * * add chaining - methods returning "this" * add<Name> methods on collection-fields with varargs-parameter * add<Name> methods on collection-fields with collection-parameter #1004 * * add public T get<Name>(Class<T>) - casting and returning the concrete type #1004 * * add public T get<Name>(Class<T>) - casting and returning the concrete type (swap Class<? extends E> for Class<E>) #1004 * * overwrite chaining - methods of abstract parents/interfaces for returning concrete type #1004 * * add with prefix for fluent setters. #1004 * add getters * * add with prefix for fluent setters. (revert to chaining setters, do not break current api) #1004 * * add with prefix for fluent setters. (revert to chaining setters, do not break current api) #1004 * use new methods within testcases * use new methods within testcases * use new methods within testcases * use new methods within testcases * use new methods within testcases * use new methods within testcases * use new methods within testcases * use new methods within testcases * remove create() methods - they do not add enough value to be justified * * use new methods within testcases * add some constructors * fix and add "with" / "add" methods * * use new methods within testcases * * use new methods within testcases * add some constructors * * renamed constant * use new methods within testcases * use new methods within testcases * use new methods within testcases * use new methods within testcases * * use new methods within testcases * add some with-methods * add getter/setter named after the field without abbrivation * * use new methods within testcases * remove empty implicit constructor * return the deparsed Statement - object * compare object tree * compare object tree * * fix ObjectTreeToStringStyle * compare object tree * remove casts not needed * * use new methods within testcases * add some "set" "with" "add" methods missing * * use new methods within testcases * add empty constructors and override with-/add-methods returning concrete type * * add ReflectionModelTest * * use new methods within testcases * fix checkstyle errors * license header * remove test-classes from ReflectionModelTest * remove visitoradapter-classes from ReflectionModelTest * remove duplicate import declaration (checkstyle error) * * fix RandomUtils to support used java.sql.* types * fix RandomUtils to support enums * fix RandomUtils to map objects by its interfaces and super-classes * filter method "setASTNode" - do not test setters (cannot randomly create a SimpleNode) * add javadoc, stating that this is a marker interface #1014 (comment) * revert formatting change #1014 (comment) * change to EXEC_TYPE.EXECUTE just so the assertion didn't change #1014 (comment) * try to revert format changes #1014 (comment) * try to revert format changes #1014 (comment) * remove brackets on @OverRide() -> @OverRide * add with-methods to new fields
1 parent f88d93f commit 6cff161

File tree

208 files changed

+6787
-893
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

208 files changed

+6787
-893
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949
<version>3.16.1</version>
5050
<scope>test</scope>
5151
</dependency>
52+
<dependency>
53+
<groupId>org.apache.commons</groupId>
54+
<artifactId>commons-lang3</artifactId>
55+
<version>3.10</version>
56+
<scope>test</scope>
57+
</dependency>
5258
</dependencies>
5359

5460
<developers>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2020 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser;
11+
12+
/**
13+
* <p>A marker interface for jsqlparser-model-classes.</p>
14+
* <p>The datastructure where the sql syntax is represented by a tree consists of {@link Model}'s</p>
15+
*/
16+
public interface Model {
17+
18+
}

src/main/java/net/sf/jsqlparser/expression/Alias.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
*/
1010
package net.sf.jsqlparser.expression;
1111

12+
import java.util.ArrayList;
13+
import java.util.Collection;
14+
import java.util.Collections;
1215
import java.util.List;
1316
import java.util.Objects;
17+
import java.util.Optional;
1418
import net.sf.jsqlparser.statement.create.table.ColDataType;
1519

1620
public class Alias {
@@ -73,6 +77,33 @@ public String toString() {
7377
return alias;
7478
}
7579

80+
public Alias withName(String name) {
81+
this.setName(name);
82+
return this;
83+
}
84+
85+
public Alias withUseAs(boolean useAs) {
86+
this.setUseAs(useAs);
87+
return this;
88+
}
89+
90+
public Alias withAliasColumns(List<AliasColumn> aliasColumns) {
91+
this.setAliasColumns(aliasColumns);
92+
return this;
93+
}
94+
95+
public Alias addAliasColumns(AliasColumn... aliasColumns) {
96+
List<AliasColumn> collection = Optional.ofNullable(getAliasColumns()).orElseGet(ArrayList::new);
97+
Collections.addAll(collection, aliasColumns);
98+
return this.withAliasColumns(collection);
99+
}
100+
101+
public Alias addAliasColumns(Collection<? extends AliasColumn> aliasColumns) {
102+
List<AliasColumn> collection = Optional.ofNullable(getAliasColumns()).orElseGet(ArrayList::new);
103+
collection.addAll(aliasColumns);
104+
return this.withAliasColumns(collection);
105+
}
106+
76107
public static class AliasColumn {
77108

78109
public final String name;

src/main/java/net/sf/jsqlparser/expression/AnalyticExpression.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,75 @@ public Expression getFilterExpression() {
237237
public void setFilterExpression(Expression filterExpression) {
238238
this.filterExpression = filterExpression;
239239
}
240+
241+
public AnalyticExpression withName(String name) {
242+
this.setName(name);
243+
return this;
244+
}
245+
246+
public AnalyticExpression withExpression(Expression expression) {
247+
this.setExpression(expression);
248+
return this;
249+
}
250+
251+
public AnalyticExpression withOffset(Expression offset) {
252+
this.setOffset(offset);
253+
return this;
254+
}
255+
256+
public AnalyticExpression withDefaultValue(Expression defaultValue) {
257+
this.setDefaultValue(defaultValue);
258+
return this;
259+
}
260+
261+
public AnalyticExpression withAllColumns(boolean allColumns) {
262+
this.setAllColumns(allColumns);
263+
return this;
264+
}
265+
266+
public AnalyticExpression withKeep(KeepExpression keep) {
267+
this.setKeep(keep);
268+
return this;
269+
}
270+
271+
public AnalyticExpression withType(AnalyticType type) {
272+
this.setType(type);
273+
return this;
274+
}
275+
276+
public AnalyticExpression withDistinct(boolean distinct) {
277+
this.setDistinct(distinct);
278+
return this;
279+
}
280+
281+
public AnalyticExpression withIgnoreNulls(boolean ignoreNulls) {
282+
this.setIgnoreNulls(ignoreNulls);
283+
return this;
284+
}
285+
286+
public AnalyticExpression withFilterExpression(Expression filterExpression) {
287+
this.setFilterExpression(filterExpression);
288+
return this;
289+
}
290+
291+
public AnalyticExpression withWindowElement(WindowElement windowElement) {
292+
this.setWindowElement(windowElement);
293+
return this;
294+
}
295+
296+
public <E extends Expression> E getExpression(Class<E> type) {
297+
return type.cast(getExpression());
298+
}
299+
300+
public <E extends Expression> E getOffset(Class<E> type) {
301+
return type.cast(getOffset());
302+
}
303+
304+
public <E extends Expression> E getDefaultValue(Class<E> type) {
305+
return type.cast(getDefaultValue());
306+
}
307+
308+
public <E extends Expression> E getFilterExpression(Class<E> type) {
309+
return type.cast(getFilterExpression());
310+
}
240311
}

src/main/java/net/sf/jsqlparser/expression/ArrayExpression.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public class ArrayExpression extends ASTNodeAccessImpl implements Expression {
1616
private Expression objExpression;
1717
private Expression indexExpression;
1818

19+
public ArrayExpression() {
20+
// empty constructor
21+
}
22+
1923
public ArrayExpression(Expression objExpression, Expression indexExpression) {
2024
this.objExpression = objExpression;
2125
this.indexExpression = indexExpression;
@@ -46,4 +50,22 @@ public void accept(ExpressionVisitor expressionVisitor) {
4650
public String toString() {
4751
return objExpression.toString() + "[" + indexExpression.toString() + "]";
4852
}
53+
54+
public ArrayExpression withObjExpression(Expression objExpression) {
55+
this.setObjExpression(objExpression);
56+
return this;
57+
}
58+
59+
public ArrayExpression withIndexExpression(Expression indexExpression) {
60+
this.setIndexExpression(indexExpression);
61+
return this;
62+
}
63+
64+
public <E extends Expression> E getObjExpression(Class<E> type) {
65+
return type.cast(getObjExpression());
66+
}
67+
68+
public <E extends Expression> E getIndexExpression(Class<E> type) {
69+
return type.cast(getIndexExpression());
70+
}
4971
}

src/main/java/net/sf/jsqlparser/expression/BinaryExpression.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public abstract class BinaryExpression extends ASTNodeAccessImpl implements Expr
1919

2020
private Expression leftExpression;
2121
private Expression rightExpression;
22-
// private boolean not = false;
22+
// private boolean not = false;
2323

2424
public BinaryExpression() {
2525
}
@@ -32,31 +32,48 @@ public Expression getRightExpression() {
3232
return rightExpression;
3333
}
3434

35+
public BinaryExpression withLeftExpression(Expression expression) {
36+
setLeftExpression(expression);
37+
return this;
38+
}
39+
3540
public void setLeftExpression(Expression expression) {
3641
leftExpression = expression;
3742
}
3843

44+
public BinaryExpression withRightExpression(Expression expression) {
45+
setRightExpression(expression);
46+
return this;
47+
}
48+
3949
public void setRightExpression(Expression expression) {
4050
rightExpression = expression;
4151
}
4252

43-
// public void setNot() {
44-
// not = true;
45-
// }
46-
//
47-
// public void removeNot() {
48-
// not = false;
49-
// }
50-
//
51-
// public boolean isNot() {
52-
// return not;
53-
// }
53+
// public void setNot() {
54+
// not = true;
55+
// }
56+
//
57+
// public void removeNot() {
58+
// not = false;
59+
// }
60+
//
61+
// public boolean isNot() {
62+
// return not;
63+
// }
5464
@Override
5565
public String toString() {
56-
return //(not ? "NOT " : "") +
66+
return // (not ? "NOT " : "") +
5767
getLeftExpression() + " " + getStringExpression() + " " + getRightExpression();
5868
}
5969

6070
public abstract String getStringExpression();
6171

72+
public <E extends Expression> E getLeftExpression(Class<E> type) {
73+
return type.cast(getLeftExpression());
74+
}
75+
76+
public <E extends Expression> E getRightExpression(Class<E> type) {
77+
return type.cast(getRightExpression());
78+
}
6279
}

src/main/java/net/sf/jsqlparser/expression/CaseExpression.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
*/
1010
package net.sf.jsqlparser.expression;
1111

12+
import java.util.ArrayList;
13+
import java.util.Collection;
14+
import java.util.Collections;
1215
import java.util.List;
13-
16+
import java.util.Optional;
1417
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
1518
import net.sf.jsqlparser.statement.select.PlainSelect;
1619

@@ -91,4 +94,39 @@ public String toString() {
9194
+ PlainSelect.getStringList(whenClauses, false, false) + " "
9295
+ ((elseExpression != null) ? "ELSE " + elseExpression + " " : "") + "END";
9396
}
97+
98+
public CaseExpression withSwitchExpression(Expression switchExpression) {
99+
this.setSwitchExpression(switchExpression);
100+
return this;
101+
}
102+
103+
public CaseExpression withWhenClauses(List<WhenClause> whenClauses) {
104+
this.setWhenClauses(whenClauses);
105+
return this;
106+
}
107+
108+
public CaseExpression withElseExpression(Expression elseExpression) {
109+
this.setElseExpression(elseExpression);
110+
return this;
111+
}
112+
113+
public CaseExpression addWhenClauses(WhenClause... whenClauses) {
114+
List<WhenClause> collection = Optional.ofNullable(getWhenClauses()).orElseGet(ArrayList::new);
115+
Collections.addAll(collection, whenClauses);
116+
return this.withWhenClauses(collection);
117+
}
118+
119+
public CaseExpression addWhenClauses(Collection<? extends WhenClause> whenClauses) {
120+
List<WhenClause> collection = Optional.ofNullable(getWhenClauses()).orElseGet(ArrayList::new);
121+
collection.addAll(whenClauses);
122+
return this.withWhenClauses(collection);
123+
}
124+
125+
public <E extends Expression> E getSwitchExpression(Class<E> type) {
126+
return type.cast(getSwitchExpression());
127+
}
128+
129+
public <E extends Expression> E getElseExpression(Class<E> type) {
130+
return type.cast(getElseExpression());
131+
}
94132
}

src/main/java/net/sf/jsqlparser/expression/CastExpression.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,23 @@ public String toString() {
5555
return leftExpression + "::" + type.toString();
5656
}
5757
}
58+
59+
public CastExpression withType(ColDataType type) {
60+
this.setType(type);
61+
return this;
62+
}
63+
64+
public CastExpression withUseCastKeyword(boolean useCastKeyword) {
65+
this.setUseCastKeyword(useCastKeyword);
66+
return this;
67+
}
68+
69+
public CastExpression withLeftExpression(Expression leftExpression) {
70+
this.setLeftExpression(leftExpression);
71+
return this;
72+
}
73+
74+
public <E extends Expression> E getLeftExpression(Class<E> type) {
75+
return type.cast(getLeftExpression());
76+
}
5877
}

src/main/java/net/sf/jsqlparser/expression/CollateExpression.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public class CollateExpression extends ASTNodeAccessImpl implements Expression {
1616
private Expression leftExpression;
1717
private String collate;
1818

19+
public CollateExpression() {
20+
// empty constructor
21+
}
22+
1923
public CollateExpression(Expression leftExpression, String collate) {
2024
this.leftExpression = leftExpression;
2125
this.collate = collate;
@@ -46,4 +50,18 @@ public void setCollate(String collate) {
4650
public String toString() {
4751
return leftExpression.toString() + " COLLATE " + collate;
4852
}
53+
54+
public CollateExpression withLeftExpression(Expression leftExpression) {
55+
this.setLeftExpression(leftExpression);
56+
return this;
57+
}
58+
59+
public CollateExpression withCollate(String collate) {
60+
this.setCollate(collate);
61+
return this;
62+
}
63+
64+
public <E extends Expression> E getLeftExpression(Class<E> type) {
65+
return type.cast(getLeftExpression());
66+
}
4967
}

src/main/java/net/sf/jsqlparser/expression/DateTimeLiteralExpression.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ public String toString() {
4242
return type.name() + " " + value;
4343
}
4444

45+
public DateTimeLiteralExpression withValue(String value) {
46+
this.setValue(value);
47+
return this;
48+
}
49+
50+
public DateTimeLiteralExpression withType(DateTime type) {
51+
this.setType(type);
52+
return this;
53+
}
54+
4555
public static enum DateTime {
4656
DATE, TIME, TIMESTAMP;
4757
}

0 commit comments

Comments
 (0)