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

zhangliang 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 670436a2cbd Refactor ShardingSphereDatabaseFactory (#34392)
670436a2cbd is described below

commit 670436a2cbd8d707ce5ca6a9590660d64188084b
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Jan 18 23:20:43 2025 +0800

    Refactor ShardingSphereDatabaseFactory (#34392)
    
    * Refactor ShardingSphereDatabaseFactory
    
    * Refactor ShardingSphereDatabaseFactory
---
 .../database/ShardingSphereDatabaseFactory.java    | 34 +---------------------
 .../ShardingSphereDatabaseFactoryTest.java         | 22 --------------
 .../mode/metadata/MetaDataContextsFactory.java     |  6 ++--
 3 files changed, 5 insertions(+), 57 deletions(-)

diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java
index ce04d679569..4268b9d8ffe 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java
@@ -41,38 +41,6 @@ import java.util.stream.Collectors;
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class ShardingSphereDatabaseFactory {
     
-    /**
-     * Create database.
-     *
-     * @param databaseName database name
-     * @param databaseConfig database configuration
-     * @param props configuration properties
-     * @param computeNodeInstanceContext compute node instance context
-     * @return created database
-     * @throws SQLException SQL exception
-     */
-    public static ShardingSphereDatabase create(final String databaseName, 
final DatabaseConfiguration databaseConfig,
-                                                final ConfigurationProperties 
props, final ComputeNodeInstanceContext computeNodeInstanceContext) throws 
SQLException {
-        DatabaseType protocolType = 
DatabaseTypeEngine.getProtocolType(databaseConfig, props);
-        return ShardingSphereDatabase.create(databaseName, protocolType, 
databaseConfig, props, computeNodeInstanceContext);
-    }
-    
-    /**
-     * Create database.
-     *
-     * @param databaseName database name
-     * @param databaseConfig database configuration
-     * @param schemas schemas
-     * @param props configuration properties
-     * @param computeNodeInstanceContext compute node instance context
-     * @return created database
-     */
-    public static ShardingSphereDatabase create(final String databaseName, 
final DatabaseConfiguration databaseConfig, final 
Collection<ShardingSphereSchema> schemas,
-                                                final ConfigurationProperties 
props, final ComputeNodeInstanceContext computeNodeInstanceContext) {
-        DatabaseType protocolType = 
DatabaseTypeEngine.getProtocolType(databaseConfig, props);
-        return ShardingSphereDatabase.create(databaseName, protocolType, 
databaseConfig, computeNodeInstanceContext, schemas);
-    }
-    
     /**
      * Create databases.
      *
@@ -97,7 +65,7 @@ public final class ShardingSphereDatabaseFactory {
                                                  final 
ComputeNodeInstanceContext computeNodeInstanceContext) {
         return databaseConfig.getStorageUnits().isEmpty()
                 ? ShardingSphereDatabase.create(databaseName, protocolType, 
props)
-                : create(databaseName, databaseConfig, schemas, props, 
computeNodeInstanceContext);
+                : ShardingSphereDatabase.create(databaseName, 
DatabaseTypeEngine.getProtocolType(databaseConfig, props), databaseConfig, 
computeNodeInstanceContext, schemas);
     }
     
     /**
diff --git 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactoryTest.java
 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactoryTest.java
index 892b3939ad3..823717b685c 100644
--- 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactoryTest.java
+++ 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactoryTest.java
@@ -20,9 +20,7 @@ package org.apache.shardingsphere.infra.metadata.database;
 import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
 import 
org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
 import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.junit.jupiter.api.Test;
 
 import java.sql.SQLException;
@@ -30,31 +28,11 @@ import java.util.Collections;
 import java.util.Map;
 import java.util.Properties;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.Mockito.mock;
 
 class ShardingSphereDatabaseFactoryTest {
     
-    @Test
-    void assertCreateDatabase() throws SQLException {
-        DatabaseConfiguration databaseConfig = new 
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(), 
Collections.emptyList());
-        ShardingSphereDatabase actual = 
ShardingSphereDatabaseFactory.create("foo_db", databaseConfig, new 
ConfigurationProperties(new Properties()), 
mock(ComputeNodeInstanceContext.class));
-        assertThat(actual.getName(), is("foo_db"));
-        assertTrue(actual.getResourceMetaData().getStorageUnits().isEmpty());
-    }
-    
-    @Test
-    void assertCreateDatabaseWithSchemas() {
-        ShardingSphereDatabase database = ShardingSphereDatabaseFactory.create(
-                "foo_db", mock(DatabaseConfiguration.class), 
Collections.emptyList(), new ConfigurationProperties(new Properties()), 
mock(ComputeNodeInstanceContext.class));
-        assertThat(database.getName(), is("foo_db"));
-        assertThat(database.getProtocolType(), 
is(TypedSPILoader.getService(DatabaseType.class, "MySQL")));
-        assertThat(database.getRuleMetaData().getRules().size(), is(1));
-        assertTrue(database.getAllSchemas().isEmpty());
-    }
-    
     @Test
     void assertCreateDatabases() throws SQLException {
         DatabaseConfiguration databaseConfig = new 
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(), 
Collections.emptyList());
diff --git 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java
 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java
index cbff980db1d..62dd8cc5037 100644
--- 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java
+++ 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java
@@ -24,6 +24,7 @@ import 
org.apache.shardingsphere.infra.config.database.impl.DataSourceGeneratedD
 import 
org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.database.DatabaseTypeEngine;
 import 
org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfiguration;
 import 
org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
 import 
org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
@@ -253,8 +254,9 @@ public final class MetaDataContextsFactory {
                                                                 final 
DatabaseConfiguration databaseConfig, final ConfigurationProperties props,
                                                                 final 
ComputeNodeInstanceContext instanceContext) throws SQLException {
         return internalLoadMetaData
-                ? ShardingSphereDatabaseFactory.create(databaseName, 
databaseConfig, 
persistService.getDatabaseMetaDataFacade().getSchema().load(databaseName), 
props, instanceContext)
-                : ShardingSphereDatabaseFactory.create(databaseName, 
databaseConfig, props, instanceContext);
+                ? ShardingSphereDatabase.create(databaseName, 
DatabaseTypeEngine.getProtocolType(databaseConfig, props),
+                        databaseConfig, instanceContext, 
persistService.getDatabaseMetaDataFacade().getSchema().load(databaseName))
+                : ShardingSphereDatabase.create(databaseName, 
DatabaseTypeEngine.getProtocolType(databaseConfig, props), databaseConfig, 
props, instanceContext);
     }
     
     private static ResourceMetaData getEffectiveResourceMetaData(final 
ShardingSphereDatabase database, final SwitchingResource resource) {

Reply via email to