Search in sources :

Example 6 with AggregationColumn

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

the class MergerTestUtil method createAggregationColumn.

public static AggregationColumn createAggregationColumn(final AggregationColumn.AggregationType aggregationType, final String name, final String alias, final int index) {
    AggregationColumn result = new AggregationColumn(name, aggregationType, Optional.fromNullable(alias), Optional.<String>absent(), index);
    if (AggregationColumn.AggregationType.AVG.equals(aggregationType)) {
        result.getDerivedColumns().add(new AggregationColumn(AggregationColumn.AggregationType.COUNT.name(), AggregationColumn.AggregationType.COUNT, Optional.of("sharding_gen_1"), Optional.<String>absent()));
        result.getDerivedColumns().add(new AggregationColumn(AggregationColumn.AggregationType.SUM.name(), AggregationColumn.AggregationType.SUM, Optional.of("sharding_gen_2"), Optional.<String>absent()));
    }
    return result;
}
Also used : AggregationColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn)

Example 7 with AggregationColumn

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

the class GroupByResultSetRowTest method assertToString.

@Test
public void assertToString() throws Exception {
    ResultSet rs = MergerTestUtil.mockResult(Arrays.asList("user_id", "number"), Arrays.<ResultSetRow>asList(new TestResultSetRow(1, 10), new TestResultSetRow(1, 20)));
    assertTrue(rs.next());
    GroupByColumn groupByColumn = new GroupByColumn(Optional.<String>absent(), "user_id", Optional.<String>absent(), OrderByColumn.OrderByType.ASC);
    groupByColumn.setColumnIndex(1);
    AggregationColumn aggregationColumn = new AggregationColumn("SUM(0)", AggregationColumn.AggregationType.SUM, Optional.<String>absent(), Optional.<String>absent());
    aggregationColumn.setColumnIndex(2);
    GroupByResultSetRow row = new GroupByResultSetRow(rs, Collections.singletonList(groupByColumn), Collections.singletonList(aggregationColumn));
    row.aggregate();
    assertTrue(rs.next());
    row.aggregate();
    row.generateResult();
    assertFalse(rs.next());
}
Also used : AggregationColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn) ResultSet(java.sql.ResultSet) TestResultSetRow(com.dangdang.ddframe.rdb.sharding.merger.fixture.TestResultSetRow) GroupByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn) Test(org.junit.Test)

Example 8 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)

Example 9 with AggregationColumn

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

the class MySQLSelectVisitor method endVisit.

@Override
public void endVisit(final MySqlSelectQueryBlock x) {
    StringBuilder derivedSelectItems = new StringBuilder();
    for (AggregationColumn aggregationColumn : getParseContext().getParsedResult().getMergeContext().getAggregationColumns()) {
        for (AggregationColumn derivedColumn : aggregationColumn.getDerivedColumns()) {
            derivedSelectItems.append(", ").append(derivedColumn.getExpression()).append(" AS ").append(derivedColumn.getAlias().get());
        }
    }
    appendSortableColumn(derivedSelectItems, getParseContext().getParsedResult().getMergeContext().getGroupByColumns());
    appendSortableColumn(derivedSelectItems, getParseContext().getParsedResult().getMergeContext().getOrderByColumns());
    if (0 != derivedSelectItems.length()) {
        getSQLBuilder().buildSQL(getParseContext().getAutoGenTokenKey(), derivedSelectItems.toString(), true);
    }
    super.endVisit(x);
    stepOutQuery();
}
Also used : AggregationColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn)

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