You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2024/11/28 04:34:28 UTC
(shardingsphere) branch master updated: Move ShardingValidator preValidate logic to ShardingSupportedChecker (#33832)
This is an automated email from the ASF dual-hosted git repository.
sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 0825ea8df16 Move ShardingValidator preValidate logic to ShardingSupportedChecker (#33832)
0825ea8df16 is described below
commit 0825ea8df16826c6da5c37a6245d57baef72f6d8
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Thu Nov 28 12:34:21 2024 +0800
Move ShardingValidator preValidate logic to ShardingSupportedChecker (#33832)
* Move ShardingValidator preValidate logic to ShardingSupportedChecker
* fix unit test
* fix unit test
* fix unit test
---
.../ShardingRuleConfigurationChecker.java | 2 +-
.../ShardingRuleConfigurationEmptyChecker.java | 2 +-
.../sql/ShardingSupportedSQLCheckersBuilder.java | 81 ++++++++++
.../sql/common/ShardingSupportedCommonChecker.java | 96 ++++++++++++
.../ddl/ShardingAlterIndexSupportedChecker.java} | 38 ++---
.../ddl/ShardingAlterTableSupportedChecker.java | 52 +++++++
.../ddl/ShardingAlterViewSupportedChecker.java} | 50 +++---
.../ShardingCreateFunctionSupportedChecker.java | 60 ++++++++
.../ddl/ShardingCreateIndexSupportedChecker.java | 55 +++++++
.../ShardingCreateProcedureSupportedChecker.java | 61 ++++++++
.../ddl/ShardingCreateTableSupportedChecker.java | 27 ++--
.../ddl/ShardingCreateViewSupportedChecker.java | 52 +++++++
.../sql/ddl/ShardingDropIndexSupportedChecker.java | 53 +++++++
.../sql/ddl/ShardingDropTableSupportedChecker.java | 51 +++++++
.../ddl/ShardingRenameTableSupportedChecker.java | 54 +++++++
.../sql/dml/ShardingCopySupportedChecker.java | 22 +--
.../sql/dml/ShardingDeleteSupportedChecker.java | 20 ++-
.../sql/dml/ShardingInsertSupportedChecker.java | 80 ++++++++++
.../sql/dml/ShardingLoadDataSupportedChecker.java | 22 +--
.../sql/dml/ShardingLoadXmlSupportedChecker.java | 22 +--
.../sql/dml/ShardingUpdateSupportedChecker.java | 20 ++-
.../sql/util/ShardingSupportedCheckUtils.java | 77 ++++++++++
.../sharding/route/engine/ShardingSQLRouter.java | 1 -
.../validator/ShardingStatementValidator.java | 13 --
.../ShardingStatementValidatorFactory.java | 40 -----
.../ddl/ShardingDDLStatementValidator.java | 127 ----------------
.../impl/ShardingAlterTableStatementValidator.java | 24 +--
.../ShardingCreateFunctionStatementValidator.java | 67 ---------
.../ShardingCreateIndexStatementValidator.java | 63 --------
.../ShardingCreateProcedureStatementValidator.java | 68 ---------
.../ShardingCreateTableStatementValidator.java | 24 +--
.../impl/ShardingCreateViewStatementValidator.java | 20 +--
.../impl/ShardingDropIndexStatementValidator.java | 25 +--
.../impl/ShardingDropTableStatementValidator.java | 24 +--
.../impl/ShardingPrepareStatementValidator.java | 9 +-
.../ShardingRenameTableStatementValidator.java | 23 +--
.../dml/ShardingDMLStatementValidator.java | 15 --
.../dml/impl/ShardingCopyStatementValidator.java | 50 ------
.../dml/impl/ShardingDeleteStatementValidator.java | 6 -
.../dml/impl/ShardingInsertStatementValidator.java | 37 -----
.../impl/ShardingLoadDataStatementValidator.java | 50 ------
.../impl/ShardingLoadXMLStatementValidator.java | 50 ------
.../dml/impl/ShardingUpdateStatementValidator.java | 6 -
...here.infra.checker.SupportedSQLCheckersBuilder} | 2 +-
...ra.config.rule.checker.RuleConfigurationChecker | 2 +-
...ule.scope.DatabaseRuleConfigurationEmptyChecker | 2 +-
.../ShardingRuleConfigurationCheckerTest.java | 2 +-
.../ShardingRuleConfigurationEmptyCheckerTest.java | 2 +-
.../ShardingAlterIndexSupportedCheckerTest.java} | 35 +++--
.../ShardingAlterTableSupportedCheckerTest.java} | 33 ++--
.../ShardingAlterViewSupportedCheckerTest.java} | 25 ++-
...hardingCreateFunctionSupportedCheckerTest.java} | 54 +++----
.../ShardingCreateIndexSupportedCheckerTest.java} | 76 +++++-----
...ardingCreateProcedureSupportedCheckerTest.java} | 52 +++----
.../ShardingCreateTableSupportedCheckerTest.java | 113 ++++++++++++++
.../ShardingCreateViewSupportedCheckerTest.java} | 40 +----
.../ddl/ShardingDropIndexSupportedCheckerTest.java | 78 ++++++++++
.../ddl/ShardingDropTableSupportedCheckerTest.java | 88 +++++++++++
.../ddl/ShardingInsertSupportedCheckerTest.java | 167 +++++++++++++++++++++
.../ShardingRenameTableSupportedCheckerTest.java | 72 +++++++++
.../sql/dml/ShardingCopySupportedCheckerTest.java} | 34 ++---
.../dml/ShardingDeleteSupportedCheckerTest.java} | 32 ++--
.../dml/ShardingLoadDataSupportedCheckerTest.java} | 20 +--
.../dml/ShardingLoadXmlSupportedCheckerTest.java} | 20 +--
.../dml/ShardingUpdateSupportedCheckerTest.java | 94 ++++++++++++
.../ShardingAlterTableStatementValidatorTest.java | 14 --
.../ShardingCreateTableStatementValidatorTest.java | 73 ---------
.../ShardingCreateViewStatementValidatorTest.java | 17 ---
.../ShardingDropIndexStatementValidatorTest.java | 28 ----
.../ShardingDropTableStatementValidatorTest.java | 9 +-
.../ShardingRenameTableStatementValidatorTest.java | 30 +---
.../impl/ShardingInsertStatementValidatorTest.java | 67 +--------
.../impl/ShardingUpdateStatementValidatorTest.java | 30 ----
.../infra/connection/kernel/KernelProcessor.java | 3 +
.../schema/SingleDropSchemaSupportedChecker.java | 2 -
.../sql/table/SingleDropTableSupportedChecker.java | 12 +-
.../sql/SingleDropTableSupportedCheckerTest.java | 8 +-
77 files changed, 1713 insertions(+), 1362 deletions(-)
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/config/ShardingRuleConfigurationChecker.java
similarity index 99%
rename from features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationChecker.java
rename to features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/config/ShardingRuleConfigurationChecker.java
index c441b36a08e..6a30a74b40c 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationChecker.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/config/ShardingRuleConfigurationChecker.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.checker;
+package org.apache.shardingsphere.sharding.checker.config;
import com.google.common.base.Joiner;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationEmptyChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/config/ShardingRuleConfigurationEmptyChecker.java
similarity index 96%
rename from features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationEmptyChecker.java
rename to features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/config/ShardingRuleConfigurationEmptyChecker.java
index f6ea6530e3f..b86024d739c 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationEmptyChecker.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/config/ShardingRuleConfigurationEmptyChecker.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.checker;
+package org.apache.shardingsphere.sharding.checker.config;
import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfigurationEmptyChecker;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ShardingSupportedSQLCheckersBuilder.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ShardingSupportedSQLCheckersBuilder.java
new file mode 100644
index 00000000000..9cebd9a9fdd
--- /dev/null
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ShardingSupportedSQLCheckersBuilder.java
@@ -0,0 +1,81 @@
+/*
+ * 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.checker.sql;
+
+import org.apache.shardingsphere.infra.checker.SupportedSQLChecker;
+import org.apache.shardingsphere.infra.checker.SupportedSQLCheckersBuilder;
+import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingAlterIndexSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingAlterTableSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingAlterViewSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateFunctionSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateIndexSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateProcedureSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateTableSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateViewSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingDropIndexSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingDropTableSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingRenameTableSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingCopySupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingDeleteSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingInsertSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingLoadDataSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingLoadXmlSupportedChecker;
+import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingUpdateSupportedChecker;
+import org.apache.shardingsphere.sharding.constant.ShardingOrder;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+/**
+ * Sharding SQL supported checker factory.
+ */
+public final class ShardingSupportedSQLCheckersBuilder implements SupportedSQLCheckersBuilder<ShardingRule> {
+
+ @Override
+ public Collection<SupportedSQLChecker<?, ShardingRule>> getSupportedSQLCheckers() {
+ return Arrays.asList(
+ new ShardingAlterIndexSupportedChecker(),
+ new ShardingAlterTableSupportedChecker(),
+ new ShardingAlterViewSupportedChecker(),
+ new ShardingCreateFunctionSupportedChecker(),
+ new ShardingCreateIndexSupportedChecker(),
+ new ShardingCreateProcedureSupportedChecker(),
+ new ShardingCreateTableSupportedChecker(),
+ new ShardingCreateViewSupportedChecker(),
+ new ShardingDropIndexSupportedChecker(),
+ new ShardingDropTableSupportedChecker(),
+ new ShardingRenameTableSupportedChecker(),
+ new ShardingCopySupportedChecker(),
+ new ShardingDeleteSupportedChecker(),
+ new ShardingInsertSupportedChecker(),
+ new ShardingLoadDataSupportedChecker(),
+ new ShardingLoadXmlSupportedChecker(),
+ new ShardingUpdateSupportedChecker());
+ }
+
+ @Override
+ public int getOrder() {
+ return ShardingOrder.ORDER;
+ }
+
+ @Override
+ public Class<ShardingRule> getTypeClass() {
+ return ShardingRule.class;
+ }
+}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/common/ShardingSupportedCommonChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/common/ShardingSupportedCommonChecker.java
new file mode 100644
index 00000000000..d3e527f0472
--- /dev/null
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/common/ShardingSupportedCommonChecker.java
@@ -0,0 +1,96 @@
+/*
+ * 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.checker.sql.common;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
+import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException;
+import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.apache.shardingsphere.sharding.exception.syntax.DMLWithMultipleShardingTablesException;
+import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+
+import java.util.Collection;
+
+/**
+ * Sharding supported common checker.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ShardingSupportedCommonChecker {
+
+ /**
+ * Check sharding table.
+ *
+ * @param shardingRule sharding rule
+ * @param operation operation
+ * @param tables tables
+ * @throws UnsupportedShardingOperationException unsupported sharding operation exception
+ */
+ public static void checkShardingTable(final ShardingRule shardingRule, final String operation, final Collection<SimpleTableSegment> tables) {
+ for (SimpleTableSegment each : tables) {
+ String tableName = each.getTableName().getIdentifier().getValue();
+ ShardingSpherePreconditions.checkState(!shardingRule.isShardingTable(tableName), () -> new UnsupportedShardingOperationException(operation, tableName));
+ }
+ }
+
+ /**
+ * Check table exist.
+ *
+ * @param schema ShardingSphere schema
+ * @param tables tables
+ * @throws NoSuchTableException no such table exception
+ */
+ public static void checkTableExist(final ShardingSphereSchema schema, final Collection<SimpleTableSegment> tables) {
+ for (SimpleTableSegment each : tables) {
+ String tableName = each.getTableName().getIdentifier().getValue();
+ ShardingSpherePreconditions.checkState(schema.containsTable(tableName), () -> new NoSuchTableException(tableName));
+ }
+ }
+
+ /**
+ * Check table not exist.
+ *
+ * @param schema ShardingSphere schema
+ * @param tables tables
+ * @throws TableExistsException table exists exception
+ */
+ public static void checkTableNotExist(final ShardingSphereSchema schema, final Collection<SimpleTableSegment> tables) {
+ for (SimpleTableSegment each : tables) {
+ String tableName = each.getTableName().getIdentifier().getValue();
+ ShardingSpherePreconditions.checkState(!schema.containsTable(tableName), () -> new TableExistsException(tableName));
+ }
+ }
+
+ /**
+ * Check multiple table.
+ *
+ * @param shardingRule sharding rule
+ * @param sqlStatementContext sqlStatementContext
+ */
+ public static void checkMultipleTable(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext) {
+ Collection<String> tableNames = ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames();
+ boolean isAllShardingTables = shardingRule.isAllShardingTables(tableNames) && (1 == tableNames.size() || shardingRule.isAllBindingTables(tableNames));
+ boolean isAllSingleTables = !shardingRule.containsShardingTable(tableNames);
+ ShardingSpherePreconditions.checkState(isAllShardingTables || isAllSingleTables, () -> new DMLWithMultipleShardingTablesException(tableNames));
+ }
+}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterIndexStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterIndexSupportedChecker.java
similarity index 52%
rename from features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterIndexStatementValidator.java
rename to features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterIndexSupportedChecker.java
index 33cb119fd40..310c419dc51 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterIndexStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterIndexSupportedChecker.java
@@ -15,49 +15,43 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
+package org.apache.shardingsphere.sharding.checker.sql.ddl;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterIndexStatementContext;
+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;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
+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.sharding.route.engine.validator.ddl.ShardingDDLStatementValidator;
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;
-import java.util.List;
import java.util.Optional;
/**
- * Sharding alter index statement validator.
+ * Alter index supported checker for sharding.
*/
-public final class ShardingAlterIndexStatementValidator extends ShardingDDLStatementValidator {
+public final class ShardingAlterIndexSupportedChecker implements SupportedSQLChecker<AlterIndexStatementContext, ShardingRule> {
@Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- AlterIndexStatement alterIndexStatement = (AlterIndexStatement) sqlStatementContext.getSqlStatement();
+ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
+ return sqlStatementContext instanceof AlterIndexStatementContext;
+ }
+
+ @Override
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final AlterIndexStatementContext sqlStatementContext) {
+ AlterIndexStatement alterIndexStatement = sqlStatementContext.getSqlStatement();
Optional<IndexSegment> index = alterIndexStatement.getIndex();
- String defaultSchemaName = new DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(database.getName());
- ShardingSphereSchema schema = index.flatMap(optional -> optional.getOwner()
- .map(owner -> database.getSchema(owner.getIdentifier().getValue()))).orElseGet(() -> database.getSchema(defaultSchemaName));
- if (index.isPresent() && !isSchemaContainsIndex(schema, index.get())) {
+ 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());
}
Optional<IndexSegment> renameIndex = alterIndexStatement.getRenameIndex();
- if (renameIndex.isPresent() && isSchemaContainsIndex(schema, renameIndex.get())) {
+ if (renameIndex.isPresent() && ShardingSupportedCheckUtils.isSchemaContainsIndex(schema, renameIndex.get())) {
throw new DuplicateIndexException(renameIndex.get().getIndexName().getIdentifier().getValue());
}
}
-
- @Override
- public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
- final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
- }
}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterTableSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterTableSupportedChecker.java
new file mode 100644
index 00000000000..1ee3f2c405e
--- /dev/null
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterTableSupportedChecker.java
@@ -0,0 +1,52 @@
+/*
+ * 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.checker.sql.ddl;
+
+import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterTableStatementContext;
+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.sharding.exception.syntax.UnsupportedShardingOperationException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+
+import java.util.Collection;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+/**
+ * Alter table supported checker for sharding.
+ */
+public final class ShardingAlterTableSupportedChecker implements SupportedSQLChecker<AlterTableStatementContext, ShardingRule> {
+
+ @Override
+ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
+ return sqlStatementContext instanceof AlterTableStatementContext;
+ }
+
+ @Override
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final AlterTableStatementContext sqlStatementContext) {
+ Collection<String> tableNames = sqlStatementContext.getTablesContext().getSimpleTables().stream()
+ .map(each -> each.getTableName().getIdentifier().getValue()).collect(Collectors.toList());
+ Optional<SimpleTableSegment> renameTable = sqlStatementContext.getSqlStatement().getRenameTable();
+ ShardingSpherePreconditions.checkState(!renameTable.isPresent() || !rule.containsShardingTable(tableNames),
+ () -> new UnsupportedShardingOperationException("ALTER TABLE ... RENAME TO ...", renameTable.map(optional -> optional.getTableName().getIdentifier().getValue()).orElse("")));
+ }
+}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterViewSupportedChecker.java
similarity index 51%
rename from features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java
rename to features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterViewSupportedChecker.java
index c9e6f97deed..285b369a8f3 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterViewSupportedChecker.java
@@ -15,61 +15,53 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
+package org.apache.shardingsphere.sharding.checker.sql.ddl;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterViewStatementContext;
+import org.apache.shardingsphere.infra.checker.SupportedSQLChecker;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
+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.EngagedViewException;
import org.apache.shardingsphere.sharding.exception.syntax.RenamedViewWithoutSameConfigurationException;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.ShardingDDLStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterViewStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
-import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
import java.util.Arrays;
-import java.util.List;
import java.util.Optional;
/**
- * Sharding alter view statement validator.
+ * Alter view supported checker for sharding.
*/
-public final class ShardingAlterViewStatementValidator extends ShardingDDLStatementValidator {
+public final class ShardingAlterViewSupportedChecker implements SupportedSQLChecker<AlterViewStatementContext, ShardingRule> {
+
+ @Override
+ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
+ return sqlStatementContext instanceof AlterViewStatementContext;
+ }
@Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- AlterViewStatement alterViewStatement = (AlterViewStatement) sqlStatementContext.getSqlStatement();
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final AlterViewStatementContext sqlStatementContext) {
+ AlterViewStatement alterViewStatement = sqlStatementContext.getSqlStatement();
Optional<SelectStatement> selectStatement = alterViewStatement.getSelectStatement();
String originView = alterViewStatement.getView().getTableName().getIdentifier().getValue();
- selectStatement.ifPresent(optional -> validateAlterViewShardingTables(shardingRule, optional, originView));
- Optional<SimpleTableSegment> renamedView = alterViewStatement.getRenameView();
- if (renamedView.isPresent()) {
- String targetView = renamedView.get().getTableName().getIdentifier().getValue();
- validateBroadcastShardingView(shardingRule, originView, targetView);
- }
+ selectStatement.ifPresent(optional -> checkAlterViewShardingTables(rule, optional, originView));
+ alterViewStatement.getRenameView().ifPresent(optional -> checkBroadcastShardingView(rule, originView, optional.getTableName().getIdentifier().getValue()));
}
- private void validateAlterViewShardingTables(final ShardingRule shardingRule, final SelectStatement selectStatement, final String viewName) {
+ private void checkAlterViewShardingTables(final ShardingRule shardingRule, final SelectStatement selectStatement, final String viewName) {
TableExtractor extractor = new TableExtractor();
extractor.extractTablesFromSelect(selectStatement);
- if (isShardingTablesNotBindingWithView(extractor.getRewriteTables(), shardingRule, viewName)) {
- throw new EngagedViewException("sharding");
- }
+ ShardingSpherePreconditions.checkState(!ShardingSupportedCheckUtils.isShardingTablesNotBindingWithView(extractor.getRewriteTables(), shardingRule, viewName),
+ () -> new EngagedViewException("sharding"));
}
- private void validateBroadcastShardingView(final ShardingRule shardingRule, final String originView, final String targetView) {
+ private void checkBroadcastShardingView(final ShardingRule shardingRule, final String originView, final String targetView) {
ShardingSpherePreconditions.checkState(!shardingRule.isShardingTable(originView) && !shardingRule.isShardingTable(targetView)
|| shardingRule.isAllBindingTables(Arrays.asList(originView, targetView)), () -> new RenamedViewWithoutSameConfigurationException(originView, targetView));
}
-
- @Override
- public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
- final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
- }
}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedChecker.java
new file mode 100644
index 00000000000..ee7ae07c1c5
--- /dev/null
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedChecker.java
@@ -0,0 +1,60 @@
+/*
+ * 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.checker.sql.ddl;
+
+import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateFunctionStatementContext;
+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;
+import org.apache.shardingsphere.sharding.checker.sql.common.ShardingSupportedCommonChecker;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.RoutineBodySegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateFunctionStatement;
+
+import java.util.Collection;
+import java.util.Optional;
+
+/**
+ * Create function supported checker for sharding.
+ */
+public final class ShardingCreateFunctionSupportedChecker implements SupportedSQLChecker<CreateFunctionStatementContext, ShardingRule> {
+
+ @Override
+ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
+ return sqlStatementContext instanceof CreateFunctionStatementContext;
+ }
+
+ @Override
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final CreateFunctionStatementContext sqlStatementContext) {
+ CreateFunctionStatement createFunctionStatement = sqlStatementContext.getSqlStatement();
+ Optional<RoutineBodySegment> routineBodySegment = createFunctionStatement.getRoutineBody();
+ if (!routineBodySegment.isPresent()) {
+ return;
+ }
+ TableExtractor extractor = new TableExtractor();
+ Collection<SimpleTableSegment> existTables = extractor.extractExistTableFromRoutineBody(routineBodySegment.get());
+ ShardingSupportedCommonChecker.checkShardingTable(rule, "CREATE FUNCTION", existTables);
+ ShardingSphereSchema schema = createFunctionStatement.getFunctionName().flatMap(optional -> optional.getOwner()
+ .map(owner -> database.getSchema(owner.getIdentifier().getValue()))).orElse(currentSchema);
+ ShardingSupportedCommonChecker.checkTableExist(schema, existTables);
+ ShardingSupportedCommonChecker.checkTableNotExist(schema, extractor.extractNotExistTableFromRoutineBody(routineBodySegment.get()));
+ }
+}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateIndexSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateIndexSupportedChecker.java
new file mode 100644
index 00000000000..a8c3c50685a
--- /dev/null
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateIndexSupportedChecker.java
@@ -0,0 +1,55 @@
+/*
+ * 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.checker.sql.ddl;
+
+import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateIndexStatementContext;
+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.sharding.checker.sql.common.ShardingSupportedCommonChecker;
+import org.apache.shardingsphere.sharding.exception.metadata.DuplicateIndexException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateIndexStatement;
+
+import java.util.Collections;
+
+/**
+ * Create index supported checker for sharding.
+ */
+public final class ShardingCreateIndexSupportedChecker implements SupportedSQLChecker<CreateIndexStatementContext, ShardingRule> {
+
+ @Override
+ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
+ return sqlStatementContext instanceof CreateIndexStatementContext;
+ }
+
+ @Override
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final CreateIndexStatementContext sqlStatementContext) {
+ CreateIndexStatement createIndexStatement = sqlStatementContext.getSqlStatement();
+ if (createIndexStatement.isIfNotExists()) {
+ return;
+ }
+ ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName().map(database::getSchema).orElse(currentSchema);
+ ShardingSupportedCommonChecker.checkTableExist(schema, Collections.singleton(createIndexStatement.getTable()));
+ String tableName = createIndexStatement.getTable().getTableName().getIdentifier().getValue();
+ String indexName = sqlStatementContext.getIndexes().stream().map(each -> each.getIndexName().getIdentifier().getValue()).findFirst().orElse(null);
+ ShardingSpherePreconditions.checkState(!schema.containsIndex(tableName, indexName), () -> new DuplicateIndexException(indexName));
+ }
+}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateProcedureSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateProcedureSupportedChecker.java
new file mode 100644
index 00000000000..aef3e1ab0bd
--- /dev/null
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateProcedureSupportedChecker.java
@@ -0,0 +1,61 @@
+/*
+ * 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.checker.sql.ddl;
+
+import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateProcedureStatementContext;
+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;
+import org.apache.shardingsphere.sharding.checker.sql.common.ShardingSupportedCommonChecker;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.RoutineBodySegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateProcedureStatement;
+
+import java.util.Collection;
+import java.util.Optional;
+
+/**
+ * Create procedure supported checker for sharding.
+ */
+public final class ShardingCreateProcedureSupportedChecker implements SupportedSQLChecker<CreateProcedureStatementContext, ShardingRule> {
+
+ @Override
+ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
+ return sqlStatementContext instanceof CreateProcedureStatementContext;
+ }
+
+ @Override
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final CreateProcedureStatementContext sqlStatementContext) {
+ CreateProcedureStatement createProcedureStatement = sqlStatementContext.getSqlStatement();
+ Optional<RoutineBodySegment> routineBodySegment = createProcedureStatement.getRoutineBody();
+ if (!routineBodySegment.isPresent()) {
+ return;
+ }
+ TableExtractor extractor = new TableExtractor();
+ Collection<SimpleTableSegment> existTables = extractor.extractExistTableFromRoutineBody(routineBodySegment.get());
+ ShardingSupportedCommonChecker.checkShardingTable(rule, "CREATE PROCEDURE", existTables);
+ ShardingSphereSchema schema = createProcedureStatement.getProcedureName().flatMap(optional -> optional.getOwner()
+ .map(owner -> database.getSchema(owner.getIdentifier().getValue()))).orElse(currentSchema);
+ ShardingSupportedCommonChecker.checkTableExist(schema, existTables);
+ Collection<SimpleTableSegment> notExistTables = extractor.extractNotExistTableFromRoutineBody(routineBodySegment.get());
+ ShardingSupportedCommonChecker.checkTableNotExist(schema, notExistTables);
+ }
+}
diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateTableSupportedChecker.java
similarity index 52%
copy from kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
copy to features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateTableSupportedChecker.java
index 1abf65ba2eb..5dab93897fc 100644
--- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateTableSupportedChecker.java
@@ -15,32 +15,35 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.single.checker.sql.table;
+package org.apache.shardingsphere.sharding.checker.sql.ddl;
-import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateTableStatementContext;
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.infra.metadata.database.schema.model.ShardingSphereSchema;
-import org.apache.shardingsphere.single.rule.SingleRule;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement;
+import org.apache.shardingsphere.sharding.checker.sql.common.ShardingSupportedCommonChecker;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateTableStatement;
+
+import java.util.Collections;
/**
- * Drop table supported checker for single.
+ * Create table supported checker for sharding.
*/
-@HighFrequencyInvocation
-public final class SingleDropTableSupportedChecker implements SupportedSQLChecker<SQLStatementContext, SingleRule> {
+public final class ShardingCreateTableSupportedChecker implements SupportedSQLChecker<CreateTableStatementContext, ShardingRule> {
@Override
public boolean isCheck(final SQLStatementContext sqlStatementContext) {
- return sqlStatementContext.getSqlStatement() instanceof DropTableStatement;
+ return sqlStatementContext instanceof CreateTableStatementContext;
}
@Override
- public void check(final SingleRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final SQLStatementContext sqlStatementContext) {
- if (((DropTableStatement) sqlStatementContext.getSqlStatement()).isContainsCascade()) {
- throw new UnsupportedDropCascadeTableException();
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final CreateTableStatementContext sqlStatementContext) {
+ CreateTableStatement createTableStatement = sqlStatementContext.getSqlStatement();
+ if (!createTableStatement.isIfNotExists()) {
+ ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName().map(database::getSchema).orElse(currentSchema);
+ ShardingSupportedCommonChecker.checkTableNotExist(schema, Collections.singleton(createTableStatement.getTable()));
}
}
}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateViewSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateViewSupportedChecker.java
new file mode 100644
index 00000000000..a526563f558
--- /dev/null
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateViewSupportedChecker.java
@@ -0,0 +1,52 @@
+/*
+ * 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.checker.sql.ddl;
+
+import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateViewStatementContext;
+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.sharding.checker.sql.util.ShardingSupportedCheckUtils;
+import org.apache.shardingsphere.sharding.exception.metadata.EngagedViewException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+
+import java.util.Collection;
+
+/**
+ * Create view supported checker for sharding.
+ */
+public final class ShardingCreateViewSupportedChecker implements SupportedSQLChecker<CreateViewStatementContext, ShardingRule> {
+
+ @Override
+ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
+ return sqlStatementContext instanceof CreateViewStatementContext;
+ }
+
+ @Override
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final CreateViewStatementContext sqlStatementContext) {
+ TableExtractor extractor = new TableExtractor();
+ extractor.extractTablesFromSelect(sqlStatementContext.getSqlStatement().getSelect());
+ Collection<SimpleTableSegment> tableSegments = extractor.getRewriteTables();
+ String viewName = sqlStatementContext.getSqlStatement().getView().getTableName().getIdentifier().getValue();
+ ShardingSpherePreconditions.checkState(!ShardingSupportedCheckUtils.isShardingTablesNotBindingWithView(tableSegments, rule, viewName), () -> new EngagedViewException("sharding"));
+ }
+}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropIndexSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropIndexSupportedChecker.java
new file mode 100644
index 00000000000..15e19b64d66
--- /dev/null
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropIndexSupportedChecker.java
@@ -0,0 +1,53 @@
+/*
+ * 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.checker.sql.ddl;
+
+import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.DropIndexStatementContext;
+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.sharding.checker.sql.util.ShardingSupportedCheckUtils;
+import org.apache.shardingsphere.sharding.exception.metadata.IndexNotExistedException;
+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;
+
+/**
+ * Drop index supported checker for sharding.
+ */
+public final class ShardingDropIndexSupportedChecker implements SupportedSQLChecker<DropIndexStatementContext, ShardingRule> {
+
+ @Override
+ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
+ return sqlStatementContext instanceof DropIndexStatementContext;
+ }
+
+ @Override
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final DropIndexStatementContext sqlStatementContext) {
+ DropIndexStatement dropIndexStatement = sqlStatementContext.getSqlStatement();
+ if (dropIndexStatement.isIfExists()) {
+ return;
+ }
+ 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()));
+ }
+ }
+}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropTableSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropTableSupportedChecker.java
new file mode 100644
index 00000000000..63adbc42c74
--- /dev/null
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropTableSupportedChecker.java
@@ -0,0 +1,51 @@
+/*
+ * 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.checker.sql.ddl;
+
+import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.DropTableStatementContext;
+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.sharding.checker.sql.common.ShardingSupportedCommonChecker;
+import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement;
+
+/**
+ * Drop table supported checker for sharding.
+ */
+public final class ShardingDropTableSupportedChecker implements SupportedSQLChecker<DropTableStatementContext, ShardingRule> {
+
+ @Override
+ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
+ return sqlStatementContext instanceof DropTableStatementContext;
+ }
+
+ @Override
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final DropTableStatementContext sqlStatementContext) {
+ DropTableStatement dropTableStatement = sqlStatementContext.getSqlStatement();
+ if (!dropTableStatement.isIfExists()) {
+ ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName().map(database::getSchema).orElse(currentSchema);
+ ShardingSupportedCommonChecker.checkTableExist(schema, sqlStatementContext.getTablesContext().getSimpleTables());
+ }
+ ShardingSpherePreconditions.checkState(!dropTableStatement.isContainsCascade(), () -> new UnsupportedShardingOperationException("DROP TABLE ... CASCADE",
+ sqlStatementContext.getTablesContext().getSimpleTables().iterator().next().getTableName().getIdentifier().getValue()));
+ }
+}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingRenameTableSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingRenameTableSupportedChecker.java
new file mode 100644
index 00000000000..f315ce09f17
--- /dev/null
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingRenameTableSupportedChecker.java
@@ -0,0 +1,54 @@
+/*
+ * 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.checker.sql.ddl;
+
+import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.RenameTableStatementContext;
+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.sharding.exception.syntax.UnsupportedShardingOperationException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.table.RenameTableDefinitionSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Rename table supported checker for sharding.
+ */
+public final class ShardingRenameTableSupportedChecker implements SupportedSQLChecker<RenameTableStatementContext, ShardingRule> {
+
+ @Override
+ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
+ return sqlStatementContext instanceof RenameTableStatementContext;
+ }
+
+ @Override
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final RenameTableStatementContext sqlStatementContext) {
+ Collection<String> tableNames = sqlStatementContext.getTablesContext().getSimpleTables().stream()
+ .map(each -> each.getTableName().getIdentifier().getValue()).collect(Collectors.toList());
+ List<SimpleTableSegment> renameTables = sqlStatementContext.getSqlStatement()
+ .getRenameTables().stream().map(RenameTableDefinitionSegment::getRenameTable).collect(Collectors.toList());
+ ShardingSpherePreconditions.checkState(renameTables.isEmpty() || !rule.containsShardingTable(tableNames),
+ () -> new UnsupportedShardingOperationException("RENAME TABLE", renameTables.get(0).getTableName().getIdentifier().getValue()));
+ }
+}
diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingCopySupportedChecker.java
similarity index 56%
copy from kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
copy to features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingCopySupportedChecker.java
index 1abf65ba2eb..e619365e0fd 100644
--- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingCopySupportedChecker.java
@@ -15,32 +15,32 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.single.checker.sql.table;
+package org.apache.shardingsphere.sharding.checker.sql.dml;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.dml.CopyStatementContext;
import org.apache.shardingsphere.infra.checker.SupportedSQLChecker;
-import org.apache.shardingsphere.infra.exception.kernel.syntax.UnsupportedDropCascadeTableException;
+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.single.rule.SingleRule;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement;
+import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
/**
- * Drop table supported checker for single.
+ * Copy supported checker for sharding.
*/
@HighFrequencyInvocation
-public final class SingleDropTableSupportedChecker implements SupportedSQLChecker<SQLStatementContext, SingleRule> {
+public final class ShardingCopySupportedChecker implements SupportedSQLChecker<CopyStatementContext, ShardingRule> {
@Override
public boolean isCheck(final SQLStatementContext sqlStatementContext) {
- return sqlStatementContext.getSqlStatement() instanceof DropTableStatement;
+ return sqlStatementContext instanceof CopyStatementContext;
}
@Override
- public void check(final SingleRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final SQLStatementContext sqlStatementContext) {
- if (((DropTableStatement) sqlStatementContext.getSqlStatement()).isContainsCascade()) {
- throw new UnsupportedDropCascadeTableException();
- }
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final CopyStatementContext sqlStatementContext) {
+ String tableName = sqlStatementContext.getSqlStatement().getTableSegment().getTableName().getIdentifier().getValue();
+ ShardingSpherePreconditions.checkState(!rule.isShardingTable(tableName), () -> new UnsupportedShardingOperationException("COPY", tableName));
}
}
diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingDeleteSupportedChecker.java
similarity index 60%
copy from kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
copy to features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingDeleteSupportedChecker.java
index 1abf65ba2eb..2699e27f7bf 100644
--- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingDeleteSupportedChecker.java
@@ -15,32 +15,30 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.single.checker.sql.table;
+package org.apache.shardingsphere.sharding.checker.sql.dml;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.dml.DeleteStatementContext;
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.infra.metadata.database.schema.model.ShardingSphereSchema;
-import org.apache.shardingsphere.single.rule.SingleRule;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement;
+import org.apache.shardingsphere.sharding.checker.sql.common.ShardingSupportedCommonChecker;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
/**
- * Drop table supported checker for single.
+ * Delete supported checker for sharding.
*/
@HighFrequencyInvocation
-public final class SingleDropTableSupportedChecker implements SupportedSQLChecker<SQLStatementContext, SingleRule> {
+public final class ShardingDeleteSupportedChecker implements SupportedSQLChecker<DeleteStatementContext, ShardingRule> {
@Override
public boolean isCheck(final SQLStatementContext sqlStatementContext) {
- return sqlStatementContext.getSqlStatement() instanceof DropTableStatement;
+ return sqlStatementContext instanceof DeleteStatementContext;
}
@Override
- public void check(final SingleRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final SQLStatementContext sqlStatementContext) {
- if (((DropTableStatement) sqlStatementContext.getSqlStatement()).isContainsCascade()) {
- throw new UnsupportedDropCascadeTableException();
- }
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final DeleteStatementContext sqlStatementContext) {
+ ShardingSupportedCommonChecker.checkMultipleTable(rule, sqlStatementContext);
}
}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingInsertSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingInsertSupportedChecker.java
new file mode 100644
index 00000000000..9497abee2e3
--- /dev/null
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingInsertSupportedChecker.java
@@ -0,0 +1,80 @@
+/*
+ * 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.checker.sql.dml;
+
+import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
+import org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext;
+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;
+import org.apache.shardingsphere.sharding.checker.sql.common.ShardingSupportedCommonChecker;
+import org.apache.shardingsphere.sharding.exception.syntax.InsertSelectTableViolationException;
+import org.apache.shardingsphere.sharding.exception.syntax.MissingGenerateKeyColumnWithInsertSelectException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubquerySegment;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement;
+
+import java.util.Collection;
+import java.util.Optional;
+
+/**
+ * Insert supported checker for sharding.
+ */
+@HighFrequencyInvocation
+public final class ShardingInsertSupportedChecker implements SupportedSQLChecker<InsertStatementContext, ShardingRule> {
+
+ @Override
+ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
+ return sqlStatementContext instanceof InsertStatementContext;
+ }
+
+ @Override
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final InsertStatementContext sqlStatementContext) {
+ if (null == sqlStatementContext.getInsertSelectContext()) {
+ ShardingSupportedCommonChecker.checkMultipleTable(rule, sqlStatementContext);
+ }
+ InsertStatement insertStatement = sqlStatementContext.getSqlStatement();
+ String tableName = insertStatement.getTable().map(optional -> optional.getTableName().getIdentifier().getValue()).orElse("");
+ Optional<SubquerySegment> insertSelectSegment = insertStatement.getInsertSelect();
+ if (!insertSelectSegment.isPresent()) {
+ return;
+ }
+ if (isContainsKeyGenerateStrategy(rule, tableName) && !isContainsKeyGenerateColumn(rule, insertStatement.getColumns(), tableName)) {
+ throw new MissingGenerateKeyColumnWithInsertSelectException();
+ }
+ TablesContext tablesContext = sqlStatementContext.getTablesContext();
+ if (rule.containsShardingTable(tablesContext.getTableNames()) && !isAllSameTables(tablesContext.getTableNames()) && !rule.isAllBindingTables(tablesContext.getTableNames())) {
+ throw new InsertSelectTableViolationException();
+ }
+ }
+
+ private boolean isContainsKeyGenerateStrategy(final ShardingRule shardingRule, final String tableName) {
+ return shardingRule.findGenerateKeyColumnName(tableName).isPresent();
+ }
+
+ private boolean isContainsKeyGenerateColumn(final ShardingRule shardingRule, final Collection<ColumnSegment> columns, final String tableName) {
+ return columns.isEmpty() || columns.stream().anyMatch(each -> shardingRule.isGenerateKeyColumn(each.getIdentifier().getValue(), tableName));
+ }
+
+ private boolean isAllSameTables(final Collection<String> tableNames) {
+ return 1L == tableNames.stream().distinct().count();
+ }
+}
diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedChecker.java
similarity index 55%
copy from kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
copy to features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedChecker.java
index 1abf65ba2eb..429fba563d9 100644
--- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedChecker.java
@@ -15,32 +15,32 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.single.checker.sql.table;
+package org.apache.shardingsphere.sharding.checker.sql.dml;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.dml.LoadDataStatementContext;
import org.apache.shardingsphere.infra.checker.SupportedSQLChecker;
-import org.apache.shardingsphere.infra.exception.kernel.syntax.UnsupportedDropCascadeTableException;
+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.single.rule.SingleRule;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement;
+import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
/**
- * Drop table supported checker for single.
+ * Load data supported checker for sharding.
*/
@HighFrequencyInvocation
-public final class SingleDropTableSupportedChecker implements SupportedSQLChecker<SQLStatementContext, SingleRule> {
+public final class ShardingLoadDataSupportedChecker implements SupportedSQLChecker<LoadDataStatementContext, ShardingRule> {
@Override
public boolean isCheck(final SQLStatementContext sqlStatementContext) {
- return sqlStatementContext.getSqlStatement() instanceof DropTableStatement;
+ return sqlStatementContext instanceof LoadDataStatementContext;
}
@Override
- public void check(final SingleRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final SQLStatementContext sqlStatementContext) {
- if (((DropTableStatement) sqlStatementContext.getSqlStatement()).isContainsCascade()) {
- throw new UnsupportedDropCascadeTableException();
- }
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final LoadDataStatementContext sqlStatementContext) {
+ String tableName = sqlStatementContext.getSqlStatement().getTableSegment().getTableName().getIdentifier().getValue();
+ ShardingSpherePreconditions.checkState(!rule.isShardingTable(tableName), () -> new UnsupportedShardingOperationException("LOAD DATA", tableName));
}
}
diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadXmlSupportedChecker.java
similarity index 55%
copy from kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
copy to features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadXmlSupportedChecker.java
index 1abf65ba2eb..88dd2ad76da 100644
--- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadXmlSupportedChecker.java
@@ -15,32 +15,32 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.single.checker.sql.table;
+package org.apache.shardingsphere.sharding.checker.sql.dml;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.dml.LoadXMLStatementContext;
import org.apache.shardingsphere.infra.checker.SupportedSQLChecker;
-import org.apache.shardingsphere.infra.exception.kernel.syntax.UnsupportedDropCascadeTableException;
+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.single.rule.SingleRule;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement;
+import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
/**
- * Drop table supported checker for single.
+ * Load xml supported checker for sharding.
*/
@HighFrequencyInvocation
-public final class SingleDropTableSupportedChecker implements SupportedSQLChecker<SQLStatementContext, SingleRule> {
+public final class ShardingLoadXmlSupportedChecker implements SupportedSQLChecker<LoadXMLStatementContext, ShardingRule> {
@Override
public boolean isCheck(final SQLStatementContext sqlStatementContext) {
- return sqlStatementContext.getSqlStatement() instanceof DropTableStatement;
+ return sqlStatementContext instanceof LoadXMLStatementContext;
}
@Override
- public void check(final SingleRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final SQLStatementContext sqlStatementContext) {
- if (((DropTableStatement) sqlStatementContext.getSqlStatement()).isContainsCascade()) {
- throw new UnsupportedDropCascadeTableException();
- }
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final LoadXMLStatementContext sqlStatementContext) {
+ String tableName = sqlStatementContext.getSqlStatement().getTableSegment().getTableName().getIdentifier().getValue();
+ ShardingSpherePreconditions.checkState(!rule.isShardingTable(tableName), () -> new UnsupportedShardingOperationException("LOAD XML", tableName));
}
}
diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingUpdateSupportedChecker.java
similarity index 60%
copy from kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
copy to features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingUpdateSupportedChecker.java
index 1abf65ba2eb..57cf2492fa3 100644
--- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingUpdateSupportedChecker.java
@@ -15,32 +15,30 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.single.checker.sql.table;
+package org.apache.shardingsphere.sharding.checker.sql.dml;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.dml.UpdateStatementContext;
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.infra.metadata.database.schema.model.ShardingSphereSchema;
-import org.apache.shardingsphere.single.rule.SingleRule;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement;
+import org.apache.shardingsphere.sharding.checker.sql.common.ShardingSupportedCommonChecker;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
/**
- * Drop table supported checker for single.
+ * Update supported checker for sharding.
*/
@HighFrequencyInvocation
-public final class SingleDropTableSupportedChecker implements SupportedSQLChecker<SQLStatementContext, SingleRule> {
+public final class ShardingUpdateSupportedChecker implements SupportedSQLChecker<UpdateStatementContext, ShardingRule> {
@Override
public boolean isCheck(final SQLStatementContext sqlStatementContext) {
- return sqlStatementContext.getSqlStatement() instanceof DropTableStatement;
+ return sqlStatementContext instanceof UpdateStatementContext;
}
@Override
- public void check(final SingleRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final SQLStatementContext sqlStatementContext) {
- if (((DropTableStatement) sqlStatementContext.getSqlStatement()).isContainsCascade()) {
- throw new UnsupportedDropCascadeTableException();
- }
+ public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final UpdateStatementContext sqlStatementContext) {
+ ShardingSupportedCommonChecker.checkMultipleTable(rule, sqlStatementContext);
}
}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/util/ShardingSupportedCheckUtils.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/util/ShardingSupportedCheckUtils.java
new file mode 100644
index 00000000000..90ce84fd319
--- /dev/null
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/checker/sql/util/ShardingSupportedCheckUtils.java
@@ -0,0 +1,77 @@
+/*
+ * 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.checker.sql.util;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
+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.segment.generic.table.SimpleTableSegment;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+/**
+ * Sharding supported check utility class.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ShardingSupportedCheckUtils {
+
+ /**
+ * Judge whether schema contains index or not.
+ *
+ * @param schema ShardingSphere schema
+ * @param index index
+ * @return whether schema contains index or not
+ */
+ public static boolean isSchemaContainsIndex(final ShardingSphereSchema schema, final IndexSegment index) {
+ return schema.getAllTableNames().stream().anyMatch(each -> schema.getTable(each).containsIndex(index.getIndexName().getIdentifier().getValue()));
+ }
+
+ /**
+ * Judge whether sharding tables not binding with view.
+ *
+ * @param tableSegments table segments
+ * @param shardingRule sharding rule
+ * @param viewName view name
+ * @return sharding tables not binding with view or not
+ */
+ public static boolean isShardingTablesNotBindingWithView(final Collection<SimpleTableSegment> tableSegments, final ShardingRule shardingRule, final String viewName) {
+ for (SimpleTableSegment each : tableSegments) {
+ String logicTable = each.getTableName().getIdentifier().getValue();
+ if (shardingRule.isShardingTable(logicTable) && !shardingRule.isAllBindingTables(Arrays.asList(viewName, logicTable))) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Judge whether route unit and data node are different size.
+ *
+ * @param shardingRule sharding rule
+ * @param routeContext route context
+ * @param tableName table name
+ * @return route unit and data node are different size or not
+ */
+ public static boolean isRouteUnitDataNodeDifferentSize(final ShardingRule shardingRule, final RouteContext routeContext, final String tableName) {
+ return shardingRule.isShardingTable(tableName) && shardingRule.getShardingTable(tableName).getActualDataNodes().size() != routeContext.getRouteUnits().size();
+ }
+}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java
index 1dac768b2a8..0fd20f7c09a 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java
@@ -71,7 +71,6 @@ public final class ShardingSQLRouter implements EntranceSQLRouter<ShardingRule>,
SQLStatement sqlStatement = queryContext.getSqlStatementContext().getSqlStatement();
ShardingConditions shardingConditions = createShardingConditions(queryContext, globalRuleMetaData, database, rule);
Optional<ShardingStatementValidator> validator = ShardingStatementValidatorFactory.newInstance(sqlStatement, shardingConditions);
- validator.ifPresent(optional -> optional.preValidate(rule, queryContext.getSqlStatementContext(), queryContext.getHintValueContext(), queryContext.getParameters(), database, props));
if (sqlStatement instanceof DMLStatement && shardingConditions.isNeedMerge()) {
shardingConditions.merge();
}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java
index dac1b31d3bc..eadc83b753b 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java
@@ -31,19 +31,6 @@ import java.util.List;
*/
public interface ShardingStatementValidator {
- /**
- * Validate whether sharding operation is supported before route.
- *
- * @param shardingRule sharding rule
- * @param sqlStatementContext SQL statement context
- * @param hintValueContext hint value context
- * @param params SQL parameters
- * @param database database
- * @param props props
- */
- void preValidate(ShardingRule shardingRule, SQLStatementContext sqlStatementContext, HintValueContext hintValueContext, List<Object> params, ShardingSphereDatabase database,
- ConfigurationProperties props);
-
/**
* Validate whether sharding operation is supported after route.
*
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidatorFactory.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidatorFactory.java
index c00096a700e..327b861369d 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidatorFactory.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidatorFactory.java
@@ -20,31 +20,18 @@ package org.apache.shardingsphere.sharding.route.engine.validator;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingAlterIndexStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingAlterTableStatementValidator;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingAlterViewStatementValidator;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingCreateFunctionStatementValidator;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingCreateIndexStatementValidator;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingCreateProcedureStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingCreateTableStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingCreateViewStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingDropIndexStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingDropTableStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingPrepareStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingRenameTableStatementValidator;
-import org.apache.shardingsphere.sharding.route.engine.validator.dml.impl.ShardingCopyStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.dml.impl.ShardingDeleteStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.dml.impl.ShardingInsertStatementValidator;
-import org.apache.shardingsphere.sharding.route.engine.validator.dml.impl.ShardingLoadDataStatementValidator;
-import org.apache.shardingsphere.sharding.route.engine.validator.dml.impl.ShardingLoadXMLStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.dml.impl.ShardingUpdateStatementValidator;
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterIndexStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterTableStatement;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterViewStatement;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateFunctionStatement;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateIndexStatement;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateProcedureStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateTableStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateViewStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;
@@ -52,13 +39,10 @@ import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropInd
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.PrepareStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.RenameTableStatement;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.CopyStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DMLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DeleteStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.UpdateStatement;
-import org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLLoadDataStatement;
-import org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLLoadXMLStatement;
import java.util.Optional;
@@ -89,30 +73,15 @@ public final class ShardingStatementValidatorFactory {
if (sqlStatement instanceof CreateTableStatement) {
return Optional.of(new ShardingCreateTableStatementValidator());
}
- if (sqlStatement instanceof CreateFunctionStatement) {
- return Optional.of(new ShardingCreateFunctionStatementValidator());
- }
- if (sqlStatement instanceof CreateProcedureStatement) {
- return Optional.of(new ShardingCreateProcedureStatementValidator());
- }
if (sqlStatement instanceof CreateViewStatement) {
return Optional.of(new ShardingCreateViewStatementValidator());
}
- if (sqlStatement instanceof CreateIndexStatement) {
- return Optional.of(new ShardingCreateIndexStatementValidator());
- }
if (sqlStatement instanceof AlterTableStatement) {
return Optional.of(new ShardingAlterTableStatementValidator());
}
if (sqlStatement instanceof RenameTableStatement) {
return Optional.of(new ShardingRenameTableStatementValidator());
}
- if (sqlStatement instanceof AlterViewStatement) {
- return Optional.of(new ShardingAlterViewStatementValidator());
- }
- if (sqlStatement instanceof AlterIndexStatement) {
- return Optional.of(new ShardingAlterIndexStatementValidator());
- }
if (sqlStatement instanceof DropTableStatement) {
return Optional.of(new ShardingDropTableStatementValidator());
}
@@ -135,15 +104,6 @@ public final class ShardingStatementValidatorFactory {
if (sqlStatement instanceof DeleteStatement) {
return Optional.of(new ShardingDeleteStatementValidator());
}
- if (sqlStatement instanceof CopyStatement) {
- return Optional.of(new ShardingCopyStatementValidator());
- }
- if (sqlStatement instanceof MySQLLoadDataStatement) {
- return Optional.of(new ShardingLoadDataStatementValidator());
- }
- if (sqlStatement instanceof MySQLLoadXMLStatement) {
- return Optional.of(new ShardingLoadXMLStatementValidator());
- }
return Optional.empty();
}
}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java
deleted file mode 100644
index 38c29db6b0a..00000000000
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java
+++ /dev/null
@@ -1,127 +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.sharding.route.engine.validator.ddl;
-
-import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException;
-import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException;
-import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
-import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
-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.segment.generic.table.SimpleTableSegment;
-
-import java.util.Arrays;
-import java.util.Collection;
-
-/**
- * Sharding DDL statement validator.
- */
-public abstract class ShardingDDLStatementValidator implements ShardingStatementValidator {
-
- /**
- * Validate sharding table.
- *
- * @param shardingRule sharding rule
- * @param operation operation
- * @param tables tables
- * @throws UnsupportedShardingOperationException unsupported sharding operation exception
- */
- protected void validateShardingTable(final ShardingRule shardingRule, final String operation, final Collection<SimpleTableSegment> tables) {
- for (SimpleTableSegment each : tables) {
- String tableName = each.getTableName().getIdentifier().getValue();
- if (shardingRule.isShardingTable(tableName)) {
- throw new UnsupportedShardingOperationException(operation, tableName);
- }
- }
- }
-
- /**
- * Validate table exist.
- *
- * @param schema ShardingSphere schema
- * @param tables tables
- * @throws NoSuchTableException no such table exception
- */
- protected void validateTableExist(final ShardingSphereSchema schema, final Collection<SimpleTableSegment> tables) {
- for (SimpleTableSegment each : tables) {
- String tableName = each.getTableName().getIdentifier().getValue();
- if (!schema.containsTable(tableName)) {
- throw new NoSuchTableException(tableName);
- }
- }
- }
-
- /**
- * Validate table not exist.
- *
- * @param schema ShardingSphere schema
- * @param tables tables
- * @throws TableExistsException table exists exception
- */
- protected void validateTableNotExist(final ShardingSphereSchema schema, final Collection<SimpleTableSegment> tables) {
- for (SimpleTableSegment each : tables) {
- String tableName = each.getTableName().getIdentifier().getValue();
- if (schema.containsTable(tableName)) {
- throw new TableExistsException(tableName);
- }
- }
- }
-
- /**
- * Judge whether route unit and data node are different size.
- *
- * @param shardingRule sharding rule
- * @param routeContext route context
- * @param tableName table name
- * @return route unit and data node are different size or not
- */
- protected boolean isRouteUnitDataNodeDifferentSize(final ShardingRule shardingRule, final RouteContext routeContext, final String tableName) {
- return shardingRule.isShardingTable(tableName) && shardingRule.getShardingTable(tableName).getActualDataNodes().size() != routeContext.getRouteUnits().size();
- }
-
- /**
- * Judge whether schema contains index or not.
- *
- * @param schema ShardingSphere schema
- * @param index index
- * @return whether schema contains index or not
- */
- protected boolean isSchemaContainsIndex(final ShardingSphereSchema schema, final IndexSegment index) {
- return schema.getAllTableNames().stream().anyMatch(each -> schema.getTable(each).containsIndex(index.getIndexName().getIdentifier().getValue()));
- }
-
- /**
- * Judge whether sharding tables not binding with view.
- *
- * @param tableSegments table segments
- * @param shardingRule sharding rule
- * @param viewName view name
- * @return sharding tables not binding with view or not
- */
- protected boolean isShardingTablesNotBindingWithView(final Collection<SimpleTableSegment> tableSegments, final ShardingRule shardingRule, final String viewName) {
- for (SimpleTableSegment each : tableSegments) {
- String logicTable = each.getTableName().getIdentifier().getValue();
- if (shardingRule.isShardingTable(logicTable) && !shardingRule.isAllBindingTables(Arrays.asList(viewName, logicTable))) {
- return true;
- }
- }
- return false;
- }
-}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidator.java
index 3532a111cca..212a4c9cae0 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidator.java
@@ -24,40 +24,24 @@ import org.apache.shardingsphere.infra.exception.core.ShardingSpherePrecondition
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.sharding.checker.sql.util.ShardingSupportedCheckUtils;
import org.apache.shardingsphere.sharding.exception.connection.ShardingDDLRouteException;
-import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.ShardingDDLStatementValidator;
+import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterTableStatement;
-import java.util.Collection;
import java.util.List;
-import java.util.Optional;
-import java.util.stream.Collectors;
/**
* Sharding alter table statement validator.
*/
-public final class ShardingAlterTableStatementValidator extends ShardingDDLStatementValidator {
-
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- AlterTableStatementContext alterTableStatementContext = (AlterTableStatementContext) sqlStatementContext;
- Collection<String> tableNames = alterTableStatementContext.getTablesContext().getSimpleTables().stream()
- .map(each -> each.getTableName().getIdentifier().getValue()).collect(Collectors.toList());
- Optional<SimpleTableSegment> renameTable = ((AlterTableStatement) sqlStatementContext.getSqlStatement()).getRenameTable();
- ShardingSpherePreconditions.checkState(!renameTable.isPresent() || !shardingRule.containsShardingTable(tableNames),
- () -> new UnsupportedShardingOperationException("ALTER TABLE ... RENAME TO ...", renameTable.map(optional -> optional.getTableName().getIdentifier().getValue()).orElse("")));
- }
+public final class ShardingAlterTableStatementValidator implements ShardingStatementValidator {
@Override
public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
AlterTableStatementContext alterTableStatementContext = (AlterTableStatementContext) sqlStatementContext;
String primaryTable = alterTableStatementContext.getSqlStatement().getTable().getTableName().getIdentifier().getValue();
- ShardingSpherePreconditions.checkState(!isRouteUnitDataNodeDifferentSize(shardingRule, routeContext, primaryTable),
+ ShardingSpherePreconditions.checkState(!ShardingSupportedCheckUtils.isRouteUnitDataNodeDifferentSize(shardingRule, routeContext, primaryTable),
() -> new ShardingDDLRouteException("ALTER", "TABLE", alterTableStatementContext.getTablesContext().getTableNames()));
}
}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateFunctionStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateFunctionStatementValidator.java
deleted file mode 100644
index 66448f12f5a..00000000000
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateFunctionStatementValidator.java
+++ /dev/null
@@ -1,67 +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.sharding.route.engine.validator.ddl.impl;
-
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
-import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.ShardingDDLStatementValidator;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.RoutineBodySegment;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateFunctionStatement;
-import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
-
-import java.util.Collection;
-import java.util.List;
-import java.util.Optional;
-
-/**
- * Sharding create function statement validator.
- */
-public final class ShardingCreateFunctionStatementValidator extends ShardingDDLStatementValidator {
-
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- CreateFunctionStatement createFunctionStatement = (CreateFunctionStatement) sqlStatementContext.getSqlStatement();
- Optional<RoutineBodySegment> routineBodySegment = createFunctionStatement.getRoutineBody();
- if (!routineBodySegment.isPresent()) {
- return;
- }
- TableExtractor extractor = new TableExtractor();
- Collection<SimpleTableSegment> existTables = extractor.extractExistTableFromRoutineBody(routineBodySegment.get());
- validateShardingTable(shardingRule, "CREATE FUNCTION", existTables);
- if (!hintValueContext.isSkipMetadataValidate()) {
- String defaultSchemaName = new DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(database.getName());
- ShardingSphereSchema schema = createFunctionStatement.getFunctionName().flatMap(optional -> optional.getOwner()
- .map(owner -> database.getSchema(owner.getIdentifier().getValue()))).orElseGet(() -> database.getSchema(defaultSchemaName));
- validateTableExist(schema, existTables);
- validateTableNotExist(schema, extractor.extractNotExistTableFromRoutineBody(routineBodySegment.get()));
- }
- }
-
- @Override
- public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
- }
-}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateIndexStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateIndexStatementValidator.java
deleted file mode 100644
index f99a9fa10c4..00000000000
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateIndexStatementValidator.java
+++ /dev/null
@@ -1,63 +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.sharding.route.engine.validator.ddl.impl;
-
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateIndexStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
-import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
-import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.sharding.exception.metadata.DuplicateIndexException;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.ShardingDDLStatementValidator;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateIndexStatement;
-
-import java.util.Collections;
-import java.util.List;
-
-/**
- * Sharding create index statement validator.
- */
-public final class ShardingCreateIndexStatementValidator extends ShardingDDLStatementValidator {
-
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- CreateIndexStatementContext createIndexStatementContext = (CreateIndexStatementContext) sqlStatementContext;
- CreateIndexStatement createIndexStatement = createIndexStatementContext.getSqlStatement();
- if (createIndexStatement.isIfNotExists()) {
- return;
- }
- String defaultSchemaName = new DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(database.getName());
- ShardingSphereSchema schema = createIndexStatementContext.getTablesContext().getSchemaName()
- .map(database::getSchema).orElseGet(() -> database.getSchema(defaultSchemaName));
- validateTableExist(schema, Collections.singleton(createIndexStatement.getTable()));
- String tableName = createIndexStatement.getTable().getTableName().getIdentifier().getValue();
- String indexName = createIndexStatementContext.getIndexes().stream().map(each -> each.getIndexName().getIdentifier().getValue()).findFirst().orElse(null);
- ShardingSpherePreconditions.checkState(!schema.containsIndex(tableName, indexName), () -> new DuplicateIndexException(indexName));
- }
-
- @Override
- public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
- final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
- }
-}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateProcedureStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateProcedureStatementValidator.java
deleted file mode 100644
index e36656201ac..00000000000
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateProcedureStatementValidator.java
+++ /dev/null
@@ -1,68 +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.sharding.route.engine.validator.ddl.impl;
-
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
-import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.ShardingDDLStatementValidator;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.RoutineBodySegment;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateProcedureStatement;
-import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
-
-import java.util.Collection;
-import java.util.List;
-import java.util.Optional;
-
-/**
- * Sharding create procedure statement validator.
- */
-public final class ShardingCreateProcedureStatementValidator extends ShardingDDLStatementValidator {
-
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- CreateProcedureStatement createProcedureStatement = (CreateProcedureStatement) sqlStatementContext.getSqlStatement();
- Optional<RoutineBodySegment> routineBodySegment = createProcedureStatement.getRoutineBody();
- if (!routineBodySegment.isPresent()) {
- return;
- }
- TableExtractor extractor = new TableExtractor();
- Collection<SimpleTableSegment> existTables = extractor.extractExistTableFromRoutineBody(routineBodySegment.get());
- validateShardingTable(shardingRule, "CREATE PROCEDURE", existTables);
- if (!hintValueContext.isSkipMetadataValidate()) {
- String defaultSchemaName = new DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(database.getName());
- ShardingSphereSchema schema = createProcedureStatement.getProcedureName().flatMap(optional -> optional.getOwner()
- .map(owner -> database.getSchema(owner.getIdentifier().getValue()))).orElseGet(() -> database.getSchema(defaultSchemaName));
- validateTableExist(schema, existTables);
- Collection<SimpleTableSegment> notExistTables = extractor.extractNotExistTableFromRoutineBody(routineBodySegment.get());
- validateTableNotExist(schema, notExistTables);
- }
- }
-
- @Override
- public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
- }
-}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidator.java
index 3aa71182621..8c8b8bdb83e 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidator.java
@@ -20,43 +20,27 @@ package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateTableStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.sharding.checker.sql.util.ShardingSupportedCheckUtils;
import org.apache.shardingsphere.sharding.exception.connection.ShardingDDLRouteException;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.ShardingDDLStatementValidator;
+import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateTableStatement;
-import java.util.Collections;
import java.util.List;
/**
* Sharding create table statement validator.
*/
-public final class ShardingCreateTableStatementValidator extends ShardingDDLStatementValidator {
-
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- CreateTableStatementContext createTableStatementContext = (CreateTableStatementContext) sqlStatementContext;
- CreateTableStatement createTableStatement = createTableStatementContext.getSqlStatement();
- if (!createTableStatement.isIfNotExists() && !hintValueContext.isSkipMetadataValidate()) {
- String defaultSchemaName = new DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(database.getName());
- ShardingSphereSchema schema = createTableStatementContext.getTablesContext().getSchemaName()
- .map(database::getSchema).orElseGet(() -> database.getSchema(defaultSchemaName));
- validateTableNotExist(schema, Collections.singleton(createTableStatement.getTable()));
- }
- }
+public final class ShardingCreateTableStatementValidator implements ShardingStatementValidator {
@Override
public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
CreateTableStatementContext createTableStatementContext = (CreateTableStatementContext) sqlStatementContext;
String primaryTable = (createTableStatementContext.getSqlStatement()).getTable().getTableName().getIdentifier().getValue();
- if (isRouteUnitDataNodeDifferentSize(shardingRule, routeContext, primaryTable)) {
+ if (ShardingSupportedCheckUtils.isRouteUnitDataNodeDifferentSize(shardingRule, routeContext, primaryTable)) {
throw new ShardingDDLRouteException("CREATE", "TABLE", createTableStatementContext.getTablesContext().getTableNames());
}
}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java
index 4986ee97a81..de0071c79f0 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java
@@ -23,37 +23,21 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.sharding.exception.metadata.EngagedViewException;
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedCreateViewException;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.ShardingDDLStatementValidator;
+import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.AggregationProjectionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionSegment;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateViewStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
-import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
-import java.util.Collection;
import java.util.List;
/**
* Sharding create view statement validator.
*/
@RequiredArgsConstructor
-public final class ShardingCreateViewStatementValidator extends ShardingDDLStatementValidator {
-
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- TableExtractor extractor = new TableExtractor();
- extractor.extractTablesFromSelect(((CreateViewStatement) sqlStatementContext.getSqlStatement()).getSelect());
- Collection<SimpleTableSegment> tableSegments = extractor.getRewriteTables();
- String viewName = ((CreateViewStatement) sqlStatementContext.getSqlStatement()).getView().getTableName().getIdentifier().getValue();
- if (isShardingTablesNotBindingWithView(tableSegments, shardingRule, viewName)) {
- throw new EngagedViewException("sharding");
- }
- }
+public final class ShardingCreateViewStatementValidator implements ShardingStatementValidator {
@Override
public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropIndexStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropIndexStatementValidator.java
index 561ffb6eea6..445653b89e3 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropIndexStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropIndexStatementValidator.java
@@ -24,9 +24,9 @@ import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.sharding.checker.sql.util.ShardingSupportedCheckUtils;
import org.apache.shardingsphere.sharding.exception.connection.ShardingDDLRouteException;
-import org.apache.shardingsphere.sharding.exception.metadata.IndexNotExistedException;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.ShardingDDLStatementValidator;
+import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
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;
@@ -39,24 +39,7 @@ import java.util.stream.Collectors;
/**
* Sharding drop index statement validator.
*/
-public final class ShardingDropIndexStatementValidator extends ShardingDDLStatementValidator {
-
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- DropIndexStatement dropIndexStatement = (DropIndexStatement) sqlStatementContext.getSqlStatement();
- if (dropIndexStatement.isIfExists()) {
- return;
- }
- String defaultSchemaName = new DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(database.getName());
- for (IndexSegment each : dropIndexStatement.getIndexes()) {
- ShardingSphereSchema schema = each.getOwner().map(optional -> optional.getIdentifier().getValue())
- .map(database::getSchema).orElseGet(() -> database.getSchema(defaultSchemaName));
- if (!isSchemaContainsIndex(schema, each)) {
- throw new IndexNotExistedException(each.getIndexName().getIdentifier().getValue());
- }
- }
- }
+public final class ShardingDropIndexStatementValidator implements ShardingStatementValidator {
@Override
public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
@@ -79,7 +62,7 @@ public final class ShardingDropIndexStatementValidator extends ShardingDDLStatem
}
private void validateDropIndexRouteUnit(final ShardingRule shardingRule, final RouteContext routeContext, final Collection<IndexSegment> indexSegments, final String logicTableName) {
- if (isRouteUnitDataNodeDifferentSize(shardingRule, routeContext, logicTableName)) {
+ if (ShardingSupportedCheckUtils.isRouteUnitDataNodeDifferentSize(shardingRule, routeContext, logicTableName)) {
Collection<String> indexNames = indexSegments.stream().map(each -> each.getIndexName().getIdentifier().getValue()).collect(Collectors.toList());
throw new ShardingDDLRouteException("DROP", "INDEX", indexNames);
}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java
index 7b26e30d750..4466101c033 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java
@@ -21,18 +21,16 @@ import com.cedarsoftware.util.CaseInsensitiveSet;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.ddl.DropTableStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
+import org.apache.shardingsphere.sharding.checker.sql.util.ShardingSupportedCheckUtils;
import org.apache.shardingsphere.sharding.exception.connection.ShardingDDLRouteException;
import org.apache.shardingsphere.sharding.exception.metadata.InUsedTablesException;
-import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.ShardingDDLStatementValidator;
+import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement;
@@ -45,21 +43,7 @@ import java.util.stream.Collectors;
/**
* Sharding drop table statement validator.
*/
-public final class ShardingDropTableStatementValidator extends ShardingDDLStatementValidator {
-
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- DropTableStatementContext dropTableStatementContext = (DropTableStatementContext) sqlStatementContext;
- DropTableStatement dropTableStatement = dropTableStatementContext.getSqlStatement();
- if (!dropTableStatement.isIfExists()) {
- String defaultSchemaName = new DatabaseTypeRegistry(dropTableStatementContext.getDatabaseType()).getDefaultSchemaName(database.getName());
- ShardingSphereSchema schema = dropTableStatementContext.getTablesContext().getSchemaName().map(database::getSchema).orElseGet(() -> database.getSchema(defaultSchemaName));
- validateTableExist(schema, dropTableStatementContext.getTablesContext().getSimpleTables());
- }
- ShardingSpherePreconditions.checkState(!dropTableStatement.isContainsCascade(), () -> new UnsupportedShardingOperationException("DROP TABLE ... CASCADE",
- dropTableStatementContext.getTablesContext().getSimpleTables().iterator().next().getTableName().getIdentifier().getValue()));
- }
+public final class ShardingDropTableStatementValidator implements ShardingStatementValidator {
@Override
public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
@@ -68,7 +52,7 @@ public final class ShardingDropTableStatementValidator extends ShardingDDLStatem
DropTableStatement dropTableStatement = dropTableStatementContext.getSqlStatement();
checkTableInUsed(shardingRule, dropTableStatementContext, routeContext);
for (SimpleTableSegment each : dropTableStatement.getTables()) {
- if (isRouteUnitDataNodeDifferentSize(shardingRule, routeContext, each.getTableName().getIdentifier().getValue())) {
+ if (ShardingSupportedCheckUtils.isRouteUnitDataNodeDifferentSize(shardingRule, routeContext, each.getTableName().getIdentifier().getValue())) {
throw new ShardingDDLRouteException("DROP", "TABLE", dropTableStatementContext.getTablesContext().getTableNames());
}
}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingPrepareStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingPrepareStatementValidator.java
index 8d0a4cfb6a7..118e5fb2a19 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingPrepareStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingPrepareStatementValidator.java
@@ -25,7 +25,7 @@ import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.sharding.exception.connection.EmptyShardingRouteResultException;
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedPrepareRouteToSameDataSourceException;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.ShardingDDLStatementValidator;
+import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import java.util.List;
@@ -34,12 +34,7 @@ import java.util.stream.Collectors;
/**
* Sharding prepare statement validator.
*/
-public final class ShardingPrepareStatementValidator extends ShardingDDLStatementValidator {
-
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- }
+public final class ShardingPrepareStatementValidator implements ShardingStatementValidator {
@Override
public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingRenameTableStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingRenameTableStatementValidator.java
index e7775391946..c9082db3592 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingRenameTableStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingRenameTableStatementValidator.java
@@ -24,34 +24,19 @@ import org.apache.shardingsphere.infra.exception.core.ShardingSpherePrecondition
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.sharding.checker.sql.util.ShardingSupportedCheckUtils;
import org.apache.shardingsphere.sharding.exception.connection.ShardingDDLRouteException;
-import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
-import org.apache.shardingsphere.sharding.route.engine.validator.ddl.ShardingDDLStatementValidator;
+import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.table.RenameTableDefinitionSegment;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.RenameTableStatement;
-import java.util.Collection;
import java.util.List;
-import java.util.stream.Collectors;
/**
* Sharding rename table statement validator.
*/
-public final class ShardingRenameTableStatementValidator extends ShardingDDLStatementValidator {
-
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- RenameTableStatementContext renameTableStatementContext = (RenameTableStatementContext) sqlStatementContext;
- Collection<String> tableNames = renameTableStatementContext.getTablesContext().getSimpleTables().stream()
- .map(each -> each.getTableName().getIdentifier().getValue()).collect(Collectors.toList());
- List<SimpleTableSegment> renameTables = renameTableStatementContext.getSqlStatement()
- .getRenameTables().stream().map(RenameTableDefinitionSegment::getRenameTable).collect(Collectors.toList());
- ShardingSpherePreconditions.checkState(renameTables.isEmpty() || !shardingRule.containsShardingTable(tableNames),
- () -> new UnsupportedShardingOperationException("RENAME TABLE", renameTables.get(0).getTableName().getIdentifier().getValue()));
- }
+public final class ShardingRenameTableStatementValidator implements ShardingStatementValidator {
@Override
public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
@@ -59,7 +44,7 @@ public final class ShardingRenameTableStatementValidator extends ShardingDDLStat
RenameTableStatement renameTableStatement = (RenameTableStatement) sqlStatementContext.getSqlStatement();
for (RenameTableDefinitionSegment each : renameTableStatement.getRenameTables()) {
String primaryTable = each.getTable().getTableName().getIdentifier().getValue();
- ShardingSpherePreconditions.checkState(!isRouteUnitDataNodeDifferentSize(shardingRule, routeContext, primaryTable),
+ ShardingSpherePreconditions.checkState(!ShardingSupportedCheckUtils.isRouteUnitDataNodeDifferentSize(shardingRule, routeContext, primaryTable),
() -> new ShardingDDLRouteException("RENAME", "TABLE", ((RenameTableStatementContext) sqlStatementContext).getTablesContext().getTableNames()));
}
}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDMLStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDMLStatementValidator.java
index 9d3d59cefaa..0a8a69c5712 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDMLStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDMLStatementValidator.java
@@ -19,11 +19,9 @@ package org.apache.shardingsphere.sharding.route.engine.validator.dml;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
-import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
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.sharding.exception.syntax.DMLWithMultipleShardingTablesException;
import org.apache.shardingsphere.sharding.route.engine.condition.ShardingCondition;
import org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions;
import org.apache.shardingsphere.sharding.route.engine.condition.value.ListShardingConditionValue;
@@ -47,19 +45,6 @@ import java.util.Optional;
*/
public abstract class ShardingDMLStatementValidator implements ShardingStatementValidator {
- /**
- * Validate multiple table.
- *
- * @param shardingRule sharding rule
- * @param sqlStatementContext sqlStatementContext
- */
- protected void validateMultipleTable(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext) {
- Collection<String> tableNames = ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames();
- boolean isAllShardingTables = shardingRule.isAllShardingTables(tableNames) && (1 == tableNames.size() || shardingRule.isAllBindingTables(tableNames));
- boolean isAllSingleTables = !shardingRule.containsShardingTable(tableNames);
- ShardingSpherePreconditions.checkState(isAllShardingTables || isAllSingleTables, () -> new DMLWithMultipleShardingTablesException(tableNames));
- }
-
/**
* Judge whether is same route context or not.
*
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingCopyStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingCopyStatementValidator.java
deleted file mode 100644
index cab25e0081a..00000000000
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingCopyStatementValidator.java
+++ /dev/null
@@ -1,50 +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.sharding.route.engine.validator.dml.impl;
-
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
-import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
-import org.apache.shardingsphere.sharding.route.engine.validator.dml.ShardingDMLStatementValidator;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.CopyStatement;
-
-import java.util.List;
-
-/**
- * Sharding copy statement validator.
- */
-public final class ShardingCopyStatementValidator extends ShardingDMLStatementValidator {
-
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- String tableName = ((CopyStatement) sqlStatementContext.getSqlStatement()).getTableSegment().getTableName().getIdentifier().getValue();
- if (shardingRule.isShardingTable(tableName)) {
- throw new UnsupportedShardingOperationException("COPY", tableName);
- }
- }
-
- @Override
- public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
- }
-}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingDeleteStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingDeleteStatementValidator.java
index ed3388c18a0..f49fe92daf0 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingDeleteStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingDeleteStatementValidator.java
@@ -34,12 +34,6 @@ import java.util.List;
*/
public final class ShardingDeleteStatementValidator extends ShardingDMLStatementValidator {
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- validateMultipleTable(shardingRule, sqlStatementContext);
- }
-
@Override
public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidator.java
index 0737f323bdf..81e7282e3a4 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidator.java
@@ -18,17 +18,13 @@
package org.apache.shardingsphere.sharding.route.engine.validator.dml.impl;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.sharding.exception.algorithm.DuplicateInsertDataRecordException;
-import org.apache.shardingsphere.sharding.exception.syntax.InsertSelectTableViolationException;
-import org.apache.shardingsphere.sharding.exception.syntax.MissingGenerateKeyColumnWithInsertSelectException;
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedUpdatingShardingValueException;
import org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions;
@@ -36,7 +32,6 @@ import org.apache.shardingsphere.sharding.route.engine.type.standard.ShardingSta
import org.apache.shardingsphere.sharding.route.engine.validator.dml.ShardingDMLStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.ColumnAssignmentSegment;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.OnDuplicateKeyColumnsSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubquerySegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement;
@@ -54,38 +49,6 @@ public final class ShardingInsertStatementValidator extends ShardingDMLStatement
private final ShardingConditions shardingConditions;
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- if (null == ((InsertStatementContext) sqlStatementContext).getInsertSelectContext()) {
- validateMultipleTable(shardingRule, sqlStatementContext);
- }
- InsertStatement insertStatement = (InsertStatement) sqlStatementContext.getSqlStatement();
- String tableName = insertStatement.getTable().map(optional -> optional.getTableName().getIdentifier().getValue()).orElse("");
- Optional<SubquerySegment> insertSelectSegment = insertStatement.getInsertSelect();
- if (insertSelectSegment.isPresent() && isContainsKeyGenerateStrategy(shardingRule, tableName)
- && !isContainsKeyGenerateColumn(shardingRule, insertStatement.getColumns(), tableName)) {
- throw new MissingGenerateKeyColumnWithInsertSelectException();
- }
- TablesContext tablesContext = ((InsertStatementContext) sqlStatementContext).getTablesContext();
- if (insertSelectSegment.isPresent() && shardingRule.containsShardingTable(tablesContext.getTableNames())
- && !isAllSameTables(tablesContext.getTableNames()) && !shardingRule.isAllBindingTables(tablesContext.getTableNames())) {
- throw new InsertSelectTableViolationException();
- }
- }
-
- private boolean isContainsKeyGenerateStrategy(final ShardingRule shardingRule, final String tableName) {
- return shardingRule.findGenerateKeyColumnName(tableName).isPresent();
- }
-
- private boolean isContainsKeyGenerateColumn(final ShardingRule shardingRule, final Collection<ColumnSegment> columns, final String tableName) {
- return columns.isEmpty() || columns.stream().anyMatch(each -> shardingRule.isGenerateKeyColumn(each.getIdentifier().getValue(), tableName));
- }
-
- private boolean isAllSameTables(final Collection<String> tableNames) {
- return 1L == tableNames.stream().distinct().count();
- }
-
@Override
public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadDataStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadDataStatementValidator.java
deleted file mode 100644
index e3d6e7fcccf..00000000000
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadDataStatementValidator.java
+++ /dev/null
@@ -1,50 +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.sharding.route.engine.validator.dml.impl;
-
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
-import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
-import org.apache.shardingsphere.sharding.route.engine.validator.dml.ShardingDMLStatementValidator;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.LoadDataStatement;
-
-import java.util.List;
-
-/**
- * Sharding load data statement validator.
- */
-public final class ShardingLoadDataStatementValidator extends ShardingDMLStatementValidator {
-
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- String tableName = ((LoadDataStatement) sqlStatementContext.getSqlStatement()).getTableSegment().getTableName().getIdentifier().getValue();
- if (shardingRule.isShardingTable(tableName)) {
- throw new UnsupportedShardingOperationException("LOAD DATA", tableName);
- }
- }
-
- @Override
- public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
- }
-}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadXMLStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadXMLStatementValidator.java
deleted file mode 100644
index 310588c9879..00000000000
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadXMLStatementValidator.java
+++ /dev/null
@@ -1,50 +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.sharding.route.engine.validator.dml.impl;
-
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
-import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
-import org.apache.shardingsphere.sharding.route.engine.validator.dml.ShardingDMLStatementValidator;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.LoadXMLStatement;
-
-import java.util.List;
-
-/**
- * Sharding load xml statement validator.
- */
-public final class ShardingLoadXMLStatementValidator extends ShardingDMLStatementValidator {
-
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- String tableName = ((LoadXMLStatement) sqlStatementContext.getSqlStatement()).getTableSegment().getTableName().getIdentifier().getValue();
- if (shardingRule.isShardingTable(tableName)) {
- throw new UnsupportedShardingOperationException("LOAD XML", tableName);
- }
- }
-
- @Override
- public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
- }
-}
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingUpdateStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingUpdateStatementValidator.java
index 8edc732b038..8f898831575 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingUpdateStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingUpdateStatementValidator.java
@@ -40,12 +40,6 @@ import java.util.Optional;
*/
public final class ShardingUpdateStatementValidator extends ShardingDMLStatementValidator {
- @Override
- public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
- final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
- validateMultipleTable(shardingRule, sqlStatementContext);
- }
-
@Override
public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
diff --git a/features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker b/features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.checker.SupportedSQLCheckersBuilder
similarity index 90%
copy from features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker
copy to features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.checker.SupportedSQLCheckersBuilder
index 7d2d3e63912..463a41cb2ef 100644
--- a/features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker
+++ b/features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.checker.SupportedSQLCheckersBuilder
@@ -15,4 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.sharding.checker.ShardingRuleConfigurationChecker
+org.apache.shardingsphere.sharding.checker.sql.ShardingSupportedSQLCheckersBuilder
diff --git a/features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker b/features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker
index 7d2d3e63912..3c2be24c5fd 100644
--- a/features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker
+++ b/features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker
@@ -15,4 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.sharding.checker.ShardingRuleConfigurationChecker
+org.apache.shardingsphere.sharding.checker.config.ShardingRuleConfigurationChecker
diff --git a/features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfigurationEmptyChecker b/features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfigurationEmptyChecker
index 9564ad0092a..06ae4eb23f8 100644
--- a/features/sharding/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfigurationEmptyChecker
+++ b/features/sharding/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.sharding.checker.ShardingRuleConfigurationEmptyChecker
+org.apache.shardingsphere.sharding.checker.config.ShardingRuleConfigurationEmptyChecker
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/config/ShardingRuleConfigurationCheckerTest.java
similarity index 99%
rename from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationCheckerTest.java
rename to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/config/ShardingRuleConfigurationCheckerTest.java
index 7592d06f6b6..a821436f35d 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationCheckerTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/config/ShardingRuleConfigurationCheckerTest.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.checker;
+package org.apache.shardingsphere.sharding.checker.config;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.exception.UnregisteredAlgorithmException;
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationEmptyCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/config/ShardingRuleConfigurationEmptyCheckerTest.java
similarity index 98%
rename from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationEmptyCheckerTest.java
rename to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/config/ShardingRuleConfigurationEmptyCheckerTest.java
index e9661eeb03b..a8602fb23b5 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationEmptyCheckerTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/config/ShardingRuleConfigurationEmptyCheckerTest.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.checker;
+package org.apache.shardingsphere.sharding.checker.config;
import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfigurationEmptyChecker;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterIndexStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterIndexSupportedCheckerTest.java
similarity index 70%
rename from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterIndexStatementValidatorTest.java
rename to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterIndexSupportedCheckerTest.java
index e7cc7808252..6bfba6b808d 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterIndexStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterIndexSupportedCheckerTest.java
@@ -15,13 +15,12 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
+package org.apache.shardingsphere.sharding.checker.sql.ddl;
import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterIndexStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
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;
@@ -44,7 +43,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
-class ShardingAlterIndexStatementValidatorTest {
+class ShardingAlterIndexSupportedCheckerTest {
@Mock
private ShardingRule shardingRule;
@@ -53,43 +52,43 @@ class ShardingAlterIndexStatementValidatorTest {
private ShardingSphereDatabase database;
@Test
- void assertPreValidateAlterIndexWhenIndexExistRenameIndexNotExistForPostgreSQL() {
+ void assertCheckWhenIndexExistRenameIndexNotExistForPostgreSQL() {
PostgreSQLAlterIndexStatement sqlStatement = new PostgreSQLAlterIndexStatement();
sqlStatement.setIndex(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
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").getAllTableNames()).thenReturn(Collections.singletonList("t_order"));
- when(database.getSchema("public").getTable("t_order")).thenReturn(table);
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.getAllTableNames()).thenReturn(Collections.singletonList("t_order"));
+ when(schema.getTable("t_order")).thenReturn(table);
when(table.containsIndex("t_order_index")).thenReturn(true);
when(table.containsIndex("t_order_index_new")).thenReturn(false);
- assertDoesNotThrow(() -> new ShardingAlterIndexStatementValidator().preValidate(shardingRule, new AlterIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME),
- mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ assertDoesNotThrow(() -> new ShardingAlterIndexSupportedChecker().check(shardingRule, database, schema, new AlterIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
@Test
- void assertPreValidateAlterIndexWhenIndexNotExistRenameIndexNotExistForPostgreSQL() {
+ void assertCheckWhenIndexNotExistRenameIndexNotExistForPostgreSQL() {
PostgreSQLAlterIndexStatement sqlStatement = new PostgreSQLAlterIndexStatement();
sqlStatement.setIndex(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
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").getAllTableNames()).thenReturn(Collections.singletonList("t_order"));
when(database.getSchema("public").getTable("t_order")).thenReturn(table);
- when(table.containsIndex("t_order_index")).thenReturn(false);
- assertThrows(IndexNotExistedException.class, () -> new ShardingAlterIndexStatementValidator().preValidate(shardingRule,
- new AlterIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ assertThrows(IndexNotExistedException.class, () -> new ShardingAlterIndexSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class),
+ new AlterIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
@Test
- void assertPreValidateAlterIndexWhenIndexExistRenameIndexExistForPostgreSQL() {
+ void assertCheckAlterIndexWhenIndexExistRenameIndexExistForPostgreSQL() {
PostgreSQLAlterIndexStatement sqlStatement = new PostgreSQLAlterIndexStatement();
sqlStatement.setIndex(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
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").getAllTableNames()).thenReturn(Collections.singletonList("t_order"));
- when(database.getSchema("public").getTable("t_order")).thenReturn(table);
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.getAllTableNames()).thenReturn(Collections.singletonList("t_order"));
+ when(schema.getTable("t_order")).thenReturn(table);
when(table.containsIndex("t_order_index")).thenReturn(true);
when(table.containsIndex("t_order_index_new")).thenReturn(true);
- assertThrows(DuplicateIndexException.class, () -> new ShardingAlterIndexStatementValidator().preValidate(shardingRule, new AlterIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME),
- mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ assertThrows(DuplicateIndexException.class,
+ () -> new ShardingAlterIndexSupportedChecker().check(shardingRule, database, schema, new AlterIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadXMLStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterTableSupportedCheckerTest.java
similarity index 56%
copy from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadXMLStatementValidatorTest.java
copy to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterTableSupportedCheckerTest.java
index 3c9af7bd161..f6ff19dd786 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadXMLStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterTableSupportedCheckerTest.java
@@ -15,34 +15,32 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.route.engine.validator.dml.impl;
+package org.apache.shardingsphere.sharding.checker.sql.ddl;
-import org.apache.shardingsphere.infra.binder.context.statement.dml.LoadXMLStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterTableStatementContext;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
-import org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLLoadXMLStatement;
+import org.apache.shardingsphere.sql.parser.statement.postgresql.ddl.PostgreSQLAlterTableStatement;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-import java.util.Collections;
+import java.util.Arrays;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
-class ShardingLoadXMLStatementValidatorTest {
+class ShardingAlterTableSupportedCheckerTest {
@Mock
private ShardingRule shardingRule;
@@ -51,18 +49,13 @@ class ShardingLoadXMLStatementValidatorTest {
private ShardingSphereDatabase database;
@Test
- void assertPreValidateLoadXMLWithSingleTable() {
- MySQLLoadXMLStatement sqlStatement = new MySQLLoadXMLStatement(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
- assertDoesNotThrow(() -> new ShardingLoadXMLStatementValidator().preValidate(shardingRule, new LoadXMLStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class),
- Collections.emptyList(), database, mock(ConfigurationProperties.class)));
- }
-
- @Test
- void assertPreValidateLoadXMLWithShardingTable() {
- MySQLLoadXMLStatement sqlStatement = new MySQLLoadXMLStatement(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
- when(shardingRule.isShardingTable("t_order")).thenReturn(true);
+ void assertCheckWithRenameTableWithShardingTable() {
+ PostgreSQLAlterTableStatement sqlStatement = new PostgreSQLAlterTableStatement();
+ sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
+ sqlStatement.setRenameTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_new"))));
+ AlterTableStatementContext sqlStatementContext = new AlterTableStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
+ when(shardingRule.containsShardingTable(Arrays.asList("t_order", "t_order_new"))).thenReturn(true);
assertThrows(UnsupportedShardingOperationException.class,
- () -> new ShardingLoadXMLStatementValidator().preValidate(shardingRule, new LoadXMLStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class),
- Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class)));
+ () -> new ShardingAlterTableSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterViewSupportedCheckerTest.java
similarity index 73%
rename from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidatorTest.java
rename to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterViewSupportedCheckerTest.java
index b8e393f86dd..3fbf2ed51da 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingAlterViewSupportedCheckerTest.java
@@ -15,14 +15,12 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
+package org.apache.shardingsphere.sharding.checker.sql.ddl;
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterViewStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
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.EngagedViewException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
@@ -36,15 +34,13 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-import java.util.Collections;
-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
-class ShardingAlterViewStatementValidatorTest {
+class ShardingAlterViewSupportedCheckerTest {
@Mock
private ShardingRule shardingRule;
@@ -56,11 +52,10 @@ class ShardingAlterViewStatementValidatorTest {
MySQLAlterViewStatement sqlStatement = new MySQLAlterViewStatement();
sqlStatement.setView(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_view"))));
sqlStatement.setSelect(selectStatement);
- SQLStatementContext sqlStatementContext = new AlterViewStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
+ AlterViewStatementContext sqlStatementContext = new AlterViewStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
when(shardingRule.isShardingTable("t_order")).thenReturn(false);
- assertDoesNotThrow(() -> new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database,
- mock(ConfigurationProperties.class)));
+ assertDoesNotThrow(() -> new ShardingAlterViewSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
}
@Test
@@ -71,11 +66,10 @@ class ShardingAlterViewStatementValidatorTest {
sqlStatement.setView(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_view"))));
sqlStatement.setSelect(selectStatement);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
- SQLStatementContext sqlStatementContext = new AlterViewStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
+ AlterViewStatementContext sqlStatementContext = new AlterViewStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
when(shardingRule.isShardingTable("t_order")).thenReturn(true);
assertThrows(EngagedViewException.class,
- () -> new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database,
- mock(ConfigurationProperties.class)));
+ () -> new ShardingAlterViewSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
}
@Test
@@ -83,9 +77,8 @@ class ShardingAlterViewStatementValidatorTest {
OpenGaussAlterViewStatement sqlStatement = new OpenGaussAlterViewStatement();
sqlStatement.setView(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_view"))));
sqlStatement.setRenameView(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_new"))));
- SQLStatementContext sqlStatementContext = new AlterViewStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
+ AlterViewStatementContext sqlStatementContext = new AlterViewStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
- assertDoesNotThrow(() -> new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database,
- mock(ConfigurationProperties.class)));
+ assertDoesNotThrow(() -> new ShardingAlterViewSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateFunctionStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedCheckerTest.java
similarity index 71%
rename from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateFunctionStatementValidatorTest.java
rename to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedCheckerTest.java
index 643b93ef9cf..b9f37c318a8 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateFunctionStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateFunctionSupportedCheckerTest.java
@@ -15,16 +15,14 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
+package org.apache.shardingsphere.sharding.checker.sql.ddl;
-import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException;
-import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException;
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateFunctionStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
+import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException;
+import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.RoutineBodySegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.ValidStatementSegment;
@@ -39,8 +37,6 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-import java.util.Collections;
-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
@@ -48,13 +44,13 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
-class ShardingCreateFunctionStatementValidatorTest {
+class ShardingCreateFunctionSupportedCheckerTest {
@Mock
private ShardingRule shardingRule;
@Test
- void assertPreValidateCreateFunctionForMySQL() {
+ void assertCheckCreateFunctionForMySQL() {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_item"))));
MySQLCreateTableStatement createTableStatement = new MySQLCreateTableStatement(false);
@@ -68,16 +64,15 @@ class ShardingCreateFunctionStatementValidatorTest {
routineBody.getValidStatements().add(selectValidStatementSegment);
MySQLCreateFunctionStatement sqlStatement = new MySQLCreateFunctionStatement();
sqlStatement.setRoutineBody(routineBody);
- SQLStatementContext sqlStatementContext = new CreateFunctionStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
- when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
- when(database.getSchema(DefaultDatabase.LOGIC_NAME).containsTable("t_order_item")).thenReturn(true);
- assertDoesNotThrow(() -> new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database,
- mock(ConfigurationProperties.class)));
+ CreateFunctionStatementContext sqlStatementContext = new CreateFunctionStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.containsTable("t_order_item")).thenReturn(true);
+ assertDoesNotThrow(() -> new ShardingCreateFunctionSupportedChecker().check(shardingRule, database, schema, sqlStatementContext));
}
@Test
- void assertPreValidateCreateFunctionWithShardingTableForMySQL() {
+ void assertCheckCreateFunctionWithShardingTableForMySQL() {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
ValidStatementSegment validStatementSegment = new ValidStatementSegment(0, 0);
@@ -86,15 +81,13 @@ class ShardingCreateFunctionStatementValidatorTest {
routineBody.getValidStatements().add(validStatementSegment);
MySQLCreateFunctionStatement sqlStatement = new MySQLCreateFunctionStatement();
sqlStatement.setRoutineBody(routineBody);
- SQLStatementContext sqlStatementContext = new CreateFunctionStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
+ CreateFunctionStatementContext sqlStatementContext = new CreateFunctionStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
- when(database.getName()).thenReturn("db_schema");
- assertThrows(NoSuchTableException.class, () -> new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class),
- Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ assertThrows(NoSuchTableException.class, () -> new ShardingCreateFunctionSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
}
@Test
- void assertPreValidateCreateFunctionWithNoSuchTableForMySQL() {
+ void assertCheckCreateFunctionWithNoSuchTableForMySQL() {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
ValidStatementSegment validStatementSegment = new ValidStatementSegment(0, 0);
@@ -103,15 +96,13 @@ class ShardingCreateFunctionStatementValidatorTest {
routineBody.getValidStatements().add(validStatementSegment);
MySQLCreateFunctionStatement sqlStatement = new MySQLCreateFunctionStatement();
sqlStatement.setRoutineBody(routineBody);
- SQLStatementContext sqlStatementContext = new CreateFunctionStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
+ CreateFunctionStatementContext sqlStatementContext = new CreateFunctionStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
- when(database.getName()).thenReturn("db_schema");
- assertThrows(NoSuchTableException.class, () -> new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class),
- Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ assertThrows(NoSuchTableException.class, () -> new ShardingCreateFunctionSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
}
@Test
- void assertPreValidateCreateFunctionWithTableExistsForMySQL() {
+ void assertCheckCreateFunctionWithTableExistsForMySQL() {
MySQLCreateTableStatement createTableStatement = new MySQLCreateTableStatement(false);
createTableStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
ValidStatementSegment validStatementSegment = new ValidStatementSegment(0, 0);
@@ -120,11 +111,10 @@ class ShardingCreateFunctionStatementValidatorTest {
routineBody.getValidStatements().add(validStatementSegment);
MySQLCreateFunctionStatement sqlStatement = new MySQLCreateFunctionStatement();
sqlStatement.setRoutineBody(routineBody);
- SQLStatementContext sqlStatementContext = new CreateFunctionStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
+ CreateFunctionStatementContext sqlStatementContext = new CreateFunctionStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
- when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
- when(database.getSchema(DefaultDatabase.LOGIC_NAME).containsTable("t_order")).thenReturn(true);
- assertThrows(TableExistsException.class, () -> new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class),
- Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.containsTable("t_order")).thenReturn(true);
+ assertThrows(TableExistsException.class, () -> new ShardingCreateFunctionSupportedChecker().check(shardingRule, database, schema, sqlStatementContext));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateIndexStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateIndexSupportedCheckerTest.java
similarity index 54%
rename from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateIndexStatementValidatorTest.java
rename to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateIndexSupportedCheckerTest.java
index 855228879a9..2ae34512ac7 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateIndexStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateIndexSupportedCheckerTest.java
@@ -15,15 +15,13 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
+package org.apache.shardingsphere.sharding.checker.sql.ddl;
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateIndexStatementContext;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException;
-import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateIndexStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sharding.exception.metadata.DuplicateIndexException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexNameSegment;
@@ -39,15 +37,13 @@ import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-import java.util.Collections;
-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
-class ShardingCreateIndexStatementValidatorTest {
+class ShardingCreateIndexSupportedCheckerTest {
@Mock
private ShardingRule shardingRule;
@@ -56,75 +52,71 @@ class ShardingCreateIndexStatementValidatorTest {
private ShardingSphereDatabase database;
@Test
- void assertPreValidateCreateIndexWhenTableExistIndexNotExistForPostgreSQL() {
+ void assertCheckWhenTableExistIndexNotExistForPostgreSQL() {
PostgreSQLCreateIndexStatement sqlStatement = new PostgreSQLCreateIndexStatement(false);
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
sqlStatement.setIndex(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
- when(database.getSchema("public").containsTable("t_order")).thenReturn(true);
- ShardingSphereTable table = mock(ShardingSphereTable.class);
- when(database.getSchema("public").getTable("t_order")).thenReturn(table);
- assertDoesNotThrow(() -> new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME),
- mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.containsTable("t_order")).thenReturn(true);
+ assertDoesNotThrow(() -> new ShardingCreateIndexSupportedChecker().check(shardingRule, database, schema, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
@Test
- void assertPreValidateCreateIndexWhenTableNotExistIndexNotExistForPostgreSQL() {
+ void assertCheckWhenTableNotExistIndexNotExistForPostgreSQL() {
PostgreSQLCreateIndexStatement sqlStatement = new PostgreSQLCreateIndexStatement(false);
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
sqlStatement.setIndex(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
- when(database.getSchema("public").containsTable("t_order")).thenReturn(false);
- assertThrows(NoSuchTableException.class, () -> new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME),
- mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.containsTable("t_order")).thenReturn(false);
+ assertThrows(NoSuchTableException.class,
+ () -> new ShardingCreateIndexSupportedChecker().check(shardingRule, database, schema, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
@Test
- void assertPreValidateCreateIndexWhenTableExistIndexExistForPostgreSQL() {
+ void assertCheckWhenTableExistIndexExistForPostgreSQL() {
PostgreSQLCreateIndexStatement sqlStatement = new PostgreSQLCreateIndexStatement(false);
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
sqlStatement.setIndex(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
- when(database.getSchema("public").containsTable("t_order")).thenReturn(true);
- ShardingSphereTable table = mock(ShardingSphereTable.class);
- when(database.getSchema("public").getTable("t_order")).thenReturn(table);
- when(database.getSchema("public").containsIndex("t_order", "t_order_index")).thenReturn(true);
- assertThrows(DuplicateIndexException.class, () -> new ShardingCreateIndexStatementValidator().preValidate(shardingRule,
- new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.containsTable("t_order")).thenReturn(true);
+ when(schema.containsIndex("t_order", "t_order_index")).thenReturn(true);
+ assertThrows(DuplicateIndexException.class,
+ () -> new ShardingCreateIndexSupportedChecker().check(shardingRule, database, schema, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
@Test
- void assertPreValidateCreateIndexWithoutIndexNameWhenTableExistIndexNotExistForPostgreSQL() {
+ void assertCheckWithoutIndexNameWhenTableExistIndexNotExistForPostgreSQL() {
PostgreSQLCreateIndexStatement sqlStatement = new PostgreSQLCreateIndexStatement(false);
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
sqlStatement.getColumns().add(new ColumnSegment(0, 0, new IdentifierValue("content")));
sqlStatement.setGeneratedIndexStartIndex(10);
- when(database.getSchema("public").containsTable("t_order")).thenReturn(true);
- ShardingSphereTable table = mock(ShardingSphereTable.class);
- when(database.getSchema("public").getTable("t_order")).thenReturn(table);
- assertDoesNotThrow(() -> new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME),
- mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.containsTable("t_order")).thenReturn(true);
+ assertDoesNotThrow(() -> new ShardingCreateIndexSupportedChecker().check(shardingRule, database, schema, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
@Test
- void assertPreValidateCreateIndexWithoutIndexNameWhenTableNotExistIndexNotExistForPostgreSQL() {
+ void assertCheckWithoutIndexNameWhenTableNotExistIndexNotExistForPostgreSQL() {
PostgreSQLCreateIndexStatement sqlStatement = new PostgreSQLCreateIndexStatement(false);
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
sqlStatement.getColumns().add(new ColumnSegment(0, 0, new IdentifierValue("content")));
sqlStatement.setGeneratedIndexStartIndex(10);
- when(database.getSchema("public").containsTable("t_order")).thenReturn(false);
- assertThrows(NoSuchTableException.class, () -> new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME),
- mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.containsTable("t_order")).thenReturn(false);
+ assertThrows(NoSuchTableException.class,
+ () -> new ShardingCreateIndexSupportedChecker().check(shardingRule, database, schema, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
@Test
- void assertPreValidateCreateIndexWithoutIndexNameWhenTableExistIndexExistForPostgreSQL() {
+ void assertCheckWithoutIndexNameWhenTableExistIndexExistForPostgreSQL() {
PostgreSQLCreateIndexStatement sqlStatement = new PostgreSQLCreateIndexStatement(false);
sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
sqlStatement.getColumns().add(new ColumnSegment(0, 0, new IdentifierValue("content")));
sqlStatement.setGeneratedIndexStartIndex(10);
- when(database.getSchema("public").containsTable("t_order")).thenReturn(true);
- ShardingSphereTable table = mock(ShardingSphereTable.class);
- when(database.getSchema("public").getTable("t_order")).thenReturn(table);
- when(database.getSchema("public").containsIndex("t_order", "content_idx")).thenReturn(true);
- assertThrows(DuplicateIndexException.class, () -> new ShardingCreateIndexStatementValidator().preValidate(shardingRule,
- new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.containsTable("t_order")).thenReturn(true);
+ when(schema.containsIndex("t_order", "content_idx")).thenReturn(true);
+ assertThrows(DuplicateIndexException.class,
+ () -> new ShardingCreateIndexSupportedChecker().check(shardingRule, database, schema, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateProcedureStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateProcedureSupportedCheckerTest.java
similarity index 71%
rename from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateProcedureStatementValidatorTest.java
rename to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateProcedureSupportedCheckerTest.java
index 8072f473338..7609bad70a1 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateProcedureStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateProcedureSupportedCheckerTest.java
@@ -15,16 +15,14 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
+package org.apache.shardingsphere.sharding.checker.sql.ddl;
-import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException;
-import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException;
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateProcedureStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
+import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException;
+import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.RoutineBodySegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.ValidStatementSegment;
@@ -39,8 +37,6 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-import java.util.Collections;
-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
@@ -48,13 +44,13 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
-class ShardingCreateProcedureStatementValidatorTest {
+class ShardingCreateProcedureSupportedCheckerTest {
@Mock
private ShardingRule shardingRule;
@Test
- void assertPreValidateCreateProcedureForMySQL() {
+ void assertCheckForMySQL() {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_item"))));
MySQLCreateTableStatement createTableStatement = new MySQLCreateTableStatement(false);
@@ -69,16 +65,15 @@ class ShardingCreateProcedureStatementValidatorTest {
MySQLCreateProcedureStatement sqlStatement = new MySQLCreateProcedureStatement();
sqlStatement.setRoutineBody(routineBody);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
- when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
- when(database.getSchema(DefaultDatabase.LOGIC_NAME).containsTable("t_order_item")).thenReturn(true);
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.containsTable("t_order_item")).thenReturn(true);
when(shardingRule.isShardingTable("t_order_item")).thenReturn(false);
- SQLStatementContext sqlStatementContext = new CreateProcedureStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
- assertDoesNotThrow(() -> new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database,
- mock(ConfigurationProperties.class)));
+ CreateProcedureStatementContext sqlStatementContext = new CreateProcedureStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
+ assertDoesNotThrow(() -> new ShardingCreateProcedureSupportedChecker().check(shardingRule, database, schema, sqlStatementContext));
}
@Test
- void assertPreValidateCreateProcedureWithShardingTableForMySQL() {
+ void assertCheckWithShardingTableForMySQL() {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
ValidStatementSegment validStatementSegment = new ValidStatementSegment(0, 0);
@@ -87,15 +82,13 @@ class ShardingCreateProcedureStatementValidatorTest {
routineBody.getValidStatements().add(validStatementSegment);
MySQLCreateProcedureStatement sqlStatement = new MySQLCreateProcedureStatement();
sqlStatement.setRoutineBody(routineBody);
- SQLStatementContext sqlStatementContext = new CreateProcedureStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
+ CreateProcedureStatementContext sqlStatementContext = new CreateProcedureStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
- when(database.getName()).thenReturn("db_schema");
- assertThrows(NoSuchTableException.class, () -> new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class),
- Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ assertThrows(NoSuchTableException.class, () -> new ShardingCreateProcedureSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
}
@Test
- void assertPreValidateCreateProcedureWithNoSuchTableForMySQL() {
+ void assertCheckWithNoSuchTableForMySQL() {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
ValidStatementSegment validStatementSegment = new ValidStatementSegment(0, 0);
@@ -104,15 +97,13 @@ class ShardingCreateProcedureStatementValidatorTest {
routineBody.getValidStatements().add(validStatementSegment);
MySQLCreateProcedureStatement sqlStatement = new MySQLCreateProcedureStatement();
sqlStatement.setRoutineBody(routineBody);
- SQLStatementContext sqlStatementContext = new CreateProcedureStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
+ CreateProcedureStatementContext sqlStatementContext = new CreateProcedureStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
- when(database.getName()).thenReturn("db_schema");
- assertThrows(NoSuchTableException.class, () -> new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class),
- Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ assertThrows(NoSuchTableException.class, () -> new ShardingCreateProcedureSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
}
@Test
- void assertPreValidateCreateProcedureWithTableExistsForMySQL() {
+ void assertCheckWithTableExistsForMySQL() {
MySQLCreateTableStatement createTableStatement = new MySQLCreateTableStatement(false);
createTableStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
ValidStatementSegment validStatementSegment = new ValidStatementSegment(0, 0);
@@ -121,11 +112,10 @@ class ShardingCreateProcedureStatementValidatorTest {
routineBody.getValidStatements().add(validStatementSegment);
MySQLCreateProcedureStatement sqlStatement = new MySQLCreateProcedureStatement();
sqlStatement.setRoutineBody(routineBody);
- SQLStatementContext sqlStatementContext = new CreateProcedureStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
+ CreateProcedureStatementContext sqlStatementContext = new CreateProcedureStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
- when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
- when(database.getSchema(DefaultDatabase.LOGIC_NAME).containsTable("t_order")).thenReturn(true);
- assertThrows(TableExistsException.class, () -> new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class),
- Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.containsTable("t_order")).thenReturn(true);
+ assertThrows(TableExistsException.class, () -> new ShardingCreateProcedureSupportedChecker().check(shardingRule, database, schema, sqlStatementContext));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateTableSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateTableSupportedCheckerTest.java
new file mode 100644
index 00000000000..94fcc32acfe
--- /dev/null
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateTableSupportedCheckerTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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.checker.sql.ddl;
+
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateTableStatementContext;
+import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
+import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateTableStatement;
+import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.apache.shardingsphere.sql.parser.statement.mysql.ddl.MySQLCreateTableStatement;
+import org.apache.shardingsphere.sql.parser.statement.oracle.ddl.OracleCreateTableStatement;
+import org.apache.shardingsphere.sql.parser.statement.postgresql.ddl.PostgreSQLCreateTableStatement;
+import org.apache.shardingsphere.sql.parser.statement.sql92.ddl.SQL92CreateTableStatement;
+import org.apache.shardingsphere.sql.parser.statement.sqlserver.ddl.SQLServerCreateTableStatement;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+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;
+
+@ExtendWith(MockitoExtension.class)
+class ShardingCreateTableSupportedCheckerTest {
+
+ @Mock
+ private ShardingRule shardingRule;
+
+ @Test
+ void assertCheckForMySQL() {
+ MySQLCreateTableStatement sqlStatement = new MySQLCreateTableStatement(false);
+ sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
+ assertThrows(TableExistsException.class, () -> assertCheck(sqlStatement, "sharding_db"));
+ }
+
+ @Test
+ void assertCheckForOracle() {
+ OracleCreateTableStatement sqlStatement = new OracleCreateTableStatement();
+ sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
+ assertThrows(TableExistsException.class, () -> assertCheck(sqlStatement, "sharding_db"));
+ }
+
+ @Test
+ void assertCheckForPostgreSQL() {
+ PostgreSQLCreateTableStatement sqlStatement = new PostgreSQLCreateTableStatement(false);
+ sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
+ assertThrows(TableExistsException.class, () -> assertCheck(sqlStatement, "public"));
+ }
+
+ @Test
+ void assertCheckForSQL92() {
+ SQL92CreateTableStatement sqlStatement = new SQL92CreateTableStatement();
+ sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
+ assertThrows(TableExistsException.class, () -> assertCheck(sqlStatement, "sharding_db"));
+ }
+
+ @Test
+ void assertCheckForSQLServer() {
+ SQLServerCreateTableStatement sqlStatement = new SQLServerCreateTableStatement();
+ sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
+ assertThrows(TableExistsException.class, () -> assertCheck(sqlStatement, "dbo"));
+ }
+
+ private void assertCheck(final CreateTableStatement sqlStatement, final String schemaName) {
+ CreateTableStatementContext sqlStatementContext = new CreateTableStatementContext(sqlStatement, "sharding_db");
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.containsTable("t_order")).thenReturn(true);
+ new ShardingCreateTableSupportedChecker().check(shardingRule, database, schema, sqlStatementContext);
+ }
+
+ @Test
+ void assertCheckIfNotExistsForMySQL() {
+ MySQLCreateTableStatement sqlStatement = new MySQLCreateTableStatement(true);
+ sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
+ assertCheckIfNotExists(sqlStatement);
+ }
+
+ @Test
+ void assertCheckIfNotExistsForPostgreSQL() {
+ PostgreSQLCreateTableStatement sqlStatement = new PostgreSQLCreateTableStatement(true);
+ sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
+ assertCheckIfNotExists(sqlStatement);
+ }
+
+ private void assertCheckIfNotExists(final CreateTableStatement sqlStatement) {
+ CreateTableStatementContext sqlStatementContext = new CreateTableStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+ new ShardingCreateTableSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext);
+ }
+}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateViewSupportedCheckerTest.java
similarity index 59%
copy from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidatorTest.java
copy to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateViewSupportedCheckerTest.java
index 02247500490..68f6b898daf 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingCreateViewSupportedCheckerTest.java
@@ -15,17 +15,14 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
+package org.apache.shardingsphere.sharding.checker.sql.ddl;
import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateViewStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.sharding.exception.metadata.EngagedViewException;
-import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedCreateViewException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionsSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateViewStatement;
@@ -40,7 +37,6 @@ import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
-import java.util.Collections;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
@@ -51,7 +47,7 @@ import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
-class ShardingCreateViewStatementValidatorTest {
+class ShardingCreateViewSupportedCheckerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private RouteContext routeContext;
@@ -78,35 +74,15 @@ class ShardingCreateViewStatementValidatorTest {
}
@Test
- void assertPreValidateCreateView() {
- assertDoesNotThrow(() -> new ShardingCreateViewStatementValidator().preValidate(shardingRule, createViewStatementContext, mock(HintValueContext.class), Collections.emptyList(),
- mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class)));
+ void assertCheck() {
+ assertDoesNotThrow(() -> new ShardingCreateViewSupportedChecker().check(shardingRule, mock(ShardingSphereDatabase.class), mock(ShardingSphereSchema.class), createViewStatementContext));
}
@Test
- void assertPreValidateCreateViewWithException() {
+ void assertCheckWithException() {
when(shardingRule.isShardingTable(any())).thenReturn(true);
when(shardingRule.isAllBindingTables(any())).thenReturn(false);
- ConfigurationProperties props = mock(ConfigurationProperties.class);
- assertThrows(EngagedViewException.class, () -> new ShardingCreateViewStatementValidator().preValidate(shardingRule, createViewStatementContext, mock(HintValueContext.class),
- Collections.emptyList(), mock(ShardingSphereDatabase.class), props));
- }
-
- @Test
- void assertPostValidateCreateView() {
- ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class);
- when(selectStatement.getProjections()).thenReturn(projectionsSegment);
- assertDoesNotThrow(() -> new ShardingCreateViewStatementValidator().postValidate(
- shardingRule, createViewStatementContext, new HintValueContext(), Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class), routeContext));
- }
-
- @Test
- void assertPostValidateCreateViewWithException() {
- ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class);
- when(projectionsSegment.isDistinctRow()).thenReturn(true);
- when(selectStatement.getProjections()).thenReturn(projectionsSegment);
- assertThrows(UnsupportedCreateViewException.class,
- () -> new ShardingCreateViewStatementValidator().postValidate(shardingRule,
- createViewStatementContext, new HintValueContext(), Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class), routeContext));
+ assertThrows(EngagedViewException.class,
+ () -> new ShardingCreateViewSupportedChecker().check(shardingRule, mock(ShardingSphereDatabase.class), mock(ShardingSphereSchema.class), createViewStatementContext));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropIndexSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropIndexSupportedCheckerTest.java
new file mode 100644
index 00000000000..43bcadd40d9
--- /dev/null
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropIndexSupportedCheckerTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.checker.sql.ddl;
+
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.DropIndexStatementContext;
+import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
+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.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;
+import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.apache.shardingsphere.sql.parser.statement.postgresql.ddl.PostgreSQLDropIndexStatement;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class ShardingDropIndexSupportedCheckerTest {
+
+ @Mock
+ private ShardingRule shardingRule;
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private ShardingSphereDatabase database;
+
+ @Test
+ void assertCheckWhenIndexExistForPostgreSQL() {
+ PostgreSQLDropIndexStatement sqlStatement = new PostgreSQLDropIndexStatement(false);
+ sqlStatement.getIndexes().add(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
+ sqlStatement.getIndexes().add(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index_new"))));
+ ShardingSphereTable table = mock(ShardingSphereTable.class);
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.getAllTableNames()).thenReturn(Collections.singletonList("t_order"));
+ when(schema.getTable("t_order")).thenReturn(table);
+ when(table.containsIndex("t_order_index")).thenReturn(true);
+ when(table.containsIndex("t_order_index_new")).thenReturn(true);
+ assertDoesNotThrow(() -> new ShardingDropIndexSupportedChecker().check(shardingRule, database, schema, new DropIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
+ }
+
+ @Test
+ void assertCheckWhenIndexNotExistForPostgreSQL() {
+ PostgreSQLDropIndexStatement sqlStatement = new PostgreSQLDropIndexStatement(false);
+ sqlStatement.getIndexes().add(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
+ sqlStatement.getIndexes().add(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index_new"))));
+ ShardingSphereTable table = mock(ShardingSphereTable.class);
+ when(database.getSchema("public").getAllTableNames()).thenReturn(Collections.singletonList("t_order"));
+ when(database.getSchema("public").getTable("t_order")).thenReturn(table);
+ assertThrows(IndexNotExistedException.class,
+ () -> new ShardingDropIndexSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), new DropIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
+ }
+}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropTableSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropTableSupportedCheckerTest.java
new file mode 100644
index 00000000000..4efeb700a4e
--- /dev/null
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingDropTableSupportedCheckerTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.checker.sql.ddl;
+
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.DropTableStatementContext;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sharding.rule.ShardingTable;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.apache.shardingsphere.sql.parser.statement.mysql.ddl.MySQLDropTableStatement;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
+
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
+class ShardingDropTableSupportedCheckerTest {
+
+ @Mock
+ private ShardingRule shardingRule;
+
+ @BeforeEach
+ void init() {
+ Map<String, ShardingTable> shardingTables = new LinkedHashMap<>(2, 1F);
+ shardingTables.put("t_order_item", createShardingTable("t_order_item"));
+ shardingTables.put("t_order", createShardingTable("t_order"));
+ when(shardingRule.getShardingTables()).thenReturn(shardingTables);
+ }
+
+ @Test
+ void assertCheckForMySQL() {
+ MySQLDropTableStatement sqlStatement = new MySQLDropTableStatement(false);
+ sqlStatement.getTables().add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_item"))));
+ DropTableStatementContext sqlStatementContext = new DropTableStatementContext(sqlStatement, "db_schema");
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
+ when(schema.containsTable("t_order_item")).thenReturn(true);
+ ShardingDropTableSupportedChecker checker = new ShardingDropTableSupportedChecker();
+ assertDoesNotThrow(() -> checker.check(shardingRule, database, schema, sqlStatementContext));
+ }
+
+ private ShardingTable createShardingTable(final String tableName) {
+ ShardingTable result = mock(ShardingTable.class);
+ when(result.getLogicTable()).thenReturn(tableName);
+ List<DataNode> dataNodes = new LinkedList<>();
+ DataNode d1 = mock(DataNode.class);
+ when(d1.getTableName()).thenReturn("t_order_item_1");
+ dataNodes.add(d1);
+ DataNode d2 = mock(DataNode.class);
+ when(d2.getTableName()).thenReturn("t_order_item_2");
+ dataNodes.add(d2);
+ when(result.getActualDataNodes()).thenReturn(dataNodes);
+ return result;
+ }
+}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingInsertSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingInsertSupportedCheckerTest.java
new file mode 100644
index 00000000000..0709e2d2d72
--- /dev/null
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingInsertSupportedCheckerTest.java
@@ -0,0 +1,167 @@
+/*
+ * 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.checker.sql.ddl;
+
+import org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext;
+import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingInsertSupportedChecker;
+import org.apache.shardingsphere.sharding.exception.syntax.DMLWithMultipleShardingTablesException;
+import org.apache.shardingsphere.sharding.exception.syntax.InsertSelectTableViolationException;
+import org.apache.shardingsphere.sharding.exception.syntax.MissingGenerateKeyColumnWithInsertSelectException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.ColumnAssignmentSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.InsertColumnsSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.OnDuplicateKeyColumnsSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubquerySegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionsSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
+import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLInsertStatement;
+import org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLSelectStatement;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class ShardingInsertSupportedCheckerTest {
+
+ @Mock
+ private ShardingRule shardingRule;
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private ShardingSphereDatabase database;
+
+ @Test
+ void assertCheckWhenInsertMultiTables() {
+ InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertStatement());
+ Collection<String> tableNames = sqlStatementContext.getTablesContext().getTableNames();
+ when(shardingRule.isAllShardingTables(tableNames)).thenReturn(false);
+ when(shardingRule.containsShardingTable(tableNames)).thenReturn(true);
+ assertThrows(DMLWithMultipleShardingTablesException.class, () -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
+ }
+
+ private InsertStatementContext createInsertStatementContext(final List<Object> params, final InsertStatement insertStatement) {
+ when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class));
+ ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class),
+ mock(RuleMetaData.class), mock(ConfigurationProperties.class));
+ return new InsertStatementContext(metaData, params, insertStatement, DefaultDatabase.LOGIC_NAME);
+ }
+
+ @Test
+ void assertCheckWhenInsertSelectWithoutKeyGenerateColumn() {
+ when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
+ when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(false);
+ InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement());
+ sqlStatementContext.getTablesContext().getTableNames().addAll(createSingleTablesContext().getTableNames());
+ assertThrows(MissingGenerateKeyColumnWithInsertSelectException.class,
+ () -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
+ }
+
+ @Test
+ void assertCheckWhenInsertSelectWithKeyGenerateColumn() {
+ when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
+ when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
+ InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement());
+ sqlStatementContext.getTablesContext().getTableNames().addAll(createSingleTablesContext().getTableNames());
+ assertDoesNotThrow(() -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
+ }
+
+ @Test
+ void assertCheckWhenInsertSelectWithoutBindingTables() {
+ when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
+ when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
+ TablesContext multiTablesContext = createMultiTablesContext();
+ when(shardingRule.isAllBindingTables(multiTablesContext.getTableNames())).thenReturn(false);
+ when(shardingRule.containsShardingTable(multiTablesContext.getTableNames())).thenReturn(true);
+ InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement());
+ sqlStatementContext.getTablesContext().getTableNames().addAll(multiTablesContext.getTableNames());
+ assertThrows(InsertSelectTableViolationException.class, () -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
+ }
+
+ @Test
+ void assertCheckWhenInsertSelectWithBindingTables() {
+ when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
+ when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
+ TablesContext multiTablesContext = createMultiTablesContext();
+ InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement());
+ sqlStatementContext.getTablesContext().getTableNames().addAll(multiTablesContext.getTableNames());
+ assertDoesNotThrow(() -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
+ }
+
+ private InsertStatement createInsertStatement() {
+ MySQLInsertStatement result = new MySQLInsertStatement();
+ result.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("user"))));
+ ColumnSegment columnSegment = new ColumnSegment(0, 0, new IdentifierValue("id"));
+ List<ColumnSegment> columnSegments = new LinkedList<>();
+ columnSegments.add(columnSegment);
+ ColumnAssignmentSegment assignmentSegment = new ColumnAssignmentSegment(0, 0, columnSegments, new ParameterMarkerExpressionSegment(0, 0, 0));
+ result.setOnDuplicateKeyColumns(new OnDuplicateKeyColumnsSegment(0, 0, Collections.singletonList(assignmentSegment)));
+ Collection<ColumnSegment> columns = new LinkedList<>();
+ columns.add(columnSegment);
+ result.setInsertColumns(new InsertColumnsSegment(0, 0, columns));
+ return result;
+ }
+
+ private InsertStatement createInsertSelectStatement() {
+ InsertStatement result = createInsertStatement();
+ SelectStatement selectStatement = new MySQLSelectStatement();
+ selectStatement.setProjections(new ProjectionsSegment(0, 0));
+ result.setInsertSelect(new SubquerySegment(0, 0, selectStatement, ""));
+ return result;
+ }
+
+ private TablesContext createSingleTablesContext() {
+ List<SimpleTableSegment> result = new LinkedList<>();
+ result.add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("user"))));
+ return new TablesContext(result, TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), DefaultDatabase.LOGIC_NAME);
+ }
+
+ private TablesContext createMultiTablesContext() {
+ List<SimpleTableSegment> result = new LinkedList<>();
+ result.add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("user"))));
+ result.add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("order"))));
+ return new TablesContext(result, TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), DefaultDatabase.LOGIC_NAME);
+ }
+}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingRenameTableSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingRenameTableSupportedCheckerTest.java
new file mode 100644
index 00000000000..1387df142d6
--- /dev/null
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingRenameTableSupportedCheckerTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.checker.sql.ddl;
+
+import org.apache.shardingsphere.infra.binder.context.statement.ddl.RenameTableStatementContext;
+import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.table.RenameTableDefinitionSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.apache.shardingsphere.sql.parser.statement.mysql.ddl.MySQLRenameTableStatement;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class ShardingRenameTableSupportedCheckerTest {
+
+ @Mock
+ private ShardingRule shardingRule;
+
+ @Test
+ void assertCheckShardingTable() {
+ RenameTableStatementContext sqlStatementContext = createRenameTableStatementContext("t_order", "t_user_order");
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+ when(shardingRule.containsShardingTable(argThat(tableNames -> tableNames.contains("t_order") || tableNames.contains("t_user_order")))).thenReturn(true);
+ assertThrows(UnsupportedShardingOperationException.class,
+ () -> new ShardingRenameTableSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
+ }
+
+ @Test
+ void assertCheckNormalCase() {
+ RenameTableStatementContext sqlStatementContext = createRenameTableStatementContext("t_not_sharding_table", "t_not_sharding_table_new");
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+ assertDoesNotThrow(() -> new ShardingRenameTableSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
+ }
+
+ private RenameTableStatementContext createRenameTableStatementContext(final String originTableName, final String newTableName) {
+ MySQLRenameTableStatement sqlStatement = new MySQLRenameTableStatement();
+ RenameTableDefinitionSegment renameTableDefinitionSegment = new RenameTableDefinitionSegment(0, 0);
+ renameTableDefinitionSegment.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue(originTableName))));
+ renameTableDefinitionSegment.setRenameTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue(newTableName))));
+ sqlStatement.getRenameTables().add(renameTableDefinitionSegment);
+ return new RenameTableStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
+ }
+}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingCopyStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingCopySupportedCheckerTest.java
similarity index 68%
rename from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingCopyStatementValidatorTest.java
rename to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingCopySupportedCheckerTest.java
index 6fdc8f06aa4..86e2e03874d 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingCopyStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingCopySupportedCheckerTest.java
@@ -15,13 +15,12 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.route.engine.validator.dml.impl;
+package org.apache.shardingsphere.sharding.checker.sql.dml;
import org.apache.shardingsphere.infra.binder.context.statement.dml.CopyStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
@@ -36,15 +35,13 @@ import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-import java.util.Collections;
-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
-class ShardingCopyStatementValidatorTest {
+class ShardingCopySupportedCheckerTest {
@Mock
private ShardingRule shardingRule;
@@ -53,37 +50,36 @@ class ShardingCopyStatementValidatorTest {
private ShardingSphereDatabase database;
@Test
- void assertPreValidateWhenTableSegmentForPostgreSQL() {
+ void assertCheckWhenTableSegmentForPostgreSQL() {
PostgreSQLCopyStatement sqlStatement = new PostgreSQLCopyStatement();
sqlStatement.setTableSegment(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
- assertDoesNotThrow(() -> new ShardingCopyStatementValidator().preValidate(shardingRule, new CopyStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class),
- Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ assertDoesNotThrow(
+ () -> new ShardingCopySupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), new CopyStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
@Test
- void assertPreValidateWhenTableSegmentForOpenGauss() {
+ void assertCheckWhenTableSegmentForOpenGauss() {
OpenGaussCopyStatement sqlStatement = new OpenGaussCopyStatement();
sqlStatement.setTableSegment(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
- assertDoesNotThrow(() -> new ShardingCopyStatementValidator().preValidate(shardingRule, new CopyStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class),
- Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ assertDoesNotThrow(
+ () -> new ShardingCopySupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), new CopyStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
@Test
- void assertPreValidateCopyWithShardingTableForPostgreSQL() {
- assertThrows(UnsupportedShardingOperationException.class, () -> assertPreValidateCopyTable(new PostgreSQLCopyStatement()));
+ void assertCheckCopyWithShardingTableForPostgreSQL() {
+ assertThrows(UnsupportedShardingOperationException.class, () -> assertCheckCopyTable(new PostgreSQLCopyStatement()));
}
@Test
- void assertPreValidateCopyWithShardingTableForOpenGauss() {
- assertThrows(UnsupportedShardingOperationException.class, () -> assertPreValidateCopyTable(new OpenGaussCopyStatement()));
+ void assertCheckCopyWithShardingTableForOpenGauss() {
+ assertThrows(UnsupportedShardingOperationException.class, () -> assertCheckCopyTable(new OpenGaussCopyStatement()));
}
- private void assertPreValidateCopyTable(final CopyStatement sqlStatement) {
+ private void assertCheckCopyTable(final CopyStatement sqlStatement) {
sqlStatement.setTableSegment(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
CopyStatementContext sqlStatementContext = new CopyStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
String tableName = "t_order";
when(shardingRule.isShardingTable(tableName)).thenReturn(true);
- new ShardingCopyStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), mock(ShardingSphereDatabase.class),
- mock(ConfigurationProperties.class));
+ new ShardingCopySupportedChecker().check(shardingRule, mock(ShardingSphereDatabase.class), mock(ShardingSphereSchema.class), sqlStatementContext);
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingDeleteStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingDeleteSupportedCheckerTest.java
similarity index 75%
rename from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingDeleteStatementValidatorTest.java
rename to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingDeleteSupportedCheckerTest.java
index ff206365541..9c4a61d8839 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingDeleteStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingDeleteSupportedCheckerTest.java
@@ -15,13 +15,12 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.route.engine.validator.dml.impl;
+package org.apache.shardingsphere.sharding.checker.sql.dml;
import org.apache.shardingsphere.infra.binder.context.statement.dml.DeleteStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sharding.exception.syntax.DMLWithMultipleShardingTablesException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.DeleteMultiTableSegment;
@@ -41,7 +40,6 @@ import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashSet;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -49,37 +47,37 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
-class ShardingDeleteStatementValidatorTest {
+class ShardingDeleteSupportedCheckerTest {
@Mock
private ShardingRule shardingRule;
@Test
- void assertPreValidateWhenDeleteMultiTablesForMySQL() {
- assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertPreValidateWhenDeleteMultiTables(new MySQLDeleteStatement()));
+ void assertCheckWhenDeleteMultiTablesForMySQL() {
+ assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertCheckWhenDeleteMultiTables(new MySQLDeleteStatement()));
}
@Test
- void assertPreValidateWhenDeleteMultiTablesForOracle() {
- assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertPreValidateWhenDeleteMultiTables(new OracleDeleteStatement()));
+ void assertCheckWhenDeleteMultiTablesForOracle() {
+ assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertCheckWhenDeleteMultiTables(new OracleDeleteStatement()));
}
@Test
- void assertPreValidateWhenDeleteMultiTablesForPostgreSQL() {
- assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertPreValidateWhenDeleteMultiTables(new PostgreSQLDeleteStatement()));
+ void assertCheckWhenDeleteMultiTablesForPostgreSQL() {
+ assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertCheckWhenDeleteMultiTables(new PostgreSQLDeleteStatement()));
}
@Test
- void assertPreValidateWhenDeleteMultiTablesForSQL92() {
- assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertPreValidateWhenDeleteMultiTables(new SQL92DeleteStatement()));
+ void assertCheckWhenDeleteMultiTablesForSQL92() {
+ assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertCheckWhenDeleteMultiTables(new SQL92DeleteStatement()));
}
@Test
- void assertPreValidateWhenDeleteMultiTablesForSQLServer() {
- assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertPreValidateWhenDeleteMultiTables(new SQLServerDeleteStatement()));
+ void assertCheckWhenDeleteMultiTablesForSQLServer() {
+ assertThrows(DMLWithMultipleShardingTablesException.class, () -> assertCheckWhenDeleteMultiTables(new SQLServerDeleteStatement()));
}
- private void assertPreValidateWhenDeleteMultiTables(final DeleteStatement sqlStatement) {
+ private void assertCheckWhenDeleteMultiTables(final DeleteStatement sqlStatement) {
DeleteMultiTableSegment tableSegment = new DeleteMultiTableSegment();
tableSegment.getActualDeleteTables().add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("user"))));
tableSegment.getActualDeleteTables().add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("order"))));
@@ -90,6 +88,6 @@ class ShardingDeleteStatementValidatorTest {
when(shardingRule.containsShardingTable(tableNames)).thenReturn(true);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
DeleteStatementContext sqlStatementContext = new DeleteStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
- new ShardingDeleteStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class));
+ new ShardingDeleteSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext);
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadDataStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedCheckerTest.java
similarity index 72%
rename from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadDataStatementValidatorTest.java
rename to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedCheckerTest.java
index 8508063b953..fd977568e83 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadDataStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadDataSupportedCheckerTest.java
@@ -15,13 +15,12 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.route.engine.validator.dml.impl;
+package org.apache.shardingsphere.sharding.checker.sql.dml;
import org.apache.shardingsphere.infra.binder.context.statement.dml.LoadDataStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
@@ -34,15 +33,13 @@ import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-import java.util.Collections;
-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
-class ShardingLoadDataStatementValidatorTest {
+class ShardingLoadDataSupportedCheckerTest {
@Mock
private ShardingRule shardingRule;
@@ -51,18 +48,17 @@ class ShardingLoadDataStatementValidatorTest {
private ShardingSphereDatabase database;
@Test
- void assertPreValidateLoadDataWithSingleTable() {
+ void assertCheckWithSingleTable() {
MySQLLoadDataStatement sqlStatement = new MySQLLoadDataStatement(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
- assertDoesNotThrow(() -> new ShardingLoadDataStatementValidator().preValidate(shardingRule, new LoadDataStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME),
- mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ assertDoesNotThrow(
+ () -> new ShardingLoadDataSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), new LoadDataStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
@Test
- void assertPreValidateLoadDataWithShardingTable() {
+ void assertCheckWithShardingTable() {
MySQLLoadDataStatement sqlStatement = new MySQLLoadDataStatement(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
when(shardingRule.isShardingTable("t_order")).thenReturn(true);
assertThrows(UnsupportedShardingOperationException.class,
- () -> new ShardingLoadDataStatementValidator().preValidate(shardingRule, new LoadDataStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class),
- Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class)));
+ () -> new ShardingLoadDataSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), new LoadDataStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadXMLStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadXmlSupportedCheckerTest.java
similarity index 72%
rename from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadXMLStatementValidatorTest.java
rename to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadXmlSupportedCheckerTest.java
index 3c9af7bd161..b024d44138a 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadXMLStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingLoadXmlSupportedCheckerTest.java
@@ -15,13 +15,12 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.route.engine.validator.dml.impl;
+package org.apache.shardingsphere.sharding.checker.sql.dml;
import org.apache.shardingsphere.infra.binder.context.statement.dml.LoadXMLStatementContext;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
-import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
@@ -34,15 +33,13 @@ import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-import java.util.Collections;
-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
-class ShardingLoadXMLStatementValidatorTest {
+class ShardingLoadXmlSupportedCheckerTest {
@Mock
private ShardingRule shardingRule;
@@ -51,18 +48,17 @@ class ShardingLoadXMLStatementValidatorTest {
private ShardingSphereDatabase database;
@Test
- void assertPreValidateLoadXMLWithSingleTable() {
+ void assertCheckWithSingleTable() {
MySQLLoadXMLStatement sqlStatement = new MySQLLoadXMLStatement(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
- assertDoesNotThrow(() -> new ShardingLoadXMLStatementValidator().preValidate(shardingRule, new LoadXMLStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class),
- Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ assertDoesNotThrow(
+ () -> new ShardingLoadXmlSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), new LoadXMLStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
@Test
- void assertPreValidateLoadXMLWithShardingTable() {
+ void assertCheckWithShardingTable() {
MySQLLoadXMLStatement sqlStatement = new MySQLLoadXMLStatement(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
when(shardingRule.isShardingTable("t_order")).thenReturn(true);
assertThrows(UnsupportedShardingOperationException.class,
- () -> new ShardingLoadXMLStatementValidator().preValidate(shardingRule, new LoadXMLStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class),
- Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class)));
+ () -> new ShardingLoadXmlSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), new LoadXMLStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME)));
}
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingUpdateSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingUpdateSupportedCheckerTest.java
new file mode 100644
index 00000000000..041f52cfc35
--- /dev/null
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/dml/ShardingUpdateSupportedCheckerTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.checker.sql.dml;
+
+import org.apache.shardingsphere.infra.binder.context.statement.dml.UpdateStatementContext;
+import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.apache.shardingsphere.sharding.exception.syntax.DMLWithMultipleShardingTablesException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.ColumnAssignmentSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.SetAssignmentSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.LiteralExpressionSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.JoinTableSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.UpdateStatement;
+import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLUpdateStatement;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class ShardingUpdateSupportedCheckerTest {
+
+ @Mock
+ private ShardingRule shardingRule;
+
+ @Mock
+ private ShardingSphereDatabase database;
+
+ @Test
+ void assertCheckWhenUpdateSingleTable() {
+ UpdateStatement updateStatement = createUpdateStatement();
+ updateStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("user"))));
+ UpdateStatementContext sqlStatementContext = new UpdateStatementContext(updateStatement, DefaultDatabase.LOGIC_NAME);
+ Collection<String> tableNames = sqlStatementContext.getTablesContext().getTableNames();
+ when(shardingRule.isAllShardingTables(tableNames)).thenReturn(true);
+ when(shardingRule.containsShardingTable(tableNames)).thenReturn(true);
+ assertDoesNotThrow(() -> new ShardingUpdateSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
+ }
+
+ @Test
+ void assertCheckWhenUpdateMultipleTables() {
+ UpdateStatement updateStatement = createUpdateStatement();
+ JoinTableSegment joinTableSegment = new JoinTableSegment();
+ joinTableSegment.setLeft(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("user"))));
+ joinTableSegment.setRight(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("order"))));
+ updateStatement.setTable(joinTableSegment);
+ UpdateStatementContext sqlStatementContext = new UpdateStatementContext(updateStatement, DefaultDatabase.LOGIC_NAME);
+ Collection<String> tableNames = sqlStatementContext.getTablesContext().getTableNames();
+ when(shardingRule.isAllShardingTables(tableNames)).thenReturn(false);
+ when(shardingRule.containsShardingTable(tableNames)).thenReturn(true);
+ assertThrows(DMLWithMultipleShardingTablesException.class, () -> new ShardingUpdateSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext));
+ }
+
+ private UpdateStatement createUpdateStatement() {
+ UpdateStatement result = new MySQLUpdateStatement();
+ result.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("user"))));
+ List<ColumnSegment> columns = new LinkedList<>();
+ columns.add(new ColumnSegment(0, 0, new IdentifierValue("id")));
+ ColumnAssignmentSegment assignment = new ColumnAssignmentSegment(0, 0, columns, new LiteralExpressionSegment(0, 0, 1));
+ result.setSetAssignment(new SetAssignmentSegment(0, 0, Collections.singleton(assignment)));
+ return result;
+ }
+}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidatorTest.java
index a2c7b733b7e..6200434eb41 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidatorTest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterTableStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
@@ -27,7 +26,6 @@ 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.sharding.exception.connection.ShardingDDLRouteException;
-import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.ShardingTable;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
@@ -62,18 +60,6 @@ class ShardingAlterTableStatementValidatorTest {
@Mock
private RouteContext routeContext;
- @Test
- void assertPreValidateAlterTableWithRenameTableWithShardingTableForPostgreSQL() {
- PostgreSQLAlterTableStatement sqlStatement = new PostgreSQLAlterTableStatement();
- sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
- sqlStatement.setRenameTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_new"))));
- SQLStatementContext sqlStatementContext = new AlterTableStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
- when(shardingRule.containsShardingTable(Arrays.asList("t_order", "t_order_new"))).thenReturn(true);
- assertThrows(UnsupportedShardingOperationException.class,
- () -> new ShardingAlterTableStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database,
- mock(ConfigurationProperties.class)));
- }
-
@Test
void assertPostValidateAlterTableWithSameRouteResultShardingTableForPostgreSQL() {
PostgreSQLAlterTableStatement sqlStatement = new PostgreSQLAlterTableStatement();
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidatorTest.java
index fb7c299d982..7db429ca14e 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidatorTest.java
@@ -17,11 +17,9 @@
package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateTableStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
-import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.route.context.RouteContext;
@@ -32,13 +30,8 @@ import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.ShardingTable;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
-import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateTableStatement;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
-import org.apache.shardingsphere.sql.parser.statement.mysql.ddl.MySQLCreateTableStatement;
-import org.apache.shardingsphere.sql.parser.statement.oracle.ddl.OracleCreateTableStatement;
import org.apache.shardingsphere.sql.parser.statement.postgresql.ddl.PostgreSQLCreateTableStatement;
-import org.apache.shardingsphere.sql.parser.statement.sql92.ddl.SQL92CreateTableStatement;
-import org.apache.shardingsphere.sql.parser.statement.sqlserver.ddl.SQLServerCreateTableStatement;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
@@ -52,7 +45,6 @@ import java.util.LinkedList;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
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;
@@ -68,71 +60,6 @@ class ShardingCreateTableStatementValidatorTest {
@Mock
private RouteContext routeContext;
- @Test
- void assertPreValidateCreateTableForMySQL() {
- MySQLCreateTableStatement sqlStatement = new MySQLCreateTableStatement(false);
- sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
- assertThrows(TableExistsException.class, () -> assertPreValidateCreateTable(sqlStatement, "sharding_db"));
- }
-
- @Test
- void assertPreValidateCreateTableForOracle() {
- OracleCreateTableStatement sqlStatement = new OracleCreateTableStatement();
- sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
- assertThrows(TableExistsException.class, () -> assertPreValidateCreateTable(sqlStatement, "sharding_db"));
- }
-
- @Test
- void assertPreValidateCreateTableForPostgreSQL() {
- PostgreSQLCreateTableStatement sqlStatement = new PostgreSQLCreateTableStatement(false);
- sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
- assertThrows(TableExistsException.class, () -> assertPreValidateCreateTable(sqlStatement, "public"));
- }
-
- @Test
- void assertPreValidateCreateTableForSQL92() {
- SQL92CreateTableStatement sqlStatement = new SQL92CreateTableStatement();
- sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
- assertThrows(TableExistsException.class, () -> assertPreValidateCreateTable(sqlStatement, "sharding_db"));
- }
-
- @Test
- void assertPreValidateCreateTableForSQLServer() {
- SQLServerCreateTableStatement sqlStatement = new SQLServerCreateTableStatement();
- sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
- assertThrows(TableExistsException.class, () -> assertPreValidateCreateTable(sqlStatement, "dbo"));
- }
-
- private void assertPreValidateCreateTable(final CreateTableStatement sqlStatement, final String schemaName) {
- SQLStatementContext sqlStatementContext = new CreateTableStatementContext(sqlStatement, "sharding_db");
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
- when(database.getName()).thenReturn("sharding_db");
- when(database.getSchema(schemaName).containsTable("t_order")).thenReturn(true);
- new ShardingCreateTableStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database,
- mock(ConfigurationProperties.class));
- }
-
- @Test
- void assertPreValidateCreateTableIfNotExistsForMySQL() {
- MySQLCreateTableStatement sqlStatement = new MySQLCreateTableStatement(true);
- sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
- assertPreValidateCreateTableIfNotExists(sqlStatement);
- }
-
- @Test
- void assertPreValidateCreateTableIfNotExistsForPostgreSQL() {
- PostgreSQLCreateTableStatement sqlStatement = new PostgreSQLCreateTableStatement(true);
- sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
- assertPreValidateCreateTableIfNotExists(sqlStatement);
- }
-
- private void assertPreValidateCreateTableIfNotExists(final CreateTableStatement sqlStatement) {
- SQLStatementContext sqlStatementContext = new CreateTableStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
- new ShardingCreateTableStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database,
- mock(ConfigurationProperties.class));
- }
-
@Test
void assertPostValidateCreateTableWithSameRouteResultShardingTableForPostgreSQL() {
PostgreSQLCreateTableStatement sqlStatement = new PostgreSQLCreateTableStatement(false);
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidatorTest.java
index 02247500490..145bf13fd8b 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidatorTest.java
@@ -22,7 +22,6 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.sharding.exception.metadata.EngagedViewException;
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedCreateViewException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionsSegment;
@@ -45,7 +44,6 @@ import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -77,21 +75,6 @@ class ShardingCreateViewStatementValidatorTest {
when(routeContext.getRouteUnits().size()).thenReturn(2);
}
- @Test
- void assertPreValidateCreateView() {
- assertDoesNotThrow(() -> new ShardingCreateViewStatementValidator().preValidate(shardingRule, createViewStatementContext, mock(HintValueContext.class), Collections.emptyList(),
- mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class)));
- }
-
- @Test
- void assertPreValidateCreateViewWithException() {
- when(shardingRule.isShardingTable(any())).thenReturn(true);
- when(shardingRule.isAllBindingTables(any())).thenReturn(false);
- ConfigurationProperties props = mock(ConfigurationProperties.class);
- assertThrows(EngagedViewException.class, () -> new ShardingCreateViewStatementValidator().preValidate(shardingRule, createViewStatementContext, mock(HintValueContext.class),
- Collections.emptyList(), mock(ShardingSphereDatabase.class), props));
- }
-
@Test
void assertPostValidateCreateView() {
ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class);
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropIndexStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropIndexStatementValidatorTest.java
index 5053d854d10..17b1bc35f30 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropIndexStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropIndexStatementValidatorTest.java
@@ -27,7 +27,6 @@ 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.sharding.exception.connection.ShardingDDLRouteException;
-import org.apache.shardingsphere.sharding.exception.metadata.IndexNotExistedException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.ShardingTable;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexNameSegment;
@@ -62,33 +61,6 @@ class ShardingDropIndexStatementValidatorTest {
@Mock
private RouteContext routeContext;
- @Test
- void assertPreValidateDropIndexWhenIndexExistForPostgreSQL() {
- PostgreSQLDropIndexStatement sqlStatement = new PostgreSQLDropIndexStatement(false);
- sqlStatement.getIndexes().add(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
- sqlStatement.getIndexes().add(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index_new"))));
- ShardingSphereTable table = mock(ShardingSphereTable.class);
- when(database.getSchema("public").getAllTableNames()).thenReturn(Collections.singletonList("t_order"));
- when(database.getSchema("public").getTable("t_order")).thenReturn(table);
- when(table.containsIndex("t_order_index")).thenReturn(true);
- when(table.containsIndex("t_order_index_new")).thenReturn(true);
- assertDoesNotThrow(() -> new ShardingDropIndexStatementValidator().preValidate(shardingRule, new DropIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME),
- mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
- }
-
- @Test
- void assertPreValidateDropIndexWhenIndexNotExistForPostgreSQL() {
- PostgreSQLDropIndexStatement sqlStatement = new PostgreSQLDropIndexStatement(false);
- sqlStatement.getIndexes().add(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index"))));
- sqlStatement.getIndexes().add(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index_new"))));
- ShardingSphereTable table = mock(ShardingSphereTable.class);
- when(database.getSchema("public").getAllTableNames()).thenReturn(Collections.singletonList("t_order"));
- when(database.getSchema("public").getTable("t_order")).thenReturn(table);
- when(table.containsIndex("t_order_index")).thenReturn(false);
- assertThrows(IndexNotExistedException.class, () -> new ShardingDropIndexStatementValidator().preValidate(shardingRule, new DropIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME),
- mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
- }
-
@Test
void assertPostValidateDropIndexWithSameRouteResultShardingTableIndexForPostgreSQL() {
PostgreSQLDropIndexStatement sqlStatement = new PostgreSQLDropIndexStatement(false);
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidatorTest.java
index b18462653a2..cc249fc8a4a 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidatorTest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.ddl.DropTableStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
@@ -78,15 +77,12 @@ class ShardingDropTableStatementValidatorTest {
}
@Test
- void assertPreValidateDropTableForMySQL() {
+ void assertPostValidateWhenDropTableInUsedForMySQL() {
MySQLDropTableStatement sqlStatement = new MySQLDropTableStatement(false);
sqlStatement.getTables().add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_item"))));
- SQLStatementContext sqlStatementContext = new DropTableStatementContext(sqlStatement, "db_schema");
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn("db_schema");
when(database.getSchema("db_schema").containsTable("t_order_item")).thenReturn(true);
- ShardingDropTableStatementValidator validator = new ShardingDropTableStatementValidator();
- validator.preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class));
Collection<RouteUnit> routeUnits = new LinkedList<>();
RouteMapper dataSourceMapper = new RouteMapper("db", "db1");
Collection<RouteMapper> tableMapper = new LinkedList<>();
@@ -97,7 +93,8 @@ class ShardingDropTableStatementValidatorTest {
RouteContext routeContext = mock(RouteContext.class);
when(routeContext.getRouteUnits()).thenReturn(routeUnits);
assertThrows(InUsedTablesException.class,
- () -> validator.postValidate(shardingRule, new DropTableStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), new HintValueContext(), Collections.emptyList(), database,
+ () -> new ShardingDropTableStatementValidator().postValidate(shardingRule, new DropTableStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), new HintValueContext(),
+ Collections.emptyList(), database,
mock(ConfigurationProperties.class), routeContext));
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingRenameTableStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingRenameTableStatementValidatorTest.java
index fb113c6b010..4224543e7f1 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingRenameTableStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingRenameTableStatementValidatorTest.java
@@ -27,7 +27,6 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.sharding.exception.connection.ShardingDDLRouteException;
-import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.ShardingTable;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.table.RenameTableDefinitionSegment;
@@ -45,7 +44,6 @@ import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -55,24 +53,6 @@ class ShardingRenameTableStatementValidatorTest {
@Mock
private ShardingRule shardingRule;
- @Test
- void assertPreValidateShardingTable() {
- SQLStatementContext sqlStatementContext = createRenameTableStatementContext("t_order", "t_user_order");
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
- when(shardingRule.containsShardingTable(argThat(tableNames -> tableNames.contains("t_order") || tableNames.contains("t_user_order")))).thenReturn(true);
- assertThrows(UnsupportedShardingOperationException.class,
- () -> new ShardingRenameTableStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database,
- mock(ConfigurationProperties.class)));
- }
-
- @Test
- void assertPreValidateNormalCase() {
- SQLStatementContext sqlStatementContext = createRenameTableStatementContext("t_not_sharding_table", "t_not_sharding_table_new");
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
- assertDoesNotThrow(() -> new ShardingRenameTableStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database,
- mock(ConfigurationProperties.class)));
- }
-
@Test
void assertPostValidateDifferentRouteUnitsAndDataNodesSize() {
RouteContext routeContext = new RouteContext();
@@ -81,7 +61,7 @@ class ShardingRenameTableStatementValidatorTest {
when(shardingTable.getActualDataNodes()).thenReturn(Arrays.asList(mock(DataNode.class), mock(DataNode.class)));
when(shardingRule.getShardingTable("t_order")).thenReturn(shardingTable);
when(shardingRule.isShardingTable("t_order")).thenReturn(true);
- SQLStatementContext sqlStatementContext = createRenameTableStatementContext("t_order", "t_user_order");
+ SQLStatementContext sqlStatementContext = createRenameTableStatementContext();
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
ConfigurationProperties props = mock(ConfigurationProperties.class);
assertThrows(ShardingDDLRouteException.class,
@@ -96,18 +76,18 @@ class ShardingRenameTableStatementValidatorTest {
when(shardingTable.getActualDataNodes()).thenReturn(Collections.singletonList(mock(DataNode.class)));
when(shardingRule.getShardingTable("t_order")).thenReturn(shardingTable);
when(shardingRule.isShardingTable("t_order")).thenReturn(true);
- SQLStatementContext sqlStatementContext = createRenameTableStatementContext("t_order", "t_user_order");
+ SQLStatementContext sqlStatementContext = createRenameTableStatementContext();
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
ConfigurationProperties props = mock(ConfigurationProperties.class);
assertDoesNotThrow(() -> new ShardingRenameTableStatementValidator().postValidate(
shardingRule, sqlStatementContext, new HintValueContext(), Collections.emptyList(), database, props, routeContext));
}
- private SQLStatementContext createRenameTableStatementContext(final String originTableName, final String newTableName) {
+ private SQLStatementContext createRenameTableStatementContext() {
MySQLRenameTableStatement sqlStatement = new MySQLRenameTableStatement();
RenameTableDefinitionSegment renameTableDefinitionSegment = new RenameTableDefinitionSegment(0, 0);
- renameTableDefinitionSegment.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue(originTableName))));
- renameTableDefinitionSegment.setRenameTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue(newTableName))));
+ renameTableDefinitionSegment.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
+ renameTableDefinitionSegment.setRenameTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_user_order"))));
sqlStatement.getRenameTables().add(renameTableDefinitionSegment);
return new RenameTableStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidatorTest.java
index 855f66dafc2..36d2d13447f 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidatorTest.java
@@ -36,9 +36,6 @@ import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.exception.algorithm.DuplicateInsertDataRecordException;
-import org.apache.shardingsphere.sharding.exception.syntax.DMLWithMultipleShardingTablesException;
-import org.apache.shardingsphere.sharding.exception.syntax.InsertSelectTableViolationException;
-import org.apache.shardingsphere.sharding.exception.syntax.MissingGenerateKeyColumnWithInsertSelectException;
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedUpdatingShardingValueException;
import org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
@@ -94,13 +91,11 @@ class ShardingInsertStatementValidatorTest {
private ShardingSphereDatabase database;
@Test
- void assertPreValidateWhenInsertMultiTables() {
- InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertStatement());
- Collection<String> tableNames = sqlStatementContext.getTablesContext().getTableNames();
- when(shardingRule.isAllShardingTables(tableNames)).thenReturn(false);
- when(shardingRule.containsShardingTable(tableNames)).thenReturn(true);
- assertThrows(DMLWithMultipleShardingTablesException.class, () -> new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule, sqlStatementContext,
- mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
+ void assertPostValidateWhenInsertWithSingleRouting() {
+ SQLStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertStatement());
+ when(routeContext.isSingleRouting()).thenReturn(true);
+ assertDoesNotThrow(() -> new ShardingInsertStatementValidator(shardingConditions).postValidate(shardingRule, sqlStatementContext, new HintValueContext(),
+ Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext));
}
private InsertStatementContext createInsertStatementContext(final List<Object> params, final InsertStatement insertStatement) {
@@ -110,58 +105,6 @@ class ShardingInsertStatementValidatorTest {
return new InsertStatementContext(metaData, params, insertStatement, DefaultDatabase.LOGIC_NAME);
}
- @Test
- void assertPreValidateWhenInsertSelectWithoutKeyGenerateColumn() {
- when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
- when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(false);
- InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement());
- sqlStatementContext.getTablesContext().getTableNames().addAll(createSingleTablesContext().getTableNames());
- assertThrows(MissingGenerateKeyColumnWithInsertSelectException.class, () -> new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule, sqlStatementContext,
- mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
- }
-
- @Test
- void assertPreValidateWhenInsertSelectWithKeyGenerateColumn() {
- when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
- when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
- InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement());
- sqlStatementContext.getTablesContext().getTableNames().addAll(createSingleTablesContext().getTableNames());
- assertDoesNotThrow(() -> new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(),
- database, mock(ConfigurationProperties.class)));
- }
-
- @Test
- void assertPreValidateWhenInsertSelectWithoutBindingTables() {
- when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
- when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
- TablesContext multiTablesContext = createMultiTablesContext();
- when(shardingRule.isAllBindingTables(multiTablesContext.getTableNames())).thenReturn(false);
- when(shardingRule.containsShardingTable(multiTablesContext.getTableNames())).thenReturn(true);
- InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement());
- sqlStatementContext.getTablesContext().getTableNames().addAll(multiTablesContext.getTableNames());
- assertThrows(InsertSelectTableViolationException.class, () -> new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule, sqlStatementContext,
- mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)));
- }
-
- @Test
- void assertPreValidateWhenInsertSelectWithBindingTables() {
- when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
- when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
- TablesContext multiTablesContext = createMultiTablesContext();
- InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement());
- sqlStatementContext.getTablesContext().getTableNames().addAll(multiTablesContext.getTableNames());
- assertDoesNotThrow(() -> new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(),
- database, mock(ConfigurationProperties.class)));
- }
-
- @Test
- void assertPostValidateWhenInsertWithSingleRouting() {
- SQLStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertStatement());
- when(routeContext.isSingleRouting()).thenReturn(true);
- assertDoesNotThrow(() -> new ShardingInsertStatementValidator(shardingConditions).postValidate(shardingRule, sqlStatementContext, new HintValueContext(),
- Collections.emptyList(), database, mock(ConfigurationProperties.class), routeContext));
- }
-
@Test
void assertPostValidateWhenInsertWithBroadcastTable() {
SQLStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertStatement());
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingUpdateStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingUpdateStatementValidatorTest.java
index a1eb30f343f..c2856b2b1df 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingUpdateStatementValidatorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingUpdateStatementValidatorTest.java
@@ -27,7 +27,6 @@ import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
-import org.apache.shardingsphere.sharding.exception.syntax.DMLWithMultipleShardingTablesException;
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedUpdatingShardingValueException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.ShardingTable;
@@ -36,7 +35,6 @@ import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignmen
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.SetAssignmentSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.LiteralExpressionSegment;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.JoinTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.UpdateStatement;
@@ -50,7 +48,6 @@ import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Arrays;
-import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@@ -71,33 +68,6 @@ class ShardingUpdateStatementValidatorTest {
@Mock
private ShardingSphereDatabase database;
- @Test
- void assertPreValidateWhenUpdateSingleTable() {
- UpdateStatement updateStatement = createUpdateStatement();
- updateStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("user"))));
- UpdateStatementContext sqlStatementContext = new UpdateStatementContext(updateStatement, DefaultDatabase.LOGIC_NAME);
- Collection<String> tableNames = sqlStatementContext.getTablesContext().getTableNames();
- when(shardingRule.isAllShardingTables(tableNames)).thenReturn(true);
- when(shardingRule.containsShardingTable(tableNames)).thenReturn(true);
- assertDoesNotThrow(() -> new ShardingUpdateStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database,
- mock(ConfigurationProperties.class)));
- }
-
- @Test
- void assertPreValidateWhenUpdateMultipleTables() {
- UpdateStatement updateStatement = createUpdateStatement();
- JoinTableSegment joinTableSegment = new JoinTableSegment();
- joinTableSegment.setLeft(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("user"))));
- joinTableSegment.setRight(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("order"))));
- updateStatement.setTable(joinTableSegment);
- UpdateStatementContext sqlStatementContext = new UpdateStatementContext(updateStatement, DefaultDatabase.LOGIC_NAME);
- Collection<String> tableNames = sqlStatementContext.getTablesContext().getTableNames();
- when(shardingRule.isAllShardingTables(tableNames)).thenReturn(false);
- when(shardingRule.containsShardingTable(tableNames)).thenReturn(true);
- assertThrows(DMLWithMultipleShardingTablesException.class, () -> new ShardingUpdateStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class),
- Collections.emptyList(), database, mock(ConfigurationProperties.class)));
- }
-
@Test
void assertPostValidateWhenNotUpdateShardingColumn() {
UpdateStatementContext sqlStatementContext = new UpdateStatementContext(createUpdateStatement(), DefaultDatabase.LOGIC_NAME);
diff --git a/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/kernel/KernelProcessor.java b/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/kernel/KernelProcessor.java
index 46b4d24771a..73b98f00da3 100644
--- a/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/kernel/KernelProcessor.java
+++ b/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/kernel/KernelProcessor.java
@@ -56,6 +56,9 @@ public final class KernelProcessor {
}
private void check(final QueryContext queryContext) {
+ if (queryContext.getHintValueContext().isSkipMetadataValidate()) {
+ return;
+ }
ShardingSphereDatabase database = queryContext.getUsedDatabase();
new SupportedSQLCheckEngine().checkSQL(database.getRuleMetaData().getRules(), queryContext.getSqlStatementContext(), database);
}
diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/schema/SingleDropSchemaSupportedChecker.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/schema/SingleDropSchemaSupportedChecker.java
index 13f5e62bdc0..c8ab2bf4800 100644
--- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/schema/SingleDropSchemaSupportedChecker.java
+++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/schema/SingleDropSchemaSupportedChecker.java
@@ -17,7 +17,6 @@
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;
@@ -32,7 +31,6 @@ import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.Iden
/**
* Drop schema supported checker for single.
*/
-@HighFrequencyInvocation
public final class SingleDropSchemaSupportedChecker implements SupportedSQLChecker<SQLStatementContext, SingleRule> {
@Override
diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
index 1abf65ba2eb..846b763d678 100644
--- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
+++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/checker/sql/table/SingleDropTableSupportedChecker.java
@@ -17,9 +17,10 @@
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.binder.context.statement.ddl.DropTableStatementContext;
import org.apache.shardingsphere.infra.checker.SupportedSQLChecker;
+import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
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;
@@ -29,8 +30,7 @@ import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTab
/**
* Drop table supported checker for single.
*/
-@HighFrequencyInvocation
-public final class SingleDropTableSupportedChecker implements SupportedSQLChecker<SQLStatementContext, SingleRule> {
+public final class SingleDropTableSupportedChecker implements SupportedSQLChecker<DropTableStatementContext, SingleRule> {
@Override
public boolean isCheck(final SQLStatementContext sqlStatementContext) {
@@ -38,9 +38,7 @@ public final class SingleDropTableSupportedChecker implements SupportedSQLChecke
}
@Override
- public void check(final SingleRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final SQLStatementContext sqlStatementContext) {
- if (((DropTableStatement) sqlStatementContext.getSqlStatement()).isContainsCascade()) {
- throw new UnsupportedDropCascadeTableException();
- }
+ public void check(final SingleRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final DropTableStatementContext sqlStatementContext) {
+ ShardingSpherePreconditions.checkState(!sqlStatementContext.getSqlStatement().isContainsCascade(), UnsupportedDropCascadeTableException::new);
}
}
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
index 07814448f7b..5d7bc256836 100644
--- 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
@@ -17,8 +17,7 @@
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.binder.context.statement.ddl.DropTableStatementContext;
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;
@@ -59,9 +58,10 @@ class SingleDropTableSupportedCheckerTest {
return result;
}
- private SQLStatementContext createSQLStatementContext(final boolean containsCascade) {
+ private DropTableStatementContext createSQLStatementContext(final boolean containsCascade) {
PostgreSQLDropTableStatement dropSchemaStatement = mock(PostgreSQLDropTableStatement.class, RETURNS_DEEP_STUBS);
when(dropSchemaStatement.isContainsCascade()).thenReturn(containsCascade);
- return new UnknownSQLStatementContext(dropSchemaStatement);
+ when(dropSchemaStatement.getTables()).thenReturn(Collections.emptyList());
+ return new DropTableStatementContext(dropSchemaStatement, DefaultDatabase.LOGIC_NAME);
}
}