Skip to content

Commit dabb03f

Browse files
committed
- allow parenthesis around from item
- allow whitespace between bars from concat
1 parent a04ccb1 commit dabb03f

File tree

9 files changed

+130
-9
lines changed

9 files changed

+130
-9
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
public class Parenthesis extends ASTNodeAccessImpl implements Expression {
3030

3131
private Expression expression;
32+
3233
private boolean not = false;
3334

3435
public Parenthesis() {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ public interface FromItemVisitor {
3636
void visit(ValuesList valuesList);
3737

3838
void visit(TableFunction tableFunction);
39+
40+
public void visit(ParenthesisFromItem aThis);
3941
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ public void visit(ValuesList valuesList) {
5454
public void visit(TableFunction valuesList) {
5555

5656
}
57+
58+
@Override
59+
public void visit(ParenthesisFromItem aThis) {
60+
61+
}
5762
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2013 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.statement.select;
23+
24+
import net.sf.jsqlparser.expression.*;
25+
26+
/**
27+
* It represents an expression like "(" expression ")"
28+
*/
29+
public class ParenthesisFromItem implements FromItem {
30+
31+
private FromItem fromItem;
32+
33+
private Alias alias;
34+
35+
public ParenthesisFromItem() {
36+
}
37+
38+
public ParenthesisFromItem(FromItem fromItem) {
39+
setFromItem(fromItem);
40+
}
41+
42+
public FromItem getFromItem() {
43+
return fromItem;
44+
}
45+
46+
public final void setFromItem(FromItem fromItem) {
47+
this.fromItem = fromItem;
48+
}
49+
50+
@Override
51+
public void accept(FromItemVisitor fromItemVisitor) {
52+
fromItemVisitor.visit(this);
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return "(" + fromItem + ")" + (alias!=null?alias.toString():"");
58+
}
59+
60+
@Override
61+
public Alias getAlias() {
62+
return alias;
63+
}
64+
65+
@Override
66+
public void setAlias(Alias alias) {
67+
this.alias = alias;
68+
}
69+
70+
@Override
71+
public Pivot getPivot() {
72+
throw new UnsupportedOperationException("Not supported yet.");
73+
}
74+
75+
@Override
76+
public void setPivot(Pivot pivot) {
77+
throw new UnsupportedOperationException("Not supported yet.");
78+
}
79+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
import net.sf.jsqlparser.statement.select.FromItemVisitor;
116116
import net.sf.jsqlparser.statement.select.Join;
117117
import net.sf.jsqlparser.statement.select.LateralSubSelect;
118+
import net.sf.jsqlparser.statement.select.ParenthesisFromItem;
118119
import net.sf.jsqlparser.statement.select.PlainSelect;
119120
import net.sf.jsqlparser.statement.select.Select;
120121
import net.sf.jsqlparser.statement.select.SelectBody;
@@ -791,4 +792,9 @@ public void visit(Upsert upsert) {
791792
@Override
792793
public void visit(UseStatement use) {
793794
}
795+
796+
@Override
797+
public void visit(ParenthesisFromItem parenthesis) {
798+
parenthesis.getFromItem().accept(this);
799+
}
794800
}

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public SelectDeParser() {
4040
}
4141

4242
/**
43-
* @param expressionVisitor a {@link ExpressionVisitor} to de-parse expressions. It has to share the same<br>
43+
* @param expressionVisitor a {@link ExpressionVisitor} to de-parse expressions. It has to share
44+
* the same<br>
4445
* StringBuilder (buffer parameter) as this object in order to work
4546
* @param buffer the buffer that will be filled with the select
4647
*/
@@ -280,7 +281,7 @@ public void visit(PivotXml pivot) {
280281
public void deparseOffset(Offset offset) {
281282
// OFFSET offset
282283
// or OFFSET offset (ROW | ROWS)
283-
if (offset.getOffsetJdbcParameter()!=null) {
284+
if (offset.getOffsetJdbcParameter() != null) {
284285
buffer.append(" OFFSET ").append(offset.getOffsetJdbcParameter());
285286
} else if (offset.getOffset() != 0) {
286287
buffer.append(" OFFSET ");
@@ -300,7 +301,7 @@ public void deparseFetch(Fetch fetch) {
300301
} else {
301302
buffer.append("NEXT ");
302303
}
303-
if (fetch.getFetchJdbcParameter()!=null) {
304+
if (fetch.getFetchJdbcParameter() != null) {
304305
buffer.append(fetch.getFetchJdbcParameter().toString());
305306
} else {
306307
buffer.append(fetch.getRowCount());
@@ -329,7 +330,7 @@ public void setExpressionVisitor(ExpressionVisitor visitor) {
329330
public void visit(SubJoin subjoin) {
330331
buffer.append("(");
331332
subjoin.getLeft().accept(this);
332-
for(Join join : subjoin.getJoinList()) {
333+
for (Join join : subjoin.getJoinList()) {
333334
deparseJoin(join);
334335
}
335336
buffer.append(")");
@@ -452,4 +453,14 @@ public void visit(AllColumns allColumns) {
452453
public void visit(TableFunction tableFunction) {
453454
buffer.append(tableFunction.toString());
454455
}
456+
457+
@Override
458+
public void visit(ParenthesisFromItem parenthesis) {
459+
buffer.append("(");
460+
parenthesis.getFromItem().accept(this);
461+
buffer.append(")");
462+
if (parenthesis.getAlias() != null) {
463+
buffer.append(parenthesis.getAlias().toString());
464+
}
465+
}
455466
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ TOKEN : /* Operators */
301301
<OP_GREATERTHANEQUALS: ">" (<WHITESPACE>)* "=">
302302
| <OP_MINORTHANEQUALS: "<" (<WHITESPACE>)* "=">
303303
| <OP_NOTEQUALSSTANDARD: "<" (<WHITESPACE>)* ">">
304-
| <OP_NOTEQUALSBANG: "!=">
304+
| <OP_NOTEQUALSBANG: "!" (<WHITESPACE>)* "=">
305+
| <OP_CONCAT: "|" (<WHITESPACE>)* "|">
305306
}
306307

307308
TOKEN : /* Date/Time with time zones */
@@ -1474,7 +1475,11 @@ FromItem FromItem():
14741475
LOOKAHEAD(SubJoin())
14751476
fromItem=SubJoin()
14761477
|
1478+
LOOKAHEAD(3)
14771479
fromItem=SubSelect()
1480+
|
1481+
fromItem=FromItem()
1482+
{ fromItem = new ParenthesisFromItem(fromItem); }
14781483
)
14791484
")"
14801485
)
@@ -2304,8 +2309,8 @@ Expression ConcatExpression():
23042309
}
23052310
{
23062311
leftExpression=BitwiseAndOr() { result = leftExpression; }
2307-
(LOOKAHEAD(2)
2308-
"||"
2312+
(LOOKAHEAD(3)
2313+
<OP_CONCAT> /* Oracle allows space between the bars. */
23092314
rightExpression=BitwiseAndOr()
23102315
{
23112316
Concat binExp = new Concat();

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
public class SelectTest extends TestCase {
2020

21-
private CCJSqlParserManager parserManager = new CCJSqlParserManager();
21+
private final CCJSqlParserManager parserManager = new CCJSqlParserManager();
2222

2323
public SelectTest(String arg0) {
2424
super(arg0);
@@ -2768,4 +2768,16 @@ public void testIssue584MySQLValueListExpression() throws JSQLParserException {
27682768
public void testIssue588NotNull() throws JSQLParserException {
27692769
assertSqlCanBeParsedAndDeparsed("SELECT * FROM mytable WHERE col1 ISNULL");
27702770
}
2771+
2772+
public void testParenthesisAroundFromItem() throws JSQLParserException {
2773+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM (mytable)");
2774+
}
2775+
2776+
public void testParenthesisAroundFromItem2() throws JSQLParserException {
2777+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM (mytable myalias)");
2778+
}
2779+
2780+
public void testParenthesisAroundFromItem3() throws JSQLParserException {
2781+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM (mytable) myalias");
2782+
}
27712783
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void testAllSqlsParseDeparse() throws IOException {
7777

7878
LOG.
7979
log(Level.INFO, "tested {0} files. got {1} correct parse results", new Object[]{count, success});
80-
assertTrue(success >= 140);
80+
assertTrue(success >= 141);
8181
}
8282

8383
@Test

0 commit comments

Comments
 (0)