Search in sources :

Example 46 with MySqlOutputVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor in project druid by alibaba.

the class SQLMergeTest method merge2.

private String merge2() {
    String sql = "INSERT INTO T (F1, F2, F3, F4, F5) VALUES (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?)";
    StringBuilder out = new StringBuilder();
    MySqlOutputVisitor visitor = new MySqlOutputVisitor(out) {

        public boolean visit(SQLInListExpr x) {
            x.getExpr().accept(this);
            if (x.isNot()) {
                print(" NOT IN (##)");
            } else {
                print(" IN (##)");
            }
            return false;
        }

        @Override
        public boolean visit(MySqlInsertStatement x) {
            print("INSERT ");
            if (x.isLowPriority()) {
                print("LOW_PRIORITY ");
            }
            if (x.isDelayed()) {
                print("DELAYED ");
            }
            if (x.isHighPriority()) {
                print("HIGH_PRIORITY ");
            }
            if (x.isIgnore()) {
                print("IGNORE ");
            }
            print("INTO ");
            x.getTableSource().accept(this);
            if (x.getColumns().size() > 0) {
                print(" (");
                for (int i = 0, size = x.getColumns().size(); i < size; ++i) {
                    if (i != 0) {
                        print(", ");
                    }
                    x.getColumns().get(i).accept(this);
                }
                print(")");
            }
            if (x.getValuesList().size() != 0) {
                print(" VALUES ");
                int size = x.getValuesList().size();
                if (size == 0) {
                    print("()");
                } else {
                    for (int i = 0; i < 1; ++i) {
                        if (i != 0) {
                            print(", ");
                        }
                        x.getValuesList().get(i).accept(this);
                    }
                }
            }
            if (x.getQuery() != null) {
                print(" ");
                x.getQuery().accept(this);
            }
            if (x.getDuplicateKeyUpdate().size() != 0) {
                print(" ON DUPLICATE KEY UPDATE ");
                printAndAccept(x.getDuplicateKeyUpdate(), ", ");
            }
            return false;
        }
    };
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    for (SQLStatement statement : statementList) {
        statement.accept(visitor);
        visitor.println();
    }
    return out.toString();
}
Also used : SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) MySqlOutputVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 47 with MySqlOutputVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor in project druid by alibaba.

the class PerfTest method execMySql.

private String execMySql(String sql) {
    StringBuilder out = new StringBuilder();
    MySqlOutputVisitor visitor = new MySqlOutputVisitor(out);
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    for (SQLStatement statement : statementList) {
        statement.accept(visitor);
        visitor.println();
    }
    return out.toString();
}
Also used : MySqlOutputVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 48 with MySqlOutputVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor in project druid by alibaba.

the class Demo0 method test_demo_0.

public void test_demo_0() throws Exception {
    String sql = "SELECT UUID();";
    // parser得到AST
    SQLStatementParser parser = new MySqlStatementParser(sql);
    //
    List<SQLStatement> stmtList = parser.parseStatementList();
    // 将AST通过visitor输出
    StringBuilder out = new StringBuilder();
    MySqlOutputVisitor visitor = new MySqlOutputVisitor(out);
    for (SQLStatement stmt : stmtList) {
        stmt.accept(visitor);
        out.append(";");
    }
    System.out.println(out.toString());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MySqlOutputVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 49 with MySqlOutputVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor in project druid by alibaba.

the class OdpsFileTest method mergValidate.

void mergValidate(String sql, String expect) {
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    StringBuilder out = new StringBuilder();
    MySqlOutputVisitor visitor = new MySqlOutputVisitor(out, true);
    statemen.accept(visitor);
    System.out.println(out.toString());
    Assert.assertEquals(expect, out.toString());
}
Also used : MySqlOutputVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 50 with MySqlOutputVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor in project druid by alibaba.

the class EncryptionAndCompressionFunctionsTest method output.

private String output(List<SQLStatement> stmtList) {
    StringBuilder out = new StringBuilder();
    for (SQLStatement stmt : stmtList) {
        stmt.accept(new MySqlOutputVisitor(out));
        out.append(";");
    }
    return out.toString();
}
Also used : MySqlOutputVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

MySqlOutputVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor)61 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)60 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)20 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)1 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)1 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)1 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)1 AggregationColumn (com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn)1 AggregationType (com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn.AggregationType)1