Search in sources :

Example 6 with MergeContext

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

the class ShardingPreparedStatement method routeSQL.

private List<PreparedStatementExecutorWrapper> routeSQL() throws SQLException {
    List<PreparedStatementExecutorWrapper> result = new ArrayList<>();
    SQLRouteResult sqlRouteResult = preparedSQLRouter.route(getParameters());
    MergeContext mergeContext = sqlRouteResult.getMergeContext();
    setMergeContext(mergeContext);
    setGeneratedKeyContext(sqlRouteResult.getGeneratedKeyContext());
    for (SQLExecutionUnit each : sqlRouteResult.getExecutionUnits()) {
        PreparedStatement preparedStatement = (PreparedStatement) getStatement(getShardingConnection().getConnection(each.getDataSource(), sqlRouteResult.getSqlStatementType()), each.getSql());
        replayMethodsInvocation(preparedStatement);
        getParameters().replayMethodsInvocation(preparedStatement);
        result.add(wrap(preparedStatement, each));
    }
    return result;
}
Also used : SQLRouteResult(com.dangdang.ddframe.rdb.sharding.router.SQLRouteResult) PreparedStatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper) ArrayList(java.util.ArrayList) SQLExecutionUnit(com.dangdang.ddframe.rdb.sharding.router.SQLExecutionUnit) PreparedStatement(java.sql.PreparedStatement) MergeContext(com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext)

Example 7 with MergeContext

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

the class AggregationResultSetTest method assertNext.

@Test
public void assertNext() throws SQLException {
    MergeContext mergeContext = new MergeContext();
    mergeContext.getAggregationColumns().add(MergerTestUtil.createAggregationColumn(aggregationType, columnNames.get(0), null, 1));
    ResultSet resultSet = ResultSetFactory.getResultSet(Arrays.asList(MergerTestUtil.mockResult(columnNames, Collections.<ResultSetRow>singletonList(new TestResultSetRow(resultSetData1))), MergerTestUtil.mockResult(columnNames, Collections.<ResultSetRow>singletonList(new TestResultSetRow(resultSetData2))), MergerTestUtil.mockResult(Collections.<String>emptyList())), mergeContext);
    assertTrue(resultSet.next());
    if (AggregationType.AVG == aggregationType) {
        assertThat(resultSet.getDouble(1), is(result));
        assertThat(resultSet.getDouble(columnNames.get(0)), is(result));
    } else {
        assertThat(resultSet.getInt(1), is(result));
        assertThat(resultSet.getInt(columnNames.get(0)), is(result));
    }
    assertFalse(resultSet.next());
}
Also used : ResultSet(java.sql.ResultSet) TestResultSetRow(com.dangdang.ddframe.rdb.sharding.merger.fixture.TestResultSetRow) MergeContext(com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext) Test(org.junit.Test)

Example 8 with MergeContext

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

the class NullableAggregationResultSetTest method assertNullable.

@Test
public void assertNullable() throws SQLException {
    MergeContext mergeContext = new MergeContext();
    mergeContext.getAggregationColumns().add(MergerTestUtil.createAggregationColumn(aggregationType, aggregationType.name() + "(*)", aggregationType.name(), 1));
    ResultSet resultSet1;
    ResultSet resultSet2;
    if (aggregationType == AggregationColumn.AggregationType.AVG) {
        resultSet1 = MergerTestUtil.mockResult(Arrays.asList(aggregationType.name(), "sharding_gen_1", "sharding_gen_2"));
        resultSet2 = MergerTestUtil.mockResult(Arrays.asList(aggregationType.name(), "sharding_gen_1", "sharding_gen_2"));
    } else {
        resultSet1 = MergerTestUtil.mockResult(Collections.singletonList(aggregationType.name()));
        resultSet2 = MergerTestUtil.mockResult(Collections.singletonList(aggregationType.name()));
    }
    when(resultSet1.next()).thenReturn(true, false);
    when(resultSet2.next()).thenReturn(true, false);
    ResultSet actual = ResultSetFactory.getResultSet(Arrays.asList(resultSet1, resultSet2), mergeContext);
    assertTrue(actual.next());
    assertNull(actual.getObject(1));
    assertThat(actual.getInt(1), is(0));
    assertFalse(actual.next());
}
Also used : ResultSet(java.sql.ResultSet) MergeContext(com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext) Test(org.junit.Test)

Example 9 with MergeContext

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

the class OrderByResultSetTest method createMergeContext.

private MergeContext createMergeContext(final OrderByType orderType) {
    MergeContext result = new MergeContext();
    result.getOrderByColumns().add(new OrderByColumn("name", orderType));
    return result;
}
Also used : MergeContext(com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext) OrderByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn)

Example 10 with MergeContext

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

the class IteratorResultSetTest method assertNextWithLimitForAllData.

@Test
public void assertNextWithLimitForAllData() throws SQLException {
    MergeContext mergeContext = new MergeContext();
    mergeContext.setLimit(new Limit(1, 10, -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(2));
}
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