Skip to content

Commit

Permalink
Move DuplicateIndexException and IndexNotFoundException from sharding…
Browse files Browse the repository at this point in the history
… module to infra module
  • Loading branch information
terrymanu committed Dec 10, 2024
1 parent 922514b commit 915cbff
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| 10001 | 42S02 | Schema '%s' does not exist. |
| 10002 | 42S02 | Table or view '%s' does not exist. |
| 10003 | 42S02 | Unknown column '%s' in '%s'. |
| 10004 | 42S02 | Index '%s' does not exist. |
| 10005 | 42S01 | Index '%s' already exists. |
| 10010 | HY000 | Rule and storage meta data mismatched, reason is: %s. |
| 10100 | HY000 | Can not %s storage units '%s'. |
| 10101 | 42S02 | There is no storage unit in database '%s'. |
Expand Down Expand Up @@ -151,8 +153,6 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| 20004 | 42S02 | Actual table '%s.%s' is not in table rule configuration. |
| 20005 | 42S02 | Can not find binding actual table, data source is '%s', logic table is '%s', other actual table is '%s'. |
| 20006 | 44000 | Actual tables '%s' are in use. |
| 20007 | 42S01 | Index '%s' already exists. |
| 20008 | 42S02 | Index '%s' does not exist. |
| 20009 | 42S01 | View name has to bind to %s tables. |
| 20010 | 44000 | Invalid binding table configuration. |
| 20011 | 44000 | Only allowed 0 or 1 sharding strategy configuration. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
| 10001 | 42S02 | Schema '%s' does not exist. |
| 10002 | 42S02 | Table or view '%s' does not exist. |
| 10003 | 42S02 | Unknown column '%s' in '%s'. |
| 10004 | 42S02 | Index '%s' does not exist. |
| 10005 | 42S01 | Index '%s' already exists. |
| 10010 | HY000 | Rule and storage meta data mismatched, reason is: %s. |
| 10100 | HY000 | Can not %s storage units '%s'. |
| 10101 | 42S02 | There is no storage unit in database '%s'. |
Expand Down Expand Up @@ -151,8 +153,6 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
| 20004 | 42S02 | Actual table '%s.%s' is not in table rule configuration. |
| 20005 | 42S02 | Can not find binding actual table, data source is '%s', logic table is '%s', other actual table is '%s'. |
| 20006 | 44000 | Actual tables '%s' are in use. |
| 20007 | 42S01 | Index '%s' already exists. |
| 20008 | 42S02 | Index '%s' does not exist. |
| 20009 | 42S01 | View name has to bind to %s tables. |
| 20010 | 44000 | Invalid binding table configuration. |
| 20011 | 44000 | Only allowed 0 or 1 sharding strategy configuration. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sharding.checker.sql.util.ShardingSupportedCheckUtils;
import org.apache.shardingsphere.sharding.exception.metadata.DuplicateIndexException;
import org.apache.shardingsphere.sharding.exception.metadata.IndexNotExistedException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.DuplicateIndexException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.IndexNotFoundException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterIndexStatement;
Expand All @@ -47,7 +47,7 @@ public void check(final ShardingRule rule, final ShardingSphereDatabase database
Optional<IndexSegment> index = alterIndexStatement.getIndex();
ShardingSphereSchema schema = index.flatMap(optional -> optional.getOwner().map(owner -> database.getSchema(owner.getIdentifier().getValue()))).orElse(currentSchema);
if (index.isPresent() && !ShardingSupportedCheckUtils.isSchemaContainsIndex(schema, index.get())) {
throw new IndexNotExistedException(index.get().getIndexName().getIdentifier().getValue());
throw new IndexNotFoundException(index.get().getIndexName().getIdentifier().getValue());
}
Optional<IndexSegment> renameIndex = alterIndexStatement.getRenameIndex();
if (renameIndex.isPresent() && ShardingSupportedCheckUtils.isSchemaContainsIndex(schema, renameIndex.get())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sharding.checker.sql.common.ShardingSupportedCommonChecker;
import org.apache.shardingsphere.sharding.exception.metadata.DuplicateIndexException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.DuplicateIndexException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateIndexStatement;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sharding.checker.sql.util.ShardingSupportedCheckUtils;
import org.apache.shardingsphere.sharding.exception.metadata.IndexNotExistedException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.IndexNotFoundException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropIndexStatement;
Expand All @@ -47,7 +47,7 @@ public void check(final ShardingRule rule, final ShardingSphereDatabase database
}
for (IndexSegment each : dropIndexStatement.getIndexes()) {
ShardingSphereSchema schema = each.getOwner().map(optional -> optional.getIdentifier().getValue()).map(database::getSchema).orElse(currentSchema);
ShardingSpherePreconditions.checkState(ShardingSupportedCheckUtils.isSchemaContainsIndex(schema, each), () -> new IndexNotExistedException(each.getIndexName().getIdentifier().getValue()));
ShardingSpherePreconditions.checkState(ShardingSupportedCheckUtils.isSchemaContainsIndex(schema, each), () -> new IndexNotFoundException(each.getIndexName().getIdentifier().getValue()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.sharding.exception.metadata.DuplicateIndexException;
import org.apache.shardingsphere.sharding.exception.metadata.IndexNotExistedException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.DuplicateIndexException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.IndexNotFoundException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexNameSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexSegment;
Expand Down Expand Up @@ -70,7 +70,7 @@ void assertCheckWhenIndexNotExistRenameIndexNotExistForPostgreSQL() {
sqlStatement.setRenameIndex(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index_new"))));
ShardingSphereTable table = mock(ShardingSphereTable.class);
when(database.getSchema("public").getTable("t_order")).thenReturn(table);
assertThrows(IndexNotExistedException.class, () -> new ShardingAlterIndexSupportedChecker().check(rule, database, mock(), new AlterIndexStatementContext(sqlStatement, "foo_db")));
assertThrows(IndexNotFoundException.class, () -> new ShardingAlterIndexSupportedChecker().check(rule, database, mock(), new AlterIndexStatementContext(sqlStatement, "foo_db")));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sharding.exception.metadata.DuplicateIndexException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.DuplicateIndexException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexNameSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexSegment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.sharding.exception.metadata.IndexNotExistedException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.IndexNotFoundException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexNameSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexSegment;
Expand Down Expand Up @@ -70,6 +70,6 @@ void assertCheckWhenIndexNotExistForPostgreSQL() {
ShardingSphereTable table = mock(ShardingSphereTable.class);
when(database.getSchema("public").getAllTables()).thenReturn(Collections.singleton(table));
when(database.getSchema("public").getTable("t_order")).thenReturn(table);
assertThrows(IndexNotExistedException.class, () -> new ShardingDropIndexSupportedChecker().check(rule, database, mock(), new DropIndexStatementContext(sqlStatement, "foo_db")));
assertThrows(IndexNotFoundException.class, () -> new ShardingDropIndexSupportedChecker().check(rule, database, mock(), new DropIndexStatementContext(sqlStatement, "foo_db")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sharding.exception.metadata;
package org.apache.shardingsphere.infra.exception.kernel.metadata;

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

/**
* Duplicate index exception.
*/
public final class DuplicateIndexException extends ShardingSQLException {
public final class DuplicateIndexException extends MetaDataSQLException {

private static final long serialVersionUID = 2884666477007627873L;
private static final long serialVersionUID = 1463379993377506978L;

public DuplicateIndexException(final String indexName) {
super(XOpenSQLState.DUPLICATE, 7, "Index '%s' already exists.", indexName);
super(XOpenSQLState.DUPLICATE, 5, "Index '%s' already exists.", indexName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sharding.exception.metadata;
package org.apache.shardingsphere.infra.exception.kernel.metadata;

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

/**
* Index not existed exception.
* Index not found exception.
*/
public final class IndexNotExistedException extends ShardingSQLException {
public final class IndexNotFoundException extends MetaDataSQLException {

private static final long serialVersionUID = 206503570677185269L;
private static final long serialVersionUID = -4182388839280501305L;

public IndexNotExistedException(final String indexName) {
super(XOpenSQLState.NOT_FOUND, 8, "Index '%s' does not exist.", indexName);
public IndexNotFoundException(final String indexName) {
super(XOpenSQLState.NOT_FOUND, 4, "Index '%s' does not exist.", indexName);
}

public IndexNotFoundException(final String indexName, final String schemaName) {
super(XOpenSQLState.NOT_FOUND, 4, "Index '%s' does not exist from schema '%s'.", indexName, schemaName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package org.apache.shardingsphere.mode.metadata.refresher.type.index;

import com.google.common.base.Preconditions;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.kernel.metadata.IndexNotFoundException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.SchemaNotFoundException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
Expand Down Expand Up @@ -54,8 +54,7 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
ShardingSpherePreconditions.checkState(database.containsSchema(actualSchemaName), () -> new SchemaNotFoundException(actualSchemaName));
ShardingSphereSchema schema = database.getSchema(actualSchemaName);
Optional<String> logicTableName = findLogicTableName(schema, indexName);
// TODO define IndexNotFoundException
Preconditions.checkState(logicTableName.isPresent(), "Can not find logic table by index '%s' of schema '%s'.", indexName, schemaName);
ShardingSpherePreconditions.checkState(logicTableName.isPresent(), () -> new IndexNotFoundException(indexName, schemaName));
ShardingSpherePreconditions.checkState(schema.containsTable(logicTableName.get()), () -> new TableNotFoundException(logicTableName.get()));
ShardingSphereTable table = schema.getTable(logicTableName.get());
ShardingSphereTable newTable = new ShardingSphereTable(table.getName(), table.getAllColumns(), table.getAllIndexes(), table.getAllConstraints(), table.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.apache.shardingsphere.mode.metadata.refresher.type.index;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
Expand Down Expand Up @@ -48,8 +46,6 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
String indexName = null == sqlStatement.getIndex()
? IndexMetaDataUtils.getGeneratedLogicIndexName(sqlStatement.getColumns())
: sqlStatement.getIndex().getIndexName().getIdentifier().getValue();
// TODO define IndexNotFoundException
Preconditions.checkArgument(!Strings.isNullOrEmpty(indexName), "Index name is not exist.");
String tableName = sqlStatement.getTable().getTableName().getIdentifier().getValue();
ShardingSpherePreconditions.checkState(database.containsSchema(schemaName), () -> new SchemaNotFoundException(schemaName));
ShardingSphereSchema schema = database.getSchema(schemaName);
Expand Down

0 comments on commit 915cbff

Please sign in to comment.