Skip to content

Commit d66a93a

Browse files
committed
astnodes for columns and tables
1 parent e594e59 commit d66a93a

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public class CCJSqlParser {
8484
access.setASTNode(node);
8585
node.jjtSetValue(access);
8686
}
87+
88+
public Node getASTRoot() {
89+
return jjtree.rootNode();
90+
}
8791
}
8892

8993
PARSER_END(CCJSqlParser)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.test.select;
20+
21+
import junit.framework.TestCase;
22+
import net.sf.jsqlparser.JSQLParserException;
23+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
24+
import net.sf.jsqlparser.parser.SimpleNode;
25+
import net.sf.jsqlparser.schema.Column;
26+
import net.sf.jsqlparser.statement.Statement;
27+
import net.sf.jsqlparser.statement.select.OrderByElement;
28+
import net.sf.jsqlparser.statement.select.PlainSelect;
29+
import net.sf.jsqlparser.statement.select.Select;
30+
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
31+
import net.sf.jsqlparser.statement.select.SelectItem;
32+
33+
/**
34+
*
35+
* @author toben
36+
*/
37+
public class SelectASTTest extends TestCase {
38+
39+
public SelectASTTest(String name) {
40+
super(name);
41+
}
42+
43+
public void testSelectASTColumn() throws JSQLParserException {
44+
String sql = "SELECT a, b FROM mytable order by b, c";
45+
StringBuilder b = new StringBuilder(sql);
46+
Statement stmt = CCJSqlParserUtil.parse(sql);
47+
Select select = (Select)stmt;
48+
PlainSelect ps = (PlainSelect)select.getSelectBody();
49+
for (SelectItem item : ps.getSelectItems()) {
50+
SelectExpressionItem sei = (SelectExpressionItem)item;
51+
Column c = (Column)sei.getExpression();
52+
SimpleNode astNode = c.getASTNode();
53+
assertNotNull(astNode);
54+
b.setCharAt(astNode.jjtGetFirstToken().beginColumn-1, '*');
55+
}
56+
for (OrderByElement item : ps.getOrderByElements()) {
57+
Column c = (Column)item.getExpression();
58+
SimpleNode astNode = c.getASTNode();
59+
assertNotNull(astNode);
60+
b.setCharAt(astNode.jjtGetFirstToken().beginColumn-1, '#');
61+
}
62+
assertEquals("SELECT *, * FROM mytable order by #, #", b.toString());
63+
}
64+
}

0 commit comments

Comments
 (0)