Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| HV008 | 10302 | Column index \`%d\` is out of range |
| HY004 | 10400 | Invalid format for actual data node \`%s\` |
| 42000 | 11000 | You have an error in your SQL syntax: %s |
| 42000 | 11001 | configuration error |
| 42000 | 11002 | Resource does not exist |
| 42000 | 11003 | Rule does not exist |
| HY000 | 11004 | File access failed, reason is: %s |
Expand Down Expand Up @@ -65,6 +64,9 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| 44000 | 20021 | Actual data nodes must be configured for sharding table \`%s\` |
| 44000 | 20022 | Actual table \`%s.%s\` is not in table rule configuration |
| 44000 | 20023 | Can not find actual data source intersection for logic tables \`%s\` |
| 44000 | 20024 | Can not find binding actual table, data source is \`%s\`, logic table is \`%s\`, other actual table is \`%s\` |
| 44000 | 20025 | Can not find strategy for generate keys with table \`%s\` |
| HY004 | 20026 | Invalid %s, datetime pattern should be \`%s\`, value is \`%s\` |
| 42S01 | 20030 | Index \`%s\` already exists |
| 42S02 | 20031 | Index \`%s\` does not exist |
| 44000 | 20032 | Actual tables \`%s\` are in use |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
| HV008 | 10302 | Column index \`%d\` is out of range |
| HY004 | 10400 | Invalid format for actual data node \`%s\` |
| 42000 | 11000 | You have an error in your SQL syntax: %s |
| 42000 | 11001 | configuration error |
| 42000 | 11002 | Resource does not exist |
| 42000 | 11003 | Rule does not exist |
| HY000 | 11004 | File access failed, reason is: %s |
Expand Down Expand Up @@ -65,6 +64,9 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
| 44000 | 20021 | Actual data nodes must be configured for sharding table \`%s\` |
| 44000 | 20022 | Actual table \`%s.%s\` is not in table rule configuration |
| 44000 | 20023 | Can not find actual data source intersection for logic tables \`%s\` |
| 44000 | 20024 | Can not find binding actual table, data source is \`%s\`, logic table is \`%s\`, other actual table is \`%s\` |
| 44000 | 20025 | Can not find strategy for generate keys with table \`%s\` |
| HY004 | 20026 | Invalid %s, datetime pattern should be \`%s\`, value is \`%s\` |
| 42S01 | 20030 | Index \`%s\` already exists |
| 42S02 | 20031 | Index \`%s\` does not exist |
| 44000 | 20032 | Actual tables \`%s\` are in use |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

package org.apache.shardingsphere.dialect.mysql.mapper;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.dialect.exception.SQLDialectException;
import org.apache.shardingsphere.dialect.exception.connection.TooManyConnectionsException;
import org.apache.shardingsphere.dialect.mysql.exception.UnknownCollationException;
import org.apache.shardingsphere.dialect.mysql.exception.UnsupportedPreparedStatementException;
import org.apache.shardingsphere.dialect.exception.data.InsertColumnsAndValuesMismatchedException;
import org.apache.shardingsphere.dialect.exception.syntax.database.DatabaseCreateExistsException;
import org.apache.shardingsphere.dialect.exception.syntax.database.DatabaseDropNotExistsException;
Expand All @@ -29,19 +28,33 @@
import org.apache.shardingsphere.dialect.exception.syntax.table.NoSuchTableException;
import org.apache.shardingsphere.dialect.exception.syntax.table.TableExistsException;
import org.apache.shardingsphere.dialect.exception.transaction.TableModifyInTransactionException;
import org.apache.shardingsphere.dialect.mysql.exception.UnknownCollationException;
import org.apache.shardingsphere.dialect.mysql.exception.UnsupportedPreparedStatementException;
import org.apache.shardingsphere.dialect.mysql.vendor.MySQLVendorError;
import org.apache.shardingsphere.infra.util.exception.external.sql.vendor.VendorError;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;

@RunWith(Parameterized.class)
@RequiredArgsConstructor
public final class MySQLDialectExceptionMapperTest {

private Collection<Object[]> getConvertParameters() {
private final Class<SQLDialectException> sqlDialectExceptionClazz;

private final VendorError vendorError;

@Parameters(name = "{1} -> {0}")
public static Collection<Object[]> getParameters() {
return Arrays.asList(new Object[][]{
{UnknownDatabaseException.class, MySQLVendorError.ER_NO_DB_ERROR},
{NoDatabaseSelectedException.class, MySQLVendorError.ER_NO_DB_ERROR},
Expand All @@ -58,11 +71,9 @@ private Collection<Object[]> getConvertParameters() {
}

@Test
@SuppressWarnings("unchecked")
public void assertConvert() {
MySQLDialectExceptionMapper mySQLDialect = new MySQLDialectExceptionMapper();
for (Object[] item : getConvertParameters()) {
assertThat(mySQLDialect.convert(mock((Class<SQLDialectException>) item[0])).getErrorCode(), is(((MySQLVendorError) item[1]).getVendorCode()));
}
SQLException actual = new MySQLDialectExceptionMapper().convert(mock(sqlDialectExceptionClazz));
assertThat(actual.getSQLState(), is(vendorError.getSqlState().getValue()));
assertThat(actual.getErrorCode(), is(vendorError.getVendorCode()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public final class PostgreSQLDialectExceptionMapperTest {

private final Class<SQLDialectException> sqlDialectExceptionClazz;

private final String psqlState;
private final String sqlState;

@Parameters(name = "{1} -> {0}")
public static Collection<Object[]> getConvertParameters() {
public static Collection<Object[]> getParameters() {
return Arrays.asList(new Object[][]{
{DatabaseCreateExistsException.class, "42P04"},
{InTransactionException.class, PSQLState.TRANSACTION_STATE_INVALID.getState()},
Expand All @@ -58,6 +58,6 @@ public static Collection<Object[]> getConvertParameters() {

@Test
public void convert() {
assertThat(new PostgreSQLDialectExceptionMapper().convert(mock(sqlDialectExceptionClazz)).getSQLState(), is(psqlState));
assertThat(new PostgreSQLDialectExceptionMapper().convert(mock(sqlDialectExceptionClazz)).getSQLState(), is(sqlState));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.Range;
import lombok.Getter;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
import org.apache.shardingsphere.sharding.exception.InvalidDatetimeFormatException;

import java.text.DecimalFormat;
import java.text.ParsePosition;
Expand Down Expand Up @@ -73,7 +73,7 @@ private LocalDateTime getDateTime(final Properties props) {
try {
return LocalDateTime.parse(value, DATE_TIME_FORMAT);
} catch (final DateTimeParseException ex) {
throw new ShardingSphereConfigurationException("Invalid %s, datetime pattern should be `yyyy-MM-dd HH:mm:ss`, value is `%s`", DATE_TIME_LOWER_KEY, value);
throw new InvalidDatetimeFormatException(DATE_TIME_LOWER_KEY, value, "yyyy-MM-dd HH:mm:ss");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import com.google.common.collect.BoundType;
import com.google.common.collect.Range;
import lombok.Getter;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
import org.apache.shardingsphere.sharding.exception.InvalidDatetimeFormatException;

import java.time.Instant;
import java.time.LocalDate;
Expand All @@ -41,10 +41,10 @@
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalQueries;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.Date;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -112,7 +112,7 @@ private TemporalAccessor getDateTime(final String dateTimeKey, final String date
try {
return dateTimeFormatter.parse(dateTimeValue);
} catch (final DateTimeParseException ex) {
throw new ShardingSphereConfigurationException("Invalid %s, datetime pattern should be `%s`, value is `%s`", dateTimeKey, dateTimePattern, dateTimeValue);
throw new InvalidDatetimeFormatException(dateTimeKey, dateTimeValue, dateTimePattern);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.external.sql.ShardingSphereSQLException;
import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;

/**
* Binding table not found exception.
*/
public final class BindingTableNotFoundException extends ShardingSphereSQLException {

private static final long serialVersionUID = -8845309665057958820L;

public BindingTableNotFoundException(final String dataSource, final String logicTable, final String otherActualTable) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 20024,
"Can not find binding actual table, data source is `%s`, logic table is `%s`, other actual table is `%s`", dataSource, logicTable, otherActualTable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.config.exception;
package org.apache.shardingsphere.sharding.exception;

import org.apache.shardingsphere.infra.util.exception.external.sql.ShardingSphereSQLException;
import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;

/**
* Configuration exception.
* Generate key strategy not found exception.
*/
public final class ShardingSphereConfigurationException extends ShardingSphereSQLException {
public final class GenerateKeyStrategyNotFoundException extends ShardingSphereSQLException {

private static final long serialVersionUID = -1360264079938958332L;
private static final long serialVersionUID = 7456922260524630374L;

public ShardingSphereConfigurationException(final String reason, final Object... messageArguments) {
super(XOpenSQLState.SYNTAX_ERROR, 11001, reason, messageArguments);
public GenerateKeyStrategyNotFoundException(final String tableName) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 20050, "Can not find strategy for generate keys with table `%s`", tableName);
}
}
Original file line number Diff line number Diff line change
@@ -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.external.sql.ShardingSphereSQLException;
import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;

/**
* Invalid datetime format exception.
*/
public final class InvalidDatetimeFormatException extends ShardingSphereSQLException {

private static final long serialVersionUID = 4472952142748467059L;

public InvalidDatetimeFormatException(final String dateTimeKey, final String dateTimeValue, final String dateTimePattern) {
super(XOpenSQLState.INVALID_DATA_TYPE, 20026, "Invalid %s, datetime pattern should be `%s`, value is `%s`", dateTimeKey, dateTimePattern, dateTimeValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.type.CursorAvailable;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
Expand Down Expand Up @@ -80,7 +79,7 @@ public RouteContext route(final ShardingRule shardingRule) {
return result;
}

private void routeWithMultipleTables(final RouteContext routeContext, final ShardingRule shardingRule) throws ShardingSphereConfigurationException {
private void routeWithMultipleTables(final RouteContext routeContext, final ShardingRule shardingRule) {
List<RouteMapper> tableMappers = new ArrayList<>(logicTables.size());
Set<String> availableDataSourceNames = Collections.emptySet();
boolean first = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.sharding.exception.ActualTableNotFoundException;
import org.apache.shardingsphere.sharding.exception.BindingTableNotFoundException;

import java.util.Collection;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -67,7 +67,7 @@ public String getBindingActualTable(final String dataSource, final String logicT
if (tableRule.isPresent()) {
return tableRule.get().getActualDataNodes().get(index).getTableName();
}
throw new ShardingSphereConfigurationException("Cannot find binding actual table, data source: %s, logic table: %s, other actual table: %s", dataSource, logicTable, otherActualTable);
throw new BindingTableNotFoundException(dataSource, logicTable, otherActualTable);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.config.algorithm.InstanceAwareAlgorithm;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
import org.apache.shardingsphere.infra.datanode.DataNode;
Expand All @@ -33,6 +32,7 @@
import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.TableContainedRule;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.util.expr.InlineExpressionParser;
import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
Expand All @@ -45,6 +45,7 @@
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
import org.apache.shardingsphere.sharding.exception.GenerateKeyStrategyNotFoundException;
import org.apache.shardingsphere.sharding.exception.ShardingRuleNotFoundException;
import org.apache.shardingsphere.sharding.factory.KeyGenerateAlgorithmFactory;
import org.apache.shardingsphere.sharding.factory.ShardingAlgorithmFactory;
Expand Down Expand Up @@ -610,9 +611,7 @@ public Optional<String> findGenerateKeyColumnName(final String logicTableName) {
*/
public Comparable<?> generateKey(final String logicTableName) {
Optional<TableRule> tableRule = findTableRule(logicTableName);
if (!tableRule.isPresent()) {
throw new ShardingSphereConfigurationException("Cannot find strategy for generate keys");
}
ShardingSpherePreconditions.checkState(tableRule.isPresent(), new GenerateKeyStrategyNotFoundException(logicTableName));
KeyGenerateAlgorithm keyGenerator = null != tableRule.get().getKeyGeneratorName() ? keyGenerators.get(tableRule.get().getKeyGeneratorName()) : defaultKeyGenerateAlgorithm;
return keyGenerator.generateKey();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

package org.apache.shardingsphere.sharding.rule;

import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
import org.apache.shardingsphere.sharding.exception.ActualTableNotFoundException;
import org.apache.shardingsphere.sharding.exception.BindingTableNotFoundException;
import org.junit.Test;

import java.util.ArrayList;
Expand Down Expand Up @@ -59,7 +59,7 @@ public void assertGetBindingActualTablesFailureWhenNotFound() {
createBindingTableRule().getBindingActualTable("no_ds", "Sub_Logic_Table", "LOGIC_TABLE", "table_1");
}

@Test(expected = ShardingSphereConfigurationException.class)
@Test(expected = BindingTableNotFoundException.class)
public void assertGetBindingActualTablesFailureWhenLogicTableNotFound() {
createBindingTableRule().getBindingActualTable("ds0", "No_Logic_Table", "LOGIC_TABLE", "table_1");
}
Expand Down
Loading