Search in sources :

Example 66 with SQLASTOutputVisitor

use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.

the class MySqlSelectTest_exportTables method test_1.

public void test_1() throws Exception {
    String sql = "SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    // print(statementList);
    assertEquals(1, statementList.size());
    StringBuffer out = new StringBuffer();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
    visitor.setExportTables(true);
    stmt.accept(visitor);
    assertEquals("SELECT *\n" + "FROM table1\n" + // 
    "\tINNER JOIN table2 ON table1.id = table2.id;", out.toString());
    assertNotNull(visitor.getTables());
    assertEquals(2, visitor.getTables().size());
    assertEquals("[table1, table2]", visitor.getTables().toString());
}
Also used : SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 67 with SQLASTOutputVisitor

use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.

the class MySqlSelectTest_exportTables method test_0.

public void test_0() throws Exception {
    String sql = "select *\n" + "from my_table\n" + "where level between 10-5 and 10+5\n" + "order by -ABS(10 - level) desc\n" + "limit 0,100";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    // print(statementList);
    assertEquals(1, statementList.size());
    StringBuffer out = new StringBuffer();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
    visitor.setExportTables(true);
    stmt.accept(visitor);
    assertEquals("SELECT *\n" + "FROM my_table\n" + "WHERE level BETWEEN 10 - 5 AND 10 + 5\n" + "ORDER BY -ABS(10 - level) DESC\n" + // 
    "LIMIT 0, 100", out.toString());
    assertNotNull(visitor.getTables());
    assertEquals(1, visitor.getTables().size());
    assertTrue(visitor.getTables().contains("my_table"));
}
Also used : SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)67 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)56 ArrayList (java.util.ArrayList)48 DbType (com.alibaba.druid.DbType)45 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)44 JSONArray (com.alibaba.fastjson.JSONArray)10 MySqlOutputVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor)3 OracleOutputVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor)3 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)2 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)2 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)2 SQLSelectGroupByClause (com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause)2 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)2 DB2SelectQueryBlock (com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock)2 DB2OutputVisitor (com.alibaba.druid.sql.dialect.db2.visitor.DB2OutputVisitor)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)2 PGSelectQueryBlock (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)2