Skip to content

Commit 3aaf11d

Browse files
committed
fixes #72
1 parent adca3ef commit 3aaf11d

File tree

7 files changed

+158
-7
lines changed

7 files changed

+158
-7
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,6 @@ public interface ExpressionVisitor {
156156
void visit(NumericBind bind);
157157

158158
void visit(KeepExpression aexpr);
159+
160+
void visit(MySQLGroupConcat groupConcat);
159161
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,16 @@ public void visit(KeepExpression expr) {
341341
element.getExpression().accept(this);
342342
}
343343
}
344+
345+
@Override
346+
public void visit(MySQLGroupConcat groupConcat) {
347+
for (Expression expr : groupConcat.getExpressionList().getExpressions()) {
348+
expr.accept(this);
349+
}
350+
if (groupConcat.getOrderByElements() != null) {
351+
for (OrderByElement element : groupConcat.getOrderByElements()) {
352+
element.getExpression().accept(this);
353+
}
354+
}
355+
}
344356
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2015 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+
/*
23+
* Copyright (C) 2015 JSQLParser.
24+
*
25+
* This library is free software; you can redistribute it and/or
26+
* modify it under the terms of the GNU Lesser General Public
27+
* License as published by the Free Software Foundation; either
28+
* version 2.1 of the License, or (at your option) any later version.
29+
*
30+
* This library is distributed in the hope that it will be useful,
31+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
32+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33+
* Lesser General Public License for more details.
34+
*
35+
* You should have received a copy of the GNU Lesser General Public
36+
* License along with this library; if not, write to the Free Software
37+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
38+
* MA 02110-1301 USA
39+
*/
40+
package net.sf.jsqlparser.expression;
41+
42+
import java.util.List;
43+
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
44+
import net.sf.jsqlparser.statement.select.OrderByElement;
45+
import net.sf.jsqlparser.statement.select.PlainSelect;
46+
47+
/**
48+
*
49+
* @author toben
50+
*/
51+
public class MySQLGroupConcat implements Expression {
52+
53+
private ExpressionList expressionList;
54+
private boolean distinct = false;
55+
private List<OrderByElement> orderByElements;
56+
private String separator;
57+
58+
public ExpressionList getExpressionList() {
59+
return expressionList;
60+
}
61+
62+
public void setExpressionList(ExpressionList expressionList) {
63+
this.expressionList = expressionList;
64+
}
65+
66+
public boolean isDistinct() {
67+
return distinct;
68+
}
69+
70+
public void setDistinct(boolean distinct) {
71+
this.distinct = distinct;
72+
}
73+
74+
public List<OrderByElement> getOrderByElements() {
75+
return orderByElements;
76+
}
77+
78+
public void setOrderByElements(List<OrderByElement> orderByElements) {
79+
this.orderByElements = orderByElements;
80+
}
81+
82+
public String getSeparator() {
83+
return separator;
84+
}
85+
86+
public void setSeparator(String separator) {
87+
this.separator = separator;
88+
}
89+
90+
@Override
91+
public void accept(ExpressionVisitor expressionVisitor) {
92+
expressionVisitor.visit(this);
93+
}
94+
95+
@Override
96+
public String toString() {
97+
StringBuilder b = new StringBuilder();
98+
b.append("GROUP_CONCAT(");
99+
if (isDistinct()) {
100+
b.append("DISTINCT ");
101+
}
102+
b.append(PlainSelect.getStringList(expressionList.getExpressions(), true, false));
103+
if (orderByElements != null && !orderByElements.isEmpty()) {
104+
b.append(" ORDER BY ");
105+
for (int i = 0; i < orderByElements.size(); i++) {
106+
if (i > 0) {
107+
b.append(", ");
108+
}
109+
b.append(orderByElements.get(i).toString());
110+
}
111+
}
112+
if (separator != null) {
113+
b.append(" SEPARATOR ").append(separator);
114+
}
115+
b.append(")");
116+
return b.toString();
117+
}
118+
}

src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,8 @@ public void visit(NumericBind bind) {
515515
@Override
516516
public void visit(KeepExpression aexpr) {
517517
}
518+
519+
@Override
520+
public void visit(MySQLGroupConcat groupConcat) {
521+
}
518522
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,4 +540,9 @@ public void visit(NumericBind bind) {
540540
public void visit(KeepExpression aexpr) {
541541
buffer.append(aexpr.toString());
542542
}
543+
544+
@Override
545+
public void visit(MySQLGroupConcat groupConcat) {
546+
buffer.append(groupConcat.toString());
547+
}
543548
}

src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ Expression PrimaryExpression():
17841784

17851785
| LOOKAHEAD(ExtractExpression()) retval=ExtractExpression()
17861786

1787-
| MySQLGroupConcat()
1787+
| retval=MySQLGroupConcat()
17881788

17891789
| LOOKAHEAD([sign="+" | sign="-"] JsonExpression()) [sign="+" | sign="-"] retval=JsonExpression()
17901790

@@ -2150,13 +2150,23 @@ Function Function():
21502150
}
21512151
}
21522152

2153-
void MySQLGroupConcat():{}
2153+
MySQLGroupConcat MySQLGroupConcat():{
2154+
MySQLGroupConcat retval = new MySQLGroupConcat();
2155+
ExpressionList expressionList = null;
2156+
List<OrderByElement> orderByList = null;
2157+
Token t;
2158+
}
21542159
{
21552160
<K_GROUP_CONCAT> "("
2156-
[<K_DISTINCT>] SimpleExpressionList()
2157-
OrderByElements()
2158-
[ <K_SEPARATOR> <S_CHAR_LITERAL> ]
2161+
[<K_DISTINCT> { retval.setDistinct(true); } ]
2162+
expressionList = SimpleExpressionList()
2163+
[ orderByList = OrderByElements() { retval.setOrderByElements(orderByList); } ]
2164+
[ <K_SEPARATOR> t=<S_CHAR_LITERAL> { retval.setSeparator(t.image); } ]
21592165
")"
2166+
{
2167+
retval.setExpressionList(expressionList);
2168+
return retval;
2169+
}
21602170
}
21612171

21622172

src/test/java/net/sf/jsqlparser/test/select/SpecialOracleTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public void testAllSqlsParseDeparse() throws IOException {
6161
success++;
6262
LOG.info(" -> SUCCESS");
6363
} catch (JSQLParserException ex) {
64-
LOG.log(Level.SEVERE, null, ex);
65-
LOG.info(" -> PROBLEM");
64+
//LOG.log(Level.SEVERE, null, ex);
65+
LOG.log(Level.INFO, " -> PROBLEM {0}", ex.toString());
6666
}
6767
}
6868
}

0 commit comments

Comments
 (0)