forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GroupByDeParser.java
53 lines (45 loc) · 1.63 KB
/
GroupByDeParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.util.deparser;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.statement.select.GroupByElement;
public class GroupByDeParser extends AbstractDeParser<GroupByElement> {
private ExpressionListDeParser expressionListDeParser;
GroupByDeParser() {
this(null, new StringBuilder());
}
public GroupByDeParser(ExpressionVisitor expressionVisitor, StringBuilder buffer) {
super(buffer);
this.expressionListDeParser = new ExpressionListDeParser(expressionVisitor, buffer);
this.buffer = buffer;
}
@Override
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
public void deParse(GroupByElement groupBy) {
buffer.append("GROUP BY ");
expressionListDeParser.deParse(groupBy.getGroupByExpressionList());
int i = 0;
if (!groupBy.getGroupingSets().isEmpty()) {
if (buffer.charAt(buffer.length() - 1) != ' ') {
buffer.append(' ');
}
buffer.append("GROUPING SETS (");
for (ExpressionList expressionList : groupBy.getGroupingSets()) {
buffer.append(i++ > 0 ? ", " : "");
expressionListDeParser.deParse(expressionList);
}
buffer.append(")");
}
if (groupBy.isMysqlWithRollup()) {
buffer.append(" WITH ROLLUP");
}
}
}