Skip to content

Commit

Permalink
Decouple AbstractResultSetAdapter and ExecutionContext (#31435)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored May 29, 2024
1 parent f7d7167 commit b02830c
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement;
import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSphereStatement;
import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedOperationResultSet;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;

import java.sql.ResultSet;
Expand All @@ -47,24 +47,24 @@ public abstract class AbstractResultSetAdapter extends AbstractUnsupportedOperat

private final boolean selectContainsEnhancedTable;

private final ExecutionContext executionContext;
private final SQLStatementContext sqlStatementContext;

private final ForceExecuteTemplate<ResultSet> forceExecuteTemplate;

private boolean closed;

protected AbstractResultSetAdapter(final List<ResultSet> resultSets, final Statement statement, final boolean selectContainsEnhancedTable, final ExecutionContext executionContext) {
protected AbstractResultSetAdapter(final List<ResultSet> resultSets, final Statement statement, final boolean selectContainsEnhancedTable, final SQLStatementContext sqlStatementContext) {
Preconditions.checkArgument(!resultSets.isEmpty());
this.resultSets = resultSets;
this.statement = statement;
this.selectContainsEnhancedTable = selectContainsEnhancedTable;
this.executionContext = executionContext;
this.sqlStatementContext = sqlStatementContext;
forceExecuteTemplate = new ForceExecuteTemplate<>();
}

@Override
public final ResultSetMetaData getMetaData() throws SQLException {
return new ShardingSphereResultSetMetaData(resultSets.get(0).getMetaData(), getDatabase(), selectContainsEnhancedTable, executionContext.getSqlStatementContext());
return new ShardingSphereResultSetMetaData(resultSets.get(0).getMetaData(), getDatabase(), selectContainsEnhancedTable, sqlStatementContext);
}

private ShardingSphereDatabase getDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

import org.apache.shardingsphere.driver.jdbc.adapter.AbstractResultSetAdapter;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.driver.jdbc.type.util.ResultSetUtils;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;

import java.io.InputStream;
import java.io.Reader;
Expand Down Expand Up @@ -65,15 +65,15 @@ public final class ShardingSphereResultSet extends AbstractResultSetAdapter {
private final Map<String, Integer> columnLabelAndIndexMap;

public ShardingSphereResultSet(final List<ResultSet> resultSets, final MergedResult mergeResultSet, final Statement statement, final boolean selectContainsEnhancedTable,
final ExecutionContext executionContext) throws SQLException {
super(resultSets, statement, selectContainsEnhancedTable, executionContext);
final SQLStatementContext sqlStatementContext) throws SQLException {
super(resultSets, statement, selectContainsEnhancedTable, sqlStatementContext);
this.mergeResultSet = mergeResultSet;
columnLabelAndIndexMap = ShardingSphereResultSetUtils.createColumnLabelAndIndexMap(executionContext.getSqlStatementContext(), selectContainsEnhancedTable, resultSets.get(0).getMetaData());
columnLabelAndIndexMap = ShardingSphereResultSetUtils.createColumnLabelAndIndexMap(sqlStatementContext, selectContainsEnhancedTable, resultSets.get(0).getMetaData());
}

public ShardingSphereResultSet(final List<ResultSet> resultSets, final MergedResult mergeResultSet, final Statement statement, final boolean selectContainsEnhancedTable,
final ExecutionContext executionContext, final Map<String, Integer> columnLabelAndIndexMap) {
super(resultSets, statement, selectContainsEnhancedTable, executionContext);
final SQLStatementContext sqlStatementContext, final Map<String, Integer> columnLabelAndIndexMap) {
super(resultSets, statement, selectContainsEnhancedTable, sqlStatementContext);
this.mergeResultSet = mergeResultSet;
this.columnLabelAndIndexMap = columnLabelAndIndexMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private ShardingSphereResultSet doExecuteQuery(final ExecutionContext executionC
if (null == columnLabelAndIndexMap) {
columnLabelAndIndexMap = ShardingSphereResultSetUtils.createColumnLabelAndIndexMap(sqlStatementContext, selectContainsEnhancedTable, resultSets.get(0).getMetaData());
}
return new ShardingSphereResultSet(resultSets, mergedResult, this, selectContainsEnhancedTable, executionContext, columnLabelAndIndexMap);
return new ShardingSphereResultSet(resultSets, mergedResult, this, selectContainsEnhancedTable, executionContext.getSqlStatementContext(), columnLabelAndIndexMap);
}

private List<QueryResult> executeQuery0(final ExecutionContext executionContext) throws SQLException {
Expand Down Expand Up @@ -447,7 +447,7 @@ public ResultSet getResultSet() throws SQLException {
if (null == columnLabelAndIndexMap) {
columnLabelAndIndexMap = ShardingSphereResultSetUtils.createColumnLabelAndIndexMap(sqlStatementContext, selectContainsEnhancedTable, resultSets.get(0).getMetaData());
}
currentResultSet = new ShardingSphereResultSet(resultSets, mergedResult, this, selectContainsEnhancedTable, executionContext, columnLabelAndIndexMap);
currentResultSet = new ShardingSphereResultSet(resultSets, mergedResult, this, selectContainsEnhancedTable, sqlStatementContext, columnLabelAndIndexMap);
}
return currentResultSet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private ShardingSphereResultSet doExecuteQuery(final ExecutionContext executionC
MergedResult mergedResult = mergeQuery(queryResults, executionContext.getSqlStatementContext());
boolean selectContainsEnhancedTable =
executionContext.getSqlStatementContext() instanceof SelectStatementContext && ((SelectStatementContext) executionContext.getSqlStatementContext()).isContainsEnhancedTable();
return new ShardingSphereResultSet(getResultSets(), mergedResult, this, selectContainsEnhancedTable, executionContext);
return new ShardingSphereResultSet(getResultSets(), mergedResult, this, selectContainsEnhancedTable, executionContext.getSqlStatementContext());
}

private List<QueryResult> executeQuery0(final ExecutionContext executionContext) throws SQLException {
Expand Down Expand Up @@ -513,7 +513,7 @@ public ResultSet getResultSet() throws SQLException {
SQLStatementContext sqlStatementContext = executionContext.getSqlStatementContext();
MergedResult mergedResult = mergeQuery(getQueryResults(resultSets), sqlStatementContext);
boolean selectContainsEnhancedTable = sqlStatementContext instanceof SelectStatementContext && ((SelectStatementContext) sqlStatementContext).isContainsEnhancedTable();
currentResultSet = new ShardingSphereResultSet(resultSets, mergedResult, this, selectContainsEnhancedTable, executionContext);
currentResultSet = new ShardingSphereResultSet(resultSets, mergedResult, this, selectContainsEnhancedTable, executionContext.getSqlStatementContext());
}
return currentResultSet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.apache.shardingsphere.driver.jdbc.core.resultset.ShardingSphereResultSet;
import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSphereStatement;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.junit.jupiter.api.Test;

Expand All @@ -29,8 +29,8 @@
import java.util.Collections;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -112,6 +112,6 @@ private ShardingSphereResultSet mockShardingSphereResultSet(final ResultSet resu
when(resultSetMetaData.getColumnCount()).thenReturn(1);
when(resultSet.getMetaData()).thenReturn(resultSetMetaData);
return new ShardingSphereResultSet(Collections.singletonList(resultSet), mock(MergedResult.class), mock(ShardingSphereStatement.class, RETURNS_DEEP_STUBS),
true, mock(ExecutionContext.class));
true, mock(SQLStatementContext.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.apache.shardingsphere.driver.jdbc.core.resultset.ShardingSphereResultSet;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -441,8 +440,6 @@ private ShardingSphereResultSet mockShardingSphereResultSet(final MergedResult m
when(resultSetMetaData.getColumnCount()).thenReturn(1);
when(resultSet.getMetaData()).thenReturn(resultSetMetaData);
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class);
ExecutionContext executionContext = mock(ExecutionContext.class);
when(executionContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
return new ShardingSphereResultSet(Collections.singletonList(resultSet), mergedResult, mock(Statement.class), true, executionContext);
return new ShardingSphereResultSet(Collections.singletonList(resultSet), mergedResult, mock(Statement.class), true, sqlStatementContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -70,16 +69,14 @@ class ShardingSphereResultSetTest {
@BeforeEach
void setUp() throws SQLException {
mergeResultSet = mock(MergedResult.class);
shardingSphereResultSet = new ShardingSphereResultSet(getResultSets(), mergeResultSet, getShardingSphereStatement(), true, createExecutionContext());
shardingSphereResultSet = new ShardingSphereResultSet(getResultSets(), mergeResultSet, getShardingSphereStatement(), true, createSQLStatementContext());
}

private ExecutionContext createExecutionContext() {
ExecutionContext result = mock(ExecutionContext.class);
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class);
private SQLStatementContext createSQLStatementContext() {
SQLStatementContext result = mock(SQLStatementContext.class);
TablesContext tablesContext = mock(TablesContext.class);
when(tablesContext.getTableNames()).thenReturn(Collections.emptyList());
when(sqlStatementContext.getTablesContext()).thenReturn(tablesContext);
when(result.getSqlStatementContext()).thenReturn(sqlStatementContext);
when(result.getTablesContext()).thenReturn(tablesContext);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.driver.jdbc.unsupported;

import org.apache.shardingsphere.driver.jdbc.core.resultset.ShardingSphereResultSet;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -40,7 +40,7 @@ class UnsupportedOperationResultSetTest {
@BeforeEach
void init() throws SQLException {
shardingSphereResultSet = new ShardingSphereResultSet(
Collections.singletonList(mock(ResultSet.class, RETURNS_DEEP_STUBS)), mock(MergedResult.class), mock(Statement.class), true, mock(ExecutionContext.class));
Collections.singletonList(mock(ResultSet.class, RETURNS_DEEP_STUBS)), mock(MergedResult.class), mock(Statement.class), true, mock(SQLStatementContext.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.driver.jdbc.unsupported;

import org.apache.shardingsphere.driver.jdbc.core.resultset.ShardingSphereResultSet;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -48,7 +48,7 @@ class UnsupportedUpdateOperationResultSetTest {
@BeforeEach
void init() throws SQLException {
shardingSphereResultSet = new ShardingSphereResultSet(
Collections.singletonList(mock(ResultSet.class, RETURNS_DEEP_STUBS)), mock(MergedResult.class), mock(Statement.class), true, mock(ExecutionContext.class));
Collections.singletonList(mock(ResultSet.class, RETURNS_DEEP_STUBS)), mock(MergedResult.class), mock(Statement.class), true, mock(SQLStatementContext.class));
}

@Test
Expand Down

0 comments on commit b02830c

Please sign in to comment.