use of net.sf.jsqlparser.parser.SimpleNode in project JSqlParser by JSQLParser.
the class CreateViewDeParserTest method testCreateViewASTNode.
@Test
public void testCreateViewASTNode() throws JSQLParserException {
String sql = "CREATE VIEW test AS SELECT a, b FROM mytable";
final StringBuilder b = new StringBuilder(sql);
SimpleNode node = (SimpleNode) CCJSqlParserUtil.parseAST(sql);
node.dump("*");
assertEquals(CCJSqlParserTreeConstants.JJTSTATEMENT, node.getId());
node.jjtAccept(new CCJSqlParserDefaultVisitor() {
int idxDelta = 0;
@Override
public Object visit(SimpleNode node, Object data) {
if (CCJSqlParserTreeConstants.JJTCOLUMN == node.getId()) {
b.insert(node.jjtGetFirstToken().beginColumn - 1 + idxDelta, '"');
idxDelta++;
b.insert(node.jjtGetLastToken().endColumn + idxDelta, '"');
idxDelta++;
}
return super.visit(node, data);
}
}, null);
assertEquals("CREATE VIEW test AS SELECT \"a\", \"b\" FROM mytable", b.toString());
}
use of net.sf.jsqlparser.parser.SimpleNode in project JSqlParser by JSQLParser.
the class SelectASTTest method testSelectASTColumn.
@Test
public void testSelectASTColumn() throws JSQLParserException {
String sql = "SELECT a, b FROM mytable order by b, c";
StringBuilder b = new StringBuilder(sql);
Statement stmt = CCJSqlParserUtil.parse(sql);
Select select = (Select) stmt;
PlainSelect ps = (PlainSelect) select.getSelectBody();
for (SelectItem item : ps.getSelectItems()) {
SelectExpressionItem sei = (SelectExpressionItem) item;
Column c = (Column) sei.getExpression();
SimpleNode astNode = c.getASTNode();
assertNotNull(astNode);
b.setCharAt(astNode.jjtGetFirstToken().beginColumn - 1, '*');
}
for (OrderByElement item : ps.getOrderByElements()) {
Column c = (Column) item.getExpression();
SimpleNode astNode = c.getASTNode();
assertNotNull(astNode);
b.setCharAt(astNode.jjtGetFirstToken().beginColumn - 1, '#');
}
assertEquals("SELECT *, * FROM mytable order by #, #", b.toString());
}
use of net.sf.jsqlparser.parser.SimpleNode in project JSqlParser by JSQLParser.
the class SelectASTTest method testSelectASTNode.
@Test
public void testSelectASTNode() throws JSQLParserException {
String sql = "SELECT a, b FROM mytable order by b, c";
SimpleNode node = (SimpleNode) CCJSqlParserUtil.parseAST(sql);
node.dump("*");
assertEquals(CCJSqlParserTreeConstants.JJTSTATEMENT, node.getId());
}
use of net.sf.jsqlparser.parser.SimpleNode in project JSqlParser by JSQLParser.
the class SelectASTTest method testSelectASTNodeSubSelect.
@Test
public void testSelectASTNodeSubSelect() throws JSQLParserException {
String sql = "SELECT * FROM mytable where 0<(select count(*) from mytable2)";
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.JJTSUBSELECT) {
subSelectStart = node.jjtGetFirstToken();
subSelectEnd = node.jjtGetLastToken();
return super.visit(node, data);
} else {
return super.visit(node, data);
}
}
}, null);
assertNotNull(subSelectStart);
assertNotNull(subSelectEnd);
assertEquals(34, subSelectStart.beginColumn);
assertEquals(62, subSelectEnd.endColumn);
}
use of net.sf.jsqlparser.parser.SimpleNode in project JSqlParser by JSQLParser.
the class SelectASTTest method testSelectASTColumnLF.
@Test
public void testSelectASTColumnLF() throws JSQLParserException {
String sql = "SELECT a, b FROM mytable \n order by b, c";
StringBuilder b = new StringBuilder(sql);
Statement stmt = CCJSqlParserUtil.parse(sql);
Select select = (Select) stmt;
PlainSelect ps = (PlainSelect) select.getSelectBody();
for (SelectItem item : ps.getSelectItems()) {
SelectExpressionItem sei = (SelectExpressionItem) item;
Column c = (Column) sei.getExpression();
SimpleNode astNode = c.getASTNode();
assertNotNull(astNode);
b.setCharAt(astNode.jjtGetFirstToken().absoluteBegin - 1, '*');
}
for (OrderByElement item : ps.getOrderByElements()) {
Column c = (Column) item.getExpression();
SimpleNode astNode = c.getASTNode();
assertNotNull(astNode);
b.setCharAt(astNode.jjtGetFirstToken().absoluteBegin - 1, '#');
}
assertEquals("SELECT *, * FROM mytable \n order by #, #", b.toString());
}
Aggregations