Skip to content

Refactor to make the code more readable and testable #629

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

Merged
merged 2 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
47 changes: 10 additions & 37 deletions src/main/java/net/sf/jsqlparser/expression/AnalyticExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import java.util.List;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.statement.select.PlainSelect;

/**
* Analytic function. The name of the function is variable but the parameters following the special
Expand All @@ -38,14 +37,13 @@
*/
public class AnalyticExpression extends ASTNodeAccessImpl implements Expression {

private ExpressionList partitionExpressionList;
private List<OrderByElement> orderByElements;
private final OrderByClause orderBy = new OrderByClause();
private final PartitionByClause partitionBy = new PartitionByClause();
private String name;
private Expression expression;
private Expression offset;
private Expression defaultValue;
private boolean allColumns = false;
private WindowElement windowElement;
private KeepExpression keep = null;
private AnalyticType type = AnalyticType.OVER;
private boolean distinct = false;
Expand All @@ -56,11 +54,11 @@ public void accept(ExpressionVisitor expressionVisitor) {
}

public List<OrderByElement> getOrderByElements() {
return orderByElements;
return orderBy.getOrderByElements();
}

public void setOrderByElements(List<OrderByElement> orderByElements) {
this.orderByElements = orderByElements;
orderBy.setOrderByElements(orderByElements);
}

public KeepExpression getKeep() {
Expand All @@ -72,11 +70,11 @@ public void setKeep(KeepExpression keep) {
}

public ExpressionList getPartitionExpressionList() {
return partitionExpressionList;
return partitionBy.getPartitionExpressionList();
}

public void setPartitionExpressionList(ExpressionList partitionExpressionList) {
this.partitionExpressionList = partitionExpressionList;
partitionBy.setPartitionExpressionList(partitionExpressionList);
}

public String getName() {
Expand Down Expand Up @@ -112,11 +110,11 @@ public void setDefaultValue(Expression defaultValue) {
}

public WindowElement getWindowElement() {
return windowElement;
return orderBy.getWindowElement();
}

public void setWindowElement(WindowElement windowElement) {
this.windowElement = windowElement;
orderBy.setWindowElement(windowElement);
}

public AnalyticType getType() {
Expand Down Expand Up @@ -168,8 +166,8 @@ public String toString() {
}
b.append(" (");

toStringPartitionBy(b);
toStringOrderByElements(b);
partitionBy.toStringPartitionBy(b);
orderBy.toStringOrderByElements(b);

b.append(")");

Expand All @@ -184,29 +182,4 @@ public void setAllColumns(boolean allColumns) {
this.allColumns = allColumns;
}

private void toStringPartitionBy(StringBuilder b) {
if (partitionExpressionList != null && !partitionExpressionList.getExpressions().isEmpty()) {
b.append("PARTITION BY ");
b.append(PlainSelect.
getStringList(partitionExpressionList.getExpressions(), true, false));
b.append(" ");
}
}

private void toStringOrderByElements(StringBuilder b) {
if (orderByElements != null && !orderByElements.isEmpty()) {
b.append("ORDER BY ");
for (int i = 0; i < orderByElements.size(); i++) {
if (i > 0) {
b.append(", ");
}
b.append(orderByElements.get(i).toString());
}

if (windowElement != null) {
b.append(' ');
b.append(windowElement);
}
}
}
}
64 changes: 64 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/OrderByClause.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2018 JSQLParser
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package net.sf.jsqlparser.expression;

import net.sf.jsqlparser.statement.select.OrderByElement;

import java.util.List;

public class OrderByClause {
private List<OrderByElement> orderByElements;
private WindowElement windowElement;

public List<OrderByElement> getOrderByElements() {
return orderByElements;
}

public void setOrderByElements(List<OrderByElement> orderByElements) {
this.orderByElements = orderByElements;
}

public WindowElement getWindowElement() {
return windowElement;
}

public void setWindowElement(WindowElement windowElement) {
this.windowElement = windowElement;
}

void toStringOrderByElements(StringBuilder b) {
if (orderByElements != null && !orderByElements.isEmpty()) {
b.append("ORDER BY ");
for (int i = 0; i < orderByElements.size(); i++) {
if (i > 0) {
b.append(", ");
}
b.append(orderByElements.get(i).toString());
}

if (windowElement != null) {
b.append(' ');
b.append(windowElement);
}
}
}
}
46 changes: 46 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/PartitionByClause.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2018 JSQLParser
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package net.sf.jsqlparser.expression;

import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.statement.select.PlainSelect;

public class PartitionByClause {
ExpressionList partitionExpressionList;

public ExpressionList getPartitionExpressionList() {
return partitionExpressionList;
}

public void setPartitionExpressionList(ExpressionList partitionExpressionList) {
this.partitionExpressionList = partitionExpressionList;
}

void toStringPartitionBy(StringBuilder b) {
if (partitionExpressionList != null && !partitionExpressionList.getExpressions().isEmpty()) {
b.append("PARTITION BY ");
b.append(PlainSelect.
getStringList(partitionExpressionList.getExpressions(), true, false));
b.append(" ");
}
}
}