Search in sources :

Example 1 with AggregationColumn

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

use of com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn in project sharding-jdbc by dangdangdotcom.

the class AbstractBaseParseTest method getDataParameter.

private static Object[] getDataParameter(final Assert assertObj) {
    Object[] result = new Object[6];
    result[0] = assertObj.getId();
    result[1] = assertObj.getSql();
    result[2] = assertObj.getExpectedSQL();
    result[3] = Lists.transform(assertObj.getTables(), new Function<com.dangdang.ddframe.rdb.sharding.parser.jaxb.Table, Table>() {

        @Override
        public Table apply(final com.dangdang.ddframe.rdb.sharding.parser.jaxb.Table input) {
            return new Table(input.getName(), input.getAlias());
        }
    });
    if (null == assertObj.getConditionContexts()) {
        result[4] = Collections.<ConditionContext>emptyList();
    } else {
        result[4] = Lists.transform(assertObj.getConditionContexts(), new Function<com.dangdang.ddframe.rdb.sharding.parser.jaxb.ConditionContext, ConditionContext>() {

            @Override
            public ConditionContext apply(final com.dangdang.ddframe.rdb.sharding.parser.jaxb.ConditionContext input) {
                ConditionContext result = new ConditionContext();
                if (null == input.getConditions()) {
                    return result;
                }
                for (com.dangdang.ddframe.rdb.sharding.parser.jaxb.Condition each : input.getConditions()) {
                    Condition condition = new Condition(new Column(each.getColumnName(), each.getTableName()), BinaryOperator.valueOf(each.getOperator().toUpperCase()));
                    condition.getValues().addAll(Lists.transform(each.getValues(), new Function<Value, Comparable<?>>() {

                        @Override
                        public Comparable<?> apply(final Value input) {
                            return input.getValueWithType();
                        }
                    }));
                    if (null != each.getValueIndices()) {
                        condition.getValueIndices().addAll(each.getValueIndices());
                    }
                    result.add(condition);
                }
                return result;
            }
        });
    }
    MergeContext mergeContext = new MergeContext();
    if (null != assertObj.getOrderByColumns()) {
        mergeContext.getOrderByColumns().addAll(Lists.transform(assertObj.getOrderByColumns(), new Function<com.dangdang.ddframe.rdb.sharding.parser.jaxb.OrderByColumn, OrderByColumn>() {

            @Override
            public OrderByColumn apply(final com.dangdang.ddframe.rdb.sharding.parser.jaxb.OrderByColumn input) {
                return Strings.isNullOrEmpty(input.getName()) ? new OrderByColumn(input.getIndex(), OrderByType.valueOf(input.getOrderByType().toUpperCase())) : new OrderByColumn(Optional.fromNullable(input.getOwner()), input.getName(), Optional.fromNullable(input.getAlias()), OrderByType.valueOf(input.getOrderByType().toUpperCase()));
            }
        }));
    }
    if (null != assertObj.getGroupByColumns()) {
        mergeContext.getGroupByColumns().addAll(Lists.transform(assertObj.getGroupByColumns(), new Function<com.dangdang.ddframe.rdb.sharding.parser.jaxb.GroupByColumn, GroupByColumn>() {

            @Override
            public GroupByColumn apply(final com.dangdang.ddframe.rdb.sharding.parser.jaxb.GroupByColumn input) {
                return new GroupByColumn(Optional.fromNullable(input.getOwner()), input.getName(), Optional.fromNullable(input.getAlias()), OrderByType.valueOf(input.getOrderByType().toUpperCase()));
            }
        }));
    }
    if (null != assertObj.getAggregationColumns()) {
        mergeContext.getAggregationColumns().addAll(Lists.transform(assertObj.getAggregationColumns(), new Function<com.dangdang.ddframe.rdb.sharding.parser.jaxb.AggregationColumn, AggregationColumn>() {

            @Override
            public AggregationColumn apply(final com.dangdang.ddframe.rdb.sharding.parser.jaxb.AggregationColumn input) {
                AggregationColumn result = new AggregationColumn(input.getExpression(), AggregationType.valueOf(input.getAggregationType().toUpperCase()), Optional.fromNullable(input.getAlias()), Optional.fromNullable(input.getOption()));
                if (null != input.getIndex()) {
                    result.setColumnIndex(input.getIndex());
                }
                for (com.dangdang.ddframe.rdb.sharding.parser.jaxb.AggregationColumn each : input.getDerivedColumns()) {
                    result.getDerivedColumns().add(new AggregationColumn(each.getExpression(), AggregationType.valueOf(each.getAggregationType().toUpperCase()), Optional.fromNullable(each.getAlias()), Optional.fromNullable(each.getOption())));
                }
                return result;
            }
        }));
    }
    if (null != assertObj.getLimit()) {
        mergeContext.setLimit(new Limit(assertObj.getLimit().getOffset(), assertObj.getLimit().getRowCount(), assertObj.getLimit().getOffsetParameterIndex(), assertObj.getLimit().getRowCountParameterIndex()));
    }
    result[5] = mergeContext;
    return result;
}
Also used : Condition(com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition) Table(com.dangdang.ddframe.rdb.sharding.parser.result.router.Table) AggregationColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn) GroupByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn) OrderByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn) Function(com.google.common.base.Function) ConditionContext(com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext) OrderByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn) AggregationColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn) Column(com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition.Column) GroupByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn) Value(com.dangdang.ddframe.rdb.sharding.parser.jaxb.Value) MergeContext(com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext) Limit(com.dangdang.ddframe.rdb.sharding.parser.result.merger.Limit)

Example 3 with AggregationColumn

use of com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn in project sharding-jdbc by dangdangdotcom.

the class AbstractBaseParseTest method assertMergeContext.

private void assertMergeContext(final SQLParsedResult actual) {
    for (OrderByColumn each : actual.getMergeContext().getOrderByColumns()) {
        assertThat(each, new ReflectionEquals(orderByColumns.next()));
    }
    assertFalse(orderByColumns.hasNext());
    for (GroupByColumn each : actual.getMergeContext().getGroupByColumns()) {
        assertThat(each, new ReflectionEquals(groupByColumns.next()));
    }
    assertFalse(groupByColumns.hasNext());
    for (AggregationColumn each : actual.getMergeContext().getAggregationColumns()) {
        AggregationColumn expected = aggregationColumns.next();
        assertThat(each, new ReflectionEquals(expected, "derivedColumns"));
        for (int i = 0; i < each.getDerivedColumns().size(); i++) {
            assertThat(each.getDerivedColumns().get(i), new ReflectionEquals(expected.getDerivedColumns().get(i)));
        }
    }
    assertFalse(aggregationColumns.hasNext());
    assertThat(actual.getMergeContext().getLimit(), new ReflectionEquals(limit));
}
Also used : AggregationColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn) ReflectionEquals(org.mockito.internal.matchers.apachecommons.ReflectionEquals) GroupByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn) OrderByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn)

Example 4 with AggregationColumn

use of com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn in project sharding-jdbc by dangdangdotcom.

the class SQLParsedResultTest method generateMergeContext.

private void generateMergeContext(final MergeContext mergeContext) {
    mergeContext.getAggregationColumns().add(new AggregationColumn("COUNT(id)", AggregationType.COUNT, Optional.of("c"), Optional.<String>absent()));
    mergeContext.getOrderByColumns().add(new OrderByColumn(Optional.<String>absent(), "id", Optional.of("a"), OrderByType.DESC));
    mergeContext.getGroupByColumns().add(new GroupByColumn(Optional.<String>absent(), "id", Optional.of("d"), OrderByType.ASC));
    mergeContext.setLimit(new Limit(0, 10, -1, -1));
}
Also used : AggregationColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn) Limit(com.dangdang.ddframe.rdb.sharding.parser.result.merger.Limit) GroupByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn) OrderByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn)

Example 5 with AggregationColumn

use of com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn in project sharding-jdbc by dangdangdotcom.

the class ResultSetMergeContext method getAllFocusedColumns.

private List<IndexColumn> getAllFocusedColumns() {
    List<IndexColumn> result = new LinkedList<>();
    result.addAll(mergeContext.getGroupByColumns());
    result.addAll(mergeContext.getOrderByColumns());
    LinkedList<AggregationColumn> allAggregationColumns = Lists.newLinkedList(mergeContext.getAggregationColumns());
    while (!allAggregationColumns.isEmpty()) {
        AggregationColumn firstElement = allAggregationColumns.poll();
        result.add(firstElement);
        if (!firstElement.getDerivedColumns().isEmpty()) {
            allAggregationColumns.addAll(firstElement.getDerivedColumns());
        }
    }
    return result;
}
Also used : AggregationColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn) LinkedList(java.util.LinkedList) IndexColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.IndexColumn)

Aggregations

AggregationColumn (com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn)9 GroupByColumn (com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn)4 OrderByColumn (com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn)3 TestResultSetRow (com.dangdang.ddframe.rdb.sharding.merger.fixture.TestResultSetRow)2 Limit (com.dangdang.ddframe.rdb.sharding.parser.result.merger.Limit)2 ResultSet (java.sql.ResultSet)2 Test (org.junit.Test)2 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)1 MySqlOutputVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor)1 Value (com.dangdang.ddframe.rdb.sharding.parser.jaxb.Value)1 AggregationType (com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn.AggregationType)1 IndexColumn (com.dangdang.ddframe.rdb.sharding.parser.result.merger.IndexColumn)1 MergeContext (com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext)1 Condition (com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition)1 Column (com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition.Column)1 ConditionContext (com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext)1 Table (com.dangdang.ddframe.rdb.sharding.parser.result.router.Table)1 Function (com.google.common.base.Function)1 LinkedList (java.util.LinkedList)1 ReflectionEquals (org.mockito.internal.matchers.apachecommons.ReflectionEquals)1