Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ShardingSphereDatabaseFactory #34392

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,6 @@
@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.
*
Expand All @@ -97,7 +65,7 @@ private static ShardingSphereDatabase create(final String databaseName, final Da
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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,19 @@
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;
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -253,8 +254,9 @@ private static ShardingSphereDatabase createChangedDatabase(final String databas
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) {
Expand Down
Loading