Search in sources :

Example 1 with SQLASTVisitor

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

the class ClearSchema_0 method test_insert_0.

public void test_insert_0() throws Exception {
    String sql = "INSERT INTO testdb.Websites (name, country)\n" + "SELECT app_name, country FROM testdb.apps;";
    SQLStatement stmt = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL).get(0);
    SQLASTVisitor v = new SQLASTVisitorAdapter() {

        @Override
        public boolean visit(SQLPropertyExpr x) {
            if (SQLUtils.replaceInParent(x, new SQLIdentifierExpr(x.getName()))) {
                return false;
            }
            return super.visit(x);
        }
    };
    stmt.accept(v);
    assertEquals("INSERT INTO Websites (name, country)\n" + "SELECT app_name, country\n" + "FROM apps;", stmt.toString());
}
Also used : SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLASTVisitor(com.alibaba.druid.sql.visitor.SQLASTVisitor) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 2 with SQLASTVisitor

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

the class Issue2038 method test_for_demo.

public void test_for_demo() throws Exception {
    String sql = "select * from (select * from t union all select * from t1 union all select * from t3) xx";
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, DbType.db2);
    SQLASTVisitor visitor = new DB2ASTVisitorAdapter() {

        public boolean visit(SQLUnionQuery x) {
            System.out.println("union");
            return true;
        }

        public boolean visit(DB2SelectQueryBlock x) {
            System.out.println("select");
            return true;
        }
    };
    for (SQLStatement stmt : stmtList) {
        stmt.accept(visitor);
    }
}
Also used : SQLASTVisitor(com.alibaba.druid.sql.visitor.SQLASTVisitor) DB2ASTVisitorAdapter(com.alibaba.druid.sql.dialect.db2.visitor.DB2ASTVisitorAdapter) SQLUnionQuery(com.alibaba.druid.sql.ast.statement.SQLUnionQuery) DB2SelectQueryBlock(com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 3 with SQLASTVisitor

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

the class Issue1912 method test_for_issue.

public void test_for_issue() throws Exception {
    String sql = "select a from t";
    SQLStatementParser parser = new SQLStatementParser(sql);
    SQLStatement stmt = parser.parseStatement();
    final Map<String, String> columnMapping = new HashMap<String, String>();
    columnMapping.put("a", "b");
    SQLASTVisitor visitor = new SQLASTVisitorAdapter() {

        public boolean visit(SQLIdentifierExpr x) {
            String destColumn = columnMapping.get(x.getName());
            if (destColumn != null) {
                x.setName(destColumn);
            }
            return super.visit(x);
        }
    };
    stmt.accept(visitor);
    assertEquals("SELECT b\n" + "FROM t", SQLUtils.toSQLString(stmt));
}
Also used : SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLASTVisitor(com.alibaba.druid.sql.visitor.SQLASTVisitor) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) HashMap(java.util.HashMap) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 4 with SQLASTVisitor

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

the class MySqlSelectTest_226 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT sum(`price`) AS aggregate, `aa` \n" + "FROM `na_orders`\n" + "WHERE `updated_at` > '2018-12-03 00:00:00'\n" + "    AND `pay_status` = 1\n" + "    AND 0 = 1\n" + "ORDER BY 1";
    // System.out.println(sql);
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    assertEquals(1, statementList.size());
    SQLStatement stmt = statementList.get(0);
    SQLASTVisitor v = new MySqlASTVisitorAdapter() {

        public boolean visit(SQLIdentifierExpr x) {
            System.out.println(x.getName());
            return true;
        }
    };
    stmt.accept(v);
// assertEquals("SELECT userid, order, unionid\n" +
// "FROM t", stmt.toString());
// 
// assertEquals("select userid, order, unionid\n" +
// "from t", stmt.clone().toLowerCaseString());
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) SQLASTVisitor(com.alibaba.druid.sql.visitor.SQLASTVisitor) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)4 SQLASTVisitor (com.alibaba.druid.sql.visitor.SQLASTVisitor)4 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)3 SQLASTVisitorAdapter (com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter)2 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)1 SQLUnionQuery (com.alibaba.druid.sql.ast.statement.SQLUnionQuery)1 DB2SelectQueryBlock (com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock)1 DB2ASTVisitorAdapter (com.alibaba.druid.sql.dialect.db2.visitor.DB2ASTVisitorAdapter)1 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)1 MySqlASTVisitorAdapter (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter)1 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)1 HashMap (java.util.HashMap)1