Skip to content

Commit 978b60e

Browse files
committed
fixes #146
1 parent 83899f8 commit 978b60e

File tree

2 files changed

+123
-10
lines changed

2 files changed

+123
-10
lines changed

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,11 @@ public void visit(GreaterThanEquals expr) {
171171

172172
@Override
173173
public void visit(InExpression expr) {
174-
expr.getLeftExpression().accept(this);
175-
expr.getLeftItemsList().accept(this);
174+
if (expr.getLeftExpression() != null) {
175+
expr.getLeftExpression().accept(this);
176+
} else if (expr.getLeftItemsList() != null) {
177+
expr.getLeftItemsList().accept(this);
178+
}
176179
expr.getRightItemsList().accept(this);
177180
}
178181

@@ -212,8 +215,8 @@ public void visit(SubSelect subSelect) {
212215
for (WithItem item : subSelect.getWithItemsList()) {
213216
item.accept(selectVisitor);
214217
}
218+
subSelect.getSelectBody().accept(selectVisitor);
215219
}
216-
subSelect.getSelectBody().accept(selectVisitor);
217220
if (subSelect.getPivot() != null) {
218221
subSelect.getPivot().accept(this);
219222
}
@@ -395,15 +398,17 @@ public void visit(Pivot pivot) {
395398
for (Column col : pivot.getForColumns()) {
396399
col.accept(this);
397400
}
398-
if (pivot.getSingleInItems()!=null)
401+
if (pivot.getSingleInItems() != null) {
399402
for (SelectExpressionItem item : pivot.getSingleInItems()) {
400403
item.accept(this);
401404
}
402-
403-
if (pivot.getMultiInItems()!=null)
405+
}
406+
407+
if (pivot.getMultiInItems() != null) {
404408
for (ExpressionListItem item : pivot.getMultiInItems()) {
405409
item.getExpressionList().accept(this);
406410
}
411+
}
407412
}
408413

409414
@Override
@@ -414,18 +419,21 @@ public void visit(PivotXml pivot) {
414419
for (Column col : pivot.getForColumns()) {
415420
col.accept(this);
416421
}
417-
if (pivot.getInSelect()!=null)
418-
pivot.getInSelect().accept(selectVisitor);
422+
if (pivot.getInSelect() != null) {
423+
if (selectVisitor != null) {
424+
pivot.getInSelect().accept(selectVisitor);
425+
}
426+
}
419427
}
420428

421429
@Override
422430
public void visit(AllColumns allColumns) {
423-
431+
424432
}
425433

426434
@Override
427435
public void visit(AllTableColumns allTableColumns) {
428-
436+
429437
}
430438

431439
@Override
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (C) 2015 JSQLParser.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301 USA
18+
*/
19+
package net.sf.jsqlparser.expression;
20+
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import net.sf.jsqlparser.JSQLParserException;
24+
import net.sf.jsqlparser.expression.operators.relational.InExpression;
25+
import net.sf.jsqlparser.expression.operators.relational.ItemsList;
26+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
27+
import net.sf.jsqlparser.statement.select.PlainSelect;
28+
import net.sf.jsqlparser.statement.select.Select;
29+
import org.junit.After;
30+
import org.junit.AfterClass;
31+
import static org.junit.Assert.assertNull;
32+
import static org.junit.Assert.assertTrue;
33+
import org.junit.Before;
34+
import org.junit.BeforeClass;
35+
import org.junit.Test;
36+
37+
/**
38+
*
39+
* @author tw
40+
*/
41+
public class ExpressionVisitorAdapterTest {
42+
43+
public ExpressionVisitorAdapterTest() {
44+
}
45+
46+
@BeforeClass
47+
public static void setUpClass() {
48+
}
49+
50+
@AfterClass
51+
public static void tearDownClass() {
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
}
57+
58+
@After
59+
public void tearDown() {
60+
}
61+
62+
@Test
63+
public void testInExpressionProblem() throws JSQLParserException {
64+
final List exprList = new ArrayList();
65+
Select select = (Select) CCJSqlParserUtil.parse( "select * from foo where x in (?,?,?)" );
66+
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
67+
Expression where = plainSelect.getWhere();
68+
where.accept( new ExpressionVisitorAdapter() {
69+
70+
@Override
71+
public void visit(InExpression expr) {
72+
super.visit(expr);
73+
exprList.add(expr.getLeftExpression());
74+
exprList.add(expr.getLeftItemsList());
75+
exprList.add(expr.getRightItemsList());
76+
}
77+
});
78+
79+
assertTrue(exprList.get(0) instanceof Expression);
80+
assertNull(exprList.get(1));
81+
assertTrue(exprList.get(2) instanceof ItemsList);
82+
}
83+
84+
@Test
85+
public void testInExpression() throws JSQLParserException {
86+
final List exprList = new ArrayList();
87+
Select select = (Select) CCJSqlParserUtil.parse( "select * from foo where (a,b) in (select a,b from foo2)" );
88+
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
89+
Expression where = plainSelect.getWhere();
90+
where.accept(new ExpressionVisitorAdapter() {
91+
92+
@Override
93+
public void visit(InExpression expr) {
94+
super.visit(expr);
95+
exprList.add(expr.getLeftExpression());
96+
exprList.add(expr.getLeftItemsList());
97+
exprList.add(expr.getRightItemsList());
98+
}
99+
});
100+
101+
assertNull(exprList.get(0));
102+
assertTrue(exprList.get(1) instanceof ItemsList);
103+
assertTrue(exprList.get(2) instanceof ItemsList);
104+
}
105+
}

0 commit comments

Comments
 (0)