Search in sources :

Example 1 with MergeContext

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

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

the class ResultSetMergeContextTest method assertIsNeedMemorySortForGroupByWithGroupByAndOrderBySame.

@Test
public void assertIsNeedMemorySortForGroupByWithGroupByAndOrderBySame() throws SQLException {
    MergeContext mergeContext = new MergeContext();
    mergeContext.getOrderByColumns().add(new OrderByColumn("col", OrderByColumn.OrderByType.ASC));
    mergeContext.getGroupByColumns().add(new GroupByColumn(Optional.<String>absent(), "col", Optional.<String>absent(), OrderByColumn.OrderByType.ASC));
    ResultSetMergeContext actual = new ResultSetMergeContext(new ShardingResultSets(Collections.singletonList(MergerTestUtil.mockResult(Collections.singletonList("col")))), mergeContext);
    assertFalse(actual.isNeedMemorySortForGroupBy());
}
Also used : MergeContext(com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext) GroupByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn) OrderByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn) Test(org.junit.Test)

Example 3 with MergeContext

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

the class ResultSetMergeContextTest method createMergeContext.

private MergeContext createMergeContext() {
    MergeContext result = new MergeContext();
    result.getOrderByColumns().add(new OrderByColumn("order_col", OrderByColumn.OrderByType.ASC));
    result.getGroupByColumns().add(new GroupByColumn(Optional.<String>absent(), "group_col", Optional.<String>absent(), OrderByColumn.OrderByType.ASC));
    result.getAggregationColumns().add(MergerTestUtil.createAggregationColumn(AggregationColumn.AggregationType.COUNT, "count_col", "count_col", -1));
    result.getAggregationColumns().add(MergerTestUtil.createAggregationColumn(AggregationColumn.AggregationType.AVG, "avg_col", "avg_col", -1));
    return result;
}
Also used : MergeContext(com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext) GroupByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn) OrderByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn)

Example 4 with MergeContext

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

the class ResultSetMergeContextTest method assertSetOrderByKeysToCurrentOrderByKeys.

@Test
public void assertSetOrderByKeysToCurrentOrderByKeys() throws SQLException {
    MergeContext mergeContext = new MergeContext();
    mergeContext.getOrderByColumns().add(new OrderByColumn("order_col", OrderByColumn.OrderByType.ASC));
    mergeContext.getGroupByColumns().add(new GroupByColumn(Optional.<String>absent(), "group_col", Optional.<String>absent(), OrderByColumn.OrderByType.ASC));
    ResultSetMergeContext actual = new ResultSetMergeContext(new ShardingResultSets(Collections.singletonList(MergerTestUtil.mockResult(Arrays.asList("order_col", "group_col")))), mergeContext);
    actual.setGroupByKeysToCurrentOrderByKeys();
    actual.setOrderByKeysToCurrentOrderByKeys();
    assertThat(actual.getCurrentOrderByKeys().size(), is(1));
    assertThat(actual.getCurrentOrderByKeys().get(0).getColumnName().get(), is("order_col"));
}
Also used : MergeContext(com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext) GroupByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn) OrderByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn) Test(org.junit.Test)

Example 5 with MergeContext

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

the class IteratorResultSetTest method assertNextWithLimitForPartData.

@Test
public void assertNextWithLimitForPartData() throws SQLException {
    MergeContext mergeContext = new MergeContext();
    mergeContext.setLimit(new Limit(1, 1, -1, -1));
    ResultSet resultSet = ResultSetFactory.getResultSet(Arrays.<ResultSet>asList(new MockResultSet<>(1), new MockResultSet<>(2, 4), new MockResultSet<Integer>()), mergeContext);
    int count = 0;
    while (resultSet.next()) {
        count++;
    }
    assertThat(count, is(1));
}
Also used : ResultSet(java.sql.ResultSet) MockResultSet(com.dangdang.ddframe.rdb.sharding.merger.fixture.MockResultSet) MergeContext(com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext) Limit(com.dangdang.ddframe.rdb.sharding.parser.result.merger.Limit) MockResultSet(com.dangdang.ddframe.rdb.sharding.merger.fixture.MockResultSet) Test(org.junit.Test)

Aggregations

MergeContext (com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext)17 Test (org.junit.Test)13 OrderByColumn (com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn)9 GroupByColumn (com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn)8 ResultSet (java.sql.ResultSet)5 MockResultSet (com.dangdang.ddframe.rdb.sharding.merger.fixture.MockResultSet)3 Limit (com.dangdang.ddframe.rdb.sharding.parser.result.merger.Limit)3 PreparedStatementExecutorWrapper (com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper)1 TestResultSetRow (com.dangdang.ddframe.rdb.sharding.merger.fixture.TestResultSetRow)1 Value (com.dangdang.ddframe.rdb.sharding.parser.jaxb.Value)1 AggregationColumn (com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn)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 SQLExecutionUnit (com.dangdang.ddframe.rdb.sharding.router.SQLExecutionUnit)1 SQLRouteResult (com.dangdang.ddframe.rdb.sharding.router.SQLRouteResult)1 Function (com.google.common.base.Function)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1