|
| 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