You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/07/16 03:04:33 UTC

[shardingsphere] branch master updated: Refactor ShowVersionExecutor (#19256)

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

duanzhengqiang 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 ff176db8eaa Refactor ShowVersionExecutor (#19256)
ff176db8eaa is described below

commit ff176db8eaa7a91053bb5541d224dfd5773b1156
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Jul 16 11:04:27 2022 +0800

    Refactor ShowVersionExecutor (#19256)
    
    * Refactor ShowVersionExecutorTest
    
    * Refactor StandaloneContextManagerBuilder
    
    * Refactor ClusterContextManagerBuilder
    
    * Fix checkstyle
    
    * Fix checkstyle
---
 .../cluster/ClusterContextManagerBuilder.java      |  3 +-
 .../StandaloneContextManagerBuilder.java           |  3 +-
 .../admin/mysql/executor/ShowVersionExecutor.java  | 11 +--
 .../mysql/executor/ShowVersionExecutorTest.java    | 99 +++++++++++-----------
 .../segment/dml/item/ProjectionsSegment.java       |  4 +-
 5 files changed, 59 insertions(+), 61 deletions(-)

diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
index 09b75b4482c..9f5ea29f7bf 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
@@ -88,9 +88,10 @@ public final class ClusterContextManagerBuilder implements ContextManagerBuilder
         Collection<String> databaseNames = parameter.getInstanceMetaData() instanceof JDBCInstanceMetaData
                 ? parameter.getDatabaseConfigs().keySet()
                 : persistService.getDatabaseMetaDataService().loadAllDatabaseNames();
+        Map<String, DatabaseConfiguration> databaseConfigMap = buildDatabaseConfigMap(databaseNames, persistService, parameter);
         Collection<RuleConfiguration> globalRuleConfigs = persistService.getGlobalRuleService().load();
         ConfigurationProperties props = new ConfigurationProperties(persistService.getPropsService().load());
-        Map<String, ShardingSphereDatabase> databases = ShardingSphereDatabasesFactory.create(buildDatabaseConfigMap(databaseNames, persistService, parameter), props, instanceContext);
+        Map<String, ShardingSphereDatabase> databases = ShardingSphereDatabasesFactory.create(databaseConfigMap, props, instanceContext);
         ShardingSphereRuleMetaData globalMetaData = new ShardingSphereRuleMetaData(GlobalRulesBuilder.buildRules(globalRuleConfigs, databases, instanceContext));
         return new MetaDataContexts(persistService, new ShardingSphereMetaData(databases, globalMetaData, props), OptimizerContextFactory.create(databases, globalMetaData));
     }
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java
index 59456008f9e..cdb24bec67c 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java
@@ -60,7 +60,8 @@ public final class StandaloneContextManagerBuilder implements ContextManagerBuil
         MetaDataPersistService persistService = new MetaDataPersistService(repository);
         persistConfigurations(persistService, parameter);
         InstanceContext instanceContext = buildInstanceContext(parameter);
-        return new ContextManager(buildMetaDataContexts(persistService, parameter, instanceContext), instanceContext);
+        MetaDataContexts metaDataContexts = buildMetaDataContexts(persistService, parameter, instanceContext);
+        return new ContextManager(metaDataContexts, instanceContext);
     }
     
     private void persistConfigurations(final MetaDataPersistService persistService, final ContextManagerBuilderParameter parameter) {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutor.java
index 963bb580a21..8633bb35428 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutor.java
@@ -29,11 +29,9 @@ import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryRes
 import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
 import org.apache.shardingsphere.proxy.backend.text.admin.executor.DatabaseAdminQueryExecutor;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
 
 import java.sql.Types;
-import java.util.Collection;
 import java.util.Collections;
 
 /**
@@ -60,12 +58,7 @@ public final class ShowVersionExecutor implements DatabaseAdminQueryExecutor {
     }
     
     private String getLabel() {
-        Collection<ProjectionSegment> projections = sqlStatement.getProjections().getProjections();
-        for (ProjectionSegment each : projections) {
-            if (each instanceof ExpressionProjectionSegment) {
-                return ((ExpressionProjectionSegment) each).getAlias().orElse(FUNCTION_NAME);
-            }
-        }
-        return FUNCTION_NAME;
+        return sqlStatement.getProjections().getProjections().stream()
+                .filter(each -> each instanceof ExpressionProjectionSegment).findFirst().map(each -> ((ExpressionProjectionSegment) each).getAlias().orElse(FUNCTION_NAME)).orElse(FUNCTION_NAME);
     }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutorTest.java
index 530b6656938..4cd2a33abb6 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutorTest.java
@@ -21,79 +21,82 @@ import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerInfo;
 import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
 import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
 
 import java.sql.SQLException;
-import java.util.Collection;
-import java.util.LinkedList;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-@RunWith(MockitoJUnitRunner.class)
 public final class ShowVersionExecutorTest {
     
+    private String previousVersion;
+    
+    @Before
+    public void setUp() {
+        previousVersion = MySQLServerInfo.getServerVersion("foo_db");
+        MySQLServerInfo.setServerVersion("foo_db", "8.0.26");
+    }
+    
+    @After
+    public void tearDown() {
+        MySQLServerInfo.setServerVersion("foo_db", previousVersion);
+    }
+    
     @Test
     public void assertExecute() throws SQLException {
-        String databaseName = "test";
-        MySQLServerInfo.setServerVersion(databaseName, "8.0.26");
-        ShowVersionExecutor executor = new ShowVersionExecutor(mockSelectStatement());
-        ConnectionSession connectionSession = mock(ConnectionSession.class);
-        when(connectionSession.getDatabaseName()).thenReturn(databaseName);
-        executor.execute(connectionSession);
-        QueryResultMetaData metaData = executor.getQueryResultMetaData();
-        assertThat(metaData.getColumnCount(), is(1));
-        assertThat(metaData.getColumnName(1), is(ShowVersionExecutor.FUNCTION_NAME));
-        assertThat(metaData.getColumnLabel(1), is(ShowVersionExecutor.FUNCTION_NAME));
-        while (executor.getMergedResult().next()) {
-            assertThat(executor.getMergedResult().getValue(1, Object.class), is(MySQLServerInfo.getServerVersion(databaseName)));
-        }
+        SelectStatement selectStatement = mock(SelectStatement.class);
+        when(selectStatement.getProjections()).thenReturn(createProjectionsSegmentWithoutAlias());
+        ShowVersionExecutor executor = new ShowVersionExecutor(selectStatement);
+        executor.execute(mockConnectionSession());
+        assertQueryResult(executor, ShowVersionExecutor.FUNCTION_NAME);
+    }
+    
+    private ProjectionsSegment createProjectionsSegmentWithoutAlias() {
+        ExpressionProjectionSegment expressionProjectionSegment = new ExpressionProjectionSegment(0, 0, "version()");
+        ProjectionsSegment result = new ProjectionsSegment(0, 0);
+        result.getProjections().add(expressionProjectionSegment);
+        return result;
+    }
+    
+    private ConnectionSession mockConnectionSession() {
+        ConnectionSession result = mock(ConnectionSession.class);
+        when(result.getDatabaseName()).thenReturn("foo_db");
+        return result;
     }
     
     @Test
     public void assertExecuteWithAlias() throws SQLException {
-        String databaseName = "test";
-        MySQLServerInfo.setServerVersion(databaseName, "8.0.26");
-        ShowVersionExecutor executor = new ShowVersionExecutor(mockSelectStatementWithAlias());
-        ConnectionSession connectionSession = mock(ConnectionSession.class);
-        when(connectionSession.getDatabaseName()).thenReturn(databaseName);
-        executor.execute(connectionSession);
-        QueryResultMetaData metaData = executor.getQueryResultMetaData();
-        assertThat(metaData.getColumnCount(), is(1));
-        assertThat(metaData.getColumnName(1), is(ShowVersionExecutor.FUNCTION_NAME));
-        assertThat(metaData.getColumnLabel(1), is("test_alias"));
-        while (executor.getMergedResult().next()) {
-            assertThat(executor.getMergedResult().getValue(1, Object.class), is(MySQLServerInfo.getServerVersion(databaseName)));
-        }
+        SelectStatement selectStatement = mock(SelectStatement.class);
+        when(selectStatement.getProjections()).thenReturn(createProjectionsSegmentWithAlias());
+        ShowVersionExecutor executor = new ShowVersionExecutor(selectStatement);
+        executor.execute(mockConnectionSession());
+        assertQueryResult(executor, "foo_alias");
     }
     
-    private SelectStatement mockSelectStatement() {
-        Collection<ProjectionSegment> projections = new LinkedList<>();
-        ProjectionsSegment segment = mock(ProjectionsSegment.class);
-        when(segment.getProjections()).thenReturn(projections);
-        SelectStatement result = mock(SelectStatement.class);
-        when(result.getProjections()).thenReturn(segment);
+    private ProjectionsSegment createProjectionsSegmentWithAlias() {
+        ExpressionProjectionSegment expressionProjectionSegment = new ExpressionProjectionSegment(0, 0, "version()");
+        expressionProjectionSegment.setAlias(new AliasSegment(0, 0, new IdentifierValue("foo_alias")));
+        ProjectionsSegment result = new ProjectionsSegment(0, 0);
+        result.getProjections().add(expressionProjectionSegment);
         return result;
     }
     
-    private SelectStatement mockSelectStatementWithAlias() {
-        Collection<ProjectionSegment> projections = new LinkedList<>();
-        ExpressionProjectionSegment projectionSegment = new ExpressionProjectionSegment(0, 0, "version()");
-        projectionSegment.setAlias(new AliasSegment(0, 0, new IdentifierValue("test_alias")));
-        projections.add(projectionSegment);
-        ProjectionsSegment segment = mock(ProjectionsSegment.class);
-        when(segment.getProjections()).thenReturn(projections);
-        SelectStatement result = mock(SelectStatement.class);
-        when(result.getProjections()).thenReturn(segment);
-        return result;
+    private void assertQueryResult(final ShowVersionExecutor executor, final String expectedColumnLabel) throws SQLException {
+        QueryResultMetaData actualQueryResultMetaData = executor.getQueryResultMetaData();
+        assertThat(actualQueryResultMetaData.getColumnCount(), is(1));
+        assertThat(actualQueryResultMetaData.getColumnName(1), is(ShowVersionExecutor.FUNCTION_NAME));
+        assertThat(actualQueryResultMetaData.getColumnLabel(1), is(expectedColumnLabel));
+        while (executor.getMergedResult().next()) {
+            assertThat(executor.getMergedResult().getValue(1, Object.class), is(MySQLServerInfo.getServerVersion("foo_db")));
+        }
     }
 }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/item/ProjectionsSegment.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/item/ProjectionsSegment.java
index f9a4e54e1ec..872ca9f216b 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/item/ProjectionsSegment.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/item/ProjectionsSegment.java
@@ -38,8 +38,8 @@ public final class ProjectionsSegment implements SQLSegment {
     
     private final int stopIndex;
     
+    private final Collection<ProjectionSegment> projections = new LinkedList<>();
+    
     @Setter
     private boolean distinctRow;
-    
-    private final Collection<ProjectionSegment> projections = new LinkedList<>();
 }