Skip to content

Commit

Permalink
fixes #91
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Feb 1, 2015
1 parent d0ce413 commit 495a7f2
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ Also I would like to know about needed examples or documentation stuff.

## Extensions in the latest SNAPSHOT version 0.9.2

* support for within group expressions, e.g. oracls LISTAGG function

```sql
SELECT LISTAGG(col1, '##') WITHIN GROUP (ORDER BY col1) FROM table1
```

* support for inner with statements

```sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public interface ExpressionVisitor {
void visit(Modulo modulo);

void visit(AnalyticExpression aexpr);

void visit(WithinGroupExpression wgexpr);

void visit(ExtractExpression eexpr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,12 @@ public void visit(JsonExpression jsonExpr) {
public void visit(RegExpMySQLOperator expr) {
visitBinaryExpression(expr);
}

@Override
public void visit(WithinGroupExpression wgexpr) {
wgexpr.getExprList().accept(this);
for (OrderByElement element : wgexpr.getOrderByElements()) {
element.getExpression().accept(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2015 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 java.util.List;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.statement.select.OrderByElement;

/**
*
* @author toben
*/
public class WithinGroupExpression implements Expression {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

private List<OrderByElement> orderByElements;

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

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

private ExpressionList exprList;

public ExpressionList getExprList() {
return exprList;
}

public void setExprList(ExpressionList exprList) {
this.exprList = exprList;
}

@Override
public void accept(ExpressionVisitor visitor) {
visitor.visit(this);
}

@Override
public String toString() {
StringBuilder b = new StringBuilder();

b.append(name);
b.append(exprList.toString());
b.append(" WITHIN GROUP (");

b.append("ORDER BY ");
for (int i = 0; i < orderByElements.size(); i++) {
if (i > 0) {
b.append(", ");
}
b.append(orderByElements.get(i).toString());
}

b.append(")");

return b.toString();
}

}
4 changes: 4 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,8 @@ public void visit(AllTableColumns allTableColumns) {
public void visit(SelectExpressionItem item) {
item.getExpression().accept(this);
}

@Override
public void visit(WithinGroupExpression wgexpr) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,9 @@ public void visit(JsonExpression jsonExpr) {
buffer.append(jsonExpr.toString());
}

@Override
public void visit(WithinGroupExpression wgexpr) {
buffer.append(wgexpr.toString());
}

}
23 changes: 23 additions & 0 deletions src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_ONLY:"ONLY">
| <K_COMMIT:"COMMIT">
| <K_UNIQUE:"UNIQUE">
| <K_WITHIN:"WITHIN">
}

TOKEN : /* Numeric Constants */
Expand Down Expand Up @@ -1733,6 +1734,8 @@ Expression PrimaryExpression():

| LOOKAHEAD(AnalyticExpression()) retval=AnalyticExpression()

| LOOKAHEAD(WithinGroupExpression()) retval=WithinGroupExpression()

| LOOKAHEAD(ExtractExpression()) retval=ExtractExpression()

| LOOKAHEAD([sign="+" | sign="-"] JsonExpression()) [sign="+" | sign="-"] retval=JsonExpression()
Expand Down Expand Up @@ -1814,6 +1817,26 @@ IntervalExpression IntervalExpression() : {
}
}

WithinGroupExpression WithinGroupExpression() :
{
Token token = null;
List<OrderByElement> orderByElements = null;
WithinGroupExpression result = new WithinGroupExpression();
ExpressionList exprList;
}
{
token = <S_IDENTIFIER> "(" exprList = SimpleExpressionList() ")"
<K_WITHIN> <K_GROUP>
"(" orderByElements = OrderByElements() ")"

{
result.setName(token.image);
result.setExprList(exprList);
result.setOrderByElements(orderByElements);
return result;
}
}

AnalyticExpression AnalyticExpression() :
{
AnalyticExpression retval = new AnalyticExpression();
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/net/sf/jsqlparser/test/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1649,4 +1649,8 @@ public void testSelectOracleColl() throws JSQLParserException {
public void testSelectInnerWith() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT * FROM (WITH actor AS (SELECT 'a' aid FROM DUAL) SELECT aid FROM actor)");
}

public void testSelectWithinGroup() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT LISTAGG(col1, '##') WITHIN GROUP (ORDER BY col1) FROM table1");
}
}

0 comments on commit 495a7f2

Please sign in to comment.