Skip to content

Commit

Permalink
Add HintSQLException (#30776)
Browse files Browse the repository at this point in the history
* Refactor SQLHintDataSourceNotExistsException

* Add HintSQLException
  • Loading branch information
terrymanu authored Apr 4, 2024
1 parent 6aaf29a commit 14e5643
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

### 连接

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,7 +83,7 @@ public RouteContext route(final ConnectionContext connectionContext, final Query
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,15 +95,15 @@ 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
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));
}
}
}

0 comments on commit 14e5643

Please sign in to comment.