Skip to content

Commit

Permalink
fix #239
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jun 26, 2017
1 parent 325683c commit e469637
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 41 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ next = "/00-overview/contribution/"

### 缺陷修正

1. [ISSUE #239](https://github.com/dangdangdotcom/sharding-jdbc/issues/239) LIMIT路由至多查询结果集,若只有一个不为空的结果集,分页结果不正确
1. [ISSUE #263](https://github.com/dangdangdotcom/sharding-jdbc/issues/263) 分片列和逻辑表配置可忽略大小写

## 1.5.0.M1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,13 @@ public final class ResultSetFactory {
*/
public static ResultSet getResultSet(final List<ResultSet> resultSets, final SQLStatement sqlStatement) throws SQLException {
ShardingResultSets shardingResultSets = new ShardingResultSets(resultSets);
log.debug("Sharding-JDBC: Sharding result sets type is '{}'", shardingResultSets.getType().toString());
switch (shardingResultSets.getType()) {
case EMPTY:
return buildEmpty(resultSets);
case SINGLE:
return buildSingle(shardingResultSets);
case MULTIPLE:
return buildMultiple(shardingResultSets, sqlStatement);
default:
throw new UnsupportedOperationException(shardingResultSets.getType().toString());
if (shardingResultSets.getResultSets().isEmpty()) {
return resultSets.get(0);
}
return buildResultSet(shardingResultSets, sqlStatement);
}

private static ResultSet buildEmpty(final List<ResultSet> resultSets) {
return resultSets.get(0);
}

private static ResultSet buildSingle(final ShardingResultSets shardingResultSets) {
return shardingResultSets.getResultSets().get(0);
}

private static ResultSet buildMultiple(final ShardingResultSets shardingResultSets, final SQLStatement sqlStatement) throws SQLException {
private static ResultSet buildResultSet(final ShardingResultSets shardingResultSets, final SQLStatement sqlStatement) throws SQLException {
ResultSetMergeContext resultSetMergeContext = new ResultSetMergeContext(shardingResultSets, sqlStatement);
return buildCoupling(buildReducer(resultSetMergeContext), resultSetMergeContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ public final class ShardingResultSets {

private final List<ResultSet> resultSets;

private final Type type;

public ShardingResultSets(final List<ResultSet> resultSets) throws SQLException {
this.resultSets = filterResultSets(resultSets);
type = generateType();
}

private List<ResultSet> filterResultSets(final List<ResultSet> resultSets) throws SQLException {
Expand All @@ -53,20 +50,6 @@ private List<ResultSet> filterResultSets(final List<ResultSet> resultSets) throw
return result;
}

private Type generateType() {
if (resultSets.isEmpty()) {
return Type.EMPTY;
} else if (1 == resultSets.size()) {
return Type.SINGLE;
} else {
return Type.MULTIPLE;
}
}

enum Type {
EMPTY, SINGLE, MULTIPLE
}

private static final class FilteredResultSet extends AbstractDelegateResultSet {

private FilteredResultSet(final ResultSet resultSetWhenNextOnce) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import java.util.Arrays;
import java.util.Collections;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -37,15 +35,14 @@ public final class ShardingResultSetsTest {
@Test
public void assertEmptyShardingResultSets() throws SQLException {
ShardingResultSets actual = new ShardingResultSets(Arrays.asList(mock(ResultSet.class), mock(ResultSet.class)));
assertThat(actual.getType(), is(ShardingResultSets.Type.EMPTY));
assertTrue(actual.getResultSets().isEmpty());
}

@Test
public void assertSingleShardingResultSets() throws SQLException {
ResultSet resultSet = MergerTestUtil.mockResult(Collections.<String>emptyList());
when(resultSet.next()).thenReturn(true, false);
ShardingResultSets actual = new ShardingResultSets(Arrays.asList(resultSet, mock(ResultSet.class)));
assertThat(actual.getType(), is(ShardingResultSets.Type.SINGLE));
assertTrue(actual.getResultSets().get(0).next());
assertFalse(actual.getResultSets().get(0).next());
}
Expand All @@ -57,7 +54,6 @@ public void assertMultipleShardingResultSets() throws SQLException {
ResultSet resultSet2 = MergerTestUtil.mockResult(Collections.<String>emptyList());
when(resultSet2.next()).thenReturn(true, false);
ShardingResultSets actual = new ShardingResultSets(Arrays.asList(resultSet1, resultSet2));
assertThat(actual.getType(), is(ShardingResultSets.Type.MULTIPLE));
assertTrue(actual.getResultSets().get(0).next());
assertFalse(actual.getResultSets().get(0).next());
assertTrue(actual.getResultSets().get(1).next());
Expand Down

0 comments on commit e469637

Please sign in to comment.