You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2020/07/29 02:49:32 UTC
[shardingsphere] branch master updated: move route validate to
ShardingStatementValidator (#6494)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 ac827d4 move route validate to ShardingStatementValidator (#6494)
ac827d4 is described below
commit ac827d4c2b85435a58f97fa6edd8497f4a83438b
Author: DuanZhengqiang <st...@gmail.com>
AuthorDate: Tue Jul 28 21:49:22 2020 -0500
move route validate to ShardingStatementValidator (#6494)
* move route validate to ShardingStatementValidator
* modify checkstyle
---
.../route/engine/ShardingRouteDecorator.java | 2 +-
.../engine/type/ShardingRouteEngineFactory.java | 16 +++----
.../type/complex/ShardingComplexRoutingEngine.java | 15 +++---
.../standard/ShardingStandardRoutingEngine.java | 31 +++----------
.../validator/ShardingStatementValidator.java | 9 ++--
.../ShardingStatementValidatorFactory.java | 5 ++
.../ShardingDeleteStatementValidator.java} | 32 ++++++-------
.../impl/ShardingInsertStatementValidator.java | 10 +++-
.../impl/ShardingUpdateStatementValidator.java | 9 +++-
.../complex/ShardingComplexRoutingEngineTest.java | 24 ++--------
.../ShardingStandardRoutingEngineTest.java | 35 ++------------
.../impl/ShardingDeleteStatementValidatorTest.java | 54 ++++++++++++++++++++++
.../impl/ShardingInsertStatementValidatorTest.java | 44 ++++++++++++++----
.../impl/ShardingUpdateStatementValidatorTest.java | 36 ++++++++++-----
14 files changed, 185 insertions(+), 137 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingRouteDecorator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingRouteDecorator.java
index 8cef1b1..560fb9b 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingRouteDecorator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingRouteDecorator.java
@@ -61,7 +61,7 @@ public final class ShardingRouteDecorator implements RouteDecorator<ShardingRule
List<Object> parameters = routeContext.getParameters();
SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
ShardingStatementValidatorFactory.newInstance(
- sqlStatement).ifPresent(validator -> validator.validate(shardingRule, sqlStatement, sqlStatementContext.getTablesContext(), parameters));
+ sqlStatement).ifPresent(validator -> validator.validate(shardingRule, sqlStatementContext, parameters));
ShardingConditions shardingConditions = getShardingConditions(parameters, sqlStatementContext, metaData.getSchema().getConfiguredSchemaMetaData(), shardingRule);
boolean needMergeShardingValues = isNeedMergeShardingValues(sqlStatementContext, shardingRule);
if (sqlStatement instanceof DMLStatement && needMergeShardingValues) {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java
index 47290e6..fb87afc 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java
@@ -19,7 +19,8 @@ package org.apache.shardingsphere.sharding.route.engine.type;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions;
import org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingDataSourceGroupBroadcastRoutingEngine;
import org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingDatabaseBroadcastRoutingEngine;
@@ -30,6 +31,7 @@ import org.apache.shardingsphere.sharding.route.engine.type.ignore.ShardingIgnor
import org.apache.shardingsphere.sharding.route.engine.type.standard.ShardingStandardRoutingEngine;
import org.apache.shardingsphere.sharding.route.engine.type.unconfigured.ShardingUnconfiguredTablesRoutingEngine;
import org.apache.shardingsphere.sharding.route.engine.type.unicast.ShardingUnicastRoutingEngine;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
@@ -44,8 +46,6 @@ import org.apache.shardingsphere.sql.parser.sql.statement.ddl.DDLStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.DMLStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.tcl.TCLStatement;
-import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import java.util.Collection;
import java.util.Map;
@@ -91,7 +91,7 @@ public final class ShardingRouteEngineFactory {
if (!shardingRule.tableRuleExists(tableNames)) {
return new ShardingUnconfiguredTablesRoutingEngine(tableNames, metaData.getSchema().getUnconfiguredSchemaMetaDataMap());
}
- return getShardingRoutingEngine(shardingRule, sqlStatementContext, shardingConditions, tableNames, props);
+ return getShardingRoutingEngine(shardingRule, shardingConditions, tableNames, props);
}
private static ShardingRouteEngine getDALRoutingEngine(final ShardingRule shardingRule,
@@ -125,13 +125,13 @@ public final class ShardingRouteEngineFactory {
return false;
}
- private static ShardingRouteEngine getShardingRoutingEngine(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext,
- final ShardingConditions shardingConditions, final Collection<String> tableNames, final ConfigurationProperties props) {
+ private static ShardingRouteEngine getShardingRoutingEngine(final ShardingRule shardingRule, final ShardingConditions shardingConditions,
+ final Collection<String> tableNames, final ConfigurationProperties props) {
Collection<String> shardingTableNames = shardingRule.getShardingLogicTableNames(tableNames);
if (1 == shardingTableNames.size() || shardingRule.isAllBindingTables(shardingTableNames)) {
- return new ShardingStandardRoutingEngine(shardingTableNames.iterator().next(), sqlStatementContext, shardingConditions, props);
+ return new ShardingStandardRoutingEngine(shardingTableNames.iterator().next(), shardingConditions, props);
}
// TODO config for cartesian set
- return new ShardingComplexRoutingEngine(tableNames, sqlStatementContext, shardingConditions, props);
+ return new ShardingComplexRoutingEngine(tableNames, shardingConditions, props);
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngine.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngine.java
index 9d5c8be..4ced012 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngine.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngine.java
@@ -18,15 +18,14 @@
package org.apache.shardingsphere.sharding.route.engine.type.complex;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sharding.rule.TableRule;
-import org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions;
-import org.apache.shardingsphere.sharding.route.engine.type.ShardingRouteEngine;
-import org.apache.shardingsphere.sharding.route.engine.type.standard.ShardingStandardRoutingEngine;
-import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.route.context.RouteResult;
+import org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions;
+import org.apache.shardingsphere.sharding.route.engine.type.ShardingRouteEngine;
+import org.apache.shardingsphere.sharding.route.engine.type.standard.ShardingStandardRoutingEngine;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sharding.rule.TableRule;
import java.util.ArrayList;
import java.util.Collection;
@@ -42,8 +41,6 @@ public final class ShardingComplexRoutingEngine implements ShardingRouteEngine {
private final Collection<String> logicTables;
- private final SQLStatementContext sqlStatementContext;
-
private final ShardingConditions shardingConditions;
private final ConfigurationProperties props;
@@ -56,7 +53,7 @@ public final class ShardingComplexRoutingEngine implements ShardingRouteEngine {
Optional<TableRule> tableRule = shardingRule.findTableRule(each);
if (tableRule.isPresent()) {
if (!bindingTableNames.contains(each)) {
- result.add(new ShardingStandardRoutingEngine(tableRule.get().getLogicTable(), sqlStatementContext, shardingConditions, props).route(shardingRule));
+ result.add(new ShardingStandardRoutingEngine(tableRule.get().getLogicTable(), shardingConditions, props).route(shardingRule));
}
shardingRule.findBindingTableRule(each).ifPresent(bindingTableRule -> bindingTableNames.addAll(
bindingTableRule.getTableRules().stream().map(TableRule::getLogicTable).collect(Collectors.toList())));
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngine.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngine.java
index 135f761..fb65f0a 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngine.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngine.java
@@ -19,6 +19,12 @@ package org.apache.shardingsphere.sharding.route.engine.type.standard;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.hint.HintManager;
+import org.apache.shardingsphere.infra.route.context.RouteMapper;
+import org.apache.shardingsphere.infra.route.context.RouteResult;
+import org.apache.shardingsphere.infra.route.context.RouteUnit;
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.type.ShardingRouteEngine;
@@ -29,18 +35,6 @@ import org.apache.shardingsphere.sharding.strategy.ShardingStrategy;
import org.apache.shardingsphere.sharding.strategy.hint.HintShardingStrategy;
import org.apache.shardingsphere.sharding.strategy.value.ListRouteValue;
import org.apache.shardingsphere.sharding.strategy.value.RouteValue;
-import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.sql.parser.binder.statement.dml.DeleteStatementContext;
-import org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext;
-import org.apache.shardingsphere.sql.parser.binder.statement.dml.UpdateStatementContext;
-import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
-import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import org.apache.shardingsphere.infra.datanode.DataNode;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
-import org.apache.shardingsphere.infra.hint.HintManager;
-import org.apache.shardingsphere.infra.route.context.RouteMapper;
-import org.apache.shardingsphere.infra.route.context.RouteResult;
-import org.apache.shardingsphere.infra.route.context.RouteUnit;
import java.util.ArrayList;
import java.util.Collection;
@@ -58,8 +52,6 @@ public final class ShardingStandardRoutingEngine implements ShardingRouteEngine
private final String logicTableName;
- private final SQLStatementContext sqlStatementContext;
-
private final ShardingConditions shardingConditions;
private final ConfigurationProperties properties;
@@ -68,20 +60,9 @@ public final class ShardingStandardRoutingEngine implements ShardingRouteEngine
@Override
public RouteResult route(final ShardingRule shardingRule) {
- if (isDMLForModify(sqlStatementContext) && !containsInsertSelect(sqlStatementContext) && 1 != ((TableAvailable) sqlStatementContext).getAllTables().size()) {
- throw new ShardingSphereException("Cannot support Multiple-Table for '%s'.", sqlStatementContext.getSqlStatement());
- }
return generateRouteResult(getDataNodes(shardingRule, shardingRule.getTableRule(logicTableName)));
}
- private boolean isDMLForModify(final SQLStatementContext sqlStatementContext) {
- return sqlStatementContext instanceof InsertStatementContext || sqlStatementContext instanceof UpdateStatementContext || sqlStatementContext instanceof DeleteStatementContext;
- }
-
- private boolean containsInsertSelect(final SQLStatementContext sqlStatementContext) {
- return sqlStatementContext instanceof InsertStatementContext && null != ((InsertStatementContext) sqlStatementContext).getInsertSelectContext();
- }
-
private RouteResult generateRouteResult(final Collection<DataNode> routedDataNodes) {
RouteResult result = new RouteResult();
result.getOriginalDataNodes().addAll(originalDataNodes);
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java
index e4cd5a6..8db86ee 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.sharding.route.engine.validator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import java.util.List;
@@ -34,9 +34,8 @@ public interface ShardingStatementValidator<T extends SQLStatement> {
* Validate whether sharding operation is supported.
*
* @param shardingRule sharding rule
- * @param sqlStatement SQL statement
- * @param tablesContext table context
- * @param parameters SQL parameters
+ * @param sqlStatementContext SQL statement context
+ * @param parameters SQL parameters
*/
- void validate(ShardingRule shardingRule, T sqlStatement, TablesContext tablesContext, List<Object> parameters);
+ void validate(ShardingRule shardingRule, SQLStatementContext<T> sqlStatementContext, List<Object> parameters);
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidatorFactory.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidatorFactory.java
index 54ff21f..42179dc 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidatorFactory.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidatorFactory.java
@@ -19,9 +19,11 @@ package org.apache.shardingsphere.sharding.route.engine.validator;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.sharding.route.engine.validator.impl.ShardingDeleteStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.impl.ShardingInsertStatementValidator;
import org.apache.shardingsphere.sharding.route.engine.validator.impl.ShardingUpdateStatementValidator;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
+import org.apache.shardingsphere.sql.parser.sql.statement.dml.DeleteStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.UpdateStatement;
@@ -46,6 +48,9 @@ public final class ShardingStatementValidatorFactory {
if (sqlStatement instanceof UpdateStatement) {
return Optional.of(new ShardingUpdateStatementValidator());
}
+ if (sqlStatement instanceof DeleteStatement) {
+ return Optional.of(new ShardingDeleteStatementValidator());
+ }
return Optional.empty();
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidator.java
similarity index 50%
copy from shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java
copy to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidator.java
index e4cd5a6..a0fdd6d 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidator.java
@@ -15,28 +15,26 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.route.engine.validator;
+package org.apache.shardingsphere.sharding.route.engine.validator.impl;
+import org.apache.shardingsphere.infra.exception.ShardingSphereException;
+import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext;
-import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
+import org.apache.shardingsphere.sql.parser.sql.statement.dml.DeleteStatement;
import java.util.List;
/**
- * Sharding statement validator.
- *
- * @param <T> type of SQL statement
+ * Sharding delete statement validator.
*/
-public interface ShardingStatementValidator<T extends SQLStatement> {
-
- /**
- * Validate whether sharding operation is supported.
- *
- * @param shardingRule sharding rule
- * @param sqlStatement SQL statement
- * @param tablesContext table context
- * @param parameters SQL parameters
- */
- void validate(ShardingRule shardingRule, T sqlStatement, TablesContext tablesContext, List<Object> parameters);
+public final class ShardingDeleteStatementValidator implements ShardingStatementValidator<DeleteStatement> {
+
+ @Override
+ public void validate(final ShardingRule shardingRule, final SQLStatementContext<DeleteStatement> sqlStatementContext, final List<Object> parameters) {
+ if (1 != ((TableAvailable) sqlStatementContext).getAllTables().size()) {
+ throw new ShardingSphereException("Cannot support Multiple-Table for '%s'.", sqlStatementContext.getSqlStatement());
+ }
+ }
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidator.java
index 1712ecd..a598a45 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidator.java
@@ -21,6 +21,9 @@ import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.OnDuplicateKeyColumnsSegment;
@@ -37,7 +40,11 @@ import java.util.Optional;
public final class ShardingInsertStatementValidator implements ShardingStatementValidator<InsertStatement> {
@Override
- public void validate(final ShardingRule shardingRule, final InsertStatement sqlStatement, final TablesContext tablesContext, final List<Object> parameters) {
+ public void validate(final ShardingRule shardingRule, final SQLStatementContext<InsertStatement> sqlStatementContext, final List<Object> parameters) {
+ if (null == ((InsertStatementContext) sqlStatementContext).getInsertSelectContext() && 1 != ((TableAvailable) sqlStatementContext).getAllTables().size()) {
+ throw new ShardingSphereException("Cannot support Multiple-Table for '%s'.", sqlStatementContext.getSqlStatement());
+ }
+ InsertStatement sqlStatement = sqlStatementContext.getSqlStatement();
Optional<OnDuplicateKeyColumnsSegment> onDuplicateKeyColumnsSegment = sqlStatement.getOnDuplicateKeyColumns();
String tableName = sqlStatement.getTable().getTableName().getIdentifier().getValue();
if (onDuplicateKeyColumnsSegment.isPresent() && isUpdateShardingKey(shardingRule, onDuplicateKeyColumnsSegment.get(), tableName)) {
@@ -48,6 +55,7 @@ public final class ShardingInsertStatementValidator implements ShardingStatement
&& !isContainsKeyGenerateColumn(shardingRule, sqlStatement.getColumns(), tableName)) {
throw new ShardingSphereException("INSERT INTO .... SELECT can not support applying keyGenerator to absent generateKeyColumn.");
}
+ TablesContext tablesContext = sqlStatementContext.getTablesContext();
if (insertSelectSegment.isPresent() && !isAllSameTables(tablesContext.getTableNames()) && !shardingRule.isAllBindingTables(tablesContext.getTableNames())) {
throw new ShardingSphereException("The table inserted and the table selected must be the same or bind tables.");
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidator.java
index 1ed8580..c33736b 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidator.java
@@ -20,7 +20,8 @@ package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.simple.LiteralExpressionSegment;
@@ -43,7 +44,11 @@ import java.util.Optional;
public final class ShardingUpdateStatementValidator implements ShardingStatementValidator<UpdateStatement> {
@Override
- public void validate(final ShardingRule shardingRule, final UpdateStatement sqlStatement, final TablesContext tablesContext, final List<Object> parameters) {
+ public void validate(final ShardingRule shardingRule, final SQLStatementContext<UpdateStatement> sqlStatementContext, final List<Object> parameters) {
+ if (1 != ((TableAvailable) sqlStatementContext).getAllTables().size()) {
+ throw new ShardingSphereException("Cannot support Multiple-Table for '%s'.", sqlStatementContext.getSqlStatement());
+ }
+ UpdateStatement sqlStatement = sqlStatementContext.getSqlStatement();
String tableName = sqlStatement.getTables().iterator().next().getTableName().getIdentifier().getValue();
for (AssignmentSegment each : sqlStatement.getSetAssignment().getAssignments()) {
String shardingColumn = each.getColumn().getIdentifier().getValue();
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngineTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngineTest.java
index 8aa53f7..cc48487 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngineTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngineTest.java
@@ -17,18 +17,12 @@
package org.apache.shardingsphere.sharding.route.engine.type.complex;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sharding.route.fixture.AbstractRoutingEngineTest;
-import org.apache.shardingsphere.sql.parser.binder.segment.select.groupby.GroupByContext;
-import org.apache.shardingsphere.sql.parser.binder.segment.select.orderby.OrderByContext;
-import org.apache.shardingsphere.sql.parser.binder.segment.select.pagination.PaginationContext;
-import org.apache.shardingsphere.sql.parser.binder.segment.select.projection.ProjectionsContext;
-import org.apache.shardingsphere.sql.parser.binder.statement.dml.SelectStatementContext;
-import org.apache.shardingsphere.sql.parser.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
+import org.apache.shardingsphere.sharding.route.fixture.AbstractRoutingEngineTest;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.junit.Test;
import java.util.ArrayList;
@@ -46,11 +40,7 @@ public final class ShardingComplexRoutingEngineTest extends AbstractRoutingEngin
@Test
public void assertRoutingForBindingTables() {
- SelectStatementContext selectStatementContext = new SelectStatementContext(new SelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
- new ProjectionsContext(0, 0, false, Collections.emptyList()),
- new PaginationContext(null, null, Collections.emptyList()));
- ShardingComplexRoutingEngine complexRoutingEngine = new ShardingComplexRoutingEngine(Arrays.asList("t_order", "t_order_item"), selectStatementContext,
+ ShardingComplexRoutingEngine complexRoutingEngine = new ShardingComplexRoutingEngine(Arrays.asList("t_order", "t_order_item"),
createShardingConditions("t_order"), new ConfigurationProperties(new Properties()));
RouteResult routeResult = complexRoutingEngine.route(createBindingShardingRule());
List<RouteUnit> routeUnits = new ArrayList<>(routeResult.getRouteUnits());
@@ -64,11 +54,7 @@ public final class ShardingComplexRoutingEngineTest extends AbstractRoutingEngin
@Test
public void assertRoutingForShardingTableJoinBroadcastTable() {
- SelectStatementContext selectStatementContext = new SelectStatementContext(new SelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
- new ProjectionsContext(0, 0, false, Collections.emptyList()),
- new PaginationContext(null, null, Collections.emptyList()));
- ShardingComplexRoutingEngine complexRoutingEngine = new ShardingComplexRoutingEngine(Arrays.asList("t_order", "t_config"), selectStatementContext,
+ ShardingComplexRoutingEngine complexRoutingEngine = new ShardingComplexRoutingEngine(Arrays.asList("t_order", "t_config"),
createShardingConditions("t_order"), new ConfigurationProperties(new Properties()));
RouteResult routeResult = complexRoutingEngine.route(createBroadcastShardingRule());
List<RouteUnit> routeUnits = new ArrayList<>(routeResult.getRouteUnits());
@@ -82,7 +68,7 @@ public final class ShardingComplexRoutingEngineTest extends AbstractRoutingEngin
@Test(expected = ShardingSphereException.class)
public void assertRoutingForNonLogicTable() {
- ShardingComplexRoutingEngine complexRoutingEngine = new ShardingComplexRoutingEngine(Collections.emptyList(), null,
+ ShardingComplexRoutingEngine complexRoutingEngine = new ShardingComplexRoutingEngine(Collections.emptyList(),
createShardingConditions("t_order"), new ConfigurationProperties(new Properties()));
complexRoutingEngine.route(mock(ShardingRule.class));
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngineTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngineTest.java
index f464d67..c6d3750 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngineTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngineTest.java
@@ -17,24 +17,12 @@
package org.apache.shardingsphere.sharding.route.engine.type.standard;
-import org.apache.shardingsphere.infra.hint.HintManager;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions;
-import org.apache.shardingsphere.sharding.route.fixture.AbstractRoutingEngineTest;
-import org.apache.shardingsphere.sql.parser.binder.segment.select.groupby.GroupByContext;
-import org.apache.shardingsphere.sql.parser.binder.segment.select.orderby.OrderByContext;
-import org.apache.shardingsphere.sql.parser.binder.segment.select.pagination.PaginationContext;
-import org.apache.shardingsphere.sql.parser.binder.segment.select.projection.ProjectionsContext;
-import org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext;
-import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext;
-import org.apache.shardingsphere.sql.parser.binder.statement.dml.SelectStatementContext;
-import org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement;
-import org.apache.shardingsphere.sql.parser.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
+import org.apache.shardingsphere.infra.hint.HintManager;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
+import org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions;
+import org.apache.shardingsphere.sharding.route.fixture.AbstractRoutingEngineTest;
import org.junit.After;
import org.junit.Test;
@@ -46,8 +34,6 @@ import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public final class ShardingStandardRoutingEngineTest extends AbstractRoutingEngineTest {
@@ -56,16 +42,6 @@ public final class ShardingStandardRoutingEngineTest extends AbstractRoutingEngi
HintManager.clear();
}
- @Test(expected = ShardingSphereException.class)
- public void assertRouteByUnsupported() {
- SQLStatementContext sqlStatementContext = mock(InsertStatementContext.class);
- when(sqlStatementContext.getSqlStatement()).thenReturn(new InsertStatement());
- TablesContext tablesContext = mock(TablesContext.class);
- when(sqlStatementContext.getTablesContext()).thenReturn(tablesContext);
- ShardingStandardRoutingEngine standardRoutingEngine = new ShardingStandardRoutingEngine(null, sqlStatementContext, null, new ConfigurationProperties(new Properties()));
- standardRoutingEngine.route(mock(ShardingRule.class));
- }
-
@Test
public void assertRouteByNonConditions() {
ShardingStandardRoutingEngine standardRoutingEngine = createShardingStandardRoutingEngine("t_order", new ShardingConditions(Collections.emptyList()));
@@ -189,9 +165,6 @@ public final class ShardingStandardRoutingEngineTest extends AbstractRoutingEngi
}
private ShardingStandardRoutingEngine createShardingStandardRoutingEngine(final String logicTableName, final ShardingConditions shardingConditions) {
- return new ShardingStandardRoutingEngine(logicTableName, new SelectStatementContext(new SelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
- new ProjectionsContext(0, 0, false, Collections.emptyList()),
- new PaginationContext(null, null, Collections.emptyList())), shardingConditions, new ConfigurationProperties(new Properties()));
+ return new ShardingStandardRoutingEngine(logicTableName, shardingConditions, new ConfigurationProperties(new Properties()));
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidatorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidatorTest.java
new file mode 100644
index 0000000..3ade40f
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidatorTest.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.route.engine.validator.impl;
+
+import org.apache.shardingsphere.infra.exception.ShardingSphereException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.dml.DeleteStatementContext;
+import org.apache.shardingsphere.sql.parser.sql.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.sql.statement.dml.DeleteStatement;
+import org.apache.shardingsphere.sql.parser.sql.value.identifier.IdentifierValue;
+import org.junit.Test;
+import org.mockito.Mock;
+
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+public class ShardingDeleteStatementValidatorTest {
+
+ @Mock
+ private ShardingRule shardingRule;
+
+ @Test(expected = ShardingSphereException.class)
+ public void assertValidateDeleteModifyMultiTables() {
+ DeleteStatement sqlStatement = new DeleteStatement();
+ sqlStatement.getTables().addAll(createMultiTablesContext().getTables());
+ SQLStatementContext<DeleteStatement> sqlStatementContext = new DeleteStatementContext(sqlStatement);
+ new ShardingDeleteStatementValidator().validate(shardingRule, sqlStatementContext, Collections.emptyList());
+ }
+
+ private TablesContext createMultiTablesContext() {
+ List<SimpleTableSegment> result = new LinkedList<>();
+ result.add(new SimpleTableSegment(0, 0, new IdentifierValue("user")));
+ result.add(new SimpleTableSegment(0, 0, new IdentifierValue("order")));
+ return new TablesContext(result);
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidatorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidatorTest.java
index db310c7..4ae7963 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidatorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidatorTest.java
@@ -19,13 +19,17 @@ package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
import org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.InsertColumnsSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.OnDuplicateKeyColumnsSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.subquery.SubquerySegment;
+import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ProjectionsSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.SelectStatement;
@@ -48,31 +52,44 @@ public final class ShardingInsertStatementValidatorTest {
@Mock
private ShardingRule shardingRule;
-
+
+ @Test(expected = ShardingSphereException.class)
+ public void assertValidateInsertModifyMultiTables() {
+ SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertStatement());
+ sqlStatementContext.getTablesContext().getTables().addAll(createMultiTablesContext().getTables());
+ new ShardingInsertStatementValidator().validate(shardingRule, sqlStatementContext, Collections.emptyList());
+ }
+
@Test
public void assertValidateOnDuplicateKeyWithoutShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(false);
- new ShardingInsertStatementValidator().validate(shardingRule, createInsertStatement(), createSingleTablesContext(), Collections.emptyList());
+ SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertStatement());
+ new ShardingInsertStatementValidator().validate(shardingRule, sqlStatementContext, Collections.emptyList());
}
@Test(expected = ShardingSphereException.class)
public void assertValidateOnDuplicateKeyWithShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
- new ShardingInsertStatementValidator().validate(shardingRule, createInsertStatement(), createSingleTablesContext(), Collections.emptyList());
+ SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertStatement());
+ new ShardingInsertStatementValidator().validate(shardingRule, sqlStatementContext, Collections.emptyList());
}
@Test(expected = ShardingSphereException.class)
public void assertValidateInsertSelectWithoutKeyGenerateColumn() {
when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(false);
- new ShardingInsertStatementValidator().validate(shardingRule, createInsertStatement(), createSingleTablesContext(), Collections.emptyList());
+ SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertSelectStatement());
+ sqlStatementContext.getTablesContext().getTables().addAll(createSingleTablesContext().getTables());
+ new ShardingInsertStatementValidator().validate(shardingRule, sqlStatementContext, Collections.emptyList());
}
@Test
public void assertValidateInsertSelectWithKeyGenerateColumn() {
when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id"));
when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
- new ShardingInsertStatementValidator().validate(shardingRule, createInsertStatement(), createSingleTablesContext(), Collections.emptyList());
+ SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertSelectStatement());
+ sqlStatementContext.getTablesContext().getTables().addAll(createSingleTablesContext().getTables());
+ new ShardingInsertStatementValidator().validate(shardingRule, sqlStatementContext, Collections.emptyList());
}
@Test(expected = ShardingSphereException.class)
@@ -81,7 +98,9 @@ public final class ShardingInsertStatementValidatorTest {
when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
TablesContext multiTablesContext = createMultiTablesContext();
when(shardingRule.isAllBindingTables(multiTablesContext.getTableNames())).thenReturn(false);
- new ShardingInsertStatementValidator().validate(shardingRule, createInsertStatement(), multiTablesContext, Collections.emptyList());
+ SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertSelectStatement());
+ sqlStatementContext.getTablesContext().getTables().addAll(multiTablesContext.getTables());
+ new ShardingInsertStatementValidator().validate(shardingRule, sqlStatementContext, Collections.emptyList());
}
@Test
@@ -90,7 +109,9 @@ public final class ShardingInsertStatementValidatorTest {
when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
TablesContext multiTablesContext = createMultiTablesContext();
when(shardingRule.isAllBindingTables(multiTablesContext.getTableNames())).thenReturn(true);
- new ShardingInsertStatementValidator().validate(shardingRule, createInsertStatement(), multiTablesContext, Collections.emptyList());
+ SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertSelectStatement());
+ sqlStatementContext.getTablesContext().getTables().addAll(multiTablesContext.getTables());
+ new ShardingInsertStatementValidator().validate(shardingRule, sqlStatementContext, Collections.emptyList());
}
private InsertStatement createInsertStatement() {
@@ -102,7 +123,14 @@ public final class ShardingInsertStatementValidatorTest {
Collection<ColumnSegment> columns = new LinkedList<>();
columns.add(columnSegment);
result.setInsertColumns(new InsertColumnsSegment(0, 0, columns));
- result.setInsertSelect(new SubquerySegment(0, 0, new SelectStatement()));
+ return result;
+ }
+
+ private InsertStatement createInsertSelectStatement() {
+ InsertStatement result = createInsertStatement();
+ SelectStatement selectStatement = new SelectStatement();
+ selectStatement.setProjections(new ProjectionsSegment(0, 0));
+ result.setInsertSelect(new SubquerySegment(0, 0, selectStatement));
return result;
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidatorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidatorTest.java
index 078b6df..92beecd 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidatorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidatorTest.java
@@ -17,8 +17,11 @@
package org.apache.shardingsphere.sharding.route.engine.validator.impl;
+import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.dml.UpdateStatementContext;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.assignment.SetAssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.ColumnSegment;
@@ -31,7 +34,6 @@ import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.value.Pred
import org.apache.shardingsphere.sql.parser.sql.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.UpdateStatement;
import org.apache.shardingsphere.sql.parser.sql.value.identifier.IdentifierValue;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -40,6 +42,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.LinkedList;
import java.util.List;
import static org.mockito.Mockito.when;
@@ -49,38 +52,47 @@ public final class ShardingUpdateStatementValidatorTest {
@Mock
private ShardingRule shardingRule;
-
+
+ @Test(expected = ShardingSphereException.class)
+ public void assertValidateUpdateModifyMultiTables() {
+ SQLStatementContext<UpdateStatement> sqlStatementContext = new UpdateStatementContext(createUpdateStatement());
+ sqlStatementContext.getTablesContext().getTables().addAll(createMultiTablesContext().getTables());
+ new ShardingUpdateStatementValidator().validate(shardingRule, sqlStatementContext, Collections.emptyList());
+ }
+
@Test
public void assertValidateUpdateWithoutShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(false);
- new ShardingUpdateStatementValidator().validate(shardingRule, createUpdateStatement(), createTablesContext(), Collections.emptyList());
+ new ShardingUpdateStatementValidator().validate(shardingRule, new UpdateStatementContext(createUpdateStatement()), Collections.emptyList());
}
@Test(expected = ShardingSphereException.class)
public void assertValidateUpdateWithShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
- new ShardingUpdateStatementValidator().validate(shardingRule, createUpdateStatement(), createTablesContext(), Collections.emptyList());
+ new ShardingUpdateStatementValidator().validate(shardingRule, new UpdateStatementContext(createUpdateStatement()), Collections.emptyList());
}
@Test
public void assertValidateUpdateWithoutShardingKeyAndParameters() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(false);
List<Object> parameters = Arrays.asList(1, 1);
- new ShardingUpdateStatementValidator().validate(shardingRule, createUpdateStatement(), createTablesContext(), parameters);
+ new ShardingUpdateStatementValidator().validate(shardingRule, new UpdateStatementContext(createUpdateStatement()), parameters);
}
@Test
public void assertValidateUpdateWithShardingKeyAndShardingParameterEquals() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
List<Object> parameters = Arrays.asList(1, 1);
- new ShardingUpdateStatementValidator().validate(shardingRule, createUpdateStatementAndParameters(1), createTablesContext(), parameters);
+ SQLStatementContext<UpdateStatement> updateStatementContext = new UpdateStatementContext(createUpdateStatementAndParameters(1));
+ new ShardingUpdateStatementValidator().validate(shardingRule, updateStatementContext, parameters);
}
@Test(expected = ShardingSphereException.class)
public void assertValidateUpdateWithShardingKeyAndShardingParameterNotEquals() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
List<Object> parameters = Arrays.asList(1, 1);
- new ShardingUpdateStatementValidator().validate(shardingRule, createUpdateStatementAndParameters(2), createTablesContext(), parameters);
+ SQLStatementContext<UpdateStatement> updateStatementContext = new UpdateStatementContext(createUpdateStatementAndParameters(2));
+ new ShardingUpdateStatementValidator().validate(shardingRule, updateStatementContext, parameters);
}
private UpdateStatement createUpdateStatement() {
@@ -105,9 +117,11 @@ public final class ShardingUpdateStatementValidatorTest {
result.setWhere(where);
return result;
}
-
- private TablesContext createTablesContext() {
- SimpleTableSegment tableSegment = new SimpleTableSegment(0, 0, new IdentifierValue("user"));
- return new TablesContext(tableSegment);
+
+ private TablesContext createMultiTablesContext() {
+ List<SimpleTableSegment> result = new LinkedList<>();
+ result.add(new SimpleTableSegment(0, 0, new IdentifierValue("user")));
+ result.add(new SimpleTableSegment(0, 0, new IdentifierValue("order")));
+ return new TablesContext(result);
}
}