diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/orderby/EncryptOrderByItemSupportedChecker.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/orderby/EncryptOrderByItemSupportedChecker.java index b53b9b352de25..f12994d2ea1a7 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/orderby/EncryptOrderByItemSupportedChecker.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/orderby/EncryptOrderByItemSupportedChecker.java @@ -26,6 +26,7 @@ import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; import org.apache.shardingsphere.infra.checker.SupportedSQLChecker; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.ColumnOrderByItemSegment; @@ -60,10 +61,10 @@ private boolean containsOrderByItem(final SelectStatementContext sqlStatementCon } @Override - public void check(final EncryptRule rule, final ShardingSphereSchema schema, final SelectStatementContext sqlStatementContext) { + public void check(final EncryptRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final SelectStatementContext sqlStatementContext) { for (OrderByItem each : getOrderByItems(sqlStatementContext)) { if (each.getSegment() instanceof ColumnOrderByItemSegment) { - checkColumnOrderByItem(rule, schema, sqlStatementContext, ((ColumnOrderByItemSegment) each.getSegment()).getColumn()); + checkColumnOrderByItem(rule, currentSchema, sqlStatementContext, ((ColumnOrderByItemSegment) each.getSegment()).getColumn()); } } } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/predicate/EncryptPredicateColumnSupportedChecker.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/predicate/EncryptPredicateColumnSupportedChecker.java index 73e9fbfc242b9..9938819dc23e6 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/predicate/EncryptPredicateColumnSupportedChecker.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/predicate/EncryptPredicateColumnSupportedChecker.java @@ -30,13 +30,14 @@ import org.apache.shardingsphere.infra.checker.SupportedSQLChecker; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; +import org.apache.shardingsphere.sql.parser.statement.core.extractor.ExpressionExtractor; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.BinaryOperationExpression; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment; -import org.apache.shardingsphere.sql.parser.statement.core.extractor.ExpressionExtractor; import java.util.Collection; import java.util.Map; @@ -54,12 +55,12 @@ public boolean isCheck(final SQLStatementContext sqlStatementContext) { } @Override - public void check(final EncryptRule rule, final ShardingSphereSchema schema, final SQLStatementContext sqlStatementContext) { + public void check(final EncryptRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final SQLStatementContext sqlStatementContext) { Collection allSubqueryContexts = SQLStatementContextExtractor.getAllSubqueryContexts(sqlStatementContext); Collection joinConditions = SQLStatementContextExtractor.getJoinConditions((WhereAvailable) sqlStatementContext, allSubqueryContexts); ShardingSpherePreconditions.checkState(JoinConditionsEncryptorComparator.isSame(joinConditions, rule), () -> new UnsupportedSQLOperationException("Can not use different encryptor in join condition")); - check(rule, schema, (WhereAvailable) sqlStatementContext); + check(rule, currentSchema, (WhereAvailable) sqlStatementContext); } private void check(final EncryptRule rule, final ShardingSphereSchema schema, final WhereAvailable sqlStatementContext) { diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedChecker.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedChecker.java index 3e3208300a095..019bc6b3bb626 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedChecker.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedChecker.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext; import org.apache.shardingsphere.infra.checker.SupportedSQLChecker; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; /** @@ -36,7 +37,7 @@ public boolean isCheck(final SQLStatementContext sqlStatementContext) { } @Override - public void check(final EncryptRule rule, final ShardingSphereSchema schema, final InsertStatementContext sqlStatementContext) { - new EncryptSelectProjectionSupportedChecker().check(rule, schema, sqlStatementContext.getInsertSelectContext().getSelectStatementContext()); + public void check(final EncryptRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final InsertStatementContext sqlStatementContext) { + new EncryptSelectProjectionSupportedChecker().check(rule, database, currentSchema, sqlStatementContext.getInsertSelectContext().getSelectStatementContext()); } } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptSelectProjectionSupportedChecker.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptSelectProjectionSupportedChecker.java index 03a2245507aff..5cb206098d8ba 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptSelectProjectionSupportedChecker.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptSelectProjectionSupportedChecker.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.infra.checker.SupportedSQLChecker; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.combine.CombineSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionSegment; @@ -48,7 +49,7 @@ public boolean isCheck(final SQLStatementContext sqlStatementContext) { } @Override - public void check(final EncryptRule rule, final ShardingSphereSchema schema, final SelectStatementContext sqlStatementContext) { + public void check(final EncryptRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final SelectStatementContext sqlStatementContext) { checkSelectStatementContext(rule, sqlStatementContext); for (SelectStatementContext each : sqlStatementContext.getSubqueryContexts().values()) { checkSelectStatementContext(rule, each); diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/orderby/EncryptOrderByItemSupportedCheckerTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/orderby/EncryptOrderByItemSupportedCheckerTest.java index 229c43c0fb3c7..8fac0725822c5 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/orderby/EncryptOrderByItemSupportedCheckerTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/orderby/EncryptOrderByItemSupportedCheckerTest.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.sql.parser.statement.core.enums.OrderDirection; @@ -95,12 +96,13 @@ void assertIsCheckWithSubQueryOrderBy() { @Test void assertCheckFailed() { assertThrows(UnsupportedEncryptSQLException.class, - () -> new EncryptOrderByItemSupportedChecker().check(mockEncryptRule(), mock(ShardingSphereSchema.class), mockSelectStatementContext("foo_tbl"))); + () -> new EncryptOrderByItemSupportedChecker().check(mockEncryptRule(), mock(ShardingSphereDatabase.class), mock(ShardingSphereSchema.class), mockSelectStatementContext("foo_tbl"))); } @Test void assertCheckSuccess() { - assertDoesNotThrow(() -> new EncryptOrderByItemSupportedChecker().check(mockEncryptRule(), mock(ShardingSphereSchema.class), mockSelectStatementContext("bar_tbl"))); + assertDoesNotThrow( + () -> new EncryptOrderByItemSupportedChecker().check(mockEncryptRule(), mock(ShardingSphereDatabase.class), mock(ShardingSphereSchema.class), mockSelectStatementContext("bar_tbl"))); } private EncryptRule mockEncryptRule() { diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/predicate/EncryptPredicateColumnSupportedCheckerTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/predicate/EncryptPredicateColumnSupportedCheckerTest.java index 1c6c1e87845fe..0504b9a939d65 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/predicate/EncryptPredicateColumnSupportedCheckerTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/predicate/EncryptPredicateColumnSupportedCheckerTest.java @@ -62,7 +62,7 @@ void assertIsCheckWithNotEmptyWhereSegments() { @Test void assertCheckWithDifferentEncryptorsInJoinCondition() { assertThrows(UnsupportedSQLOperationException.class, () -> new EncryptPredicateColumnSupportedChecker() - .check(EncryptGeneratorFixtureBuilder.createEncryptRule(), null, mockSelectStatementContextWithDifferentEncryptorsInJoinCondition())); + .check(EncryptGeneratorFixtureBuilder.createEncryptRule(), null, null, mockSelectStatementContextWithDifferentEncryptorsInJoinCondition())); } private SQLStatementContext mockSelectStatementContextWithDifferentEncryptorsInJoinCondition() { @@ -80,7 +80,7 @@ private SQLStatementContext mockSelectStatementContextWithDifferentEncryptorsInJ @Test void assertCheckWithNotMatchedLikeQueryEncryptor() { assertThrows(MissingMatchedEncryptQueryAlgorithmException.class, () -> new EncryptPredicateColumnSupportedChecker() - .check(EncryptGeneratorFixtureBuilder.createEncryptRule(), null, mockSelectStatementContextWithLike())); + .check(EncryptGeneratorFixtureBuilder.createEncryptRule(), null, null, mockSelectStatementContextWithLike())); } private SQLStatementContext mockSelectStatementContextWithLike() { @@ -98,7 +98,7 @@ private SQLStatementContext mockSelectStatementContextWithLike() { @Test void assertCheckSuccess() { - assertDoesNotThrow(() -> new EncryptPredicateColumnSupportedChecker().check(EncryptGeneratorFixtureBuilder.createEncryptRule(), null, mockSelectStatementContextWithEqual())); + assertDoesNotThrow(() -> new EncryptPredicateColumnSupportedChecker().check(EncryptGeneratorFixtureBuilder.createEncryptRule(), null, null, mockSelectStatementContextWithEqual())); } private SQLStatementContext mockSelectStatementContextWithEqual() { diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedCheckerTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedCheckerTest.java index b83eaad74f380..062f509493694 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedCheckerTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptInsertSelectProjectionSupportedCheckerTest.java @@ -21,6 +21,7 @@ import org.apache.shardingsphere.infra.binder.context.segment.insert.values.InsertSelectContext; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.junit.jupiter.api.Test; @@ -55,6 +56,7 @@ void assertIsCheckWithInsertSelect() { @Test void assertCheckSuccess() { assertDoesNotThrow( - () -> new EncryptInsertSelectProjectionSupportedChecker().check(mock(EncryptRule.class), mock(ShardingSphereSchema.class), mock(InsertStatementContext.class, RETURNS_DEEP_STUBS))); + () -> new EncryptInsertSelectProjectionSupportedChecker().check(mock(EncryptRule.class), mock(ShardingSphereDatabase.class), mock(ShardingSphereSchema.class), + mock(InsertStatementContext.class, RETURNS_DEEP_STUBS))); } } diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptSelectProjectionSupportedCheckerTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptSelectProjectionSupportedCheckerTest.java index 10157dd52c506..023781df499f1 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptSelectProjectionSupportedCheckerTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/projection/EncryptSelectProjectionSupportedCheckerTest.java @@ -70,19 +70,19 @@ void assertCheckWhenShorthandExpandContainsSubqueryTable() { SelectStatementContext sqlStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS); when(sqlStatementContext.containsTableSubquery()).thenReturn(true); when(sqlStatementContext.getSqlStatement().getProjections().getProjections()).thenReturn(Collections.singleton(new ShorthandProjectionSegment(0, 0))); - assertThrows(UnsupportedSQLOperationException.class, () -> new EncryptSelectProjectionSupportedChecker().check(mock(EncryptRule.class, RETURNS_DEEP_STUBS), null, sqlStatementContext)); + assertThrows(UnsupportedSQLOperationException.class, () -> new EncryptSelectProjectionSupportedChecker().check(mock(EncryptRule.class, RETURNS_DEEP_STUBS), null, null, sqlStatementContext)); } @Test void assertCheckWhenCombineStatementContainsEncryptColumn() { SelectStatementContext sqlStatementContext = mockSelectStatementContext(); - assertThrows(UnsupportedSQLOperationException.class, () -> new EncryptSelectProjectionSupportedChecker().check(mock(EncryptRule.class, RETURNS_DEEP_STUBS), null, sqlStatementContext)); + assertThrows(UnsupportedSQLOperationException.class, () -> new EncryptSelectProjectionSupportedChecker().check(mock(EncryptRule.class, RETURNS_DEEP_STUBS), null, null, sqlStatementContext)); } @Test void assertCheckSuccess() { SelectStatementContext sqlStatementContext = mockSelectStatementContext(); - assertDoesNotThrow(() -> new EncryptSelectProjectionSupportedChecker().check(mockEncryptRule(), null, sqlStatementContext)); + assertDoesNotThrow(() -> new EncryptSelectProjectionSupportedChecker().check(mockEncryptRule(), null, null, sqlStatementContext)); } private SelectStatementContext mockSelectStatementContext() { diff --git a/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SupportedSQLCheckEngine.java b/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SupportedSQLCheckEngine.java index da53593db4e5d..dd562faae1202 100644 --- a/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SupportedSQLCheckEngine.java +++ b/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SupportedSQLCheckEngine.java @@ -20,6 +20,7 @@ import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.type.TableAvailable; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader; @@ -38,23 +39,22 @@ public final class SupportedSQLCheckEngine { * * @param rules rules * @param sqlStatementContext to be checked SQL statement context - * @param schemas schemas - * @param databaseName database name + * @param database database */ @SuppressWarnings({"rawtypes", "unchecked"}) - public void checkSQL(final Collection rules, final SQLStatementContext sqlStatementContext, final Map schemas, final String databaseName) { - ShardingSphereSchema schema = getSchema(sqlStatementContext, schemas, databaseName); + public void checkSQL(final Collection rules, final SQLStatementContext sqlStatementContext, final ShardingSphereDatabase database) { + ShardingSphereSchema currentSchema = getCurrentSchema(sqlStatementContext, database.getSchemas(), database.getName()); for (Entry entry : OrderedSPILoader.getServices(SupportedSQLCheckersBuilder.class, rules).entrySet()) { Collection checkers = entry.getValue().getSupportedSQLCheckers(); for (SupportedSQLChecker each : checkers) { if (each.isCheck(sqlStatementContext)) { - each.check(entry.getKey(), schema, sqlStatementContext); + each.check(entry.getKey(), database, currentSchema, sqlStatementContext); } } } } - private ShardingSphereSchema getSchema(final SQLStatementContext sqlStatementContext, final Map schemas, final String databaseName) { + private ShardingSphereSchema getCurrentSchema(final SQLStatementContext sqlStatementContext, final Map schemas, final String databaseName) { ShardingSphereSchema defaultSchema = schemas.get(new DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(databaseName)); return sqlStatementContext instanceof TableAvailable ? ((TableAvailable) sqlStatementContext).getTablesContext().getSchemaName().map(schemas::get).orElse(defaultSchema) : defaultSchema; } diff --git a/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SupportedSQLChecker.java b/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SupportedSQLChecker.java index 438d65c2801af..c3c0a6cf6e597 100644 --- a/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SupportedSQLChecker.java +++ b/infra/checker/src/main/java/org/apache/shardingsphere/infra/checker/SupportedSQLChecker.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.infra.checker; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; @@ -41,8 +42,9 @@ public interface SupportedSQLChecker { + + @Override + public Collection> getSupportedSQLCheckers() { + return Arrays.asList( + new SingleDropSchemaSupportedChecker(), + new SingleDropTableSupportedChecker()); + } + + @Override + public int getOrder() { + return SingleOrder.ORDER; + } + + @Override + public Class getTypeClass() { + return SingleRule.class; + } +} diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/validator/ddl/SingleDropSchemaMetaDataValidator.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/schema/SingleDropSchemaSupportedChecker.java similarity index 74% rename from kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/validator/ddl/SingleDropSchemaMetaDataValidator.java rename to kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/schema/SingleDropSchemaSupportedChecker.java index 291a8db88754d..13f5e62bdc02e 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/validator/ddl/SingleDropSchemaMetaDataValidator.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/schema/SingleDropSchemaSupportedChecker.java @@ -15,26 +15,33 @@ * limitations under the License. */ -package org.apache.shardingsphere.single.route.validator.ddl; +package org.apache.shardingsphere.single.checker.sql.schema; +import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; +import org.apache.shardingsphere.infra.checker.SupportedSQLChecker; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.exception.kernel.metadata.SchemaNotFoundException; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.single.exception.DropNotEmptySchemaException; -import org.apache.shardingsphere.single.route.validator.SingleMetaDataValidator; import org.apache.shardingsphere.single.rule.SingleRule; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropSchemaStatement; import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; /** - * Single drop schema meta data validator. + * Drop schema supported checker for single. */ -public final class SingleDropSchemaMetaDataValidator implements SingleMetaDataValidator { +@HighFrequencyInvocation +public final class SingleDropSchemaSupportedChecker implements SupportedSQLChecker { @Override - public void validate(final SingleRule rule, final SQLStatementContext sqlStatementContext, final ShardingSphereDatabase database) { + public boolean isCheck(final SQLStatementContext sqlStatementContext) { + return sqlStatementContext.getSqlStatement() instanceof DropSchemaStatement; + } + + @Override + public void check(final SingleRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final SQLStatementContext sqlStatementContext) { DropSchemaStatement dropSchemaStatement = (DropSchemaStatement) sqlStatementContext.getSqlStatement(); boolean containsCascade = dropSchemaStatement.isContainsCascade(); for (IdentifierValue each : dropSchemaStatement.getSchemaNames()) { diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/validator/ddl/SingleDropTableValidator.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java similarity index 62% rename from kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/validator/ddl/SingleDropTableValidator.java rename to kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java index 8bf723dce75ae..1abf65ba2ebcd 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/validator/ddl/SingleDropTableValidator.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java @@ -15,22 +15,30 @@ * limitations under the License. */ -package org.apache.shardingsphere.single.route.validator.ddl; +package org.apache.shardingsphere.single.checker.sql.table; +import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; +import org.apache.shardingsphere.infra.checker.SupportedSQLChecker; import org.apache.shardingsphere.infra.exception.kernel.syntax.UnsupportedDropCascadeTableException; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.single.route.validator.SingleMetaDataValidator; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.single.rule.SingleRule; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement; /** - * Single drop table validator. + * Drop table supported checker for single. */ -public final class SingleDropTableValidator implements SingleMetaDataValidator { +@HighFrequencyInvocation +public final class SingleDropTableSupportedChecker implements SupportedSQLChecker { @Override - public void validate(final SingleRule rule, final SQLStatementContext sqlStatementContext, final ShardingSphereDatabase database) { + public boolean isCheck(final SQLStatementContext sqlStatementContext) { + return sqlStatementContext.getSqlStatement() instanceof DropTableStatement; + } + + @Override + public void check(final SingleRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final SQLStatementContext sqlStatementContext) { if (((DropTableStatement) sqlStatementContext.getSqlStatement()).isContainsCascade()) { throw new UnsupportedDropCascadeTableException(); } diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java index f278ff348549d..e7fd34700a083 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java @@ -33,7 +33,6 @@ import org.apache.shardingsphere.infra.session.query.QueryContext; import org.apache.shardingsphere.single.constant.SingleOrder; import org.apache.shardingsphere.single.route.engine.SingleRouteEngineFactory; -import org.apache.shardingsphere.single.route.validator.SingleMetaDataValidatorFactory; import org.apache.shardingsphere.single.rule.SingleRule; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateTableStatement; @@ -54,24 +53,18 @@ public RouteContext createRouteContext(final QueryContext queryContext, final Ru return createSingleDataSourceRouteContext(rule, database, queryContext); } RouteContext result = new RouteContext(); - SQLStatementContext sqlStatementContext = queryContext.getSqlStatementContext(); - SingleMetaDataValidatorFactory.newInstance(sqlStatementContext.getSqlStatement()).ifPresent(optional -> optional.validate(rule, sqlStatementContext, database)); - Collection singleTables = getSingleTables(database, rule, result, sqlStatementContext); - SingleRouteEngineFactory.newInstance(singleTables, sqlStatementContext.getSqlStatement(), queryContext.getHintValueContext()).ifPresent(optional -> optional.route(result, rule)); + Collection singleTables = getSingleTables(database, rule, result, queryContext.getSqlStatementContext()); + SingleRouteEngineFactory.newInstance(singleTables, queryContext.getSqlStatementContext().getSqlStatement(), queryContext.getHintValueContext()) + .ifPresent(optional -> optional.route(result, rule)); return result; } - private Collection getSingleTables(final ShardingSphereDatabase database, final SingleRule rule, final RouteContext routeContext, final SQLStatementContext sqlStatementContext) { - Collection qualifiedTables = rule.getQualifiedTables(sqlStatementContext, database); - return routeContext.getRouteUnits().isEmpty() && sqlStatementContext.getSqlStatement() instanceof CreateTableStatement ? qualifiedTables : rule.getSingleTables(qualifiedTables); - } - @Override public void decorateRouteContext(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database, final SingleRule rule, final Collection tableNames, final ConfigurationProperties props) { - SQLStatementContext sqlStatementContext = queryContext.getSqlStatementContext(); - Collection singleTables = getSingleTables(database, rule, routeContext, sqlStatementContext); - SingleRouteEngineFactory.newInstance(singleTables, sqlStatementContext.getSqlStatement(), queryContext.getHintValueContext()).ifPresent(optional -> optional.route(routeContext, rule)); + Collection singleTables = getSingleTables(database, rule, routeContext, queryContext.getSqlStatementContext()); + SingleRouteEngineFactory.newInstance(singleTables, queryContext.getSqlStatementContext().getSqlStatement(), queryContext.getHintValueContext()) + .ifPresent(optional -> optional.route(routeContext, rule)); } private RouteContext createSingleDataSourceRouteContext(final SingleRule rule, final ShardingSphereDatabase database, final QueryContext queryContext) { @@ -93,6 +86,11 @@ private Collection createTableMappers(final Collection tabl return result; } + private Collection getSingleTables(final ShardingSphereDatabase database, final SingleRule rule, final RouteContext routeContext, final SQLStatementContext sqlStatementContext) { + Collection qualifiedTables = rule.getQualifiedTables(sqlStatementContext, database); + return routeContext.getRouteUnits().isEmpty() && sqlStatementContext.getSqlStatement() instanceof CreateTableStatement ? qualifiedTables : rule.getSingleTables(qualifiedTables); + } + @Override public int getOrder() { return SingleOrder.ORDER; diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleDatabaseBroadcastRouteEngine.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleDatabaseBroadcastRouteEngine.java deleted file mode 100644 index 6df1e847cea14..0000000000000 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleDatabaseBroadcastRouteEngine.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.single.route.engine; - -import org.apache.shardingsphere.infra.route.context.RouteContext; -import org.apache.shardingsphere.infra.route.context.RouteMapper; -import org.apache.shardingsphere.infra.route.context.RouteUnit; -import org.apache.shardingsphere.single.rule.SingleRule; - -import java.util.Collections; - -/** - * Single databases broadcast route engine. - */ -public final class SingleDatabaseBroadcastRouteEngine implements SingleRouteEngine { - - @Override - public void route(final RouteContext routeContext, final SingleRule singleRule) { - for (String each : singleRule.getDataSourceNames()) { - routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList())); - } - } -} diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactory.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactory.java index b230ac6010c09..cb8dd5bc3b52f 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactory.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactory.java @@ -21,10 +21,8 @@ import lombok.NoArgsConstructor; import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable; +import org.apache.shardingsphere.single.route.engine.standard.SingleStandardRouteEngine; import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterSchemaStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateSchemaStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropSchemaStatement; import java.util.Collection; import java.util.Optional; @@ -44,17 +42,6 @@ public final class SingleRouteEngineFactory { * @return created instance */ public static Optional newInstance(final Collection singleTables, final SQLStatement sqlStatement, final HintValueContext hintValueContext) { - if (!singleTables.isEmpty()) { - return Optional.of(new SingleStandardRouteEngine(singleTables, sqlStatement, hintValueContext)); - } - // TODO move this logic to common route logic - if (isSchemaDDLStatement(sqlStatement)) { - return Optional.of(new SingleDatabaseBroadcastRouteEngine()); - } - return Optional.empty(); - } - - private static boolean isSchemaDDLStatement(final SQLStatement sqlStatement) { - return sqlStatement instanceof CreateSchemaStatement || sqlStatement instanceof AlterSchemaStatement || sqlStatement instanceof DropSchemaStatement; + return singleTables.isEmpty() ? Optional.empty() : Optional.of(new SingleStandardRouteEngine(singleTables, sqlStatement, hintValueContext)); } } diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/standard/SingleStandardRouteEngine.java similarity index 98% rename from kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java rename to kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/standard/SingleStandardRouteEngine.java index e8026039b8173..02ea6cfef44b9 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/standard/SingleStandardRouteEngine.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.single.route.engine; +package org.apache.shardingsphere.single.route.engine.standard; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.datanode.DataNode; @@ -29,6 +29,7 @@ import org.apache.shardingsphere.infra.route.context.RouteUnit; import org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute; import org.apache.shardingsphere.single.exception.SingleTableNotFoundException; +import org.apache.shardingsphere.single.route.engine.SingleRouteEngine; import org.apache.shardingsphere.single.rule.SingleRule; import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateTableStatement; diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/validator/SingleMetaDataValidator.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/validator/SingleMetaDataValidator.java deleted file mode 100644 index aa3a3fed348c2..0000000000000 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/validator/SingleMetaDataValidator.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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.single.route.validator; - -import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.single.rule.SingleRule; - -/** - * Single meta data validator. - */ -public interface SingleMetaDataValidator { - - /** - * Validate single table meta data. - * - * @param rule single rule - * @param sqlStatementContext SQL statement context - * @param database database - */ - void validate(SingleRule rule, SQLStatementContext sqlStatementContext, ShardingSphereDatabase database); -} diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/validator/SingleMetaDataValidatorFactory.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/validator/SingleMetaDataValidatorFactory.java deleted file mode 100644 index 9d8c0d336b47c..0000000000000 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/validator/SingleMetaDataValidatorFactory.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.single.route.validator; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.apache.shardingsphere.single.route.validator.ddl.SingleDropSchemaMetaDataValidator; -import org.apache.shardingsphere.single.route.validator.ddl.SingleDropTableValidator; -import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropSchemaStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement; - -import java.util.Optional; - -/** - * Single meta data validator factory. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class SingleMetaDataValidatorFactory { - - /** - * New instance of single meta data validator. - * - * @param sqlStatement SQL statement - * @return created instance - */ - public static Optional newInstance(final SQLStatement sqlStatement) { - if (sqlStatement instanceof DropSchemaStatement) { - return Optional.of(new SingleDropSchemaMetaDataValidator()); - } - if (sqlStatement instanceof DropTableStatement) { - return Optional.of(new SingleDropTableValidator()); - } - return Optional.empty(); - } -} diff --git a/kernel/single/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.checker.SupportedSQLCheckersBuilder b/kernel/single/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.checker.SupportedSQLCheckersBuilder new file mode 100644 index 0000000000000..ab0224ef84c10 --- /dev/null +++ b/kernel/single/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.checker.SupportedSQLCheckersBuilder @@ -0,0 +1,18 @@ +# +# 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. +# + +org.apache.shardingsphere.single.checker.sql.SingleSupportedSQLCheckersBuilder diff --git a/kernel/single/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfigurationEmptyChecker b/kernel/single/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfigurationEmptyChecker index 4a0c35b255ec8..d22cfbeb1cdc4 100644 --- a/kernel/single/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfigurationEmptyChecker +++ b/kernel/single/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfigurationEmptyChecker @@ -15,4 +15,4 @@ # limitations under the License. # -org.apache.shardingsphere.single.checker.SingleRuleConfigurationEmptyChecker +org.apache.shardingsphere.single.checker.config.SingleRuleConfigurationEmptyChecker diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/SingleRuleConfigurationEmptyCheckerTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/config/SingleRuleConfigurationEmptyCheckerTest.java similarity index 97% rename from kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/SingleRuleConfigurationEmptyCheckerTest.java rename to kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/config/SingleRuleConfigurationEmptyCheckerTest.java index a1c087b23a344..b37f4dfc6f0cf 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/SingleRuleConfigurationEmptyCheckerTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/config/SingleRuleConfigurationEmptyCheckerTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.single.checker; +package org.apache.shardingsphere.single.checker.config; import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfigurationEmptyChecker; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/validator/ddl/SingleDropSchemaMetaDataValidatorTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/sql/SingleDropSchemaSupportedCheckerTest.java similarity index 76% rename from kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/validator/ddl/SingleDropSchemaMetaDataValidatorTest.java rename to kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/sql/SingleDropSchemaSupportedCheckerTest.java index 94302a0efdc3b..950215c2da9a2 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/validator/ddl/SingleDropSchemaMetaDataValidatorTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/sql/SingleDropSchemaSupportedCheckerTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.single.route.validator.ddl; +package org.apache.shardingsphere.single.checker.sql; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.UnknownSQLStatementContext; @@ -25,6 +25,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.single.checker.sql.schema.SingleDropSchemaSupportedChecker; import org.apache.shardingsphere.single.exception.DropNotEmptySchemaException; import org.apache.shardingsphere.single.rule.SingleRule; import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; @@ -38,25 +39,27 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -class SingleDropSchemaMetaDataValidatorTest { +class SingleDropSchemaSupportedCheckerTest { @Test - void assertValidateWithoutCascadeSchema() { + void assertCheckWithoutCascadeSchema() { assertThrows(DropNotEmptySchemaException.class, - () -> new SingleDropSchemaMetaDataValidator().validate(mock(SingleRule.class, RETURNS_DEEP_STUBS), createSQLStatementContext("foo_schema", false), mockDatabase())); + () -> new SingleDropSchemaSupportedChecker().check(mock(SingleRule.class, RETURNS_DEEP_STUBS), mockDatabase(), mock(ShardingSphereSchema.class), + createSQLStatementContext("foo_schema", false))); } @Test - void assertValidateWithNotExistedSchema() { + void assertCheckWithNotExistedSchema() { ShardingSphereDatabase database = mockDatabase(); when(database.getSchema("not_existed_schema")).thenReturn(null); assertThrows(SchemaNotFoundException.class, - () -> new SingleDropSchemaMetaDataValidator().validate(mock(SingleRule.class, RETURNS_DEEP_STUBS), createSQLStatementContext("not_existed_schema", true), database)); + () -> new SingleDropSchemaSupportedChecker().check(mock(SingleRule.class, RETURNS_DEEP_STUBS), database, mock(ShardingSphereSchema.class), + createSQLStatementContext("not_existed_schema", true))); } @Test - void assertValidate() { - new SingleDropSchemaMetaDataValidator().validate(mock(SingleRule.class, RETURNS_DEEP_STUBS), createSQLStatementContext("foo_schema", true), mockDatabase()); + void assertCheck() { + new SingleDropSchemaSupportedChecker().check(mock(SingleRule.class, RETURNS_DEEP_STUBS), mockDatabase(), mock(ShardingSphereSchema.class), createSQLStatementContext("foo_schema", true)); } private ShardingSphereDatabase mockDatabase() { @@ -67,9 +70,9 @@ private ShardingSphereDatabase mockDatabase() { return result; } - private SQLStatementContext createSQLStatementContext(final String schemaName, final boolean isCascade) { + private SQLStatementContext createSQLStatementContext(final String schemaName, final boolean containsCascade) { PostgreSQLDropSchemaStatement dropSchemaStatement = mock(PostgreSQLDropSchemaStatement.class, RETURNS_DEEP_STUBS); - when(dropSchemaStatement.isContainsCascade()).thenReturn(isCascade); + when(dropSchemaStatement.isContainsCascade()).thenReturn(containsCascade); when(dropSchemaStatement.getSchemaNames()).thenReturn(Collections.singleton(new IdentifierValue(schemaName))); return new UnknownSQLStatementContext(dropSchemaStatement); } diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/sql/SingleDropTableSupportedCheckerTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/sql/SingleDropTableSupportedCheckerTest.java new file mode 100644 index 0000000000000..07814448f7b20 --- /dev/null +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/checker/sql/SingleDropTableSupportedCheckerTest.java @@ -0,0 +1,67 @@ +/* + * 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.single.checker.sql; + +import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; +import org.apache.shardingsphere.infra.binder.context.statement.UnknownSQLStatementContext; +import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.database.core.metadata.database.enums.TableType; +import org.apache.shardingsphere.infra.exception.kernel.syntax.UnsupportedDropCascadeTableException; +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.single.checker.sql.table.SingleDropTableSupportedChecker; +import org.apache.shardingsphere.single.rule.SingleRule; +import org.apache.shardingsphere.sql.parser.statement.postgresql.ddl.PostgreSQLDropTableStatement; +import org.junit.jupiter.api.Test; + +import java.util.Collections; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class SingleDropTableSupportedCheckerTest { + + @Test + void assertCheckWithCascade() { + assertThrows(UnsupportedDropCascadeTableException.class, + () -> new SingleDropTableSupportedChecker().check(mock(SingleRule.class, RETURNS_DEEP_STUBS), mockDatabase(), mock(ShardingSphereSchema.class), + createSQLStatementContext(true))); + } + + @Test + void assertCheckWithoutCascade() { + new SingleDropTableSupportedChecker().check(mock(SingleRule.class, RETURNS_DEEP_STUBS), mockDatabase(), mock(ShardingSphereSchema.class), createSQLStatementContext(false)); + } + + private ShardingSphereDatabase mockDatabase() { + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME); + schema.putTable(new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), TableType.TABLE)); + when(result.getSchemas()).thenReturn(Collections.singletonMap("foo_schema", schema)); + return result; + } + + private SQLStatementContext createSQLStatementContext(final boolean containsCascade) { + PostgreSQLDropTableStatement dropSchemaStatement = mock(PostgreSQLDropTableStatement.class, RETURNS_DEEP_STUBS); + when(dropSchemaStatement.isContainsCascade()).thenReturn(containsCascade); + return new UnknownSQLStatementContext(dropSchemaStatement); + } +} diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleDatabaseBroadcastRouteEngineTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleDatabaseBroadcastRouteEngineTest.java deleted file mode 100644 index 4a1552cdf67ca..0000000000000 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleDatabaseBroadcastRouteEngineTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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.single.route.engine; - -import org.apache.shardingsphere.infra.database.core.DefaultDatabase; -import org.apache.shardingsphere.infra.database.h2.type.H2DatabaseType; -import org.apache.shardingsphere.infra.route.context.RouteContext; -import org.apache.shardingsphere.infra.route.context.RouteUnit; -import org.apache.shardingsphere.single.config.SingleRuleConfiguration; -import org.apache.shardingsphere.single.rule.SingleRule; -import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; -import org.junit.jupiter.api.Test; - -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -class SingleDatabaseBroadcastRouteEngineTest { - - @Test - void assertRoute() throws SQLException { - SingleRule singleRule = new SingleRule(new SingleRuleConfiguration(), DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), createDataSourceMap(), Collections.emptyList()); - RouteContext routeContext = new RouteContext(); - SingleDatabaseBroadcastRouteEngine engine = new SingleDatabaseBroadcastRouteEngine(); - engine.route(routeContext, singleRule); - List routeUnits = new ArrayList<>(routeContext.getRouteUnits()); - assertThat(routeContext.getRouteUnits().size(), is(2)); - assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(), is("ds_0")); - assertTrue(routeUnits.get(0).getTableMappers().isEmpty()); - assertThat(routeUnits.get(1).getDataSourceMapper().getActualName(), is("ds_1")); - assertTrue(routeUnits.get(1).getTableMappers().isEmpty()); - } - - private Map createDataSourceMap() throws SQLException { - Map result = new LinkedHashMap<>(2, 1F); - Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS); - when(connection.getMetaData().getURL()).thenReturn("jdbc:h2:mem:db"); - result.put("ds_0", new MockedDataSource(connection)); - result.put("ds_1", new MockedDataSource(connection)); - return result; - } -} diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactoryTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactoryTest.java index c04b8448c1afa..d81bb51ccf9a3 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactoryTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactoryTest.java @@ -20,9 +20,6 @@ import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable; import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterSchemaStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateSchemaStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropSchemaStatement; import org.junit.jupiter.api.Test; import java.util.Collections; @@ -38,21 +35,6 @@ void assertNewInstanceWithNotEmptySingleTables() { assertTrue(SingleRouteEngineFactory.newInstance(Collections.singleton(new QualifiedTable("demo_ds", "t_order")), mock(SQLStatement.class), mock(HintValueContext.class)).isPresent()); } - @Test - void assertNewInstanceWithEmptySingleTableNameAndCreateSchemaStatement() { - assertTrue(SingleRouteEngineFactory.newInstance(Collections.emptyList(), mock(CreateSchemaStatement.class), mock(HintValueContext.class)).isPresent()); - } - - @Test - void assertNewInstanceWithEmptySingleTableNameAndAlterSchemaStatement() { - assertTrue(SingleRouteEngineFactory.newInstance(Collections.emptyList(), mock(AlterSchemaStatement.class), mock(HintValueContext.class)).isPresent()); - } - - @Test - void assertNewInstanceWithEmptySingleTableNameAndDropSchemaStatement() { - assertTrue(SingleRouteEngineFactory.newInstance(Collections.emptyList(), mock(DropSchemaStatement.class), mock(HintValueContext.class)).isPresent()); - } - @Test void assertNewInstanceWithEmptySingleTableNameAndOtherStatement() { assertFalse(SingleRouteEngineFactory.newInstance(Collections.emptyList(), mock(SQLStatement.class), mock(HintValueContext.class)).isPresent()); diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngineTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/engine/SingleStandardRouteEngineTest.java similarity index 98% rename from kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngineTest.java rename to kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/engine/SingleStandardRouteEngineTest.java index c7d5669859cab..4f52e8efe31a8 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngineTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/engine/SingleStandardRouteEngineTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.single.route.engine; +package org.apache.shardingsphere.single.route.engine.engine; import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType; import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException; @@ -30,6 +30,7 @@ import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes; import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute; import org.apache.shardingsphere.single.config.SingleRuleConfiguration; +import org.apache.shardingsphere.single.route.engine.standard.SingleStandardRouteEngine; import org.apache.shardingsphere.single.rule.SingleRule; import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment; diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/validator/SingleMetaDataValidatorFactoryTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/validator/SingleMetaDataValidatorFactoryTest.java deleted file mode 100644 index 7d88ed996ffd5..0000000000000 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/validator/SingleMetaDataValidatorFactoryTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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.single.route.validator; - -import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropSchemaStatement; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.mock; - -class SingleMetaDataValidatorFactoryTest { - - @Test - void assertNewInstanceForDropSchemaStatement() { - assertTrue(SingleMetaDataValidatorFactory.newInstance(mock(DropSchemaStatement.class)).isPresent()); - } - - @Test - void assertNewInstanceForNotDropSchemaStatement() { - assertFalse(SingleMetaDataValidatorFactory.newInstance(mock(SQLStatement.class)).isPresent()); - } -}