Search in sources :

Example 1 with MySqlOutputVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor in project sharding-jdbc by dangdangdotcom.

the class MySQLSelectVisitor method visit.

@Override
public boolean visit(final SQLAggregateExpr x) {
    if (!(x.getParent() instanceof SQLSelectItem)) {
        return super.visit(x);
    }
    AggregationType aggregationType;
    try {
        aggregationType = AggregationType.valueOf(x.getMethodName().toUpperCase());
    } catch (final IllegalArgumentException ex) {
        return super.visit(x);
    }
    StringBuilder expression = new StringBuilder();
    x.accept(new MySqlOutputVisitor(expression));
    // TODO index获取不准,考虑使用别名替换
    AggregationColumn column = new AggregationColumn(expression.toString(), aggregationType, Optional.fromNullable(((SQLSelectItem) x.getParent()).getAlias()), null == x.getOption() ? Optional.<String>absent() : Optional.of(x.getOption().toString()), getParseContext().getItemIndex());
    getParseContext().getParsedResult().getMergeContext().getAggregationColumns().add(column);
    if (AggregationType.AVG.equals(aggregationType)) {
        getParseContext().addDerivedColumnsForAvgColumn(column);
    // TODO 将AVG列替换成常数,避免数据库再计算无用的AVG函数
    }
    return super.visit(x);
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) AggregationColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn) MySqlOutputVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor) AggregationType(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn.AggregationType)

Example 2 with MySqlOutputVisitor

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

the class Bug_for_weizhi method test_for_issue.

public void test_for_issue() throws Exception {
    String sql = "insert into aaa values(1,2,'这是个反斜杠\\\\');";
    String expected = "INSERT INTO aaa\nVALUES (1, 2, '这是个反斜杠\\\\');";
    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.print(";");
    }
    System.out.println(out.toString());
    Assert.assertEquals(expected, 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 3 with MySqlOutputVisitor

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

the class MySqlPerfTest 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();
    // }
    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 4 with MySqlOutputVisitor

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

the class AssignmentOperatorsTest 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)

Example 5 with MySqlOutputVisitor

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

the class BooleanValuesTest 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