Skip to content

Commit

Permalink
Refactor ShardingSpherePreparedStatement for support multi executionC…
Browse files Browse the repository at this point in the history
…ontext.
  • Loading branch information
tuichenchuxin committed Oct 19, 2023
1 parent e52bb9b commit d5fb2a4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,15 @@ public ResultSet executeQuery() throws SQLException {

private ShardingSphereResultSet doExecuteQuery(final Collection<ExecutionContext> executionContexts) throws SQLException {
ShardingSphereResultSet result = null;
// TODO support multi execution context, currently executionContexts.size() always equals 1
for (ExecutionContext each : executionContexts) {
List<QueryResult> queryResults = executeQuery0(each);
MergedResult mergedResult = mergeQuery(queryResults, each.getSqlStatementContext());
List<ResultSet> resultSets = getResultSets();
if (null == columnLabelAndIndexMap) {
columnLabelAndIndexMap = ShardingSphereResultSetUtils.createColumnLabelAndIndexMap(sqlStatementContext, selectContainsEnhancedTable, resultSets.get(0).getMetaData());
}
if (null == result) {
result = new ShardingSphereResultSet(resultSets, mergedResult, this, selectContainsEnhancedTable, each, columnLabelAndIndexMap);
}
result = new ShardingSphereResultSet(resultSets, mergedResult, this, selectContainsEnhancedTable, each, columnLabelAndIndexMap);
}
return result;
}
Expand Down Expand Up @@ -384,14 +383,12 @@ public int executeUpdate() throws SQLException {
private int useDriverToExecuteUpdate() throws SQLException {
Integer result = null;
Preconditions.checkArgument(!executionContexts.isEmpty());
// TODO support multi execution context, currently executionContexts.size() always equals 1
for (ExecutionContext each : executionContexts) {
ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext = createExecutionGroupContext(each);
cacheStatements(executionGroupContext.getInputGroups());
int effectedCount = executor.getRegularExecutor().executeUpdate(executionGroupContext,
result = executor.getRegularExecutor().executeUpdate(executionGroupContext,
each.getQueryContext(), each.getRouteContext().getRouteUnits(), createExecuteUpdateCallback());
if (null == result) {
result = effectedCount;
}
}
return result;
}
Expand Down Expand Up @@ -514,14 +511,12 @@ private int executeUpdateWithImplicitCommitTransaction() throws SQLException {
private boolean useDriverToExecute() throws SQLException {
Boolean result = null;
Preconditions.checkArgument(!executionContexts.isEmpty());
// TODO support multi execution context, currently executionContexts.size() always equals 1
for (ExecutionContext each : executionContexts) {
ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext = createExecutionGroupContext(each);
cacheStatements(executionGroupContext.getInputGroups());
boolean isWrite = executor.getRegularExecutor().execute(executionGroupContext,
result = executor.getRegularExecutor().execute(executionGroupContext,
each.getQueryContext(), each.getRouteContext().getRouteUnits(), createExecuteCallback());
if (null == result) {
result = isWrite;
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ public ResultSet executeQuery(final String sql) throws SQLException {

private ShardingSphereResultSet doExecuteQuery(final Collection<ExecutionContext> executionContexts) throws SQLException {
ShardingSphereResultSet result = null;
// TODO support multi execution context, currently executionContexts.size() always equals 1
for (ExecutionContext each : executionContexts) {
List<QueryResult> queryResults = executeQuery0(each);
MergedResult mergedResult = mergeQuery(queryResults, each.getSqlStatementContext());
boolean selectContainsEnhancedTable =
each.getSqlStatementContext() instanceof SelectStatementContext && ((SelectStatementContext) each.getSqlStatementContext()).isContainsEnhancedTable();
if (null == result) {
result = new ShardingSphereResultSet(getResultSets(), mergedResult, this, selectContainsEnhancedTable, each);
}
result = new ShardingSphereResultSet(getResultSets(), mergedResult, this, selectContainsEnhancedTable, each);

}
return result;
}
Expand Down Expand Up @@ -363,15 +363,13 @@ private int useDriverToExecuteUpdate(final ExecuteUpdateCallback updateCallback,
final Collection<ExecutionContext> executionContexts) throws SQLException {
Integer result = null;
Preconditions.checkArgument(!executionContexts.isEmpty());
// TODO support multi execution context, currently executionContexts.size() always equals 1
for (ExecutionContext each : executionContexts) {
ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext = createExecutionGroupContext(each);
cacheStatements(executionGroupContext.getInputGroups());
JDBCExecutorCallback<Integer> callback = createExecuteUpdateCallback(updateCallback, sqlStatementContext);
int effectedCount = executor.getRegularExecutor().executeUpdate(executionGroupContext,
result = executor.getRegularExecutor().executeUpdate(executionGroupContext,
each.getQueryContext(), each.getRouteContext().getRouteUnits(), callback);
if (null == result) {
result = effectedCount;
}
}
return result;
}
Expand Down Expand Up @@ -576,15 +574,13 @@ private boolean executeWithImplicitCommitTransaction(final ExecuteCallback callb
private boolean useDriverToExecute(final ExecuteCallback callback, final Collection<ExecutionContext> executionContexts) throws SQLException {
Boolean result = null;
Preconditions.checkArgument(!executionContexts.isEmpty());
// TODO support multi execution context, currently executionContexts.size() always equals 1
for (ExecutionContext each : executionContexts) {
ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext = createExecutionGroupContext(each);
cacheStatements(executionGroupContext.getInputGroups());
JDBCExecutorCallback<Boolean> jdbcExecutorCallback = createExecuteCallback(callback, each.getSqlStatementContext().getSqlStatement());
boolean isWrite = executor.getRegularExecutor().execute(executionGroupContext,
result = executor.getRegularExecutor().execute(executionGroupContext,
each.getQueryContext(), each.getRouteContext().getRouteUnits(), jdbcExecutorCallback);
if (null == result) {
result = isWrite;
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,9 @@ private ResponseHeader doExecuteWithImplicitCommitTransaction(final Collection<E

private ResponseHeader doExecute(final Collection<ExecutionContext> executionContexts) throws SQLException {
ResponseHeader result = null;
// TODO support multi execution context, currently executionContexts.size() always equals 1
for (ExecutionContext each : executionContexts) {
ResponseHeader responseHeader = doExecute(each);
if (null == result) {
result = responseHeader;
}
result = doExecute(each);
}
return result;
}
Expand Down

0 comments on commit d5fb2a4

Please sign in to comment.