Skip to content

Commit ffbf96e

Browse files
authored
Merge pull request #629 from A4Vision/extractclass/AnalyticExpression
Refactor to make the code more readable and testable
2 parents aff6516 + e3515d1 commit ffbf96e

File tree

3 files changed

+120
-37
lines changed

3 files changed

+120
-37
lines changed

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

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import java.util.List;
2828
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
29-
import net.sf.jsqlparser.statement.select.PlainSelect;
3029

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

41-
private ExpressionList partitionExpressionList;
42-
private List<OrderByElement> orderByElements;
40+
private final OrderByClause orderBy = new OrderByClause();
41+
private final PartitionByClause partitionBy = new PartitionByClause();
4342
private String name;
4443
private Expression expression;
4544
private Expression offset;
4645
private Expression defaultValue;
4746
private boolean allColumns = false;
48-
private WindowElement windowElement;
4947
private KeepExpression keep = null;
5048
private AnalyticType type = AnalyticType.OVER;
5149
private boolean distinct = false;
@@ -56,11 +54,11 @@ public void accept(ExpressionVisitor expressionVisitor) {
5654
}
5755

5856
public List<OrderByElement> getOrderByElements() {
59-
return orderByElements;
57+
return orderBy.getOrderByElements();
6058
}
6159

6260
public void setOrderByElements(List<OrderByElement> orderByElements) {
63-
this.orderByElements = orderByElements;
61+
orderBy.setOrderByElements(orderByElements);
6462
}
6563

6664
public KeepExpression getKeep() {
@@ -72,11 +70,11 @@ public void setKeep(KeepExpression keep) {
7270
}
7371

7472
public ExpressionList getPartitionExpressionList() {
75-
return partitionExpressionList;
73+
return partitionBy.getPartitionExpressionList();
7674
}
7775

7876
public void setPartitionExpressionList(ExpressionList partitionExpressionList) {
79-
this.partitionExpressionList = partitionExpressionList;
77+
partitionBy.setPartitionExpressionList(partitionExpressionList);
8078
}
8179

8280
public String getName() {
@@ -112,11 +110,11 @@ public void setDefaultValue(Expression defaultValue) {
112110
}
113111

114112
public WindowElement getWindowElement() {
115-
return windowElement;
113+
return orderBy.getWindowElement();
116114
}
117115

118116
public void setWindowElement(WindowElement windowElement) {
119-
this.windowElement = windowElement;
117+
orderBy.setWindowElement(windowElement);
120118
}
121119

122120
public AnalyticType getType() {
@@ -168,8 +166,8 @@ public String toString() {
168166
}
169167
b.append(" (");
170168

171-
toStringPartitionBy(b);
172-
toStringOrderByElements(b);
169+
partitionBy.toStringPartitionBy(b);
170+
orderBy.toStringOrderByElements(b);
173171

174172
b.append(")");
175173

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

187-
private void toStringPartitionBy(StringBuilder b) {
188-
if (partitionExpressionList != null && !partitionExpressionList.getExpressions().isEmpty()) {
189-
b.append("PARTITION BY ");
190-
b.append(PlainSelect.
191-
getStringList(partitionExpressionList.getExpressions(), true, false));
192-
b.append(" ");
193-
}
194-
}
195-
196-
private void toStringOrderByElements(StringBuilder b) {
197-
if (orderByElements != null && !orderByElements.isEmpty()) {
198-
b.append("ORDER BY ");
199-
for (int i = 0; i < orderByElements.size(); i++) {
200-
if (i > 0) {
201-
b.append(", ");
202-
}
203-
b.append(orderByElements.get(i).toString());
204-
}
205-
206-
if (windowElement != null) {
207-
b.append(' ');
208-
b.append(windowElement);
209-
}
210-
}
211-
}
212185
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2018 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
package net.sf.jsqlparser.expression;
23+
24+
import net.sf.jsqlparser.statement.select.OrderByElement;
25+
26+
import java.util.List;
27+
28+
public class OrderByClause {
29+
private List<OrderByElement> orderByElements;
30+
private WindowElement windowElement;
31+
32+
public List<OrderByElement> getOrderByElements() {
33+
return orderByElements;
34+
}
35+
36+
public void setOrderByElements(List<OrderByElement> orderByElements) {
37+
this.orderByElements = orderByElements;
38+
}
39+
40+
public WindowElement getWindowElement() {
41+
return windowElement;
42+
}
43+
44+
public void setWindowElement(WindowElement windowElement) {
45+
this.windowElement = windowElement;
46+
}
47+
48+
void toStringOrderByElements(StringBuilder b) {
49+
if (orderByElements != null && !orderByElements.isEmpty()) {
50+
b.append("ORDER BY ");
51+
for (int i = 0; i < orderByElements.size(); i++) {
52+
if (i > 0) {
53+
b.append(", ");
54+
}
55+
b.append(orderByElements.get(i).toString());
56+
}
57+
58+
if (windowElement != null) {
59+
b.append(' ');
60+
b.append(windowElement);
61+
}
62+
}
63+
}
64+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2018 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
package net.sf.jsqlparser.expression;
23+
24+
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
25+
import net.sf.jsqlparser.statement.select.PlainSelect;
26+
27+
public class PartitionByClause {
28+
ExpressionList partitionExpressionList;
29+
30+
public ExpressionList getPartitionExpressionList() {
31+
return partitionExpressionList;
32+
}
33+
34+
public void setPartitionExpressionList(ExpressionList partitionExpressionList) {
35+
this.partitionExpressionList = partitionExpressionList;
36+
}
37+
38+
void toStringPartitionBy(StringBuilder b) {
39+
if (partitionExpressionList != null && !partitionExpressionList.getExpressions().isEmpty()) {
40+
b.append("PARTITION BY ");
41+
b.append(PlainSelect.
42+
getStringList(partitionExpressionList.getExpressions(), true, false));
43+
b.append(" ");
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)