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 6b32e32b3e1fe..1aee36579f9b3 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 162b8e0a2053f..1a6cbc6a42b30 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 0e0e2988b8dc5..57a444a4ed32f 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 58ecaa231b95b..d58546cbb8601 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/hint/HintSQLException.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/hint/HintSQLException.java new file mode 100644 index 0000000000000..05a403eeec264 --- /dev/null +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/syntax/hint/HintSQLException.java @@ -0,0 +1,41 @@ +/* + * 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.syntax.hint; + +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; + +/** + * Hint SQL exception. + */ +public abstract class HintSQLException extends SyntaxSQLException { + + private static final long serialVersionUID = -1856442132834477905L; + + 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 748d5eee69982..e845dbae56838 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.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 RouteContext route(final ConnectionContext connectionContext, final Query private Optional findDataSourceByHint(final HintValueContext hintValueContext, final Map storageUnits) { Optional 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 2f8d86ddb7802..29359273746e5 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.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 @@ void assertRouteByHintManagerHint() { 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 @@ void assertRouteByHintManagerHintWithException() { 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)); } } }