Skip to content

Commit

Permalink
Refactor GenerateKeyStrategyNotFoundException to AlgorithmNotFoundExc…
Browse files Browse the repository at this point in the history
…eption
  • Loading branch information
terrymanu committed Mar 16, 2024
1 parent 8ae2ab4 commit bae3357
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| 42S02 | 10007 | Table or view \`%s\` does not exist. |
| 42000 | 10010 | Rule does not exist. |
| 44000 | 10011 | '%s.'%s' initialization failed, reason is: %s. |
| 44000 | 10012 | Can not find '%s' algorithm on table '%s'. |
| 42S02 | 10020 | Schema \`%s\` does not exist. |
| 42S02 | 10021 | Single table \`%s\` does not exist. |
| HY000 | 10022 | Can not load table with database name \`%s\` and data source name \`%s\`. |
Expand Down Expand Up @@ -276,12 +277,6 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
|-----------|-------------|-------------------------------------------------------------|
| 42S02 | 20990 | Invalid mask algorithm \`%s\` in database \`%s\`. |

### 基础算法 - 分布式序列

| SQL State | Vendor Code | 错误信息 |
|-----------|-------------|---------------------------------------------------------------------|
| 44000 | 21180 | Can not find strategy for generate keys with table \`%s\`. |

### 联邦查询

| SQL State | Vendor Code | 错误信息 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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 | '%s.'%s' initialization failed, reason is: %s. |
| 44000 | 10012 | Can not find '%s' algorithm on table '%s'. |
| 42S02 | 10020 | Schema \`%s\` does not exist. |
| 42S02 | 10021 | Single table \`%s\` does not exist. |
| HY000 | 10022 | Can not load table with database name \`%s\` and data source name \`%s\`. |
Expand Down Expand Up @@ -291,12 +292,6 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
|-----------|-------------|-------------------------------------------------------------|
| 42S02 | 20990 | Invalid mask algorithm \`%s\` in database \`%s\`. |

### Infra algorithm - key generate

| SQL State | Vendor Code | Reason |
|-----------|-------------|---------------------------------------------------------------------|
| 44000 | 21180 | Can not find strategy for generate keys with table \`%s\`. |

### SQL Federation

| SQL State | Vendor Code | Reason |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import lombok.Getter;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
import org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
import org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmNotFoundException;
import org.apache.shardingsphere.infra.algorithm.keygen.core.KeyGenerateAlgorithm;
import org.apache.shardingsphere.infra.algorithm.keygen.core.exception.GenerateKeyStrategyNotFoundException;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
Expand Down Expand Up @@ -592,7 +592,7 @@ public Collection<? extends Comparable<?>> generateKeys(final AlgorithmSQLContex

private KeyGenerateAlgorithm getKeyGenerateAlgorithm(final String logicTableName) {
Optional<ShardingTable> shardingTable = findShardingTable(logicTableName);
ShardingSpherePreconditions.checkState(shardingTable.isPresent(), () -> new GenerateKeyStrategyNotFoundException(logicTableName));
ShardingSpherePreconditions.checkState(shardingTable.isPresent(), () -> new AlgorithmNotFoundException("key generator", logicTableName));
return null == shardingTable.get().getKeyGeneratorName() ? defaultKeyGenerateAlgorithm : keyGenerators.get(shardingTable.get().getKeyGeneratorName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
import org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
import org.apache.shardingsphere.infra.algorithm.keygen.core.exception.GenerateKeyStrategyNotFoundException;
import org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmNotFoundException;
import org.apache.shardingsphere.infra.algorithm.keygen.snowflake.SnowflakeKeyGenerateAlgorithm;
import org.apache.shardingsphere.infra.algorithm.keygen.uuid.UUIDKeyGenerateAlgorithm;
import org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext;
Expand Down Expand Up @@ -336,7 +336,7 @@ void assertNotFindGenerateKeyColumn() {
void assertGenerateKeyFailure() {
AlgorithmSQLContext generateContext = mock(AlgorithmSQLContext.class);
when(generateContext.getTableName()).thenReturn("table_0");
assertThrows(GenerateKeyStrategyNotFoundException.class, () -> createMaximumShardingRule().generateKeys(generateContext, 1));
assertThrows(AlgorithmNotFoundException.class, () -> createMaximumShardingRule().generateKeys(generateContext, 1));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.algorithm.keygen.core.exception;
package org.apache.shardingsphere.infra.algorithm.core.exception;

import org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;
import org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.MetaDataSQLException;

/**
* Generate key strategy not found exception.
* Algorithm not found exception.
*/
public final class GenerateKeyStrategyNotFoundException extends KeyGenerateSQLException {
public final class AlgorithmNotFoundException extends MetaDataSQLException {

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

public GenerateKeyStrategyNotFoundException(final String tableName) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 80, "Can not find strategy for generate keys with table `%s`.", tableName);
public AlgorithmNotFoundException(final String algorithmType, final String tableName) {
super(XOpenSQLState.CHECK_OPTION_VIOLATION, 12, "Can not find '%s' algorithm on table '%s'.", algorithmType, tableName);
}
}

0 comments on commit bae3357

Please sign in to comment.