Skip to content

Commit

Permalink
Fluent builder api #1004 (#1014)
Browse files Browse the repository at this point in the history
* #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
  • Loading branch information
gitmotte authored Aug 23, 2020
1 parent f88d93f commit 6cff161
Show file tree
Hide file tree
Showing 208 changed files with 6,787 additions and 893 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
<version>3.16.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
<scope>test</scope>
</dependency>
</dependencies>

<developers>
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/net/sf/jsqlparser/Model.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2020 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser;

/**
* <p>A marker interface for jsqlparser-model-classes.</p>
* <p>The datastructure where the sql syntax is represented by a tree consists of {@link Model}'s</p>
*/
public interface Model {

}
31 changes: 31 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/Alias.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
*/
package net.sf.jsqlparser.expression;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import net.sf.jsqlparser.statement.create.table.ColDataType;

public class Alias {
Expand Down Expand Up @@ -73,6 +77,33 @@ public String toString() {
return alias;
}

public Alias withName(String name) {
this.setName(name);
return this;
}

public Alias withUseAs(boolean useAs) {
this.setUseAs(useAs);
return this;
}

public Alias withAliasColumns(List<AliasColumn> aliasColumns) {
this.setAliasColumns(aliasColumns);
return this;
}

public Alias addAliasColumns(AliasColumn... aliasColumns) {
List<AliasColumn> collection = Optional.ofNullable(getAliasColumns()).orElseGet(ArrayList::new);
Collections.addAll(collection, aliasColumns);
return this.withAliasColumns(collection);
}

public Alias addAliasColumns(Collection<? extends AliasColumn> aliasColumns) {
List<AliasColumn> collection = Optional.ofNullable(getAliasColumns()).orElseGet(ArrayList::new);
collection.addAll(aliasColumns);
return this.withAliasColumns(collection);
}

public static class AliasColumn {

public final String name;
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/AnalyticExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,75 @@ public Expression getFilterExpression() {
public void setFilterExpression(Expression filterExpression) {
this.filterExpression = filterExpression;
}

public AnalyticExpression withName(String name) {
this.setName(name);
return this;
}

public AnalyticExpression withExpression(Expression expression) {
this.setExpression(expression);
return this;
}

public AnalyticExpression withOffset(Expression offset) {
this.setOffset(offset);
return this;
}

public AnalyticExpression withDefaultValue(Expression defaultValue) {
this.setDefaultValue(defaultValue);
return this;
}

public AnalyticExpression withAllColumns(boolean allColumns) {
this.setAllColumns(allColumns);
return this;
}

public AnalyticExpression withKeep(KeepExpression keep) {
this.setKeep(keep);
return this;
}

public AnalyticExpression withType(AnalyticType type) {
this.setType(type);
return this;
}

public AnalyticExpression withDistinct(boolean distinct) {
this.setDistinct(distinct);
return this;
}

public AnalyticExpression withIgnoreNulls(boolean ignoreNulls) {
this.setIgnoreNulls(ignoreNulls);
return this;
}

public AnalyticExpression withFilterExpression(Expression filterExpression) {
this.setFilterExpression(filterExpression);
return this;
}

public AnalyticExpression withWindowElement(WindowElement windowElement) {
this.setWindowElement(windowElement);
return this;
}

public <E extends Expression> E getExpression(Class<E> type) {
return type.cast(getExpression());
}

public <E extends Expression> E getOffset(Class<E> type) {
return type.cast(getOffset());
}

public <E extends Expression> E getDefaultValue(Class<E> type) {
return type.cast(getDefaultValue());
}

public <E extends Expression> E getFilterExpression(Class<E> type) {
return type.cast(getFilterExpression());
}
}
22 changes: 22 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/ArrayExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class ArrayExpression extends ASTNodeAccessImpl implements Expression {
private Expression objExpression;
private Expression indexExpression;

public ArrayExpression() {
// empty constructor
}

public ArrayExpression(Expression objExpression, Expression indexExpression) {
this.objExpression = objExpression;
this.indexExpression = indexExpression;
Expand Down Expand Up @@ -46,4 +50,22 @@ public void accept(ExpressionVisitor expressionVisitor) {
public String toString() {
return objExpression.toString() + "[" + indexExpression.toString() + "]";
}

public ArrayExpression withObjExpression(Expression objExpression) {
this.setObjExpression(objExpression);
return this;
}

public ArrayExpression withIndexExpression(Expression indexExpression) {
this.setIndexExpression(indexExpression);
return this;
}

public <E extends Expression> E getObjExpression(Class<E> type) {
return type.cast(getObjExpression());
}

public <E extends Expression> E getIndexExpression(Class<E> type) {
return type.cast(getIndexExpression());
}
}
43 changes: 30 additions & 13 deletions src/main/java/net/sf/jsqlparser/expression/BinaryExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class BinaryExpression extends ASTNodeAccessImpl implements Expr

private Expression leftExpression;
private Expression rightExpression;
// private boolean not = false;
// private boolean not = false;

public BinaryExpression() {
}
Expand All @@ -32,31 +32,48 @@ public Expression getRightExpression() {
return rightExpression;
}

public BinaryExpression withLeftExpression(Expression expression) {
setLeftExpression(expression);
return this;
}

public void setLeftExpression(Expression expression) {
leftExpression = expression;
}

public BinaryExpression withRightExpression(Expression expression) {
setRightExpression(expression);
return this;
}

public void setRightExpression(Expression expression) {
rightExpression = expression;
}

// public void setNot() {
// not = true;
// }
//
// public void removeNot() {
// not = false;
// }
//
// public boolean isNot() {
// return not;
// }
// public void setNot() {
// not = true;
// }
//
// public void removeNot() {
// not = false;
// }
//
// public boolean isNot() {
// return not;
// }
@Override
public String toString() {
return //(not ? "NOT " : "") +
return // (not ? "NOT " : "") +
getLeftExpression() + " " + getStringExpression() + " " + getRightExpression();
}

public abstract String getStringExpression();

public <E extends Expression> E getLeftExpression(Class<E> type) {
return type.cast(getLeftExpression());
}

public <E extends Expression> E getRightExpression(Class<E> type) {
return type.cast(getRightExpression());
}
}
40 changes: 39 additions & 1 deletion src/main/java/net/sf/jsqlparser/expression/CaseExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
*/
package net.sf.jsqlparser.expression;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import java.util.Optional;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
import net.sf.jsqlparser.statement.select.PlainSelect;

Expand Down Expand Up @@ -91,4 +94,39 @@ public String toString() {
+ PlainSelect.getStringList(whenClauses, false, false) + " "
+ ((elseExpression != null) ? "ELSE " + elseExpression + " " : "") + "END";
}

public CaseExpression withSwitchExpression(Expression switchExpression) {
this.setSwitchExpression(switchExpression);
return this;
}

public CaseExpression withWhenClauses(List<WhenClause> whenClauses) {
this.setWhenClauses(whenClauses);
return this;
}

public CaseExpression withElseExpression(Expression elseExpression) {
this.setElseExpression(elseExpression);
return this;
}

public CaseExpression addWhenClauses(WhenClause... whenClauses) {
List<WhenClause> collection = Optional.ofNullable(getWhenClauses()).orElseGet(ArrayList::new);
Collections.addAll(collection, whenClauses);
return this.withWhenClauses(collection);
}

public CaseExpression addWhenClauses(Collection<? extends WhenClause> whenClauses) {
List<WhenClause> collection = Optional.ofNullable(getWhenClauses()).orElseGet(ArrayList::new);
collection.addAll(whenClauses);
return this.withWhenClauses(collection);
}

public <E extends Expression> E getSwitchExpression(Class<E> type) {
return type.cast(getSwitchExpression());
}

public <E extends Expression> E getElseExpression(Class<E> type) {
return type.cast(getElseExpression());
}
}
19 changes: 19 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/CastExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,23 @@ public String toString() {
return leftExpression + "::" + type.toString();
}
}

public CastExpression withType(ColDataType type) {
this.setType(type);
return this;
}

public CastExpression withUseCastKeyword(boolean useCastKeyword) {
this.setUseCastKeyword(useCastKeyword);
return this;
}

public CastExpression withLeftExpression(Expression leftExpression) {
this.setLeftExpression(leftExpression);
return this;
}

public <E extends Expression> E getLeftExpression(Class<E> type) {
return type.cast(getLeftExpression());
}
}
18 changes: 18 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/CollateExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class CollateExpression extends ASTNodeAccessImpl implements Expression {
private Expression leftExpression;
private String collate;

public CollateExpression() {
// empty constructor
}

public CollateExpression(Expression leftExpression, String collate) {
this.leftExpression = leftExpression;
this.collate = collate;
Expand Down Expand Up @@ -46,4 +50,18 @@ public void setCollate(String collate) {
public String toString() {
return leftExpression.toString() + " COLLATE " + collate;
}

public CollateExpression withLeftExpression(Expression leftExpression) {
this.setLeftExpression(leftExpression);
return this;
}

public CollateExpression withCollate(String collate) {
this.setCollate(collate);
return this;
}

public <E extends Expression> E getLeftExpression(Class<E> type) {
return type.cast(getLeftExpression());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public String toString() {
return type.name() + " " + value;
}

public DateTimeLiteralExpression withValue(String value) {
this.setValue(value);
return this;
}

public DateTimeLiteralExpression withType(DateTime type) {
this.setType(type);
return this;
}

public static enum DateTime {
DATE, TIME, TIMESTAMP;
}
Expand Down
Loading

0 comments on commit 6cff161

Please sign in to comment.