Skip to content

Commit

Permalink
Refactor ShardingSpherePreparedStatement (#31400)
Browse files Browse the repository at this point in the history
* Remove ShardingSpherePreparedStatement

* Remove ShardingSpherePreparedStatement

* Remove ShardingSpherePreparedStatement

* Remove ShardingSpherePreparedStatement

* Remove ShardingSpherePreparedStatement

* Remove ShardingSpherePreparedStatement
  • Loading branch information
terrymanu authored May 26, 2024
1 parent c5b6602 commit 61da148
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,6 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState

private ResultSet currentResultSet;

private String trafficInstanceId;

private boolean useFederation;

private final HintValueContext hintValueContext;

private ResultSet currentBatchGeneratedKeysResultSet;
Expand Down Expand Up @@ -224,7 +220,6 @@ private boolean isStatementsCacheable(final RuleMetaData databaseRuleMetaData) {

@Override
public ResultSet executeQuery() throws SQLException {
ResultSet result;
try {
if (statementsCacheable && !statements.isEmpty()) {
resetParameters();
Expand All @@ -233,28 +228,28 @@ public ResultSet executeQuery() throws SQLException {
clearPrevious();
QueryContext queryContext = createQueryContext();
handleAutoCommit(queryContext);
trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null);
String trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null);
if (null != trafficInstanceId) {
JDBCExecutionUnit executionUnit = createTrafficExecutionUnit(trafficInstanceId, queryContext);
return executor.getTrafficExecutor().execute(executionUnit, (statement, sql) -> ((PreparedStatement) statement).executeQuery());
currentResultSet = executor.getTrafficExecutor().execute(executionUnit, (statement, sql) -> ((PreparedStatement) statement).executeQuery());
return currentResultSet;
}
useFederation = decide(queryContext,
metaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.getMetaData().getGlobalRuleMetaData());
if (useFederation) {
return executeFederationQuery(queryContext);
if (decide(queryContext, metaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.getMetaData().getGlobalRuleMetaData())) {
currentResultSet = executeFederationQuery(queryContext);
return currentResultSet;
}
executionContext = createExecutionContext(queryContext);
result = doExecuteQuery(executionContext);
currentResultSet = doExecuteQuery(executionContext);
return currentResultSet;
// CHECKSTYLE:OFF
} catch (final RuntimeException ex) {
// CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaDataContexts);
throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType());
} finally {
clearBatch();
batchPreparedStatementExecutor.clear();
clearParameters();
}
currentResultSet = result;
return result;
}

private ShardingSphereResultSet doExecuteQuery(final ExecutionContext executionContext) throws SQLException {
Expand Down Expand Up @@ -347,7 +342,7 @@ public int executeUpdate() throws SQLException {
clearPrevious();
QueryContext queryContext = createQueryContext();
handleAutoCommit(queryContext);
trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null);
String trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null);
if (null != trafficInstanceId) {
JDBCExecutionUnit executionUnit = createTrafficExecutionUnit(trafficInstanceId, queryContext);
return executor.getTrafficExecutor().execute(executionUnit, (statement, sql) -> ((PreparedStatement) statement).executeUpdate());
Expand Down Expand Up @@ -411,17 +406,19 @@ public boolean execute() throws SQLException {
clearPrevious();
QueryContext queryContext = createQueryContext();
handleAutoCommit(queryContext);
trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null);
String trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null);
if (null != trafficInstanceId) {
JDBCExecutionUnit executionUnit = createTrafficExecutionUnit(trafficInstanceId, queryContext);
return executor.getTrafficExecutor().execute(executionUnit, (statement, sql) -> ((PreparedStatement) statement).execute());
boolean result = executor.getTrafficExecutor().execute(executionUnit, (statement, sql) -> ((PreparedStatement) statement).execute());
currentResultSet = executor.getTrafficExecutor().getResultSet();
return result;
}
useFederation = decide(queryContext,
metaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.getMetaData().getGlobalRuleMetaData());
if (useFederation) {
if (decide(queryContext, metaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.getMetaData().getGlobalRuleMetaData())) {
ResultSet resultSet = executeFederationQuery(queryContext);
currentResultSet = resultSet;
return null != resultSet;
}
currentResultSet = null;
executionContext = createExecutionContext(queryContext);
if (hasRawExecutionRule()) {
Collection<ExecuteResult> results =
Expand All @@ -435,7 +432,8 @@ public boolean execute() throws SQLException {
handleExceptionInTransaction(connection, metaDataContexts);
throw SQLExceptionTransformEngine.toSQLException(ex, metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType());
} finally {
clearBatch();
batchPreparedStatementExecutor.clear();
clearParameters();
}
}

Expand Down Expand Up @@ -496,14 +494,7 @@ public ResultSet getResultSet() throws SQLException {
if (null != currentResultSet) {
return currentResultSet;
}
if (null != trafficInstanceId) {
return executor.getTrafficExecutor().getResultSet();
}
if (useFederation) {
return executor.getSqlFederationEngine().getResultSet();
}
if (executionContext.getSqlStatementContext() instanceof SelectStatementContext
|| executionContext.getSqlStatementContext().getSqlStatement() instanceof DALStatement) {
if (executionContext.getSqlStatementContext() instanceof SelectStatementContext || executionContext.getSqlStatementContext().getSqlStatement() instanceof DALStatement) {
List<ResultSet> resultSets = getResultSets();
if (resultSets.isEmpty()) {
return currentResultSet;
Expand Down Expand Up @@ -591,6 +582,7 @@ private void replaySetParameter() throws SQLException {
}

private void clearPrevious() {
currentResultSet = null;
statements.clear();
parameterSets.clear();
generatedValues.clear();
Expand Down Expand Up @@ -629,7 +621,7 @@ private String getGeneratedKeysColumnName(final String columnName) {
public void addBatch() {
try {
QueryContext queryContext = createQueryContext();
trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null);
String trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null);
executionContext = null == trafficInstanceId ? createExecutionContext(queryContext) : createExecutionContext(queryContext, trafficInstanceId);
batchPreparedStatementExecutor.addBatchForExecutionUnits(executionContext.getExecutionUnits());
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public DatabaseConnector(final String driverType, final ShardingSphereDatabase d
this.driverType = driverType;
this.database = database;
this.queryContext = queryContext;
this.selectContainsEnhancedTable = sqlStatementContext instanceof SelectStatementContext && ((SelectStatementContext) sqlStatementContext).isContainsEnhancedTable();
selectContainsEnhancedTable = sqlStatementContext instanceof SelectStatementContext && ((SelectStatementContext) sqlStatementContext).isContainsEnhancedTable();
this.databaseConnectionManager = databaseConnectionManager;
if (sqlStatementContext instanceof CursorAvailable) {
prepareCursorStatementContext((CursorAvailable) sqlStatementContext, databaseConnectionManager.getConnectionSession());
Expand Down

0 comments on commit 61da148

Please sign in to comment.