This is an automated email from the ASF dual-hosted git repository.
wuweijie 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 d3e7d1236c1 Add SQLExceptionIdentifier (#30651)
d3e7d1236c1 is described below
commit d3e7d1236c1a7212a4d4307641f94672e4fbdfd6
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Mar 26 13:10:55 2024 +0800
Add SQLExceptionIdentifier (#30651)
* Use UnregisteredAlgorithmException on
ReadwriteSplittingRuleConfigurationChecker
* Add SQLExceptionIdentifier
* Add SQLExceptionIdentifier
---
.../user-manual/error-code/sql-error-code.cn.md | 4 +-
.../user-manual/error-code/sql-error-code.en.md | 4 +-
.../checker/EncryptRuleConfigurationChecker.java | 13 +++---
.../mask/checker/MaskRuleConfigurationChecker.java | 3 +-
...ReadwriteSplittingRuleConfigurationChecker.java | 3 +-
.../exception/EmptyAlgorithmOnColumnException.java | 5 ++-
.../exception/UnregisteredAlgorithmException.java | 9 ++--
.../sql/identifier/SQLExceptionIdentifier.java | 52 ++++++++++++++++++++++
.../identifier/SQLExceptionIdentifierTest.java} | 28 +++++++-----
9 files changed, 91 insertions(+), 30 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 0d43dee8368..15c42ca1bdb 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,8 +23,8 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| 42S02 | 10007 | Table or view \`%s\` does not exist.
|
| 42000 | 10010 | Rule does not exist.
|
| 44000 | 10011 | Algorithm '%s.'%s' initialization failed, reason
is: %s. |
-| 44000 | 10012 | '%s' algorithm on database.table.column:
'%s'.'%s'.'%s' is required. |
-| 42S02 | 10013 | '%s' algorithm '%s' on database.table.column:
'%s'.'%s'.'%s' is unregistered. |
+| 44000 | 10012 | '%s' algorithm on %s is required.
|
+| 42S02 | 10013 | '%s' algorithm '%s' on %s is unregistered.
|
| HY000 | 10015 | Algorithm '%s.%s' execute failed, reason is: %s.
|
| 42S02 | 10020 | Schema \`%s\` does not exist.
|
| 42S02 | 10021 | Single table \`%s\` does not exist.
|
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 7f5fcd4850a..9ce9f252c86 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,8 +23,8 @@ SQL error codes provide by standard `SQL State`, `Vendor
Code` and `Reason`, whi
| 42S02 | 10007 | Table or view \`%s\` does not exist.
|
| 42000 | 10010 | Rule does not exist.
|
| 44000 | 10011 | Algorithm '%s.'%s' initialization failed, reason
is: %s. |
-| 44000 | 10012 | '%s' algorithm on database.table.column:
'%s'.'%s'.'%s' is required. |
-| 42S02 | 10013 | '%s' algorithm '%s' on database.table.column:
'%s'.'%s'.'%s' is unregistered. |
+| 44000 | 10012 | '%s' algorithm on %s is required.
|
+| 42S02 | 10013 | '%s' algorithm '%s' on %s is unregistered.
|
| HY000 | 10015 | Algorithm '%s.%s' execute failed, reason is: %s.
|
| 42S02 | 10020 | Schema \`%s\` does not exist.
|
| 42S02 | 10021 | Single table \`%s\` does not exist.
|
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/EncryptRuleConfigurationChecker.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/EncryptRuleConfigurationChecker.java
index 0912cea66c7..0dd8a7d1b85 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/EncryptRuleConfigurationChecker.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/EncryptRuleConfigurationChecker.java
@@ -32,6 +32,7 @@ import
org.apache.shardingsphere.infra.algorithm.core.exception.EmptyAlgorithmOn
import
org.apache.shardingsphere.infra.algorithm.core.exception.UnregisteredAlgorithmException;
import
org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker;
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import
org.apache.shardingsphere.infra.exception.core.external.sql.identifier.SQLExceptionIdentifier;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
@@ -73,27 +74,27 @@ public final class EncryptRuleConfigurationChecker
implements RuleConfigurationC
final EncryptColumnItemRuleConfiguration
cipherColumnConfig, final Map<String, AlgorithmConfiguration> encryptors) {
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(cipherColumnConfig.getName()),
() -> new EncryptCipherColumnNotFoundException(logicColumnName, databaseName));
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(cipherColumnConfig.getEncryptorName()),
- () -> new EmptyAlgorithmOnColumnException("Standard encrypt",
databaseName, tableName, logicColumnName));
+ () -> new EmptyAlgorithmOnColumnException("Standard encrypt",
new SQLExceptionIdentifier(databaseName, tableName, logicColumnName)));
ShardingSpherePreconditions.checkState(encryptors.containsKey(cipherColumnConfig.getEncryptorName()),
- () -> new UnregisteredAlgorithmException("Standard encrypt",
cipherColumnConfig.getEncryptorName(), databaseName, tableName,
logicColumnName));
+ () -> new UnregisteredAlgorithmException("Standard encrypt",
cipherColumnConfig.getEncryptorName(), new SQLExceptionIdentifier(databaseName,
tableName, logicColumnName)));
}
private void checkAssistColumn(final String databaseName, final String
tableName, final String logicColumnName,
final EncryptColumnItemRuleConfiguration
assistedQueryColumnConfig, final Map<String, AlgorithmConfiguration>
encryptors) {
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(assistedQueryColumnConfig.getName()),
() -> new EncryptAssistedQueryColumnNotFoundException(logicColumnName,
databaseName));
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(assistedQueryColumnConfig.getEncryptorName()),
- () -> new EmptyAlgorithmOnColumnException("Assist query
encrypt", databaseName, tableName, logicColumnName));
+ () -> new EmptyAlgorithmOnColumnException("Assist query
encrypt", new SQLExceptionIdentifier(databaseName, tableName,
logicColumnName)));
ShardingSpherePreconditions.checkState(encryptors.containsKey(assistedQueryColumnConfig.getEncryptorName()),
- () -> new UnregisteredAlgorithmException("Assist query
encrypt", assistedQueryColumnConfig.getEncryptorName(), databaseName,
tableName, logicColumnName));
+ () -> new UnregisteredAlgorithmException("Assist query
encrypt", assistedQueryColumnConfig.getEncryptorName(), new
SQLExceptionIdentifier(databaseName, tableName, logicColumnName)));
}
private void checkLikeColumn(final String databaseName, final String
tableName, final String logicColumnName,
final EncryptColumnItemRuleConfiguration
likeQueryColumnConfig, final Map<String, AlgorithmConfiguration> encryptors) {
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(likeQueryColumnConfig.getName()),
() -> new EncryptLikeQueryColumnNotFoundException(logicColumnName,
databaseName));
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(likeQueryColumnConfig.getEncryptorName()),
- () -> new EmptyAlgorithmOnColumnException("Like query",
databaseName, tableName, logicColumnName));
+ () -> new EmptyAlgorithmOnColumnException("Like query", new
SQLExceptionIdentifier(databaseName, tableName, logicColumnName)));
ShardingSpherePreconditions.checkState(encryptors.containsKey(likeQueryColumnConfig.getEncryptorName()),
- () -> new UnregisteredAlgorithmException("Like query encrypt",
likeQueryColumnConfig.getEncryptorName(), databaseName, tableName,
logicColumnName));
+ () -> new UnregisteredAlgorithmException("Like query encrypt",
likeQueryColumnConfig.getEncryptorName(), new
SQLExceptionIdentifier(databaseName, tableName, logicColumnName)));
}
@Override
diff --git
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/checker/MaskRuleConfigurationChecker.java
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/checker/MaskRuleConfigurationChecker.java
index d7cfda42b7e..494aa0862ec 100644
---
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/checker/MaskRuleConfigurationChecker.java
+++
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/checker/MaskRuleConfigurationChecker.java
@@ -21,6 +21,7 @@ import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfigurat
import
org.apache.shardingsphere.infra.algorithm.core.exception.UnregisteredAlgorithmException;
import
org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker;
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import
org.apache.shardingsphere.infra.exception.core.external.sql.identifier.SQLExceptionIdentifier;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
@@ -59,7 +60,7 @@ public final class MaskRuleConfigurationChecker implements
RuleConfigurationChec
private void checkColumn(final String databaseName, final String
tableName, final MaskColumnRuleConfiguration columnRuleConfig, final
Map<String, AlgorithmConfiguration> maskAlgorithms) {
ShardingSpherePreconditions.checkState(maskAlgorithms.containsKey(columnRuleConfig.getMaskAlgorithm()),
- () -> new UnregisteredAlgorithmException("Mask",
columnRuleConfig.getMaskAlgorithm(), databaseName, tableName,
columnRuleConfig.getLogicColumn()));
+ () -> new UnregisteredAlgorithmException("Mask",
columnRuleConfig.getMaskAlgorithm(), new SQLExceptionIdentifier(databaseName,
tableName, columnRuleConfig.getLogicColumn())));
}
@Override
diff --git
a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationChecker.java
b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationChecker.java
index 12f02038692..866d817056a 100644
---
a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationChecker.java
+++
b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationChecker.java
@@ -22,6 +22,7 @@ import
org.apache.shardingsphere.infra.algorithm.core.exception.UnregisteredAlgo
import
org.apache.shardingsphere.infra.algorithm.loadbalancer.core.LoadBalanceAlgorithm;
import
org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker;
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import
org.apache.shardingsphere.infra.exception.core.external.sql.identifier.SQLExceptionIdentifier;
import org.apache.shardingsphere.infra.expr.core.InlineExpressionParserFactory;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import
org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
@@ -107,7 +108,7 @@ public final class
ReadwriteSplittingRuleConfigurationChecker implements RuleCon
continue;
}
LoadBalanceAlgorithm loadBalancer =
loadBalancers.get(each.getLoadBalancerName());
- ShardingSpherePreconditions.checkNotNull(loadBalancer, () -> new
UnregisteredAlgorithmException("Load balancer", each.getLoadBalancerName(),
databaseName));
+ ShardingSpherePreconditions.checkNotNull(loadBalancer, () -> new
UnregisteredAlgorithmException("Load balancer", each.getLoadBalancerName(), new
SQLExceptionIdentifier(databaseName)));
loadBalancer.check(databaseName, each.getReadDataSourceNames());
}
}
diff --git
a/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/EmptyAlgorithmOnColumnException.java
b/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/EmptyAlgorithmOnColumnException.java
index b2de509faf7..006600e60ee 100644
---
a/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/EmptyAlgorithmOnColumnException.java
+++
b/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/EmptyAlgorithmOnColumnException.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.infra.algorithm.core.exception;
+import
org.apache.shardingsphere.infra.exception.core.external.sql.identifier.SQLExceptionIdentifier;
import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;
import
org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.MetaDataSQLException;
@@ -27,7 +28,7 @@ public final class EmptyAlgorithmOnColumnException extends
MetaDataSQLException
private static final long serialVersionUID = 8128067899672436211L;
- public EmptyAlgorithmOnColumnException(final String algorithmType, final
String databaseName, final String tableName, final String columnName) {
- super(XOpenSQLState.NOT_FOUND, 12, "'%s' algorithm on
database.table.column: '%s'.'%s'.'%s' is required.", algorithmType,
databaseName, tableName, columnName);
+ public EmptyAlgorithmOnColumnException(final String algorithmType, final
SQLExceptionIdentifier sqlExceptionIdentifier) {
+ super(XOpenSQLState.NOT_FOUND, 12, "'%s' algorithm on %s is
required.", algorithmType, sqlExceptionIdentifier);
}
}
diff --git
a/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/UnregisteredAlgorithmException.java
b/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/UnregisteredAlgorithmException.java
index 29f25c71a1a..5a08a6093fe 100644
---
a/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/UnregisteredAlgorithmException.java
+++
b/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/UnregisteredAlgorithmException.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.infra.algorithm.core.exception;
+import
org.apache.shardingsphere.infra.exception.core.external.sql.identifier.SQLExceptionIdentifier;
import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;
import
org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.MetaDataSQLException;
@@ -27,11 +28,7 @@ public final class UnregisteredAlgorithmException extends
MetaDataSQLException {
private static final long serialVersionUID = -4570489906443880879L;
- public UnregisteredAlgorithmException(final String algorithmType, final
String algorithmName, final String databaseName) {
- super(XOpenSQLState.NOT_FOUND, 13, "'%s' algorithm '%s' on database:
'%s' is unregistered.", algorithmType, algorithmName, databaseName);
- }
-
- public UnregisteredAlgorithmException(final String algorithmType, final
String algorithmName, final String databaseName, final String tableName, final
String columnName) {
- super(XOpenSQLState.NOT_FOUND, 13, "'%s' algorithm '%s' on
database.table.column: '%s'.'%s'.'%s' is unregistered.", algorithmType,
algorithmName, databaseName, tableName, columnName);
+ public UnregisteredAlgorithmException(final String algorithmType, final
String algorithmName, final SQLExceptionIdentifier sqlExceptionIdentifier) {
+ super(XOpenSQLState.NOT_FOUND, 13, "'%s' algorithm '%s' on %s is
unregistered.", algorithmType, algorithmName, sqlExceptionIdentifier);
}
}
diff --git
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/identifier/SQLExceptionIdentifier.java
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/identifier/SQLExceptionIdentifier.java
new file mode 100644
index 00000000000..62e0269ade1
--- /dev/null
+++
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/identifier/SQLExceptionIdentifier.java
@@ -0,0 +1,52 @@
+/*
+ * 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.infra.exception.core.external.sql.identifier;
+
+import lombok.RequiredArgsConstructor;
+
+/**
+ * SQL Exception identifier.
+ */
+@RequiredArgsConstructor
+public final class SQLExceptionIdentifier {
+
+ private final String database;
+
+ private final String table;
+
+ private final String column;
+
+ public SQLExceptionIdentifier(final String database) {
+ this(database, null, null);
+ }
+
+ public SQLExceptionIdentifier(final String database, final String table) {
+ this(database, table, null);
+ }
+
+ @Override
+ public String toString() {
+ if (null != table && null != column) {
+ return String.format("database.table.column: '%s'.'%s'.'%s'",
database, table, column);
+ }
+ if (null != table) {
+ return String.format("database.table: '%s'.'%s'", database, table);
+ }
+ return String.format("database: '%s'", database);
+ }
+}
diff --git
a/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/EmptyAlgorithmOnColumnException.java
b/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/identifier/SQLExceptionIdentifierTest.java
similarity index 51%
copy from
infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/EmptyAlgorithmOnColumnException.java
copy to
infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/identifier/SQLExceptionIdentifierTest.java
index b2de509faf7..1807b711e78 100644
---
a/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/EmptyAlgorithmOnColumnException.java
+++
b/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/identifier/SQLExceptionIdentifierTest.java
@@ -15,19 +15,27 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.algorithm.core.exception;
+package org.apache.shardingsphere.infra.exception.core.external.sql.identifier;
-import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;
-import
org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.MetaDataSQLException;
+import org.junit.jupiter.api.Test;
-/**
- * Empty algorithm on column exception.
- */
-public final class EmptyAlgorithmOnColumnException extends
MetaDataSQLException {
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class SQLExceptionIdentifierTest {
- private static final long serialVersionUID = 8128067899672436211L;
+ @Test
+ void assertToStringForColumnIdentifier() {
+ assertThat(new SQLExceptionIdentifier("foo_db", "foo_tbl",
"foo_col").toString(), is("database.table.column:
'foo_db'.'foo_tbl'.'foo_col'"));
+ }
+
+ @Test
+ void assertToStringForTableIdentifier() {
+ assertThat(new SQLExceptionIdentifier("foo_db", "foo_tbl").toString(),
is("database.table: 'foo_db'.'foo_tbl'"));
+ }
- public EmptyAlgorithmOnColumnException(final String algorithmType, final
String databaseName, final String tableName, final String columnName) {
- super(XOpenSQLState.NOT_FOUND, 12, "'%s' algorithm on
database.table.column: '%s'.'%s'.'%s' is required.", algorithmType,
databaseName, tableName, columnName);
+ @Test
+ void assertToStringForDatabaseIdentifier() {
+ assertThat(new SQLExceptionIdentifier("foo_db").toString(),
is("database: 'foo_db'"));
}
}