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

Refactor implementation smells #1982

Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
124 changes: 68 additions & 56 deletions src/main/java/net/sf/jsqlparser/expression/JsonAggregateFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@

public class JsonAggregateFunction extends FilterOverImpl implements Expression {
private JsonFunctionType functionType;

private Expression expression = null;
private final OrderByClause expressionOrderBy = new OrderByClause();

private boolean usingKeyKeyword = false;
private String key;
private boolean usingValueKeyword = false;
private Object value;

private boolean usingFormatJson = false;

private JsonAggregateOnNullType onNullType;
private JsonAggregateUniqueKeysType uniqueKeysType;


public JsonAggregateOnNullType getOnNullType() {
return onNullType;
Expand All @@ -42,7 +42,7 @@ public JsonAggregateOnNullType getOnNullType() {
public void setOnNullType(JsonAggregateOnNullType onNullType) {
this.onNullType = onNullType;
}

public JsonAggregateFunction withOnNullType(JsonAggregateOnNullType onNullType) {
this.setOnNullType(onNullType);
return this;
Expand All @@ -55,7 +55,7 @@ public JsonAggregateUniqueKeysType getUniqueKeysType() {
public void setUniqueKeysType(JsonAggregateUniqueKeysType uniqueKeysType) {
this.uniqueKeysType = uniqueKeysType;
}

public JsonAggregateFunction withUniqueKeysType(JsonAggregateUniqueKeysType uniqueKeysType) {
this.setUniqueKeysType(uniqueKeysType);
return this;
Expand All @@ -64,21 +64,25 @@ public JsonAggregateFunction withUniqueKeysType(JsonAggregateUniqueKeysType uniq
public JsonFunctionType getType() {
return functionType;
}

public void setType(JsonFunctionType type) {
this.functionType = Objects.requireNonNull(type, "The Type of the JSON Aggregate Function must not be null");
this.functionType = Objects.requireNonNull(type,
"The Type of the JSON Aggregate Function must not be null");
}

public JsonAggregateFunction withType(JsonFunctionType type) {
this.setType(type);
return this;
}

public void setType(String typeName) {
this.functionType = JsonFunctionType
.valueOf( Objects.requireNonNull(typeName, "The Type of the JSON Aggregate Function must not be null").toUpperCase());
.valueOf(Objects
.requireNonNull(typeName,
"The Type of the JSON Aggregate Function must not be null")
.toUpperCase());
}

public JsonAggregateFunction withType(String typeName) {
this.setType(typeName);
return this;
Expand All @@ -91,7 +95,7 @@ public Expression getExpression() {
public void setExpression(Expression expression) {
this.expression = expression;
}

public JsonAggregateFunction withExpression(Expression expression) {
this.setExpression(expression);
return this;
Expand All @@ -104,7 +108,7 @@ public boolean isUsingKeyKeyword() {
public void setUsingKeyKeyword(boolean usingKeyKeyword) {
this.usingKeyKeyword = usingKeyKeyword;
}

public JsonAggregateFunction withUsingKeyKeyword(boolean usingKeyKeyword) {
this.setUsingKeyKeyword(usingKeyKeyword);
return this;
Expand All @@ -117,7 +121,7 @@ public String getKey() {
public void setKey(String key) {
this.key = key;
}

public JsonAggregateFunction withKey(String key) {
this.setKey(key);
return this;
Expand All @@ -130,7 +134,7 @@ public boolean isUsingValueKeyword() {
public void setUsingValueKeyword(boolean usingValueKeyword) {
this.usingValueKeyword = usingValueKeyword;
}

public JsonAggregateFunction withUsingValueKeyword(boolean usingValueKeyword) {
this.setUsingValueKeyword(usingValueKeyword);
return this;
Expand All @@ -143,34 +147,35 @@ public Object getValue() {
public void setValue(Object value) {
this.value = value;
}

public JsonAggregateFunction withValue(Object value) {
this.setValue(value);
return this;
}

public boolean isUsingFormatJson() {
return usingFormatJson;
}

public void setUsingFormatJson(boolean usingFormatJson) {
this.usingFormatJson = usingFormatJson;
}

public JsonAggregateFunction withUsingFormatJson(boolean usingFormatJson) {
this.setUsingFormatJson(usingFormatJson);
return this;
}

public List<OrderByElement> getExpressionOrderByElements() {
return expressionOrderBy.getOrderByElements();
}

public void setExpressionOrderByElements(List<OrderByElement> orderByElements) {
expressionOrderBy.setOrderByElements(orderByElements);
}

public JsonAggregateFunction withExpressionOrderByElements(List<OrderByElement> orderByElements) {

public JsonAggregateFunction withExpressionOrderByElements(
List<OrderByElement> orderByElements) {
this.setExpressionOrderByElements(orderByElements);
return this;
}
Expand All @@ -179,7 +184,7 @@ public JsonAggregateFunction withExpressionOrderByElements(List<OrderByElement>
public void accept(ExpressionVisitor expressionVisitor) {
expressionVisitor.visit(this);
}

// avoid countless Builder --> String conversion
@Override
public StringBuilder append(StringBuilder builder) {
Expand All @@ -192,28 +197,26 @@ public StringBuilder append(StringBuilder builder) {
break;
default:
// this should never happen really
throw new UnsupportedOperationException("JSON Aggregate Function of the type " + functionType.name() + " has not been implemented yet.");
throw new UnsupportedOperationException("JSON Aggregate Function of the type "
+ functionType.name() + " has not been implemented yet.");
}
return builder;
}

@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})

public StringBuilder appendObject(StringBuilder builder) {
builder.append("JSON_OBJECTAGG( ");
if (usingValueKeyword) {
if (usingKeyKeyword) {
builder.append("KEY ");
}
builder.append(key).append(" VALUE ").append(value);
} else {
builder.append(key).append(":").append(value);
}

keywordAppender(builder);
if (usingFormatJson) {
builder.append(" FORMAT JSON");
}

if (onNullType!=null) {
nullTypeAppender(builder);
keysAppender(builder);
super.append(builder);
return builder;
}

public void nullTypeAppender(StringBuilder builder) {
if (onNullType != null) {
switch (onNullType) {
case NULL:
builder.append(" NULL ON NULL");
Expand All @@ -225,8 +228,23 @@ public StringBuilder appendObject(StringBuilder builder) {
// this should never happen
}
}

if (uniqueKeysType!=null) {
}

public void keywordAppender(StringBuilder builder) {
if (usingValueKeyword) {
if (usingKeyKeyword) {
builder.append("KEY ");
}
builder.append(key).append(" VALUE ").append(value);
} else {
builder.append(key).append(":").append(value);
}

}

public void keysAppender(StringBuilder builder) {

if (uniqueKeysType != null) {
switch (uniqueKeysType) {
case WITH:
builder.append(" WITH UNIQUE KEYS");
Expand All @@ -238,28 +256,22 @@ public StringBuilder appendObject(StringBuilder builder) {
// this should never happen
}
}

builder.append(" ) ");


// FILTER( WHERE expression ) OVER windowNameOrSpecification
super.append(builder);

return builder;
}

@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
public StringBuilder appendArray(StringBuilder builder) {
builder.append("JSON_ARRAYAGG( ");
builder.append(expression).append(" ");

if (usingFormatJson) {
builder.append("FORMAT JSON ");
}

expressionOrderBy.toStringOrderByElements(builder);
if (onNullType!=null) {

if (onNullType != null) {
switch (onNullType) {
case NULL:
builder.append(" NULL ON NULL ");
Expand All @@ -272,17 +284,17 @@ public StringBuilder appendArray(StringBuilder builder) {
}
}
builder.append(") ");


// FILTER( WHERE expression ) OVER windowNameOrSpecification
super.append(builder);

return builder;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
return append(builder).toString();
StringBuilder builder = new StringBuilder();
return append(builder).toString();
}
}