Skip to content

Commit 1ef3930

Browse files
committed
fixes #17
1 parent 82f3da8 commit 1ef3930

File tree

5 files changed

+81
-7
lines changed

5 files changed

+81
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Also I would like to know about needed examples or documentation stuff.
5959

6060
## Extensions in the latest SNAPSHOT version 2.0
6161

62+
* support for **GROUPING SETS**
6263
* first support for db date arithmentic
6364
* support for chained functions
6465
* first support for **FOR XML PATH**

src/main/java/net/sf/jsqlparser/statement/select/GroupByElement.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
import java.util.ArrayList;
1313
import java.util.List;
1414
import net.sf.jsqlparser.expression.Expression;
15+
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
1516

1617
public class GroupByElement {
1718

1819
private List<Expression> groupByExpressions = new ArrayList<>();
20+
private List groupingSets = new ArrayList();
1921

2022
public void accept(GroupByVisitor groupByVisitor) {
2123
groupByVisitor.visit(this);
@@ -33,12 +35,47 @@ public void addGroupByExpression(Expression groupByExpression) {
3335
groupByExpressions.add(groupByExpression);
3436
}
3537

38+
public List getGroupingSets() {
39+
return groupingSets;
40+
}
41+
42+
public void setGroupingSets(List groupingSets) {
43+
this.groupingSets = groupingSets;
44+
}
45+
46+
public void addGroupingSet(Expression expr) {
47+
this.groupingSets.add(expr);
48+
}
49+
50+
public void addGroupingSet(ExpressionList list) {
51+
this.groupingSets.add(list);
52+
}
53+
3654
@Override
3755
public String toString() {
3856
StringBuilder b = new StringBuilder();
3957
b.append("GROUP BY ");
4058

41-
b.append(PlainSelect.getStringList(groupByExpressions));
59+
if (groupByExpressions.size() > 0) {
60+
b.append(PlainSelect.getStringList(groupByExpressions));
61+
} else if (groupingSets.size() > 0) {
62+
b.append("GROUPING SETS (");
63+
boolean first = true;
64+
for (Object o : groupingSets) {
65+
if (first) {
66+
first = false;
67+
} else {
68+
b.append(", ");
69+
}
70+
if (o instanceof Expression) {
71+
b.append(o.toString());
72+
} else if (o instanceof ExpressionList) {
73+
ExpressionList list = (ExpressionList) o;
74+
b.append(list.getExpressions() == null ? "()" : list.toString());
75+
}
76+
}
77+
b.append(")");
78+
}
4279

4380
return b.toString();
4481
}

src/main/java/net/sf/jsqlparser/util/deparser/GroupByDeParser.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Iterator;
1313
import net.sf.jsqlparser.expression.Expression;
1414
import net.sf.jsqlparser.expression.ExpressionVisitor;
15+
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
1516
import net.sf.jsqlparser.statement.select.GroupByElement;
1617

1718
public class GroupByDeParser {
@@ -35,6 +36,24 @@ public void deParse(GroupByElement groupBy) {
3536
buffer.append(", ");
3637
}
3738
}
39+
if (groupBy.getGroupingSets().size() > 0) {
40+
buffer.append("GROUPING SETS (");
41+
boolean first = true;
42+
for (Object o : groupBy.getGroupingSets()) {
43+
if (first) {
44+
first = false;
45+
} else {
46+
buffer.append(", ");
47+
}
48+
if (o instanceof Expression) {
49+
buffer.append(o.toString());
50+
} else if (o instanceof ExpressionList) {
51+
ExpressionList list = (ExpressionList) o;
52+
buffer.append(list.getExpressions() == null ? "()" : list.toString());
53+
}
54+
}
55+
buffer.append(")");
56+
}
3857
}
3958

4059
void setExpressionVisitor(ExpressionVisitor expressionVisitor) {

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
173173
| <K_FULL:"FULL">
174174
| <K_FULLTEXT:"FULLTEXT">
175175
| <K_GROUP:"GROUP">
176+
| <K_GROUPING:"GROUPING">
176177
| <K_GROUP_CONCAT:"GROUP_CONCAT">
177178
| <K_HAVING:"HAVING">
178179
| <K_HIGH_PRIORITY : "HIGH_PRIORITY">
@@ -248,6 +249,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
248249
| <K_SEMI : "SEMI">
249250
| <K_SEPARATOR:"SEPARATOR">
250251
| <K_SET:"SET">
252+
| <K_SETS:"SETS">
251253
| <K_SHOW : "SHOW">
252254
| <K_SIBLINGS:"SIBLINGS">
253255
| <K_SKIP: "SKIP">
@@ -1799,10 +1801,25 @@ GroupByElement GroupByColumnReferences():
17991801
{
18001802
Expression columnReference;
18011803
GroupByElement groupBy = new GroupByElement();
1804+
Expression expr;
1805+
ExpressionList list;
18021806
}
18031807
{
1804-
<K_GROUP> <K_BY> columnReference=SimpleExpression() {groupBy.addGroupByExpression(columnReference); }
1805-
("," columnReference=SimpleExpression() {groupBy.addGroupByExpression(columnReference); } )*
1808+
<K_GROUP> <K_BY>
1809+
(
1810+
columnReference=SimpleExpression() {groupBy.addGroupByExpression(columnReference); }
1811+
("," columnReference=SimpleExpression() {groupBy.addGroupByExpression(columnReference); } )*
1812+
|
1813+
<K_GROUPING> <K_SETS> "("
1814+
( LOOKAHEAD(2) "(" ")" { groupBy.addGroupingSet(new ExpressionList()); }
1815+
| LOOKAHEAD(3) "(" list = SimpleExpressionList() ")" { groupBy.addGroupingSet(list); }
1816+
| expr = SimpleExpression() { groupBy.addGroupingSet(expr); } )
1817+
1818+
( "," ( LOOKAHEAD(2) "(" ")" { groupBy.addGroupingSet(new ExpressionList()); }
1819+
| LOOKAHEAD(3) "(" list = SimpleExpressionList() ")" { groupBy.addGroupingSet(list); }
1820+
| expr = SimpleExpression() { groupBy.addGroupingSet(expr); } ) )*
1821+
")"
1822+
)
18061823
{
18071824
return groupBy;
18081825
}

src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,17 +3595,17 @@ public void visit(PlainSelect plainSelect) {
35953595
@Test
35963596
public void testGroupingSets1() throws JSQLParserException {
35973597
assertSqlCanBeParsedAndDeparsed("SELECT COL_1, COL_2, COL_3, COL_4, COL_5, COL_6 FROM TABLE_1 "
3598-
+ "GROUP BY "
3599-
+ " GROUPING SETS( (COL_1, COL_2, COL_3, COL_4), (COL_5, COL_6))");
3598+
+ "GROUP BY "
3599+
+ "GROUPING SETS ((COL_1, COL_2, COL_3, COL_4), (COL_5, COL_6))");
36003600
}
36013601

36023602
@Test
36033603
public void testGroupingSets2() throws JSQLParserException {
3604-
assertSqlCanBeParsedAndDeparsed("SELECT COL_1 FROM TABLE_1 GROUP BY GROUPING SETS( COL_1 )");
3604+
assertSqlCanBeParsedAndDeparsed("SELECT COL_1 FROM TABLE_1 GROUP BY GROUPING SETS (COL_1)");
36053605
}
36063606

36073607
@Test
36083608
public void testGroupingSets3() throws JSQLParserException {
3609-
assertSqlCanBeParsedAndDeparsed("SELECT COL_1 FROM TABLE_1 GROUP BY GROUPING SETS( COL_1, () )");
3609+
assertSqlCanBeParsedAndDeparsed("SELECT COL_1 FROM TABLE_1 GROUP BY GROUPING SETS (COL_1, ())");
36103610
}
36113611
}

0 commit comments

Comments
 (0)