This is an automated email from the ASF dual-hosted git repository.
panjuan 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 4e4318793b3 Add ShardingAlgorithmClassImplementationException (#20503)
4e4318793b3 is described below
commit 4e4318793b3e7fc6af7235c991d975331177bbf9
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Aug 25 11:47:29 2022 +0800
Add ShardingAlgorithmClassImplementationException (#20503)
* Add ShardingAlgorithmClassImplementationException
* Add ShardingAlgorithmClassImplementationException
---
.../user-manual/error-code/sql-error-code.cn.md | 1 +
.../user-manual/error-code/sql-error-code.en.md | 1 +
.../ClassBasedShardingAlgorithmFactory.java | 4 +--
...rdingAlgorithmClassImplementationException.java | 33 ++++++++++++++++++++++
.../ClassBasedShardingAlgorithmFactoryTest.java | 4 +--
.../ClassBasedShardingAlgorithmTest.java | 4 +--
.../core/statement/ShardingSphereStatement.java | 3 +-
7 files changed, 43 insertions(+), 7 deletions(-)
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 06e78f4eea3..7a6e666a42a 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
@@ -32,6 +32,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| HY000 | 15000 | Work ID assigned failed, which can not exceed 1024
|
| HY000 | 16000 | Can not find pipeline job \`%s\` |
| HY000 | 16001 | Failed to get DDL for table \`%s\` |
+| HY000 | 20000 | Sharding algorithm class \`%s\` should be
implement \`%s\` |
| HY004 | 24000 | Encrypt algorithm \`%s\` initialize failed, reason
is: %s |
| HY004 | 24001 | The SQL clause \`%s\` is unsupported in encrypt
rule |
| 44000 | 24002 | Altered column \`%s\` must use same encrypt
algorithm with previous column \`%s\` in table \`%s\` |
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 689bd0d8981..7745c46de33 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
@@ -32,6 +32,7 @@ SQL error codes provide by standard `SQL State`, `Vendor
Code` and `Reason`, whi
| HY000 | 15000 | Work ID assigned failed, which can not exceed 1024
|
| HY000 | 16000 | Can not find pipeline job \`%s\` |
| HY000 | 16001 | Failed to get DDL for table \`%s\` |
+| HY000 | 20000 | Sharding algorithm class \`%s\` should be
implement \`%s\` |
| HY004 | 24000 | Encrypt algorithm \`%s\` initialize failed, reason
is: %s |
| HY004 | 24001 | The SQL clause \`%s\` is unsupported in encrypt
rule |
| 44000 | 24002 | Altered column \`%s\` must use same encrypt
algorithm with previous column \`%s\` in table \`%s\` |
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactory.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactory.java
index 23e210c79ec..0b63c8eedd8 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactory.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactory.java
@@ -20,7 +20,7 @@ package
org.apache.shardingsphere.sharding.algorithm.sharding.classbased;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
+import
org.apache.shardingsphere.sharding.exception.ShardingAlgorithmClassImplementationException;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import java.util.Properties;
@@ -45,7 +45,7 @@ public final class ClassBasedShardingAlgorithmFactory {
public static <T extends ShardingAlgorithm> T newInstance(final String
shardingAlgorithmClassName, final Class<T> superShardingAlgorithmClass, final
Properties props) {
Class<?> algorithmClass = Class.forName(shardingAlgorithmClassName);
if (!superShardingAlgorithmClass.isAssignableFrom(algorithmClass)) {
- throw new ShardingSphereException("Class %s should be implement
%s", shardingAlgorithmClassName, superShardingAlgorithmClass.getName());
+ throw new
ShardingAlgorithmClassImplementationException(shardingAlgorithmClassName,
superShardingAlgorithmClass);
}
T result = (T) algorithmClass.getDeclaredConstructor().newInstance();
result.init(convertToStringTypedProperties(props));
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/ShardingAlgorithmClassImplementationException.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/ShardingAlgorithmClassImplementationException.java
new file mode 100644
index 00000000000..5aba1f4187a
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/ShardingAlgorithmClassImplementationException.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.sql.ShardingSphereSQLException;
+import
org.apache.shardingsphere.infra.util.exception.sql.sqlstate.XOpenSQLState;
+
+/**
+ * Sharding algorithm class implementation exception.
+ */
+public final class ShardingAlgorithmClassImplementationException extends
ShardingSphereSQLException {
+
+ private static final long serialVersionUID = 3053033454701332815L;
+
+ public ShardingAlgorithmClassImplementationException(final String
shardingAlgorithmClassName, final Class<?> superShardingAlgorithmClass) {
+ super(XOpenSQLState.GENERAL_ERROR, 11400, "Sharding algorithm class
`%s` should be implement `%s`", shardingAlgorithmClassName,
superShardingAlgorithmClass.getName());
+ }
+}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactoryTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactoryTest.java
index e109fa23d76..bbe53609a13 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactoryTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmFactoryTest.java
@@ -17,8 +17,8 @@
package org.apache.shardingsphere.sharding.algorithm.sharding.classbased;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import
org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
+import
org.apache.shardingsphere.sharding.exception.ShardingAlgorithmClassImplementationException;
import
org.apache.shardingsphere.sharding.fixture.ClassBasedHintShardingAlgorithmFixture;
import
org.apache.shardingsphere.sharding.fixture.ClassBasedStandardShardingAlgorithmFixture;
import org.junit.Test;
@@ -30,7 +30,7 @@ import static org.junit.Assert.assertThat;
public final class ClassBasedShardingAlgorithmFactoryTest {
- @Test(expected = ShardingSphereException.class)
+ @Test(expected = ShardingAlgorithmClassImplementationException.class)
public void assertNewInstanceWithUnAssignableFrom() {
ClassBasedShardingAlgorithmFactory.newInstance(ClassBasedHintShardingAlgorithmFixture.class.getName(),
StandardShardingAlgorithm.class, new Properties());
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmTest.java
index a2fc26e83c0..024157a3d67 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmTest.java
@@ -20,11 +20,11 @@ package
org.apache.shardingsphere.sharding.algorithm.sharding.classbased;
import com.google.common.collect.Range;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import
org.apache.shardingsphere.sharding.api.sharding.complex.ComplexKeysShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingValue;
import
org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import
org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
+import
org.apache.shardingsphere.sharding.exception.ShardingAlgorithmClassImplementationException;
import org.apache.shardingsphere.sharding.factory.ShardingAlgorithmFactory;
import
org.apache.shardingsphere.sharding.fixture.ClassBasedComplexKeysShardingAlgorithmFixture;
import
org.apache.shardingsphere.sharding.fixture.ClassBasedHintShardingAlgorithmFixture;
@@ -69,7 +69,7 @@ public final class ClassBasedShardingAlgorithmTest {
ShardingAlgorithmFactory.newInstance(new
AlgorithmConfiguration("CLASS_BASED", props));
}
- @Test(expected = ShardingSphereException.class)
+ @Test(expected = ShardingAlgorithmClassImplementationException.class)
public void assertInitWithMismatchStrategy() {
Properties props = new Properties();
props.setProperty("strategy", "standard");
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
index 510eaab9a9a..9995efe500f 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
@@ -79,6 +79,7 @@ import org.apache.shardingsphere.parser.rule.SQLParserRule;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement;
import org.apache.shardingsphere.traffic.engine.TrafficEngine;
+import
org.apache.shardingsphere.traffic.exception.EmptyTrafficExecutionUnitException;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import java.sql.Connection;
@@ -481,7 +482,7 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
DriverExecutionPrepareEngine<JDBCExecutionUnit, Connection>
prepareEngine = createDriverExecutionPrepareEngine();
ExecutionUnit executionUnit = new ExecutionUnit(trafficInstanceId, new
SQLUnit(queryContext.getSql(), queryContext.getParameters()));
ExecutionGroupContext<JDBCExecutionUnit> context =
prepareEngine.prepare(new RouteContext(),
Collections.singletonList(executionUnit));
- return context.getInputGroups().stream().flatMap(each ->
each.getInputs().stream()).findFirst().orElseThrow(() -> new
ShardingSphereException("Can not get traffic execution unit."));
+ return context.getInputGroups().stream().flatMap(each ->
each.getInputs().stream()).findFirst().orElseThrow(EmptyTrafficExecutionUnitException::new);
}
private void clearStatements() throws SQLException {