未验证 提交 48ddf2b9 编写于 作者: J Juan Pan(Trista) 提交者: GitHub

Refactor ShardingDeleteStatementValidator to simplify the parameter list (#7033)

上级 665fd5ff
......@@ -62,7 +62,7 @@ public final class ShardingRouteDecorator implements RouteDecorator<ShardingRule
List<Object> parameters = routeContext.getParameters();
SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
Optional<ShardingStatementValidator> shardingStatementValidator = ShardingStatementValidatorFactory.newInstance(sqlStatement);
shardingStatementValidator.ifPresent(validator -> validator.preValidate(shardingRule, sqlStatementContext, parameters));
shardingStatementValidator.ifPresent(validator -> validator.preValidate(shardingRule, routeContext));
ShardingConditions shardingConditions = getShardingConditions(parameters, sqlStatementContext, metaData.getSchema().getConfiguredSchemaMetaData(), shardingRule);
boolean needMergeShardingValues = isNeedMergeShardingValues(sqlStatementContext, shardingRule);
if (sqlStatement instanceof DMLStatement && needMergeShardingValues) {
......
......@@ -17,13 +17,11 @@
package org.apache.shardingsphere.sharding.route.engine.validator;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import java.util.List;
/**
* Sharding statement validator.
*
......@@ -35,10 +33,9 @@ public interface ShardingStatementValidator<T extends SQLStatement> {
* Validate whether sharding operation is supported before route.
*
* @param shardingRule sharding rule
* @param sqlStatementContext SQL statement context
* @param parameters SQL parameters
* @param routeContext route context
*/
void preValidate(ShardingRule shardingRule, SQLStatementContext<T> sqlStatementContext, List<Object> parameters);
void preValidate(ShardingRule shardingRule, RouteContext routeContext);
/**
* Validate whether sharding operation is supported after route.
......
......@@ -18,6 +18,7 @@
package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
......@@ -26,15 +27,14 @@ import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.DeleteStatement;
import java.util.List;
/**
* Sharding delete statement validator.
*/
public final class ShardingDeleteStatementValidator implements ShardingStatementValidator<DeleteStatement> {
@Override
public void preValidate(final ShardingRule shardingRule, final SQLStatementContext<DeleteStatement> sqlStatementContext, final List<Object> parameters) {
public void preValidate(final ShardingRule shardingRule, final RouteContext routeContext) {
SQLStatementContext sqlStatementContext = routeContext.getSqlStatementContext();
if (1 != ((TableAvailable) sqlStatementContext).getAllTables().size()) {
throw new ShardingSphereException("Cannot support Multiple-Table for '%s'.", sqlStatementContext.getSqlStatement());
}
......
......@@ -18,6 +18,7 @@
package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
......@@ -33,7 +34,6 @@ import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
/**
......@@ -42,11 +42,12 @@ import java.util.Optional;
public final class ShardingInsertStatementValidator implements ShardingStatementValidator<InsertStatement> {
@Override
public void preValidate(final ShardingRule shardingRule, final SQLStatementContext<InsertStatement> sqlStatementContext, final List<Object> parameters) {
public void preValidate(final ShardingRule shardingRule, final RouteContext routeContext) {
SQLStatementContext sqlStatementContext = routeContext.getSqlStatementContext();
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();
InsertStatement sqlStatement = (InsertStatement) sqlStatementContext.getSqlStatement();
Optional<OnDuplicateKeyColumnsSegment> onDuplicateKeyColumnsSegment = sqlStatement.getOnDuplicateKeyColumns();
String tableName = sqlStatement.getTable().getTableName().getIdentifier().getValue();
if (onDuplicateKeyColumnsSegment.isPresent() && isUpdateShardingKey(shardingRule, onDuplicateKeyColumnsSegment.get(), tableName)) {
......
......@@ -18,6 +18,7 @@
package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
......@@ -46,20 +47,21 @@ import java.util.Optional;
public final class ShardingUpdateStatementValidator implements ShardingStatementValidator<UpdateStatement> {
@Override
public void preValidate(final ShardingRule shardingRule, final SQLStatementContext<UpdateStatement> sqlStatementContext, final List<Object> parameters) {
public void preValidate(final ShardingRule shardingRule, final RouteContext routeContext) {
SQLStatementContext sqlStatementContext = routeContext.getSqlStatementContext();
if (1 != ((TableAvailable) sqlStatementContext).getAllTables().size()) {
throw new ShardingSphereException("Cannot support Multiple-Table for '%s'.", sqlStatementContext.getSqlStatement());
}
UpdateStatement sqlStatement = sqlStatementContext.getSqlStatement();
UpdateStatement sqlStatement = (UpdateStatement) sqlStatementContext.getSqlStatement();
String tableName = sqlStatement.getTables().iterator().next().getTableName().getIdentifier().getValue();
for (AssignmentSegment each : sqlStatement.getSetAssignment().getAssignments()) {
String shardingColumn = each.getColumn().getIdentifier().getValue();
if (shardingRule.isShardingColumn(shardingColumn, tableName)) {
Optional<Object> shardingColumnSetAssignmentValue = getShardingColumnSetAssignmentValue(each, parameters);
Optional<Object> shardingColumnSetAssignmentValue = getShardingColumnSetAssignmentValue(each, routeContext.getParameters());
Optional<Object> shardingValue = Optional.empty();
Optional<WhereSegment> whereSegmentOptional = sqlStatement.getWhere();
if (whereSegmentOptional.isPresent()) {
shardingValue = getShardingValue(whereSegmentOptional.get(), parameters, shardingColumn);
shardingValue = getShardingValue(whereSegmentOptional.get(), routeContext.getParameters(), shardingColumn);
}
if (shardingColumnSetAssignmentValue.isPresent() && shardingValue.isPresent() && shardingColumnSetAssignmentValue.get().equals(shardingValue.get())) {
continue;
......
......@@ -18,6 +18,8 @@
package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
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;
......@@ -42,7 +44,8 @@ public final class ShardingDeleteStatementValidatorTest {
DeleteStatement sqlStatement = new DeleteStatement();
sqlStatement.getTables().addAll(createMultiTablesContext().getTables());
SQLStatementContext<DeleteStatement> sqlStatementContext = new DeleteStatementContext(sqlStatement);
new ShardingDeleteStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList());
RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList(), new RouteResult());
new ShardingDeleteStatementValidator().preValidate(shardingRule, routeContext);
}
private TablesContext createMultiTablesContext() {
......
......@@ -18,6 +18,8 @@
package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
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;
......@@ -57,21 +59,24 @@ public final class ShardingInsertStatementValidatorTest {
public void assertValidateInsertModifyMultiTables() {
SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertStatement());
sqlStatementContext.getTablesContext().getTables().addAll(createMultiTablesContext().getTables());
new ShardingInsertStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList());
RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList(), new RouteResult());
new ShardingInsertStatementValidator().preValidate(shardingRule, routeContext);
}
@Test
public void assertValidateOnDuplicateKeyWithoutShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(false);
SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertStatement());
new ShardingInsertStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList());
RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList(), new RouteResult());
new ShardingInsertStatementValidator().preValidate(shardingRule, routeContext);
}
@Test(expected = ShardingSphereException.class)
public void assertValidateOnDuplicateKeyWithShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertStatement());
new ShardingInsertStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList());
RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList(), new RouteResult());
new ShardingInsertStatementValidator().preValidate(shardingRule, routeContext);
}
@Test(expected = ShardingSphereException.class)
......@@ -80,7 +85,8 @@ public final class ShardingInsertStatementValidatorTest {
when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(false);
SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertSelectStatement());
sqlStatementContext.getTablesContext().getTables().addAll(createSingleTablesContext().getTables());
new ShardingInsertStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList());
RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList(), new RouteResult());
new ShardingInsertStatementValidator().preValidate(shardingRule, routeContext);
}
@Test
......@@ -89,7 +95,8 @@ public final class ShardingInsertStatementValidatorTest {
when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertSelectStatement());
sqlStatementContext.getTablesContext().getTables().addAll(createSingleTablesContext().getTables());
new ShardingInsertStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList());
RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList(), new RouteResult());
new ShardingInsertStatementValidator().preValidate(shardingRule, routeContext);
}
@Test(expected = ShardingSphereException.class)
......@@ -100,7 +107,8 @@ public final class ShardingInsertStatementValidatorTest {
when(shardingRule.isAllBindingTables(multiTablesContext.getTableNames())).thenReturn(false);
SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertSelectStatement());
sqlStatementContext.getTablesContext().getTables().addAll(multiTablesContext.getTables());
new ShardingInsertStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList());
RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList(), new RouteResult());
new ShardingInsertStatementValidator().preValidate(shardingRule, routeContext);
}
@Test
......@@ -111,7 +119,8 @@ public final class ShardingInsertStatementValidatorTest {
when(shardingRule.isAllBindingTables(multiTablesContext.getTableNames())).thenReturn(true);
SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertSelectStatement());
sqlStatementContext.getTablesContext().getTables().addAll(multiTablesContext.getTables());
new ShardingInsertStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList());
RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList(), new RouteResult());
new ShardingInsertStatementValidator().preValidate(shardingRule, routeContext);
}
private InsertStatement createInsertStatement() {
......
......@@ -18,6 +18,8 @@
package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
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;
......@@ -57,26 +59,30 @@ public final class ShardingUpdateStatementValidatorTest {
public void assertValidateUpdateModifyMultiTables() {
SQLStatementContext<UpdateStatement> sqlStatementContext = new UpdateStatementContext(createUpdateStatement());
sqlStatementContext.getTablesContext().getTables().addAll(createMultiTablesContext().getTables());
new ShardingUpdateStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList());
RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList(), new RouteResult());
new ShardingUpdateStatementValidator().preValidate(shardingRule, routeContext);
}
@Test
public void assertValidateUpdateWithoutShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(false);
new ShardingUpdateStatementValidator().preValidate(shardingRule, new UpdateStatementContext(createUpdateStatement()), Collections.emptyList());
RouteContext routeContext = new RouteContext(new UpdateStatementContext(createUpdateStatement()), Collections.emptyList(), new RouteResult());
new ShardingUpdateStatementValidator().preValidate(shardingRule, routeContext);
}
@Test(expected = ShardingSphereException.class)
public void assertValidateUpdateWithShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
new ShardingUpdateStatementValidator().preValidate(shardingRule, new UpdateStatementContext(createUpdateStatement()), Collections.emptyList());
RouteContext routeContext = new RouteContext(new UpdateStatementContext(createUpdateStatement()), Collections.emptyList(), new RouteResult());
new ShardingUpdateStatementValidator().preValidate(shardingRule, routeContext);
}
@Test
public void assertValidateUpdateWithoutShardingKeyAndParameters() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(false);
List<Object> parameters = Arrays.asList(1, 1);
new ShardingUpdateStatementValidator().preValidate(shardingRule, new UpdateStatementContext(createUpdateStatement()), parameters);
RouteContext routeContext = new RouteContext(new UpdateStatementContext(createUpdateStatement()), parameters, new RouteResult());
new ShardingUpdateStatementValidator().preValidate(shardingRule, routeContext);
}
@Test
......@@ -84,7 +90,8 @@ public final class ShardingUpdateStatementValidatorTest {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
List<Object> parameters = Arrays.asList(1, 1);
SQLStatementContext<UpdateStatement> updateStatementContext = new UpdateStatementContext(createUpdateStatementAndParameters(1));
new ShardingUpdateStatementValidator().preValidate(shardingRule, updateStatementContext, parameters);
RouteContext routeContext = new RouteContext(updateStatementContext, parameters, new RouteResult());
new ShardingUpdateStatementValidator().preValidate(shardingRule, routeContext);
}
@Test(expected = ShardingSphereException.class)
......@@ -92,7 +99,8 @@ public final class ShardingUpdateStatementValidatorTest {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
List<Object> parameters = Arrays.asList(1, 1);
SQLStatementContext<UpdateStatement> updateStatementContext = new UpdateStatementContext(createUpdateStatementAndParameters(2));
new ShardingUpdateStatementValidator().preValidate(shardingRule, updateStatementContext, parameters);
RouteContext routeContext = new RouteContext(updateStatementContext, parameters, new RouteResult());
new ShardingUpdateStatementValidator().preValidate(shardingRule, routeContext);
}
private UpdateStatement createUpdateStatement() {
......
......@@ -21,6 +21,8 @@ import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
/**
......@@ -45,4 +47,17 @@ public final class RuleSchemaMetaData {
result.merge(configuredSchemaMetaData);
return result;
}
/**
* Get all table names.
*
* @return all table names
*/
public Collection<String> getAllTableNames() {
Collection<String> result = new LinkedList<>(configuredSchemaMetaData.getAllTableNames());
for (SchemaMetaData each : unconfiguredSchemaMetaDataMap.values()) {
result.addAll(each.getAllTableNames());
}
return result;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册