Skip to content

Commit

Permalink
Remove ShardingSpherePreparedStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed May 26, 2024
1 parent 1103aa6 commit 3adbc3a
Showing 1 changed file with 17 additions and 29 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,18 +228,19 @@ 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
Expand All @@ -253,8 +249,6 @@ public ResultSet executeQuery() throws SQLException {
} finally {
clearBatch();
}
currentResultSet = result;
return result;
}

private ShardingSphereResultSet doExecuteQuery(final ExecutionContext executionContext) throws SQLException {
Expand Down Expand Up @@ -347,7 +341,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,15 +405,16 @@ 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;
}
executionContext = createExecutionContext(queryContext);
Expand Down Expand Up @@ -496,14 +491,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 @@ -629,7 +617,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

0 comments on commit 3adbc3a

Please sign in to comment.