diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md b/docs/document/content/user-manual/error-code/sql-error-code.cn.md index 34b9d24c08c50..947bf3c0ad652 100644 --- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md +++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md @@ -23,7 +23,6 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供, | HV008 | 10302 | Column index \`%d\` is out of range | | HY004 | 10400 | Invalid format for actual data node \`%s\` | | 42000 | 11000 | You have an error in your SQL syntax: %s | -| 42000 | 11001 | configuration error | | 42000 | 11002 | Resource does not exist | | 42000 | 11003 | Rule does not exist | | HY000 | 11004 | File access failed, reason is: %s | @@ -65,6 +64,9 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供, | 44000 | 20021 | Actual data nodes must be configured for sharding table \`%s\` | | 44000 | 20022 | Actual table \`%s.%s\` is not in table rule configuration | | 44000 | 20023 | Can not find actual data source intersection for logic tables \`%s\` | +| 44000 | 20024 | Can not find binding actual table, data source is \`%s\`, logic table is \`%s\`, other actual table is \`%s\` | +| 44000 | 20025 | Can not find strategy for generate keys with table \`%s\` | +| HY004 | 20026 | Invalid %s, datetime pattern should be \`%s\`, value is \`%s\` | | 42S01 | 20030 | Index \`%s\` already exists | | 42S02 | 20031 | Index \`%s\` does not exist | | 44000 | 20032 | Actual tables \`%s\` are in use | diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md b/docs/document/content/user-manual/error-code/sql-error-code.en.md index 2db37ab5824dd..eb84e13f0dc70 100644 --- a/docs/document/content/user-manual/error-code/sql-error-code.en.md +++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md @@ -23,7 +23,6 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi | HV008 | 10302 | Column index \`%d\` is out of range | | HY004 | 10400 | Invalid format for actual data node \`%s\` | | 42000 | 11000 | You have an error in your SQL syntax: %s | -| 42000 | 11001 | configuration error | | 42000 | 11002 | Resource does not exist | | 42000 | 11003 | Rule does not exist | | HY000 | 11004 | File access failed, reason is: %s | @@ -65,6 +64,9 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi | 44000 | 20021 | Actual data nodes must be configured for sharding table \`%s\` | | 44000 | 20022 | Actual table \`%s.%s\` is not in table rule configuration | | 44000 | 20023 | Can not find actual data source intersection for logic tables \`%s\` | +| 44000 | 20024 | Can not find binding actual table, data source is \`%s\`, logic table is \`%s\`, other actual table is \`%s\` | +| 44000 | 20025 | Can not find strategy for generate keys with table \`%s\` | +| HY004 | 20026 | Invalid %s, datetime pattern should be \`%s\`, value is \`%s\` | | 42S01 | 20030 | Index \`%s\` already exists | | 42S02 | 20031 | Index \`%s\` does not exist | | 44000 | 20032 | Actual tables \`%s\` are in use | diff --git a/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/test/java/org/apache/shardingsphere/dialect/mysql/mapper/MySQLDialectExceptionMapperTest.java b/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/test/java/org/apache/shardingsphere/dialect/mysql/mapper/MySQLDialectExceptionMapperTest.java index d9f301b948db2..328ee93bbe842 100644 --- a/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/test/java/org/apache/shardingsphere/dialect/mysql/mapper/MySQLDialectExceptionMapperTest.java +++ b/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/test/java/org/apache/shardingsphere/dialect/mysql/mapper/MySQLDialectExceptionMapperTest.java @@ -17,10 +17,9 @@ package org.apache.shardingsphere.dialect.mysql.mapper; +import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.dialect.exception.SQLDialectException; import org.apache.shardingsphere.dialect.exception.connection.TooManyConnectionsException; -import org.apache.shardingsphere.dialect.mysql.exception.UnknownCollationException; -import org.apache.shardingsphere.dialect.mysql.exception.UnsupportedPreparedStatementException; import org.apache.shardingsphere.dialect.exception.data.InsertColumnsAndValuesMismatchedException; import org.apache.shardingsphere.dialect.exception.syntax.database.DatabaseCreateExistsException; import org.apache.shardingsphere.dialect.exception.syntax.database.DatabaseDropNotExistsException; @@ -29,9 +28,16 @@ import org.apache.shardingsphere.dialect.exception.syntax.table.NoSuchTableException; import org.apache.shardingsphere.dialect.exception.syntax.table.TableExistsException; import org.apache.shardingsphere.dialect.exception.transaction.TableModifyInTransactionException; +import org.apache.shardingsphere.dialect.mysql.exception.UnknownCollationException; +import org.apache.shardingsphere.dialect.mysql.exception.UnsupportedPreparedStatementException; import org.apache.shardingsphere.dialect.mysql.vendor.MySQLVendorError; +import org.apache.shardingsphere.infra.util.exception.external.sql.vendor.VendorError; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import java.sql.SQLException; import java.util.Arrays; import java.util.Collection; @@ -39,9 +45,16 @@ import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; +@RunWith(Parameterized.class) +@RequiredArgsConstructor public final class MySQLDialectExceptionMapperTest { - private Collection getConvertParameters() { + private final Class sqlDialectExceptionClazz; + + private final VendorError vendorError; + + @Parameters(name = "{1} -> {0}") + public static Collection getParameters() { return Arrays.asList(new Object[][]{ {UnknownDatabaseException.class, MySQLVendorError.ER_NO_DB_ERROR}, {NoDatabaseSelectedException.class, MySQLVendorError.ER_NO_DB_ERROR}, @@ -58,11 +71,9 @@ private Collection getConvertParameters() { } @Test - @SuppressWarnings("unchecked") public void assertConvert() { - MySQLDialectExceptionMapper mySQLDialect = new MySQLDialectExceptionMapper(); - for (Object[] item : getConvertParameters()) { - assertThat(mySQLDialect.convert(mock((Class) item[0])).getErrorCode(), is(((MySQLVendorError) item[1]).getVendorCode())); - } + SQLException actual = new MySQLDialectExceptionMapper().convert(mock(sqlDialectExceptionClazz)); + assertThat(actual.getSQLState(), is(vendorError.getSqlState().getValue())); + assertThat(actual.getErrorCode(), is(vendorError.getVendorCode())); } } diff --git a/shardingsphere-dialect-exception/shardingsphere-postgresql-dialect-exception/src/test/java/org/apache/shardingsphere/dialect/postgresql/mapper/PostgreSQLDialectExceptionMapperTest.java b/shardingsphere-dialect-exception/shardingsphere-postgresql-dialect-exception/src/test/java/org/apache/shardingsphere/dialect/postgresql/mapper/PostgreSQLDialectExceptionMapperTest.java index 785d13832f69d..2082d6ba0e413 100644 --- a/shardingsphere-dialect-exception/shardingsphere-postgresql-dialect-exception/src/test/java/org/apache/shardingsphere/dialect/postgresql/mapper/PostgreSQLDialectExceptionMapperTest.java +++ b/shardingsphere-dialect-exception/shardingsphere-postgresql-dialect-exception/src/test/java/org/apache/shardingsphere/dialect/postgresql/mapper/PostgreSQLDialectExceptionMapperTest.java @@ -43,10 +43,10 @@ public final class PostgreSQLDialectExceptionMapperTest { private final Class sqlDialectExceptionClazz; - private final String psqlState; + private final String sqlState; @Parameters(name = "{1} -> {0}") - public static Collection getConvertParameters() { + public static Collection getParameters() { return Arrays.asList(new Object[][]{ {DatabaseCreateExistsException.class, "42P04"}, {InTransactionException.class, PSQLState.TRANSACTION_STATE_INVALID.getState()}, @@ -58,6 +58,6 @@ public static Collection getConvertParameters() { @Test public void convert() { - assertThat(new PostgreSQLDialectExceptionMapper().convert(mock(sqlDialectExceptionClazz)).getSQLState(), is(psqlState)); + assertThat(new PostgreSQLDialectExceptionMapper().convert(mock(sqlDialectExceptionClazz)).getSQLState(), is(sqlState)); } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/AutoIntervalShardingAlgorithm.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/AutoIntervalShardingAlgorithm.java index 546e864efbd99..e01d0ea6e58d1 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/AutoIntervalShardingAlgorithm.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/AutoIntervalShardingAlgorithm.java @@ -20,11 +20,11 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Range; import lombok.Getter; -import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException; import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm; import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue; import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue; import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm; +import org.apache.shardingsphere.sharding.exception.InvalidDatetimeFormatException; import java.text.DecimalFormat; import java.text.ParsePosition; @@ -73,7 +73,7 @@ private LocalDateTime getDateTime(final Properties props) { try { return LocalDateTime.parse(value, DATE_TIME_FORMAT); } catch (final DateTimeParseException ex) { - throw new ShardingSphereConfigurationException("Invalid %s, datetime pattern should be `yyyy-MM-dd HH:mm:ss`, value is `%s`", DATE_TIME_LOWER_KEY, value); + throw new InvalidDatetimeFormatException(DATE_TIME_LOWER_KEY, value, "yyyy-MM-dd HH:mm:ss"); } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java index 1e362bda50ad9..0db93971402ef 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java @@ -21,10 +21,10 @@ import com.google.common.collect.BoundType; import com.google.common.collect.Range; import lombok.Getter; -import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException; import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue; import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue; import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm; +import org.apache.shardingsphere.sharding.exception.InvalidDatetimeFormatException; import java.time.Instant; import java.time.LocalDate; @@ -41,10 +41,10 @@ import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalQueries; import java.util.Collection; +import java.util.Date; import java.util.HashSet; import java.util.Properties; import java.util.Set; -import java.util.Date; import java.util.stream.Collectors; /** @@ -112,7 +112,7 @@ private TemporalAccessor getDateTime(final String dateTimeKey, final String date try { return dateTimeFormatter.parse(dateTimeValue); } catch (final DateTimeParseException ex) { - throw new ShardingSphereConfigurationException("Invalid %s, datetime pattern should be `%s`, value is `%s`", dateTimeKey, dateTimePattern, dateTimeValue); + throw new InvalidDatetimeFormatException(dateTimeKey, dateTimeValue, dateTimePattern); } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/BindingTableNotFoundException.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/BindingTableNotFoundException.java new file mode 100644 index 0000000000000..3680472c1dc70 --- /dev/null +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/BindingTableNotFoundException.java @@ -0,0 +1,34 @@ +/* + * 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.sharding.exception; + +import org.apache.shardingsphere.infra.util.exception.external.sql.ShardingSphereSQLException; +import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState; + +/** + * Binding table not found exception. + */ +public final class BindingTableNotFoundException extends ShardingSphereSQLException { + + private static final long serialVersionUID = -8845309665057958820L; + + public BindingTableNotFoundException(final String dataSource, final String logicTable, final String otherActualTable) { + super(XOpenSQLState.CHECK_OPTION_VIOLATION, 20024, + "Can not find binding actual table, data source is `%s`, logic table is `%s`, other actual table is `%s`", dataSource, logicTable, otherActualTable); + } +} diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/exception/ShardingSphereConfigurationException.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/GenerateKeyStrategyNotFoundException.java similarity index 70% rename from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/exception/ShardingSphereConfigurationException.java rename to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/GenerateKeyStrategyNotFoundException.java index fad28425e4155..be87823d83000 100644 --- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/exception/ShardingSphereConfigurationException.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/GenerateKeyStrategyNotFoundException.java @@ -15,19 +15,19 @@ * limitations under the License. */ -package org.apache.shardingsphere.infra.config.exception; +package org.apache.shardingsphere.sharding.exception; import org.apache.shardingsphere.infra.util.exception.external.sql.ShardingSphereSQLException; import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState; /** - * Configuration exception. + * Generate key strategy not found exception. */ -public final class ShardingSphereConfigurationException extends ShardingSphereSQLException { +public final class GenerateKeyStrategyNotFoundException extends ShardingSphereSQLException { - private static final long serialVersionUID = -1360264079938958332L; + private static final long serialVersionUID = 7456922260524630374L; - public ShardingSphereConfigurationException(final String reason, final Object... messageArguments) { - super(XOpenSQLState.SYNTAX_ERROR, 11001, reason, messageArguments); + public GenerateKeyStrategyNotFoundException(final String tableName) { + super(XOpenSQLState.CHECK_OPTION_VIOLATION, 20050, "Can not find strategy for generate keys with table `%s`", tableName); } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/InvalidDatetimeFormatException.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/InvalidDatetimeFormatException.java new file mode 100644 index 0000000000000..a054c48def37a --- /dev/null +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/InvalidDatetimeFormatException.java @@ -0,0 +1,33 @@ +/* + * 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.sharding.exception; + +import org.apache.shardingsphere.infra.util.exception.external.sql.ShardingSphereSQLException; +import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState; + +/** + * Invalid datetime format exception. + */ +public final class InvalidDatetimeFormatException extends ShardingSphereSQLException { + + private static final long serialVersionUID = 4472952142748467059L; + + public InvalidDatetimeFormatException(final String dateTimeKey, final String dateTimeValue, final String dateTimePattern) { + super(XOpenSQLState.INVALID_DATA_TYPE, 20026, "Invalid %s, datetime pattern should be `%s`, value is `%s`", dateTimeKey, dateTimePattern, dateTimeValue); + } +} diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java index 5fc01c82adebb..84eee206c1c82 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java @@ -21,7 +21,6 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.type.CursorAvailable; -import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException; import org.apache.shardingsphere.infra.datanode.DataNode; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; @@ -80,7 +79,7 @@ public RouteContext route(final ShardingRule shardingRule) { return result; } - private void routeWithMultipleTables(final RouteContext routeContext, final ShardingRule shardingRule) throws ShardingSphereConfigurationException { + private void routeWithMultipleTables(final RouteContext routeContext, final ShardingRule shardingRule) { List tableMappers = new ArrayList<>(logicTables.size()); Set availableDataSourceNames = Collections.emptySet(); boolean first = true; diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/BindingTableRule.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/BindingTableRule.java index 08bc27c1515cf..4c1710c29e321 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/BindingTableRule.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/BindingTableRule.java @@ -19,8 +19,8 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException; import org.apache.shardingsphere.sharding.exception.ActualTableNotFoundException; +import org.apache.shardingsphere.sharding.exception.BindingTableNotFoundException; import java.util.Collection; import java.util.LinkedHashMap; @@ -67,7 +67,7 @@ public String getBindingActualTable(final String dataSource, final String logicT if (tableRule.isPresent()) { return tableRule.get().getActualDataNodes().get(index).getTableName(); } - throw new ShardingSphereConfigurationException("Cannot find binding actual table, data source: %s, logic table: %s, other actual table: %s", dataSource, logicTable, otherActualTable); + throw new BindingTableNotFoundException(dataSource, logicTable, otherActualTable); } /** diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java index e5f09dd4c7a6f..76276802aca15 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java @@ -23,7 +23,6 @@ import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext; import org.apache.shardingsphere.infra.config.algorithm.InstanceAwareAlgorithm; -import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine; import org.apache.shardingsphere.infra.datanode.DataNode; @@ -33,6 +32,7 @@ import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule; import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule; import org.apache.shardingsphere.infra.rule.identifier.type.TableContainedRule; +import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.util.expr.InlineExpressionParser; import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; @@ -45,6 +45,7 @@ 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.exception.GenerateKeyStrategyNotFoundException; import org.apache.shardingsphere.sharding.exception.ShardingRuleNotFoundException; import org.apache.shardingsphere.sharding.factory.KeyGenerateAlgorithmFactory; import org.apache.shardingsphere.sharding.factory.ShardingAlgorithmFactory; @@ -610,9 +611,7 @@ public Optional findGenerateKeyColumnName(final String logicTableName) { */ public Comparable generateKey(final String logicTableName) { Optional tableRule = findTableRule(logicTableName); - if (!tableRule.isPresent()) { - throw new ShardingSphereConfigurationException("Cannot find strategy for generate keys"); - } + ShardingSpherePreconditions.checkState(tableRule.isPresent(), new GenerateKeyStrategyNotFoundException(logicTableName)); KeyGenerateAlgorithm keyGenerator = null != tableRule.get().getKeyGeneratorName() ? keyGenerators.get(tableRule.get().getKeyGeneratorName()) : defaultKeyGenerateAlgorithm; return keyGenerator.generateKey(); } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/BindingTableRuleTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/BindingTableRuleTest.java index dadfd6eb5cad2..58533cdc46d52 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/BindingTableRuleTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/BindingTableRuleTest.java @@ -17,9 +17,9 @@ package org.apache.shardingsphere.sharding.rule; -import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException; import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; import org.apache.shardingsphere.sharding.exception.ActualTableNotFoundException; +import org.apache.shardingsphere.sharding.exception.BindingTableNotFoundException; import org.junit.Test; import java.util.ArrayList; @@ -59,7 +59,7 @@ public void assertGetBindingActualTablesFailureWhenNotFound() { createBindingTableRule().getBindingActualTable("no_ds", "Sub_Logic_Table", "LOGIC_TABLE", "table_1"); } - @Test(expected = ShardingSphereConfigurationException.class) + @Test(expected = BindingTableNotFoundException.class) public void assertGetBindingActualTablesFailureWhenLogicTableNotFound() { createBindingTableRule().getBindingActualTable("ds0", "No_Logic_Table", "LOGIC_TABLE", "table_1"); } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java index 5553d35ad048c..6eb6541143872 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java @@ -22,7 +22,6 @@ import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext; import org.apache.shardingsphere.infra.binder.statement.dml.UpdateStatementContext; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException; import org.apache.shardingsphere.infra.database.DefaultDatabase; import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine; import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType; @@ -42,6 +41,7 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration; 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.exception.GenerateKeyStrategyNotFoundException; import org.apache.shardingsphere.sharding.exception.ShardingRuleNotFoundException; import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression; @@ -288,7 +288,7 @@ public void assertNotFindGenerateKeyColumn() { assertFalse(createMinimumShardingRule().findGenerateKeyColumnName("sub_logic_table").isPresent()); } - @Test(expected = ShardingSphereConfigurationException.class) + @Test(expected = GenerateKeyStrategyNotFoundException.class) public void assertGenerateKeyFailure() { createMaximumShardingRule().generateKey("table_0"); }