Search in sources :

Example 1 with AggregationType

use of com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn.AggregationType 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)

Aggregations

SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)1 MySqlOutputVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor)1 AggregationColumn (com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn)1 AggregationType (com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn.AggregationType)1