This is an automated email from the ASF dual-hosted git repository.
yx9o 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 14e5643786a Add HintSQLException (#30776)
14e5643786a is described below
commit 14e5643786a8a4781b0e7de8a7eadb294575acb1
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Apr 5 00:42:06 2024 +0800
Add HintSQLException (#30776)
* Refactor SQLHintDataSourceNotExistsException
* Add HintSQLException
---
.../user-manual/error-code/sql-error-code.cn.md | 4 ++--
.../user-manual/error-code/sql-error-code.en.md | 4 ++--
.../exception/syntax/audit/SQLAuditException.java | 2 +-
.../DataSourceHintNotExistsException.java} | 10 +++++-----
.../HintSQLException.java} | 23 +++++++++++++++-------
.../route/engine/impl/PartialSQLRouteExecutor.java | 4 ++--
.../engine/impl/PartialSQLRouteExecutorTest.java | 6 +++---
7 files changed, 31 insertions(+), 22 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 6b32e32b3e1..1aee36579f9 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
@@ -64,8 +64,8 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| 12100 | 0A000 | DROP TABLE ... CASCADE is not supported. |
| 12100 | 42000 | You have an error in your SQL syntax: %s |
| 12101 | 42000 | Can not accept SQL type '%s'. |
-| 12200 | 42000 | SQL audit failed, error message: %s. |
-| 12201 | 42000 | Hint datasource '%s' does not exist. |
+| 12200 | 42000 | Hint data source '%s' does not exist. |
+| 12201 | 42000 | SQL audit failed, error message: %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 162b8e0a205..1a6cbc6a42b 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
@@ -64,8 +64,8 @@ SQL error codes provide by standard `SQL State`, `Vendor
Code` and `Reason`, whi
| 12100 | 0A000 | DROP TABLE ... CASCADE is not supported. |
| 12100 | 42000 | You have an error in your SQL syntax: %s |
| 12101 | 42000 | Can not accept SQL type '%s'. |
-| 12200 | 42000 | SQL audit failed, error message: %s. |
-| 12201 | 42000 | Hint datasource '%s' does not exist. |
+| 12200 | 42000 | Hint data source '%s' does not exist. |
+| 12201 | 42000 | SQL audit failed, error message: %s. |
### Connection
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/audit/SQLAuditException.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/audit/SQLAuditException.java
index 0e0e2988b8d..57a444a4ed3 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/audit/SQLAuditException.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/audit/SQLAuditException.java
@@ -27,6 +27,6 @@ public final class SQLAuditException extends
AuditSQLException {
private static final long serialVersionUID = 4183020614721058122L;
public SQLAuditException(final String errorMessage) {
- super(XOpenSQLState.SYNTAX_ERROR, 0, "SQL audit failed, error message:
%s.", errorMessage);
+ super(XOpenSQLState.SYNTAX_ERROR, 1, "SQL audit failed, error message:
%s.", errorMessage);
}
}
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/audit/SQLHintDataSourceNotExistsException.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/hint/DataSourceHintNotExistsException.java
similarity index 72%
rename from
infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/audit/SQLHintDataSourceNotExistsException.java
rename to
infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/hint/DataSourceHintNotExistsException.java
index 58ecaa231b9..d58546cbb86 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/audit/SQLHintDataSourceNotExistsException.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/hint/DataSourceHintNotExistsException.java
@@ -15,18 +15,18 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.exception.syntax.audit;
+package org.apache.shardingsphere.infra.exception.syntax.hint;
import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;
/**
- * SQL Hint data source not exists exception.
+ * Data source hint not exists exception.
*/
-public final class SQLHintDataSourceNotExistsException extends
AuditSQLException {
+public final class DataSourceHintNotExistsException extends HintSQLException {
private static final long serialVersionUID = -8222967059220727514L;
- public SQLHintDataSourceNotExistsException(final String errorMessage) {
- super(XOpenSQLState.SYNTAX_ERROR, 1, "Hint datasource '%s' does not
exist.", errorMessage);
+ public DataSourceHintNotExistsException(final String errorMessage) {
+ super(XOpenSQLState.SYNTAX_ERROR, 0, "Hint data source '%s' does not
exist.", errorMessage);
}
}
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/audit/SQLAuditException.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/hint/HintSQLException.java
similarity index 50%
copy from
infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/audit/SQLAuditException.java
copy to
infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/hint/HintSQLException.java
index 0e0e2988b8d..05a403eeec2 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/audit/SQLAuditException.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/hint/HintSQLException.java
@@ -15,18 +15,27 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.exception.syntax.audit;
+package org.apache.shardingsphere.infra.exception.syntax.hint;
-import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;
+import com.google.common.base.Preconditions;
+import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.SQLState;
+import
org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.SyntaxSQLException;
/**
- * SQL audit exception.
+ * Hint SQL exception.
*/
-public final class SQLAuditException extends AuditSQLException {
+public abstract class HintSQLException extends SyntaxSQLException {
- private static final long serialVersionUID = 4183020614721058122L;
+ private static final long serialVersionUID = -1856442132834477905L;
- public SQLAuditException(final String errorMessage) {
- super(XOpenSQLState.SYNTAX_ERROR, 0, "SQL audit failed, error message:
%s.", errorMessage);
+ private static final int HINT_CODE = 2;
+
+ protected HintSQLException(final SQLState sqlState, final int errorCode,
final String reason, final Object... messageArgs) {
+ super(sqlState, getErrorCode(errorCode), reason, messageArgs);
+ }
+
+ private static int getErrorCode(final int errorCode) {
+ Preconditions.checkArgument(errorCode >= 0 && errorCode < 100, "The
value range of error code should be [0, 100).");
+ return HINT_CODE * 100 + errorCode;
}
}
diff --git
a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java
b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java
index 748d5eee699..e845dbae568 100644
---
a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java
+++
b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java
@@ -21,7 +21,7 @@ import
org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.apache.shardingsphere.infra.hint.HintValueContext;
-import
org.apache.shardingsphere.infra.exception.syntax.audit.SQLHintDataSourceNotExistsException;
+import
org.apache.shardingsphere.infra.exception.syntax.hint.DataSourceHintNotExistsException;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
@@ -83,7 +83,7 @@ public final class PartialSQLRouteExecutor implements
SQLRouteExecutor {
private Optional<String> findDataSourceByHint(final HintValueContext
hintValueContext, final Map<String, StorageUnit> storageUnits) {
Optional<String> result = HintManager.isInstantiated() &&
HintManager.getDataSourceName().isPresent() ? HintManager.getDataSourceName() :
hintValueContext.findHintDataSourceName();
if (result.isPresent() && !storageUnits.containsKey(result.get())) {
- throw new SQLHintDataSourceNotExistsException(result.get());
+ throw new DataSourceHintNotExistsException(result.get());
}
return result;
}
diff --git
a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutorTest.java
b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutorTest.java
index 2f8d86ddb78..29359273746 100644
---
a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutorTest.java
+++
b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutorTest.java
@@ -21,7 +21,7 @@ import
org.apache.shardingsphere.infra.binder.context.statement.CommonSQLStateme
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.apache.shardingsphere.infra.hint.HintValueContext;
-import
org.apache.shardingsphere.infra.exception.syntax.audit.SQLHintDataSourceNotExistsException;
+import
org.apache.shardingsphere.infra.exception.syntax.hint.DataSourceHintNotExistsException;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
@@ -95,7 +95,7 @@ class PartialSQLRouteExecutorTest {
void assertRouteBySQLCommentHintWithException() {
when(hintValueContext.findHintDataSourceName()).thenReturn(Optional.of("ds_3"));
QueryContext queryContext = new
QueryContext(commonSQLStatementContext, "", Collections.emptyList(),
hintValueContext);
- assertThrows(SQLHintDataSourceNotExistsException.class, () ->
partialSQLRouteExecutor.route(connectionContext, queryContext,
mock(RuleMetaData.class), database));
+ assertThrows(DataSourceHintNotExistsException.class, () ->
partialSQLRouteExecutor.route(connectionContext, queryContext,
mock(RuleMetaData.class), database));
}
@Test
@@ -103,7 +103,7 @@ class PartialSQLRouteExecutorTest {
try (HintManager hintManager = HintManager.getInstance()) {
hintManager.setDataSourceName("ds-3");
QueryContext logicSQL = new
QueryContext(commonSQLStatementContext, "", Collections.emptyList(), new
HintValueContext());
- assertThrows(SQLHintDataSourceNotExistsException.class, () ->
partialSQLRouteExecutor.route(connectionContext, logicSQL,
mock(RuleMetaData.class), database));
+ assertThrows(DataSourceHintNotExistsException.class, () ->
partialSQLRouteExecutor.route(connectionContext, logicSQL,
mock(RuleMetaData.class), database));
}
}
}