Skip to content

Commit

Permalink
integrated some additional AST - Nodes for InExpression and SimpleExp…
Browse files Browse the repository at this point in the history
…ressionList
  • Loading branch information
wumpz committed Feb 4, 2019
1 parent 4e0a732 commit cb0e0b7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2243,7 +2243,7 @@ Expression SQLCondition():
{ return result; }
}

Expression InExpression() :
Expression InExpression() #InExpression :
{
InExpression result = new InExpression();
ItemsList leftItemsList = null;
Expand All @@ -2265,6 +2265,7 @@ Expression InExpression() :
[<K_NOT> { result.setNot(true); } ] <K_IN> "(" (LOOKAHEAD(3) rightItemsList=SubSelect() | rightItemsList=SimpleExpressionList() ) ")"
{
result.setRightItemsList(rightItemsList);
linkAST(result,jjtThis);
return result;
}
}
Expand Down Expand Up @@ -2349,7 +2350,7 @@ ExpressionList SQLExpressionList():
}
}

ExpressionList SimpleExpressionList():
ExpressionList SimpleExpressionList() #ExpressionList:
{
ExpressionList retval = new ExpressionList();
List<Expression> expressions = new ArrayList<Expression>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import net.sf.jsqlparser.parser.CCJSqlParserDefaultVisitor;
import net.sf.jsqlparser.parser.CCJSqlParserTreeConstants;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.parser.Token;
import net.sf.jsqlparser.parser.SimpleNode;
import net.sf.jsqlparser.parser.Token;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.Statement;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -94,7 +94,7 @@ public Object visit(SimpleNode node, Object data) {
assertEquals(34, subSelectStart.beginColumn);
assertEquals(62, subSelectEnd.endColumn);
}

@Test
public void testSelectASTColumnLF() throws JSQLParserException {
String sql = "SELECT a, b FROM mytable \n order by b, c";
Expand All @@ -117,7 +117,7 @@ public void testSelectASTColumnLF() throws JSQLParserException {
}
assertEquals("SELECT *, * FROM mytable \n order by #, #", b.toString());
}

@Test
public void testSelectASTCommentLF() throws JSQLParserException {
String sql = "SELECT /* testcomment */ \n a, b FROM -- testcomment2 \n mytable \n order by b, c";
Expand All @@ -140,7 +140,7 @@ public void testSelectASTCommentLF() throws JSQLParserException {
}
assertEquals("SELECT /* testcomment */ \n *, * FROM -- testcomment2 \n mytable \n order by #, #", b.toString());
}

@Test
public void testSelectASTCommentCRLF() throws JSQLParserException {
String sql = "SELECT /* testcomment */ \r\n a, b FROM -- testcomment2 \r\n mytable \r\n order by b, c";
Expand All @@ -163,4 +163,29 @@ public void testSelectASTCommentCRLF() throws JSQLParserException {
}
assertEquals("SELECT /* testcomment */ \r\n *, * FROM -- testcomment2 \r\n mytable \r\n order by #, #", b.toString());
}

@Test
public void testDetectInExpressions() throws JSQLParserException {
String sql = "SELECT * FROM mytable WHERE a IN (1,2,3,4,5,6,7)";
SimpleNode node = (SimpleNode) CCJSqlParserUtil.parseAST(sql);
node.dump("*");
assertEquals(CCJSqlParserTreeConstants.JJTSTATEMENT, node.getId());
node.jjtAccept(new CCJSqlParserDefaultVisitor() {
@Override
public Object visit(SimpleNode node, Object data) {
if (node.getId() == CCJSqlParserTreeConstants.JJTINEXPRESSION) {
subSelectStart = node.jjtGetFirstToken();
subSelectEnd = node.jjtGetLastToken();
return super.visit(node, data);
} else {
return super.visit(node, data);
}
}
}, null);

assertNotNull(subSelectStart);
assertNotNull(subSelectEnd);
assertEquals(30, subSelectStart.beginColumn);
assertEquals(49, subSelectEnd.endColumn);
}
}

0 comments on commit cb0e0b7

Please sign in to comment.