Skip to content

Commit

Permalink
fix #491
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Dec 12, 2017
1 parent 099b4db commit 94cf835
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package io.shardingjdbc.core.jdbc.adapter;

import io.shardingjdbc.core.jdbc.unsupported.AbstractUnsupportedOperationResultSet;
import com.google.common.base.Preconditions;
import io.shardingjdbc.core.jdbc.unsupported.AbstractUnsupportedOperationResultSet;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -41,28 +41,26 @@ public abstract class AbstractResultSetAdapter extends AbstractUnsupportedOperat

@Getter
private final List<ResultSet> resultSets;


@Getter
private final Statement statement;

private boolean closed;

public AbstractResultSetAdapter(final List<ResultSet> resultSets) {
public AbstractResultSetAdapter(final List<ResultSet> resultSets, final Statement statement) {
Preconditions.checkArgument(!resultSets.isEmpty());
this.resultSets = resultSets;
}

@Override
// TODO should return sharding statement in future
public final Statement getStatement() throws SQLException {
return getResultSets().get(0).getStatement();
this.statement = statement;
}

@Override
public final ResultSetMetaData getMetaData() throws SQLException {
return getResultSets().get(0).getMetaData();
return resultSets.get(0).getMetaData();
}

@Override
public int findColumn(final String columnLabel) throws SQLException {
return getResultSets().get(0).findColumn(columnLabel);
return resultSets.get(0).findColumn(columnLabel);
}

@Override
Expand Down Expand Up @@ -99,7 +97,7 @@ public final void setFetchDirection(final int direction) throws SQLException {

@Override
public final int getFetchDirection() throws SQLException {
return getResultSets().get(0).getFetchDirection();
return resultSets.get(0).getFetchDirection();
}

@Override
Expand All @@ -117,28 +115,28 @@ public final void setFetchSize(final int rows) throws SQLException {

@Override
public final int getFetchSize() throws SQLException {
return getResultSets().get(0).getFetchSize();
return resultSets.get(0).getFetchSize();
}

@Override
public final int getType() throws SQLException {
return getResultSets().get(0).getType();
return resultSets.get(0).getType();
}

@Override
public final int getConcurrency() throws SQLException {
return getResultSets().get(0).getConcurrency();
return resultSets.get(0).getConcurrency();
}

@Override
public final SQLWarning getWarnings() throws SQLException {
return getResultSets().get(0).getWarnings();
return resultSets.get(0).getWarnings();
}

@Override
public final void clearWarnings() throws SQLException {
Collection<SQLException> exceptions = new LinkedList<>();
for (ResultSet each : getResultSets()) {
for (ResultSet each : resultSets) {
try {
each.clearWarnings();
} catch (final SQLException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
Expand All @@ -45,8 +46,8 @@ public final class ShardingResultSet extends AbstractResultSetAdapter {

private final ResultSetMerger mergeResultSet;

public ShardingResultSet(final List<ResultSet> resultSets, final ResultSetMerger mergeResultSet) {
super(resultSets);
public ShardingResultSet(final List<ResultSet> resultSets, final ResultSetMerger mergeResultSet, final Statement statement) {
super(resultSets, statement);
this.mergeResultSet = mergeResultSet;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public ResultSet executeQuery() throws SQLException {
Collection<PreparedStatementUnit> preparedStatementUnits = route();
List<ResultSet> resultSets = new PreparedStatementExecutor(
getConnection().getShardingContext().getExecutorEngine(), routeResult.getSqlStatement().getType(), preparedStatementUnits, getParameters()).executeQuery();
result = new ShardingResultSet(resultSets, new MergeEngine(resultSets, (SelectStatement) routeResult.getSqlStatement()).merge());
result = new ShardingResultSet(resultSets, new MergeEngine(resultSets, (SelectStatement) routeResult.getSqlStatement()).merge(), this);
} finally {
clearBatch();
}
Expand Down Expand Up @@ -271,7 +271,7 @@ public ResultSet getResultSet() throws SQLException {
for (PreparedStatement each : routedStatements) {
resultSets.add(each.getResultSet());
}
currentResultSet = new ShardingResultSet(resultSets, new MergeEngine(resultSets, (SelectStatement) routeResult.getSqlStatement()).merge());
currentResultSet = new ShardingResultSet(resultSets, new MergeEngine(resultSets, (SelectStatement) routeResult.getSqlStatement()).merge(), this);
return currentResultSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public ResultSet executeQuery(final String sql) throws SQLException {
try {
List<ResultSet> resultSets = generateExecutor(sql).executeQuery();
result = new ShardingResultSet(
resultSets, new MergeEngine(resultSets, (SelectStatement) routeResult.getSqlStatement()).merge());
resultSets, new MergeEngine(resultSets, (SelectStatement) routeResult.getSqlStatement()).merge(), this);
} finally {
currentResultSet = null;
}
Expand Down Expand Up @@ -247,7 +247,7 @@ public ResultSet getResultSet() throws SQLException {
for (Statement each : routedStatements) {
resultSets.add(each.getResultSet());
}
currentResultSet = new ShardingResultSet(resultSets, new MergeEngine(resultSets, (SelectStatement) routeResult.getSqlStatement()).merge());
currentResultSet = new ShardingResultSet(resultSets, new MergeEngine(resultSets, (SelectStatement) routeResult.getSqlStatement()).merge(), this);
return currentResultSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
Expand All @@ -53,11 +54,14 @@ public final class ShardingResultSetTest {
@Mock
private ResultSetMerger mergeResultSet;

@Mock
private Statement statement;

private ShardingResultSet shardingResultSet;

@Before
public void setUp() {
shardingResultSet = new ShardingResultSet(Collections.singletonList(mock(ResultSet.class)), mergeResultSet);
shardingResultSet = new ShardingResultSet(Collections.singletonList(mock(ResultSet.class)), mergeResultSet, statement);
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions sharding-jdbc-core/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<logger name="org.dbunit" level="info" additivity="false">
<appender-ref ref="console"/>
</logger>
<logger name="io.shardingjdbc.core.executor.threadlocal.ExecutorExceptionHandler" level="off">
</logger>
<logger name="io.shardingjdbc.core.executor.threadlocal.ExecutorExceptionHandler" level="off" />
<logger name="org.dbunit.database.DatabaseTableMetaData" level="off" />

<root>
<level value="error" />
Expand Down

0 comments on commit 94cf835

Please sign in to comment.