Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fluent builder api #1004 #1014

Merged
merged 67 commits into from
Aug 23, 2020
Merged
Show file tree
Hide file tree
Changes from 65 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
7e2a670
https://github.com/JSQLParser/JSqlParser/issues/1004
gitmotte Jun 29, 2020
8e71f9b
1004 add chaining - methods returning "this"
gitmotte Jun 30, 2020
aeb1502
#1004 add chaining - methods returning "this"
gitmotte Jul 1, 2020
274ef21
Merge branch 'master.fluent' of https://github.com/gitmotte/JSqlParse…
gitmotte Jul 1, 2020
f38084c
gitmotte Jul 1, 2020
a6ae42f
* add<Name> methods on collection-fields with varargs-parameter
gitmotte Jul 2, 2020
88a5e4f
Merge branch 'master.fluent' of https://github.com/gitmotte/JSqlParse…
gitmotte Jul 3, 2020
f0b06e8
* add chaining - methods returning "this"
gitmotte Jul 3, 2020
74a0eb6
* add public T get<Name>(Class<T>) - casting and returning the concrete
gitmotte Jul 3, 2020
8a53418
* add public T get<Name>(Class<T>) - casting and returning the concrete
gitmotte Jul 3, 2020
812b6ab
* overwrite chaining - methods of abstract parents/interfaces for
gitmotte Jul 4, 2020
4b3a1a4
Merge branch 'master.fluent' of https://github.com/gitmotte/JSqlParse…
gitmotte Jul 6, 2020
34db4f5
* add with prefix for fluent setters.
gitmotte Jul 6, 2020
a2600c7
add getters
gitmotte Jul 6, 2020
bb2b15f
* add with prefix for fluent setters. (revert to chaining setters, do
gitmotte Jul 6, 2020
8b6fd6b
* add with prefix for fluent setters. (revert to chaining setters, do
gitmotte Jul 6, 2020
0880fde
use new methods within testcases
gitmotte Jul 6, 2020
63679e0
use new methods within testcases
gitmotte Jul 6, 2020
1d0a24d
use new methods within testcases
gitmotte Jul 6, 2020
fda2f67
use new methods within testcases
gitmotte Jul 6, 2020
8d2cde4
use new methods within testcases
gitmotte Jul 6, 2020
1027829
use new methods within testcases
gitmotte Jul 6, 2020
8de6c09
use new methods within testcases
gitmotte Jul 6, 2020
a0cc175
use new methods within testcases
gitmotte Jul 7, 2020
dd60c2e
Merge branch 'master.fluent' of
gitmotte Jul 7, 2020
ea4356a
remove create() methods - they do not add enough value to be justified
gitmotte Jul 7, 2020
8ece698
* use new methods within testcases
gitmotte Jul 7, 2020
77d92c0
* use new methods within testcases
gitmotte Jul 7, 2020
0c99e8c
* use new methods within testcases
gitmotte Jul 7, 2020
89fe5f4
* renamed constant
gitmotte Jul 7, 2020
4d94a4d
use new methods within testcases
gitmotte Jul 7, 2020
d2cec9d
use new methods within testcases
gitmotte Jul 7, 2020
0554831
use new methods within testcases
gitmotte Jul 7, 2020
b55b988
use new methods within testcases
gitmotte Jul 7, 2020
8b68980
Merge branch 'master.fluent' of https://github.com/gitmotte/JSqlParse…
gitmotte Jul 8, 2020
5f10ac4
* use new methods within testcases
gitmotte Jul 8, 2020
a7a0b70
* use new methods within testcases
gitmotte Jul 8, 2020
77cf290
remove empty implicit constructor
gitmotte Jul 8, 2020
09691fe
return the deparsed Statement - object
gitmotte Jul 8, 2020
d0c12d6
compare object tree
gitmotte Jul 8, 2020
06684c1
compare object tree
gitmotte Jul 8, 2020
2babbd7
* fix ObjectTreeToStringStyle
gitmotte Jul 8, 2020
c0dddbf
remove casts not needed
gitmotte Jul 8, 2020
cb2d078
* use new methods within testcases
gitmotte Jul 8, 2020
48d859c
* use new methods within testcases
gitmotte Jul 8, 2020
6267772
add empty constructors and override with-/add-methods returning concrete
gitmotte Jul 9, 2020
bfa2e20
* add ReflectionModelTest
gitmotte Jul 9, 2020
b80b2d5
* use new methods within testcases
gitmotte Jul 9, 2020
f1dfc4f
fix checkstyle errors
gitmotte Jul 9, 2020
e383883
license header
gitmotte Jul 9, 2020
bc8c2a4
Merge branch 'master.fluent' of https://github.com/gitmotte/JSqlParse…
gitmotte Jul 10, 2020
41568fa
remove test-classes from ReflectionModelTest
gitmotte Jul 10, 2020
94adc21
remove visitoradapter-classes from ReflectionModelTest
gitmotte Jul 10, 2020
4fc8b27
Merge branch 'master' into master.fluent
gitmotte Jul 11, 2020
8c5322e
Merge branch 'master.fluent' of https://github.com/gitmotte/JSqlParse…
gitmotte Jul 13, 2020
ce769d1
Merge branch 'master' into master.fluent
gitmotte Jul 14, 2020
73b338b
Merge branch 'master.fluent' of https://github.com/gitmotte/JSqlParse…
gitmotte Jul 15, 2020
d8afd17
remove duplicate import declaration (checkstyle error)
gitmotte Jul 15, 2020
4f2443c
* fix RandomUtils to support used java.sql.* types
gitmotte Jul 15, 2020
9a9422f
add javadoc, stating that this is a marker interface
gitmotte Jul 15, 2020
db3b4ae
revert formatting change
gitmotte Jul 15, 2020
d77f3af
change to EXEC_TYPE.EXECUTE just so the assertion didn't change
gitmotte Jul 15, 2020
324ccf1
try to revert format changes
gitmotte Jul 15, 2020
0d434b8
try to revert format changes
gitmotte Jul 15, 2020
9865783
remove brackets on @Override() -> @Override
gitmotte Jul 15, 2020
f7ec34e
Merge branch 'master' into master.fluent
gitmotte Aug 10, 2020
f3d113d
add with-methods to new fields
gitmotte Aug 17, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add a bit of doc that this is a marker interface for anything that is a certain type (in order to be able to make a limiting parameter that isn't Object). (Or something of that nature), just seeing a Model type with no methods might not explain our intent with what a Model is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should rename it to SqlModel?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SqlModel would be a good name. That doc is good tho!

Copy link
Contributor Author

@gitmotte gitmotte Jul 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know if we need this kind of marker, but it's sometimes helpful to group things into a type hierarchy, which shares a common usage or programming style (with-/add methods, casting getters) to help others on implementation new model types

The first thought was to add a type variable to the model-type which represents the real model-type, but it makes the use complicated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, a marker works :)


}
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