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;
}
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());
}
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;
}
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();
}
Aggregations