Skip to content

Commit

Permalink
Use UpdateResult instead of ExecuteResult in UpdateResponseHeader (ap…
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Dec 1, 2020
1 parent d238cc3 commit 13574e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import org.apache.shardingsphere.infra.executor.sql.execute.result.update.UpdateResult;
import org.apache.shardingsphere.infra.executor.sql.log.SQLLogger;
import org.apache.shardingsphere.infra.merge.MergeEngine;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
Expand Down Expand Up @@ -98,30 +99,30 @@ private ResponseHeader doExecute(final ExecutionContext executionContext) throws
Collection<ExecuteResult> executeResults = proxySQLExecutor.execute(executionContext);
ExecuteResult executeResultSample = executeResults.iterator().next();
return executeResultSample instanceof QueryResult
? processExecuteQuery(executionContext, executeResults, (QueryResult) executeResultSample) : processExecuteUpdate(executionContext, executeResults);
? processExecuteQuery(executionContext, executeResults.stream().map(each -> (QueryResult) each).collect(Collectors.toList()), (QueryResult) executeResultSample)
: processExecuteUpdate(executionContext, executeResults.stream().map(each -> (UpdateResult) each).collect(Collectors.toList()));
}

private QueryResponseHeader processExecuteQuery(final ExecutionContext executionContext,
final Collection<ExecuteResult> executeResults, final QueryResult executeResultSample) throws SQLException {
queryHeaders = createQueryHeaders(executionContext, executeResultSample);
mergedResult = mergeQuery(executionContext.getSqlStatementContext(), executeResults.stream().map(each -> (QueryResult) each).collect(Collectors.toList()));
private QueryResponseHeader processExecuteQuery(final ExecutionContext executionContext, final List<QueryResult> queryResults, final QueryResult queryResultSample) throws SQLException {
queryHeaders = createQueryHeaders(executionContext, queryResultSample);
mergedResult = mergeQuery(executionContext.getSqlStatementContext(), queryResults);
return new QueryResponseHeader(queryHeaders);
}

private List<QueryHeader> createQueryHeaders(final ExecutionContext executionContext, final QueryResult executeResultSample) throws SQLException {
int columnCount = executeResultSample.getMetaData().getColumnCount();
private List<QueryHeader> createQueryHeaders(final ExecutionContext executionContext, final QueryResult queryResultSample) throws SQLException {
int columnCount = queryResultSample.getMetaData().getColumnCount();
List<QueryHeader> result = new ArrayList<>(columnCount);
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
result.add(createQueryHeader(executionContext, executeResultSample, metaData, columnIndex));
result.add(createQueryHeader(executionContext, queryResultSample, metaData, columnIndex));
}
return result;
}

private QueryHeader createQueryHeader(final ExecutionContext executionContext,
final QueryResult executeResultSample, final ShardingSphereMetaData metaData, final int columnIndex) throws SQLException {
final QueryResult queryResultSample, final ShardingSphereMetaData metaData, final int columnIndex) throws SQLException {
return hasSelectExpandProjections(executionContext.getSqlStatementContext())
? QueryHeaderBuilder.build(((SelectStatementContext) executionContext.getSqlStatementContext()).getProjectionsContext(), executeResultSample, metaData, columnIndex)
: QueryHeaderBuilder.build(executeResultSample, metaData, columnIndex);
? QueryHeaderBuilder.build(((SelectStatementContext) executionContext.getSqlStatementContext()).getProjectionsContext(), queryResultSample, metaData, columnIndex)
: QueryHeaderBuilder.build(queryResultSample, metaData, columnIndex);
}

private boolean hasSelectExpandProjections(final SQLStatementContext<?> sqlStatementContext) {
Expand All @@ -134,8 +135,8 @@ private MergedResult mergeQuery(final SQLStatementContext<?> sqlStatementContext
return mergeEngine.merge(queryResults, sqlStatementContext);
}

private UpdateResponseHeader processExecuteUpdate(final ExecutionContext executionContext, final Collection<ExecuteResult> executeResults) throws SQLException {
UpdateResponseHeader result = new UpdateResponseHeader(executionContext.getSqlStatementContext().getSqlStatement(), executeResults);
private UpdateResponseHeader processExecuteUpdate(final ExecutionContext executionContext, final Collection<UpdateResult> updateResults) throws SQLException {
UpdateResponseHeader result = new UpdateResponseHeader(executionContext.getSqlStatementContext().getSqlStatement(), updateResults);
refreshSchema(executionContext);
mergeUpdateCount(executionContext.getSqlStatementContext(), result);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import lombok.AccessLevel;
import lombok.Getter;
import org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
import org.apache.shardingsphere.infra.executor.sql.execute.result.update.UpdateResult;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
Expand Down Expand Up @@ -47,19 +46,19 @@ public UpdateResponseHeader(final SQLStatement sqlStatement) {
this(sqlStatement, Collections.emptyList());
}

public UpdateResponseHeader(final SQLStatement sqlStatement, final Collection<ExecuteResult> executeResults) {
public UpdateResponseHeader(final SQLStatement sqlStatement, final Collection<UpdateResult> updateResults) {
this.sqlStatement = sqlStatement;
lastInsertId = getLastInsertId(executeResults);
updateCount = executeResults.iterator().hasNext() ? ((UpdateResult) executeResults.iterator().next()).getUpdateCount() : 0;
for (ExecuteResult each : executeResults) {
updateCounts.add(((UpdateResult) each).getUpdateCount());
lastInsertId = getLastInsertId(updateResults);
updateCount = updateResults.iterator().hasNext() ? updateResults.iterator().next().getUpdateCount() : 0;
for (UpdateResult each : updateResults) {
updateCounts.add(each.getUpdateCount());
}
}

private long getLastInsertId(final Collection<ExecuteResult> executeResults) {
private long getLastInsertId(final Collection<UpdateResult> updateResults) {
long result = 0;
for (ExecuteResult each : executeResults) {
result = Math.max(result, ((UpdateResult) each).getLastInsertId());
for (UpdateResult each : updateResults) {
result = Math.max(result, each.getLastInsertId());
}
return result;
}
Expand Down

0 comments on commit 13574e1

Please sign in to comment.