Search in sources :

Example 1 with CCJSqlParserDefaultVisitor

use of net.sf.jsqlparser.parser.CCJSqlParserDefaultVisitor 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());
}
Also used : CCJSqlParserDefaultVisitor(net.sf.jsqlparser.parser.CCJSqlParserDefaultVisitor) SimpleNode(net.sf.jsqlparser.parser.SimpleNode) Test(org.junit.Test)

Example 2 with CCJSqlParserDefaultVisitor

use of net.sf.jsqlparser.parser.CCJSqlParserDefaultVisitor 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);
}
Also used : CCJSqlParserDefaultVisitor(net.sf.jsqlparser.parser.CCJSqlParserDefaultVisitor) SimpleNode(net.sf.jsqlparser.parser.SimpleNode) Test(org.junit.Test)

Aggregations

CCJSqlParserDefaultVisitor (net.sf.jsqlparser.parser.CCJSqlParserDefaultVisitor)2 SimpleNode (net.sf.jsqlparser.parser.SimpleNode)2 Test (org.junit.Test)2