From 9a7def5d5a6f77a3957d35a3e696c33a7da4ed7f Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Sat, 25 May 2024 23:01:08 +0800 Subject: [PATCH] Refactor ShardingSphereStatement (#31394) --- .../CreateDefaultShadowAlgorithmExecutorTest.java | 5 ----- .../infra/expr/espresso/ReflectValue.java | 6 ++---- .../core/statement/ShardingSphereStatement.java | 15 +++++---------- .../table/calculator/CalculationContext.java | 2 -- 4 files changed, 7 insertions(+), 21 deletions(-) diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmExecutorTest.java index c21247f8fcab6..6af74d5bb0d8d 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmExecutorTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmExecutorTest.java @@ -20,7 +20,6 @@ import org.apache.shardingsphere.distsql.segment.AlgorithmSegment; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException; -import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.distsql.handler.update.CreateDefaultShadowAlgorithmExecutor; import org.apache.shardingsphere.shadow.distsql.segment.ShadowAlgorithmSegment; import org.apache.shardingsphere.shadow.distsql.statement.CreateDefaultShadowAlgorithmStatement; @@ -30,7 +29,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -43,9 +41,6 @@ class CreateDefaultShadowAlgorithmExecutorTest { private final CreateDefaultShadowAlgorithmExecutor executor = new CreateDefaultShadowAlgorithmExecutor(); - @Mock - private ShadowRuleConfiguration currentConfig; - @BeforeEach void setUp() { executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); diff --git a/infra/expr/type/espresso/src/main/java/org/apache/shardingsphere/infra/expr/espresso/ReflectValue.java b/infra/expr/type/espresso/src/main/java/org/apache/shardingsphere/infra/expr/espresso/ReflectValue.java index aa46ad95d8a75..2b0e7edff3023 100644 --- a/infra/expr/type/espresso/src/main/java/org/apache/shardingsphere/infra/expr/espresso/ReflectValue.java +++ b/infra/expr/type/espresso/src/main/java/org/apache/shardingsphere/infra/expr/espresso/ReflectValue.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.infra.expr.espresso; +import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import java.util.stream.Stream; @@ -25,16 +26,13 @@ * Reflect Value. * Avoid using JDK21 bytecode during compilation. Refer to `org.graalvm.polyglot.Value`. */ +@RequiredArgsConstructor public class ReflectValue { private static final String VALUE_CLASS_NAME = "org.graalvm.polyglot.Value"; private final Object valueInstance; - public ReflectValue(final Object valueInstance) { - this.valueInstance = valueInstance; - } - /** * Returns the member with a given identifier or null if the member does not exist. * @param identifier the member identifier diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java index 650e6cb13f9c1..d2ec9025c9015 100644 --- a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java +++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.driver.jdbc.core.statement; -import com.google.common.base.Strings; import lombok.AccessLevel; import lombok.Getter; import org.apache.shardingsphere.driver.executor.DriverExecutor; @@ -39,6 +38,7 @@ import org.apache.shardingsphere.infra.connection.kernel.KernelProcessor; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType; +import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.exception.dialect.SQLExceptionTransformEngine; import org.apache.shardingsphere.infra.exception.kernel.syntax.EmptySQLException; import org.apache.shardingsphere.infra.executor.audit.SQLAuditEngine; @@ -159,9 +159,7 @@ public ShardingSphereStatement(final ShardingSphereConnection connection, final @Override public ResultSet executeQuery(final String sql) throws SQLException { - if (Strings.isNullOrEmpty(sql)) { - throw new EmptySQLException().toSQLException(); - } + ShardingSpherePreconditions.checkNotEmpty(sql, () -> new EmptySQLException().toSQLException()); ResultSet result; try { QueryContext queryContext = createQueryContext(sql); @@ -170,11 +168,9 @@ public ResultSet executeQuery(final String sql) throws SQLException { connection.getDatabaseConnectionManager().getConnectionContext().setCurrentDatabase(databaseName); trafficInstanceId = getInstanceIdAndSet(queryContext).orElse(null); if (null != trafficInstanceId) { - JDBCExecutionUnit executionUnit = createTrafficExecutionUnit(trafficInstanceId, queryContext); - return executor.getTrafficExecutor().execute(executionUnit, Statement::executeQuery); + return executor.getTrafficExecutor().execute(createTrafficExecutionUnit(trafficInstanceId, queryContext), Statement::executeQuery); } - useFederation = decide(queryContext, - metaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.getMetaData().getGlobalRuleMetaData()); + useFederation = decide(queryContext, metaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.getMetaData().getGlobalRuleMetaData()); if (useFederation) { return executeFederationQuery(queryContext); } @@ -435,8 +431,7 @@ private boolean execute0(final String sql, final ExecuteCallback executeCallback JDBCExecutionUnit executionUnit = createTrafficExecutionUnit(trafficInstanceId, queryContext); return executor.getTrafficExecutor().execute(executionUnit, trafficCallback); } - useFederation = decide(queryContext, - metaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.getMetaData().getGlobalRuleMetaData()); + useFederation = decide(queryContext, metaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.getMetaData().getGlobalRuleMetaData()); if (useFederation) { ResultSet resultSet = executeFederationQuery(queryContext); return null != resultSet; diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/table/calculator/CalculationContext.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/table/calculator/CalculationContext.java index a1183457834ba..30b02a7446e4e 100644 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/table/calculator/CalculationContext.java +++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/table/calculator/CalculationContext.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.data.pipeline.core.consistencycheck.table.calculator; -import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.util.close.QuietlyCloser; import java.sql.Connection; @@ -28,7 +27,6 @@ /** * Calculation context. */ -@RequiredArgsConstructor public final class CalculationContext implements AutoCloseable { private final AtomicReference connection = new AtomicReference<>();