diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerErrorCode.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerErrorCode.java index adaa7bccb2a036e474c4386ce3e14d3f9931c2a9..13ff0102d9e55ba0e95d359a6ec271754445284d 100644 --- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerErrorCode.java +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerErrorCode.java @@ -48,7 +48,7 @@ public enum MySQLServerErrorCode implements SQLErrorCode { ER_TABLE_EXISTS_ERROR(1050, "42S01", "Table '%s' already exists"), - ER_NO_SUCH_TABLE(1146, "42S02", "Table '%s.%s' doesn't exist"), + ER_NO_SUCH_TABLE(1146, "42S02", "Table '%s' doesn't exist"), ER_NOT_SUPPORTED_YET(1235, "42000", "This version of ShardingProxy doesn't yet support this SQL. '%s'"), diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/SingleTableRuleLoader.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/SingleTableRuleLoader.java index 272f4260ef79e123a9e73b64f9f5bd1b34102423..3174ee8c411b3b2bb24b3befb491728b0190c020 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/SingleTableRuleLoader.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/SingleTableRuleLoader.java @@ -19,16 +19,15 @@ package org.apache.shardingsphere.sharding.metadata; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException; import org.apache.shardingsphere.infra.database.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.schema.builder.loader.SchemaMetaDataLoader; -import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.apache.shardingsphere.sharding.rule.SingleTableRule; import javax.sql.DataSource; import java.sql.SQLException; import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; @@ -43,13 +42,11 @@ public final class SingleTableRuleLoader { * * @param databaseType database type * @param dataSourceMap data source map - * @param shardingRule sharding rule + * @param excludedTables exclude table names * @return single table rule map - * @throws SQLException SQL exception */ @SuppressWarnings("CollectionWithoutInitialCapacity") - public static Map load(final DatabaseType databaseType, final Map dataSourceMap, final ShardingRule shardingRule) throws SQLException { - Collection excludedTables = getExcludedTables(shardingRule); + public static Map load(final DatabaseType databaseType, final Map dataSourceMap, final Collection excludedTables) { Map result = new HashMap<>(); for (Entry entry : dataSourceMap.entrySet()) { result.putAll(load(databaseType, entry.getKey(), entry.getValue(), excludedTables)); @@ -58,8 +55,13 @@ public final class SingleTableRuleLoader { } private static Map load(final DatabaseType databaseType, - final String dataSourceName, final DataSource dataSource, final Collection excludedTables) throws SQLException { - Collection tables = SchemaMetaDataLoader.loadAllTableNames(dataSource, databaseType); + final String dataSourceName, final DataSource dataSource, final Collection excludedTables) { + Collection tables; + try { + tables = SchemaMetaDataLoader.loadAllTableNames(dataSource, databaseType); + } catch (final SQLException ex) { + throw new ShardingSphereConfigurationException("Can not load table: ", ex.getMessage()); + } Map result = new HashMap<>(tables.size(), 1); for (String each : tables) { if (!excludedTables.contains(each)) { @@ -68,10 +70,4 @@ public final class SingleTableRuleLoader { } return result; } - - private static Collection getExcludedTables(final ShardingRule shardingRule) { - Collection result = new HashSet<>(shardingRule.getTables()); - result.addAll(shardingRule.getAllActualTables()); - return result; - } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java index 647f2e972ae08500040d967687c38de65b2c9561..4077670e14e468ba28507357cca7bf6cb72d2dc1 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java @@ -41,11 +41,13 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShard import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm; +import org.apache.shardingsphere.sharding.metadata.SingleTableRuleLoader; import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm; import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm; import javax.sql.DataSource; import java.util.Collection; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.LinkedList; @@ -77,6 +79,8 @@ public final class ShardingRule implements DataNodeContainedRule, TableContained private final Collection bindingTableRules; private final Collection broadcastTables; + + private final Map singleTableRules; @Getter(AccessLevel.NONE) private final ShardingStrategyConfiguration defaultDatabaseShardingStrategyConfig; @@ -96,6 +100,7 @@ public final class ShardingRule implements DataNodeContainedRule, TableContained tableRules.addAll(createAutoTableRules(config.getAutoTables(), config.getDefaultKeyGenerateStrategy())); broadcastTables = config.getBroadcastTables(); bindingTableRules = createBindingTableRules(config.getBindingTableGroups()); + singleTableRules = SingleTableRuleLoader.load(databaseType, dataSourceMap, getExcludedTables()); defaultDatabaseShardingStrategyConfig = null == config.getDefaultDatabaseShardingStrategy() ? new NoneShardingStrategyConfiguration() : config.getDefaultDatabaseShardingStrategy(); defaultTableShardingStrategyConfig = null == config.getDefaultTableShardingStrategy() ? new NoneShardingStrategyConfiguration() : config.getDefaultTableShardingStrategy(); defaultKeyGenerateAlgorithm = null == config.getDefaultKeyGenerateStrategy() @@ -112,6 +117,7 @@ public final class ShardingRule implements DataNodeContainedRule, TableContained tableRules.addAll(createAutoTableRules(config.getAutoTables(), config.getDefaultKeyGenerateStrategy())); broadcastTables = config.getBroadcastTables(); bindingTableRules = createBindingTableRules(config.getBindingTableGroups()); + singleTableRules = SingleTableRuleLoader.load(databaseType, dataSourceMap, getExcludedTables()); defaultDatabaseShardingStrategyConfig = null == config.getDefaultDatabaseShardingStrategy() ? new NoneShardingStrategyConfiguration() : config.getDefaultDatabaseShardingStrategy(); defaultTableShardingStrategyConfig = null == config.getDefaultTableShardingStrategy() ? new NoneShardingStrategyConfiguration() : config.getDefaultTableShardingStrategy(); defaultKeyGenerateAlgorithm = null == config.getDefaultKeyGenerateStrategy() @@ -163,6 +169,12 @@ public final class ShardingRule implements DataNodeContainedRule, TableContained return new BindingTableRule(Splitter.on(",").trimResults().splitToList(bindingTableGroup).stream().map(this::getTableRule).collect(Collectors.toList())); } + private Collection getExcludedTables() { + Collection result = new HashSet<>(getTables()); + result.addAll(getAllActualTables()); + return result; + } + /** * Get database sharding strategy configuration. * diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java index 88ccce6c3da8eb6f75abc365d4e0c4cc803130e3..545cff9d9ddcebf1feacad24cfd5d57a7b313b5f 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java @@ -43,6 +43,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; public final class ShardingRuleTest { @@ -343,8 +344,8 @@ public final class ShardingRuleTest { private Map createDataSourceMap() { Map result = new HashMap<>(2, 1); - result.put("ds_0", mock(DataSource.class)); - result.put("ds_1", mock(DataSource.class)); + result.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS)); + result.put("ds_1", mock(DataSource.class, RETURNS_DEEP_STUBS)); return result; } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/AlgorithmProvidedShardingRuleBuilderTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/AlgorithmProvidedShardingRuleBuilderTest.java index d9496df69faa9fd5bb6b013bc56fa1ef144285d5..039b5403a403bc521f5ef184c5abc6e60617b56d 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/AlgorithmProvidedShardingRuleBuilderTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/AlgorithmProvidedShardingRuleBuilderTest.java @@ -30,6 +30,7 @@ import java.util.Collections; import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; public final class AlgorithmProvidedShardingRuleBuilderTest { @@ -44,7 +45,7 @@ public final class AlgorithmProvidedShardingRuleBuilderTest { AlgorithmProvidedShardingRuleConfiguration ruleConfig = mock(AlgorithmProvidedShardingRuleConfiguration.class); ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig); ((AlgorithmProvidedShardingRuleBuilder) builder).setDatabaseType(mock(DatabaseType.class)); - ((AlgorithmProvidedShardingRuleBuilder) builder).setDataSourceMap(Collections.singletonMap("name", mock(DataSource.class))); + ((AlgorithmProvidedShardingRuleBuilder) builder).setDataSourceMap(Collections.singletonMap("name", mock(DataSource.class, RETURNS_DEEP_STUBS))); assertThat(builder.build(ruleConfig), instanceOf(ShardingRule.class)); } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java index 36b0113923e1db1076345bbed96753d0af2325f8..6299985cf5a573621812de641cd08e4d71a353b9 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java @@ -30,6 +30,7 @@ import java.util.Collections; import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; public final class ShardingRuleBuilderTest { @@ -44,7 +45,7 @@ public final class ShardingRuleBuilderTest { ShardingRuleConfiguration ruleConfig = mock(ShardingRuleConfiguration.class); ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig); ((ShardingRuleBuilder) builder).setDatabaseType(mock(DatabaseType.class)); - ((ShardingRuleBuilder) builder).setDataSourceMap(Collections.singletonMap("name", mock(DataSource.class))); + ((ShardingRuleBuilder) builder).setDataSourceMap(Collections.singletonMap("name", mock(DataSource.class, RETURNS_DEEP_STUBS))); assertThat(builder.build(ruleConfig), instanceOf(ShardingRule.class)); } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowCreateTableMergedResultTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowCreateTableMergedResultTest.java index a5589c0b205e75eb513a59d2c64f233fb340dd69..ea94a0d2668a0a923f6a2c65e5929916eddf606d 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowCreateTableMergedResultTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowCreateTableMergedResultTest.java @@ -36,6 +36,7 @@ import java.util.Map; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -55,7 +56,7 @@ public final class ShowCreateTableMergedResultTest { ShardingTableRuleConfiguration tableRuleConfig = new ShardingTableRuleConfiguration("table", "ds.table_${0..2}"); ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); shardingRuleConfig.getTables().add(tableRuleConfig); - return new ShardingRule(shardingRuleConfig, mock(DatabaseType.class), Collections.singletonMap("ds", mock(DataSource.class))); + return new ShardingRule(shardingRuleConfig, mock(DatabaseType.class), Collections.singletonMap("ds", mock(DataSource.class, RETURNS_DEEP_STUBS))); } private ShardingSphereSchema buildSchema() { diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTablesMergedResultTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTablesMergedResultTest.java index 79dded0ebc1626935d539059fc99b8a80745ff52..4ae381010b375be769766b643e9812be411a8351 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTablesMergedResultTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTablesMergedResultTest.java @@ -36,6 +36,7 @@ import java.util.Map; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -55,7 +56,7 @@ public final class ShowTablesMergedResultTest { ShardingTableRuleConfiguration tableRuleConfig = new ShardingTableRuleConfiguration("table", "ds.table_${0..2}"); ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); shardingRuleConfig.getTables().add(tableRuleConfig); - return new ShardingRule(shardingRuleConfig, mock(DatabaseType.class), Collections.singletonMap("ds", mock(DataSource.class))); + return new ShardingRule(shardingRuleConfig, mock(DatabaseType.class), Collections.singletonMap("ds", mock(DataSource.class, RETURNS_DEEP_STUBS))); } private ShardingSphereSchema buildSchema() { diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/exception/NoSuchTableException.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/exception/NoSuchTableException.java index 8e9d842aa7c272906dd02ad75a186e5024420524..b5664ecc0f6267192a812b98c50a4690035aebc6 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/exception/NoSuchTableException.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/exception/NoSuchTableException.java @@ -28,13 +28,10 @@ public final class NoSuchTableException extends ShardingSphereException { private static final long serialVersionUID = 8311953084941769743L; - private final String databaseName; - private final String tableName; - public NoSuchTableException(final String databaseName, final String tableName) { - super(String.format("Table '%s.%s' doesn't exist", databaseName, tableName)); - this.databaseName = databaseName; + public NoSuchTableException(final String tableName) { + super(String.format("Table '%s' doesn't exist", tableName)); this.tableName = tableName; } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java index 571ab0a34f6048d693cfcff36ce4f580e9cf78cd..b4e23d60d3599ea232566dffc442eca81fc94df3 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java @@ -93,7 +93,7 @@ public final class ShardingRouteEngineFactory { return new ShardingUnicastRoutingEngine(tableNames); } if (!shardingRule.tableRuleExists(tableNames)) { - return new SingleTableRoutingEngine(tableNames, metaData.getSchema(), sqlStatement); + return new SingleTableRoutingEngine(tableNames, sqlStatement); } return getShardingRoutingEngine(shardingRule, shardingConditions, tableNames, props); } @@ -107,7 +107,7 @@ public final class ShardingRouteEngineFactory { } Collection tableNames = sqlStatementContext.getTablesContext().getTableNames(); if (!tableNames.isEmpty() && !shardingRule.tableRuleExists(tableNames)) { - return new SingleTableRoutingEngine(tableNames, metaData.getSchema(), sqlStatement); + return new SingleTableRoutingEngine(tableNames, sqlStatement); } return new ShardingTableBroadcastRoutingEngine(metaData.getSchema(), sqlStatementContext); } @@ -121,7 +121,7 @@ public final class ShardingRouteEngineFactory { return new ShardingDatabaseBroadcastRoutingEngine(); } if (!tableNames.isEmpty() && !shardingRule.tableRuleExists(tableNames)) { - return new SingleTableRoutingEngine(tableNames, metaData.getSchema(), sqlStatement); + return new SingleTableRoutingEngine(tableNames, sqlStatement); } if (!tableNames.isEmpty()) { return new ShardingUnicastRoutingEngine(tableNames); @@ -134,7 +134,7 @@ public final class ShardingRouteEngineFactory { Collection tableNames = sqlStatementContext.getTablesContext().getTableNames(); return shardingRule.tableRuleExists(tableNames) ? new ShardingTableBroadcastRoutingEngine(metaData.getSchema(), sqlStatementContext) - : new SingleTableRoutingEngine(tableNames, metaData.getSchema(), sqlStatementContext.getSqlStatement()); + : new SingleTableRoutingEngine(tableNames, sqlStatementContext.getSqlStatement()); } else { return new ShardingInstanceBroadcastRoutingEngine(metaData.getResource().getDataSourcesMetaData()); } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTableRoutingEngine.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTableRoutingEngine.java index aeb7a421c40ff179446e8472f8d15a8e0a9160f9..9c6a644f4dfafa6e9ffd8e4e0501385cf3accf0f 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTableRoutingEngine.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTableRoutingEngine.java @@ -20,7 +20,6 @@ package org.apache.shardingsphere.sharding.route.engine.type.single; import com.google.common.collect.Lists; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.exception.ShardingSphereException; -import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; import org.apache.shardingsphere.infra.route.context.RouteUnit; @@ -43,13 +42,11 @@ public final class SingleTableRoutingEngine implements ShardingRouteEngine { private final Collection logicTables; - private final ShardingSphereSchema schema; - private final SQLStatement sqlStatement; @Override public void route(final RouteContext routeContext, final ShardingRule shardingRule) { - Optional dataSourceName = sqlStatement instanceof CreateTableStatement ? getRandomDataSourceName(shardingRule.getDataSourceNames()) : findDataSourceName(); + Optional dataSourceName = sqlStatement instanceof CreateTableStatement ? getRandomDataSourceName(shardingRule.getDataSourceNames()) : findDataSourceNameOfSingleTable(shardingRule); if (!dataSourceName.isPresent()) { throw new ShardingSphereException("Can not route tables for `%s`, please make sure the tables are in same schema.", logicTables); } @@ -57,11 +54,10 @@ public final class SingleTableRoutingEngine implements ShardingRouteEngine { routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper(dataSourceName.get(), dataSourceName.get()), routingTables)); } - // TODO maybe enhance here, only return one data source for multiple tables for now - private Optional findDataSourceName() { + private Optional findDataSourceNameOfSingleTable(final ShardingRule shardingRule) { for (String each : logicTables) { - if (schema.containsTable(each)) { - return Optional.of(schema.get(each).getAddressingDataSources().iterator().next()); + if (shardingRule.getSingleTableRules().containsKey(each)) { + return Optional.of(shardingRule.getSingleTableRules().get(each).getDataSourceName()); } } return Optional.empty(); diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java index 86fcdec4a9b9b34aeeae38410dad1593a5802dc6..bd0bbaff926e6dbf6f13b65c365c40b86e50ad5a 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java @@ -57,8 +57,7 @@ public abstract class ShardingDDLStatementValidator impl for (SimpleTableSegment each : tables) { String tableName = each.getTableName().getIdentifier().getValue(); if (!schema.containsTable(tableName)) { - String dataSourceName = schema.get(tableName).getAddressingDataSources().iterator().next(); - throw new NoSuchTableException(dataSourceName, tableName); + throw new NoSuchTableException(tableName); } } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/fixture/AbstractRoutingEngineTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/fixture/AbstractRoutingEngineTest.java index 4d48f209a1ba84211226ec0f0786870a9e3d25e8..2e92fd9505be2e1997a716001a636c966ab86c4f 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/fixture/AbstractRoutingEngineTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/fixture/AbstractRoutingEngineTest.java @@ -29,6 +29,7 @@ import org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditi import org.apache.shardingsphere.sharding.route.engine.condition.value.ListShardingConditionValue; import org.apache.shardingsphere.sharding.route.engine.condition.value.ShardingConditionValue; import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sharding.rule.SingleTableRule; import javax.sql.DataSource; import java.util.ArrayList; @@ -38,6 +39,7 @@ import java.util.List; import java.util.Map; import java.util.Properties; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; public abstract class AbstractRoutingEngineTest { @@ -133,7 +135,9 @@ public abstract class AbstractRoutingEngineTest { props3.setProperty("algorithm-expression", "t_user_${user_id % 2}"); shardingRuleConfig.getShardingAlgorithms().put("t_user_inline", new ShardingSphereAlgorithmConfiguration("INLINE", props3)); shardingRuleConfig.getShardingAlgorithms().put("hint_test", new ShardingSphereAlgorithmConfiguration("HINT_TEST", new Properties())); - return new ShardingRule(shardingRuleConfig, mock(DatabaseType.class), createDataSourceMapWithMain()); + ShardingRule result = new ShardingRule(shardingRuleConfig, mock(DatabaseType.class), createDataSourceMapWithMain()); + result.getSingleTableRules().put("t_category", new SingleTableRule("t_category", "ds_0")); + return result; } private ShardingTableRuleConfiguration createInlineTableRuleConfig(final String tableName, final String actualDataNodes, final String algorithmExpression, final String dsAlgorithmExpression) { @@ -176,16 +180,16 @@ public abstract class AbstractRoutingEngineTest { private Map createDataSourceMap() { Map result = new HashMap<>(2, 1); - result.put("ds_0", mock(DataSource.class)); - result.put("ds_1", mock(DataSource.class)); + result.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS)); + result.put("ds_1", mock(DataSource.class, RETURNS_DEEP_STUBS)); return result; } private Map createDataSourceMapWithMain() { Map result = new HashMap<>(3, 1); - result.put("ds_0", mock(DataSource.class)); - result.put("ds_1", mock(DataSource.class)); - result.put("main", mock(DataSource.class)); + result.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS)); + result.put("ds_1", mock(DataSource.class, RETURNS_DEEP_STUBS)); + result.put("main", mock(DataSource.class, RETURNS_DEEP_STUBS)); return result; } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingDatabaseBroadcastRoutingEngineTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingDatabaseBroadcastRoutingEngineTest.java index 4d7678f49028db10c6a0830ba51110ddab3d0a88..57dc71b0cb4e13557e43c99813822c74ed586f22 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingDatabaseBroadcastRoutingEngineTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingDatabaseBroadcastRoutingEngineTest.java @@ -33,6 +33,7 @@ import java.util.Map; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; public final class ShardingDatabaseBroadcastRoutingEngineTest { @@ -53,8 +54,8 @@ public final class ShardingDatabaseBroadcastRoutingEngineTest { private Map createDataSourceMap() { Map result = new HashMap<>(2, 1); - result.put("ds_0", mock(DataSource.class)); - result.put("ds_1", mock(DataSource.class)); + result.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS)); + result.put("ds_1", mock(DataSource.class, RETURNS_DEEP_STUBS)); return result; } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngineTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngineTest.java index ec6ce22d4a566b9a64f3d9b124e7ee5177351ecb..c482b1551af2f153207754269ac49e5526fa1a81 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngineTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngineTest.java @@ -51,6 +51,7 @@ import java.util.Map; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -91,8 +92,8 @@ public final class ShardingTableBroadcastRoutingEngineTest { private Map createDataSourceMap() { Map result = new HashMap<>(2, 1); - result.put("ds0", mock(DataSource.class)); - result.put("ds1", mock(DataSource.class)); + result.put("ds0", mock(DataSource.class, RETURNS_DEEP_STUBS)); + result.put("ds1", mock(DataSource.class, RETURNS_DEEP_STUBS)); return result; } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTableRoutingEngineTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTableRoutingEngineTest.java index f70f39bb6dfebfac748cf2c2177186d091e202b9..d974db46d2138a0ebfeeb50c8546315a50ee7d7a 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTableRoutingEngineTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTableRoutingEngineTest.java @@ -18,13 +18,12 @@ package org.apache.shardingsphere.sharding.route.engine.type.single; import org.apache.shardingsphere.infra.database.type.DatabaseType; -import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema; -import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; import org.apache.shardingsphere.infra.route.context.RouteUnit; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sharding.rule.SingleTableRule; import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLCreateTableStatement; import org.junit.Test; @@ -38,15 +37,18 @@ import java.util.Map; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; public final class SingleTableRoutingEngineTest { @Test public void assertRoute() { - SingleTableRoutingEngine singleTableRoutingEngine = new SingleTableRoutingEngine(Arrays.asList("t_order", "t_order_item"), buildSchema(), null); + SingleTableRoutingEngine singleTableRoutingEngine = new SingleTableRoutingEngine(Arrays.asList("t_order", "t_order_item"), null); ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, mock(DatabaseType.class), createDataSourceMap()); + shardingRule.getSingleTableRules().put("t_order", new SingleTableRule("t_order", "ds_0")); + shardingRule.getSingleTableRules().put("t_order_item", new SingleTableRule("t_order_item", "ds_0")); RouteContext routeContext = new RouteContext(); singleTableRoutingEngine.route(routeContext, shardingRule); List routeUnits = new ArrayList<>(routeContext.getRouteUnits()); @@ -64,7 +66,7 @@ public final class SingleTableRoutingEngineTest { @Test public void assertRouteWithoutShardingRule() { - SingleTableRoutingEngine singleTableRoutingEngine = new SingleTableRoutingEngine(Arrays.asList("t_order", "t_order_item"), buildSchema(), new MySQLCreateTableStatement()); + SingleTableRoutingEngine singleTableRoutingEngine = new SingleTableRoutingEngine(Arrays.asList("t_order", "t_order_item"), new MySQLCreateTableStatement()); ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, mock(DatabaseType.class), createDataSourceMap()); RouteContext routeContext = new RouteContext(); @@ -81,23 +83,10 @@ public final class SingleTableRoutingEngineTest { assertThat(tableMapper1.getLogicName(), is("t_order_item")); } - private ShardingSphereSchema buildSchema() { - Map tables = new HashMap<>(2, 1); - tables.put("t_order", buildTableMetaData()); - tables.put("t_order_item", buildTableMetaData()); - return new ShardingSphereSchema(tables); - } - - private TableMetaData buildTableMetaData() { - TableMetaData result = new TableMetaData(); - result.getAddressingDataSources().add("ds_0"); - return result; - } - private Map createDataSourceMap() { Map result = new HashMap<>(2, 1); - result.put("ds_0", mock(DataSource.class)); - result.put("ds_1", mock(DataSource.class)); + result.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS)); + result.put("ds_1", mock(DataSource.class, RETURNS_DEEP_STUBS)); return result; } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngineTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngineTest.java index a792189f1efd2012e2ff790ed1584282a312909f..fd1da05d1aae6bbd4eb03128d979e458d82c79b4 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngineTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngineTest.java @@ -36,6 +36,7 @@ import java.util.Set; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; public final class ShardingUnicastRoutingEngineTest { @@ -88,7 +89,7 @@ public final class ShardingUnicastRoutingEngineTest { @Test(expected = ShardingSphereConfigurationException.class) public void assertRouteForWithNoIntersection() { - Set sets = new HashSet<>(); + Set sets = new HashSet<>(3, 1); sets.add("t_order"); sets.add("t_config"); sets.add("t_product"); @@ -107,9 +108,9 @@ public final class ShardingUnicastRoutingEngineTest { private Map createDataSourceMap() { Map result = new HashMap<>(3, 1); - result.put("ds_0", mock(DataSource.class)); - result.put("ds_1", mock(DataSource.class)); - result.put("ds_2", mock(DataSource.class)); + result.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS)); + result.put("ds_1", mock(DataSource.class, RETURNS_DEEP_STUBS)); + result.put("ds_2", mock(DataSource.class, RETURNS_DEEP_STUBS)); return result; } } diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaDataTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaDataTest.java index f085685c5a2cc812ca713537fab4c9e58807c3b2..1367efa918cda7ea2009f648d143726bfbebdcc1 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaDataTest.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/metadata/ShardingSphereDatabaseMetaDataTest.java @@ -96,7 +96,8 @@ public final class ShardingSphereDatabaseMetaDataTest { ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS); when(metaDataContexts.getDefaultMetaData()).thenReturn(metaData); when(metaData.getResource().getCachedDatabaseMetaData()).thenReturn(cachedDatabaseMetaData); - when(metaData.getRuleMetaData().getRules()).thenReturn(Collections.singletonList(mockShardingRule())); + ShardingRule shardingRule = mockShardingRule(); + when(metaData.getRuleMetaData().getRules()).thenReturn(Collections.singleton(shardingRule)); shardingSphereDatabaseMetaData = new ShardingSphereDatabaseMetaData(shardingSphereConnection); } @@ -104,7 +105,7 @@ public final class ShardingSphereDatabaseMetaDataTest { ShardingRuleConfiguration ruleConfig = new ShardingRuleConfiguration(); ShardingTableRuleConfiguration shardingTableRuleConfig = new ShardingTableRuleConfiguration(TABLE_NAME, DATA_SOURCE_NAME + "." + TABLE_NAME); ruleConfig.setTables(Collections.singletonList(shardingTableRuleConfig)); - return new ShardingRule(ruleConfig, mock(DatabaseType.class), Collections.singletonMap(DATA_SOURCE_NAME, mock(DataSource.class))); + return new ShardingRule(ruleConfig, mock(DatabaseType.class), Collections.singletonMap(DATA_SOURCE_NAME, mock(DataSource.class, RETURNS_DEEP_STUBS))); } @Test diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java index 066e03b715accad43aaa07c54122f9376fc07102..413919c5deb0289e12f4151240c7a1169a543484 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java +++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java @@ -81,7 +81,7 @@ public final class MySQLErrPacketFactory { return new MySQLErrPacket(1, MySQLServerErrorCode.ER_TABLE_EXISTS_ERROR, ((TableExistsException) cause).getTableName()); } if (cause instanceof NoSuchTableException) { - return new MySQLErrPacket(1, MySQLServerErrorCode.ER_NO_SUCH_TABLE, ((NoSuchTableException) cause).getDatabaseName(), ((NoSuchTableException) cause).getTableName()); + return new MySQLErrPacket(1, MySQLServerErrorCode.ER_NO_SUCH_TABLE, ((NoSuchTableException) cause).getTableName()); } if (cause instanceof CircuitBreakException) { return new MySQLErrPacket(1, CommonErrorCode.CIRCUIT_BREAK_MODE); diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java index 63ccb1d1e69740329b40dbb651fbf5244b40c57c..3a7a9bb4d19383f6649467b74c698c4899f68833 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java +++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java @@ -154,11 +154,11 @@ public final class MySQLErrPacketFactoryTest { @Test public void assertNewInstanceWithNoSuchTableException() { - MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new NoSuchTableException("ds_0", "table_name")); + MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new NoSuchTableException("table_name")); assertThat(actual.getSequenceId(), is(1)); assertThat(actual.getErrorCode(), is(1146)); assertThat(actual.getSqlState(), is("42S02")); - assertThat(actual.getErrorMessage(), is("Table 'ds_0.table_name' doesn't exist")); + assertThat(actual.getErrorMessage(), is("Table 'table_name' doesn't exist")); } @Test