This is an automated email from the ASF dual-hosted git repository.

sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 984331812c9 Add DistSQLExecutorAwareSetter (#29987)
984331812c9 is described below

commit 984331812c90a784ed96a0e2aa305635aebc6c9c
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Feb 4 19:47:18 2024 +0800

    Add DistSQLExecutorAwareSetter (#29987)
    
    * Add DistSQLExecutorAwareSetter
    
    * Add DistSQLExecutorAwareSetter
    
    * Add DistSQLExecutorAwareSetter
    
    * Add DistSQLExecutorAwareSetter
---
 .../handler/aware/DistSQLExecutorAwareSetter.java  | 73 ++++++++++++++++++++++
 .../type/query/DistSQLQueryExecuteEngine.java      | 39 +++---------
 .../type/update/DistSQLUpdateExecuteEngine.java    | 25 +-------
 .../distsql/DistSQLBackendHandlerFactoryTest.java  |  3 +-
 .../ral/QueryableRALBackendHandlerTest.java        |  6 +-
 5 files changed, 88 insertions(+), 58 deletions(-)

diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/aware/DistSQLExecutorAwareSetter.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/aware/DistSQLExecutorAwareSetter.java
new file mode 100644
index 00000000000..1981f3f8451
--- /dev/null
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/aware/DistSQLExecutorAwareSetter.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.distsql.handler.aware;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.distsql.handler.type.DistSQLConnectionContext;
+import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import 
org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException;
+import 
org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.NoDatabaseSelectedException;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+
+import java.util.Optional;
+
+/**
+ * DistSQL executor aware setter.
+ */
+@RequiredArgsConstructor
+public final class DistSQLExecutorAwareSetter {
+    
+    private final ContextManager contextManager;
+    
+    private final ShardingSphereDatabase database;
+    
+    private final DistSQLConnectionContext connectionContext;
+    
+    private final Object executor;
+    
+    /**
+     * Set aware context.
+     */
+    @SuppressWarnings("rawtypes")
+    public void set() {
+        if (executor instanceof DistSQLExecutorDatabaseAware) {
+            ShardingSpherePreconditions.checkNotNull(database, 
NoDatabaseSelectedException::new);
+            ((DistSQLExecutorDatabaseAware) executor).setDatabase(database);
+        }
+        if (executor instanceof DistSQLExecutorRuleAware) {
+            setRule((DistSQLExecutorRuleAware) executor);
+        }
+        if (executor instanceof DistSQLExecutorConnectionContextAware) {
+            ((DistSQLExecutorConnectionContextAware) 
executor).setConnectionContext(connectionContext);
+        }
+    }
+    
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    private void setRule(final DistSQLExecutorRuleAware executor) throws 
UnsupportedSQLOperationException {
+        Optional<ShardingSphereRule> rule = findRule(executor.getRuleClass());
+        ShardingSpherePreconditions.checkState(rule.isPresent(), () -> new 
UnsupportedSQLOperationException(String.format("The current database has no 
`%s` rules", executor.getRuleClass())));
+        executor.setRule(rule.get());
+    }
+    
+    private Optional<ShardingSphereRule> findRule(final 
Class<ShardingSphereRule> ruleClass) {
+        Optional<ShardingSphereRule> globalRule = 
contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(ruleClass);
+        return globalRule.isPresent() ? globalRule : 
database.getRuleMetaData().findSingleRule(ruleClass);
+    }
+}
diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/query/DistSQLQueryExecuteEngine.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/query/DistSQLQueryExecuteEngine.java
index 2f0af218af2..8ae68095b20 100644
--- 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/query/DistSQLQueryExecuteEngine.java
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/query/DistSQLQueryExecuteEngine.java
@@ -18,22 +18,19 @@
 package org.apache.shardingsphere.distsql.handler.type.query;
 
 import lombok.Getter;
-import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorConnectionContextAware;
-import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseAware;
-import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorRuleAware;
+import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorAwareSetter;
 import org.apache.shardingsphere.distsql.handler.type.DistSQLConnectionContext;
 import org.apache.shardingsphere.distsql.handler.util.DatabaseNameUtils;
 import org.apache.shardingsphere.distsql.statement.DistSQLStatement;
+import 
org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException;
 import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Optional;
 
 /**
  * DistSQL query execute engine.
@@ -63,37 +60,17 @@ public abstract class DistSQLQueryExecuteEngine {
      *
      * @throws SQLException SQL exception
      */
-    @SuppressWarnings({"unchecked", "rawtypes"})
+    @SuppressWarnings("unchecked")
     public void executeQuery() throws SQLException {
         DistSQLQueryExecutor<DistSQLStatement> executor = 
TypedSPILoader.getService(DistSQLQueryExecutor.class, sqlStatement.getClass());
         columnNames = executor.getColumnNames();
-        if (executor instanceof DistSQLExecutorDatabaseAware) {
-            ((DistSQLExecutorDatabaseAware) 
executor).setDatabase(getDatabase(databaseName));
-        }
-        if (executor instanceof DistSQLExecutorConnectionContextAware) {
-            ((DistSQLExecutorConnectionContextAware) 
executor).setConnectionContext(getDistSQLConnectionContext());
-        }
-        if (executor instanceof DistSQLExecutorRuleAware) {
-            setRule((DistSQLExecutorRuleAware) executor);
-        }
-        if (null == rows) {
-            rows = executor.getRows(sqlStatement, contextManager);
-        }
-    }
-    
-    @SuppressWarnings({"rawtypes", "unchecked"})
-    private void setRule(final DistSQLExecutorRuleAware executor) {
-        Optional<ShardingSphereRule> rule = findRule(executor.getRuleClass());
-        if (rule.isPresent()) {
-            executor.setRule(rule.get());
-        } else {
+        try {
+            new DistSQLExecutorAwareSetter(contextManager, null == 
databaseName ? null : getDatabase(databaseName), getDistSQLConnectionContext(), 
executor).set();
+        } catch (final UnsupportedSQLOperationException ignored) {
             rows = Collections.emptyList();
+            return;
         }
-    }
-    
-    private Optional<ShardingSphereRule> findRule(final 
Class<ShardingSphereRule> ruleClass) {
-        Optional<ShardingSphereRule> globalRule = 
contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(ruleClass);
-        return globalRule.isPresent() ? globalRule : 
getDatabase(databaseName).getRuleMetaData().findSingleRule(ruleClass);
+        rows = executor.getRows(sqlStatement, contextManager);
     }
     
     protected abstract ShardingSphereDatabase getDatabase(String databaseName);
diff --git 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/DistSQLUpdateExecuteEngine.java
 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/DistSQLUpdateExecuteEngine.java
index 95c0899eae5..06b00e92196 100644
--- 
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/DistSQLUpdateExecuteEngine.java
+++ 
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/update/DistSQLUpdateExecuteEngine.java
@@ -17,8 +17,7 @@
 
 package org.apache.shardingsphere.distsql.handler.type.update;
 
-import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseAware;
-import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorRuleAware;
+import 
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorAwareSetter;
 import 
org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorRequiredChecker;
 import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.engine.database.DatabaseRuleDefinitionExecuteEngine;
 import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.engine.global.GlobalRuleDefinitionExecuteEngine;
@@ -29,10 +28,7 @@ import 
org.apache.shardingsphere.distsql.handler.type.update.rdl.rule.spi.global
 import org.apache.shardingsphere.distsql.handler.util.DatabaseNameUtils;
 import org.apache.shardingsphere.distsql.statement.DistSQLStatement;
 import 
org.apache.shardingsphere.distsql.statement.rdl.rule.RuleDefinitionStatement;
-import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
-import 
org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 
@@ -110,27 +106,10 @@ public abstract class DistSQLUpdateExecuteEngine {
     @SuppressWarnings({"rawtypes", "unchecked"})
     private void executeNormalUpdate() throws SQLException {
         DistSQLUpdateExecutor executor = 
TypedSPILoader.getService(DistSQLUpdateExecutor.class, sqlStatement.getClass());
-        if (executor instanceof DistSQLExecutorDatabaseAware) {
-            ((DistSQLExecutorDatabaseAware) 
executor).setDatabase(getDatabase(databaseName));
-        }
-        if (executor instanceof DistSQLExecutorRuleAware) {
-            setRule((DistSQLExecutorRuleAware) executor);
-        }
+        new DistSQLExecutorAwareSetter(contextManager, null == databaseName ? 
null : getDatabase(databaseName), null, executor).set();
         new DistSQLExecutorRequiredChecker(sqlStatement, contextManager, 
databaseName, executor).check(null);
         executor.executeUpdate(sqlStatement, contextManager);
     }
     
-    @SuppressWarnings({"rawtypes", "unchecked"})
-    private void setRule(final DistSQLExecutorRuleAware executor) throws 
UnsupportedSQLOperationException {
-        Optional<ShardingSphereRule> rule = findRule(executor.getRuleClass());
-        ShardingSpherePreconditions.checkState(rule.isPresent(), () -> new 
UnsupportedSQLOperationException(String.format("The current database has no 
`%s` rules", executor.getRuleClass())));
-        executor.setRule(rule.get());
-    }
-    
-    private Optional<ShardingSphereRule> findRule(final 
Class<ShardingSphereRule> ruleClass) {
-        Optional<ShardingSphereRule> globalRule = 
contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(ruleClass);
-        return globalRule.isPresent() ? globalRule : 
getDatabase(databaseName).getRuleMetaData().findSingleRule(ruleClass);
-    }
-    
     protected abstract ShardingSphereDatabase getDatabase(String databaseName);
 }
diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/DistSQLBackendHandlerFactoryTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/DistSQLBackendHandlerFactoryTest.java
index de39252e1bc..39e215dd0da 100644
--- 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/DistSQLBackendHandlerFactoryTest.java
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/DistSQLBackendHandlerFactoryTest.java
@@ -55,6 +55,7 @@ import 
org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Answers;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoSettings;
 import org.mockito.quality.Strictness;
@@ -76,7 +77,7 @@ import static org.mockito.Mockito.when;
 @MockitoSettings(strictness = Strictness.LENIENT)
 class DistSQLBackendHandlerFactoryTest {
     
-    @Mock
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private ConnectionSession connectionSession;
     
     @BeforeEach
diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandlerTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandlerTest.java
index 860e1672501..c40cbce5190 100644
--- 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandlerTest.java
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandlerTest.java
@@ -62,8 +62,8 @@ class QueryableRALBackendHandlerTest {
     
     @Test
     void assertExecuteWithNoDatabase() {
-        
when(ProxyContext.getInstance().getDatabase(null)).thenThrow(NoDatabaseSelectedException.class);
-        assertThrows(NoDatabaseSelectedException.class, () -> new 
DistSQLQueryBackendHandler(mock(ExportDatabaseConfigurationStatement.class), 
mock(ConnectionSession.class)).execute());
+        assertThrows(NoDatabaseSelectedException.class,
+                () -> new 
DistSQLQueryBackendHandler(mock(ExportDatabaseConfigurationStatement.class), 
mock(ConnectionSession.class, RETURNS_DEEP_STUBS)).execute());
     }
     
     @Test
@@ -90,7 +90,7 @@ class QueryableRALBackendHandlerTest {
         
when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class,
 "FIXTURE"));
         when(database.getSchema("foo_db")).thenReturn(new 
ShardingSphereSchema(createTableMap(), Collections.emptyMap()));
         
when(ProxyContext.getInstance().getDatabase("foo_db")).thenReturn(database);
-        assertDoesNotThrow(() -> new 
DistSQLQueryBackendHandler(createSqlStatement(), 
mock(ConnectionSession.class)).execute());
+        assertDoesNotThrow(() -> new 
DistSQLQueryBackendHandler(createSqlStatement(), mock(ConnectionSession.class, 
RETURNS_DEEP_STUBS)).execute());
     }
     
     private Map<String, ShardingSphereTable> createTableMap() {

Reply via email to