提交 1a443379 编写于 作者: X xiyelife 提交者: Liang Zhang

bugfix for #3144 (#3205)

* bugfix for #3144

* bugfix for #3144

* fix for junit test checksytle

* modify by code revirew

* org.apache.shardingsphere.core.optimize.segment.table.TablesContext

* bugfix for #3144 #3205

* apache/incubator-shardingsphere

* modify by code view

* fix for unit checksytle

* fix for PR:
https://github.com/apache/incubator-shardingsphere/pull/3205/files/3d5740a75ff265239d4fa02cf107a73799cb7a37#diff-da4509990765c2640ef290335b3c7bba

* fix for code view:
https://github.com/apache/incubator-shardingsphere/pull/3205/files/4d9f1021601bc0a15a3ec0399ad9f3b74d0d5737
上级 f43529f9
...@@ -92,7 +92,7 @@ public final class ShardingRouter { ...@@ -92,7 +92,7 @@ public final class ShardingRouter {
public SQLRouteResult route(final String logicSQL, final List<Object> parameters, final SQLStatement sqlStatement) { public SQLRouteResult route(final String logicSQL, final List<Object> parameters, final SQLStatement sqlStatement) {
Optional<ShardingStatementValidator> shardingStatementValidator = ShardingStatementValidatorFactory.newInstance(sqlStatement); Optional<ShardingStatementValidator> shardingStatementValidator = ShardingStatementValidatorFactory.newInstance(sqlStatement);
if (shardingStatementValidator.isPresent()) { if (shardingStatementValidator.isPresent()) {
shardingStatementValidator.get().validate(shardingRule, sqlStatement); shardingStatementValidator.get().validate(shardingRule, sqlStatement, parameters);
} }
SQLStatementContext sqlStatementContext = SQLStatementContextFactory.newInstance(metaData.getTables(), logicSQL, parameters, sqlStatement); SQLStatementContext sqlStatementContext = SQLStatementContextFactory.newInstance(metaData.getTables(), logicSQL, parameters, sqlStatement);
Optional<GeneratedKey> generatedKey = sqlStatement instanceof InsertStatement Optional<GeneratedKey> generatedKey = sqlStatement instanceof InsertStatement
......
/* /*
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. * this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 * 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 not use this file except in compliance with
* the License. You may obtain a copy of the License at * the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.shardingsphere.core.route.router.sharding.validator; package org.apache.shardingsphere.core.route.router.sharding.validator;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.rule.ShardingRule; import org.apache.shardingsphere.core.rule.ShardingRule;
/** import java.util.List;
* Sharding statement validator.
* /**
* @author zhangliang * Sharding statement validator.
* *
* @param <T> type of SQL statement * @author zhangliang
*/ *
public interface ShardingStatementValidator<T extends SQLStatement> { * @param <T> type of SQL statement
*/
/** public interface ShardingStatementValidator<T extends SQLStatement> {
* Validate whether sharding operation is supported.
* /**
* @param shardingRule sharding rule * Validate whether sharding operation is supported.
* @param sqlStatement SQL statement *
*/ * @param shardingRule sharding rule
void validate(ShardingRule shardingRule, T sqlStatement); * @param sqlStatement SQL statement
} * @param parameters SQL parameters
*/
void validate(ShardingRule shardingRule, T sqlStatement, List<Object> parameters);
}
/* /*
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. * this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 * 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 not use this file except in compliance with
* the License. You may obtain a copy of the License at * the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.shardingsphere.core.route.router.sharding.validator.impl; package org.apache.shardingsphere.core.route.router.sharding.validator.impl;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import org.apache.shardingsphere.core.exception.ShardingException; import org.apache.shardingsphere.core.exception.ShardingException;
import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.column.OnDuplicateKeyColumnsSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.column.OnDuplicateKeyColumnsSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.core.route.router.sharding.validator.ShardingStatementValidator; import org.apache.shardingsphere.core.route.router.sharding.validator.ShardingStatementValidator;
import org.apache.shardingsphere.core.rule.ShardingRule; import org.apache.shardingsphere.core.rule.ShardingRule;
/** import java.util.List;
* Sharding insert statement validator.
* /**
* @author zhangliang * Sharding insert statement validator.
*/ *
public final class ShardingInsertStatementValidator implements ShardingStatementValidator<InsertStatement> { * @author zhangliang
*/
@Override public final class ShardingInsertStatementValidator implements ShardingStatementValidator<InsertStatement> {
public void validate(final ShardingRule shardingRule, final InsertStatement sqlStatement) {
Optional<OnDuplicateKeyColumnsSegment> onDuplicateKeyColumnsSegment = sqlStatement.findSQLSegment(OnDuplicateKeyColumnsSegment.class); @Override
if (onDuplicateKeyColumnsSegment.isPresent() && isUpdateShardingKey(shardingRule, onDuplicateKeyColumnsSegment.get(), sqlStatement.getTable().getTableName())) { public void validate(final ShardingRule shardingRule, final InsertStatement sqlStatement, final List<Object> parameters) {
throw new ShardingException("INSERT INTO .... ON DUPLICATE KEY UPDATE can not support update for sharding column."); Optional<OnDuplicateKeyColumnsSegment> onDuplicateKeyColumnsSegment = sqlStatement.findSQLSegment(OnDuplicateKeyColumnsSegment.class);
} if (onDuplicateKeyColumnsSegment.isPresent() && isUpdateShardingKey(shardingRule, onDuplicateKeyColumnsSegment.get(), sqlStatement.getTable().getTableName())) {
} throw new ShardingException("INSERT INTO .... ON DUPLICATE KEY UPDATE can not support update for sharding column.");
}
private boolean isUpdateShardingKey(final ShardingRule shardingRule, final OnDuplicateKeyColumnsSegment onDuplicateKeyColumnsSegment, final String tableName) { }
for (ColumnSegment each : onDuplicateKeyColumnsSegment.getColumns()) {
if (shardingRule.isShardingColumn(each.getName(), tableName)) { private boolean isUpdateShardingKey(final ShardingRule shardingRule, final OnDuplicateKeyColumnsSegment onDuplicateKeyColumnsSegment, final String tableName) {
return true; for (ColumnSegment each : onDuplicateKeyColumnsSegment.getColumns()) {
} if (shardingRule.isShardingColumn(each.getName(), tableName)) {
} return true;
return false; }
} }
} return false;
}
}
/* /*
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. * this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 * 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 not use this file except in compliance with
* the License. You may obtain a copy of the License at * the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.shardingsphere.core.route.router.sharding.validator.impl; package org.apache.shardingsphere.core.route.router.sharding.validator.impl;
import org.apache.shardingsphere.core.exception.ShardingException; import java.util.Collection;
import org.apache.shardingsphere.core.preprocessor.segment.table.TablesContext; import java.util.List;
import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dml.UpdateStatement; import org.apache.shardingsphere.core.exception.ShardingException;
import org.apache.shardingsphere.core.route.router.sharding.validator.ShardingStatementValidator; import org.apache.shardingsphere.core.preprocessor.segment.table.TablesContext;
import org.apache.shardingsphere.core.rule.ShardingRule; import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment;
/** import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment;
* Sharding update statement validator. import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
* import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.AndPredicate;
* @author zhangliang import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateSegment;
*/ import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.WhereSegment;
public final class ShardingUpdateStatementValidator implements ShardingStatementValidator<UpdateStatement> { import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateCompareRightValue;
import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateInRightValue;
@Override import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateRightValue;
public void validate(final ShardingRule shardingRule, final UpdateStatement sqlStatement) { import org.apache.shardingsphere.core.parse.sql.statement.dml.UpdateStatement;
String tableName = new TablesContext(sqlStatement).getSingleTableName(); import org.apache.shardingsphere.core.route.router.sharding.validator.ShardingStatementValidator;
for (AssignmentSegment each : sqlStatement.getSetAssignment().getAssignments()) { import org.apache.shardingsphere.core.rule.ShardingRule;
if (shardingRule.isShardingColumn(each.getColumn().getName(), tableName)) {
throw new ShardingException("Can not update sharding key, logic table: [%s], column: [%s].", tableName, each); import com.google.common.base.Optional;
}
} /**
} * Sharding update statement validator.
} *
* @author zhangliang
*/
public final class ShardingUpdateStatementValidator implements ShardingStatementValidator<UpdateStatement> {
@Override
public void validate(final ShardingRule shardingRule, final UpdateStatement sqlStatement, final List<Object> parameters) {
String tableName = new TablesContext(sqlStatement).getSingleTableName();
for (AssignmentSegment each : sqlStatement.getSetAssignment().getAssignments()) {
String shardingColumn = each.getColumn().getName();
if (shardingRule.isShardingColumn(shardingColumn, tableName)) {
Optional<Object> shardingColumnSetAssignmentValue = getShardingColumnSetAssignmentValue(each, parameters);
Optional<Object> shardingValue = Optional.absent();
Optional<WhereSegment> whereSegmentOptional = sqlStatement.getWhere();
if (whereSegmentOptional.isPresent()) {
shardingValue = getShardingValue(whereSegmentOptional.get(), parameters, shardingColumn);
}
if (shardingColumnSetAssignmentValue.isPresent() && shardingValue.isPresent() && shardingColumnSetAssignmentValue.get().equals(shardingValue.get())) {
continue;
}
throw new ShardingException("Can not update sharding key, logic table: [%s], column: [%s].", tableName, each);
}
}
}
private Optional<Object> getShardingColumnSetAssignmentValue(final AssignmentSegment assignmentSegment, final List<Object> parameters) {
ExpressionSegment segment = assignmentSegment.getValue();
int shardingSetAssignIndex = -1;
if (segment instanceof ParameterMarkerExpressionSegment) {
shardingSetAssignIndex = ((ParameterMarkerExpressionSegment) segment).getParameterMarkerIndex();
}
if (segment instanceof LiteralExpressionSegment) {
return Optional.of(((LiteralExpressionSegment) segment).getLiterals());
}
if (-1 == shardingSetAssignIndex || shardingSetAssignIndex > parameters.size() - 1) {
return Optional.absent();
}
return Optional.of(parameters.get(shardingSetAssignIndex));
}
private Optional<Object> getShardingValue(final WhereSegment whereSegment, final List<Object> parameters, final String shardingColumn) {
for (AndPredicate each : whereSegment.getAndPredicates()) {
return getShardingValue(each, parameters, shardingColumn);
}
return Optional.absent();
}
private Optional<Object> getShardingValue(final AndPredicate andPredicate, final List<Object> parameters, final String shardingColumn) {
for (PredicateSegment each : andPredicate.getPredicates()) {
if (!shardingColumn.equalsIgnoreCase(each.getColumn().getName())) {
continue;
}
PredicateRightValue rightValue = each.getRightValue();
if (rightValue instanceof PredicateCompareRightValue) {
ExpressionSegment segment = ((PredicateCompareRightValue) rightValue).getExpression();
return getPredicateCompareShardingValue(segment, parameters, shardingColumn);
}
if (rightValue instanceof PredicateInRightValue) {
Collection<ExpressionSegment> segments = ((PredicateInRightValue) rightValue).getSqlExpressions();
return getPredicateInShardingValue(segments, parameters, shardingColumn);
}
}
return Optional.absent();
}
private Optional<Object> getPredicateCompareShardingValue(final ExpressionSegment segment, final List<Object> parameters, final String shardingColumn) {
int shardingValueParameterMarkerIndex = -1;
if (segment instanceof ParameterMarkerExpressionSegment) {
shardingValueParameterMarkerIndex = ((ParameterMarkerExpressionSegment) segment).getParameterMarkerIndex();
if (-1 == shardingValueParameterMarkerIndex || shardingValueParameterMarkerIndex > parameters.size() - 1) {
return Optional.absent();
}
return Optional.of(parameters.get(shardingValueParameterMarkerIndex));
}
if (segment instanceof LiteralExpressionSegment) {
return Optional.of(((LiteralExpressionSegment) segment).getLiterals());
}
return Optional.absent();
}
private Optional<Object> getPredicateInShardingValue(final Collection<ExpressionSegment> segments, final List<Object> parameters, final String shardingColumn) {
int shardingColumnWhereIndex = -1;
for (ExpressionSegment each : segments) {
if (each instanceof ParameterMarkerExpressionSegment) {
shardingColumnWhereIndex = ((ParameterMarkerExpressionSegment) each).getParameterMarkerIndex();
if (-1 == shardingColumnWhereIndex || shardingColumnWhereIndex > parameters.size() - 1) {
continue;
}
return Optional.of(parameters.get(shardingColumnWhereIndex));
}
if (each instanceof LiteralExpressionSegment) {
return Optional.of(((LiteralExpressionSegment) each).getLiterals());
}
}
return Optional.absent();
}
}
...@@ -17,6 +17,10 @@ ...@@ -17,6 +17,10 @@
package org.apache.shardingsphere.core.route.router.sharding.validator.impl; package org.apache.shardingsphere.core.route.router.sharding.validator.impl;
import static org.mockito.Mockito.when;
import java.util.Collections;
import org.apache.shardingsphere.core.exception.ShardingException; import org.apache.shardingsphere.core.exception.ShardingException;
import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.column.OnDuplicateKeyColumnsSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.column.OnDuplicateKeyColumnsSegment;
...@@ -28,10 +32,6 @@ import org.junit.runner.RunWith; ...@@ -28,10 +32,6 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public final class ShardingInsertStatementValidatorTest { public final class ShardingInsertStatementValidatorTest {
...@@ -41,13 +41,13 @@ public final class ShardingInsertStatementValidatorTest { ...@@ -41,13 +41,13 @@ public final class ShardingInsertStatementValidatorTest {
@Test @Test
public void assertValidateOnDuplicateKeyWithoutShardingKey() { public void assertValidateOnDuplicateKeyWithoutShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(false); when(shardingRule.isShardingColumn("id", "user")).thenReturn(false);
new ShardingInsertStatementValidator().validate(shardingRule, createInsertStatement()); new ShardingInsertStatementValidator().validate(shardingRule, createInsertStatement(), Collections.emptyList());
} }
@Test(expected = ShardingException.class) @Test(expected = ShardingException.class)
public void assertValidateOnDuplicateKeyWithShardingKey() { public void assertValidateOnDuplicateKeyWithShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true); when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
new ShardingInsertStatementValidator().validate(shardingRule, createInsertStatement()); new ShardingInsertStatementValidator().validate(shardingRule, createInsertStatement(), Collections.emptyList());
} }
private InsertStatement createInsertStatement() { private InsertStatement createInsertStatement() {
......
/* /*
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. * this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 * 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 not use this file except in compliance with
* the License. You may obtain a copy of the License at * the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.shardingsphere.core.route.router.sharding.validator.impl; package org.apache.shardingsphere.core.route.router.sharding.validator.impl;
import org.apache.shardingsphere.core.exception.ShardingException; import org.apache.shardingsphere.core.exception.ShardingException;
import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.AssignmentSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.SetAssignmentsSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.SetAssignmentsSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.segment.generic.TableSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dml.UpdateStatement; import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.core.rule.ShardingRule; import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateSegment;
import org.junit.Test; import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.WhereSegment;
import org.junit.runner.RunWith; import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateCompareRightValue;
import org.mockito.Mock; import org.apache.shardingsphere.core.parse.sql.segment.generic.TableSegment;
import org.mockito.junit.MockitoJUnitRunner; import org.apache.shardingsphere.core.parse.sql.statement.dml.UpdateStatement;
import org.apache.shardingsphere.core.rule.ShardingRule;
import java.util.Collections; import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.Mockito.when; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public final class ShardingUpdateStatementValidatorTest { import java.util.Arrays;
import java.util.Collection;
@Mock import java.util.Collections;
private ShardingRule shardingRule; import java.util.List;
@Test import static org.mockito.Mockito.when;
public void assertValidateUpdateWithoutShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(false); @RunWith(MockitoJUnitRunner.class)
new ShardingUpdateStatementValidator().validate(shardingRule, createUpdateStatement()); public final class ShardingUpdateStatementValidatorTest {
}
@Mock
@Test(expected = ShardingException.class) private ShardingRule shardingRule;
public void assertValidateUpdateWithShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true); @Test
new ShardingUpdateStatementValidator().validate(shardingRule, createUpdateStatement()); public void assertValidateUpdateWithoutShardingKey() {
} when(shardingRule.isShardingColumn("id", "user")).thenReturn(false);
new ShardingUpdateStatementValidator().validate(shardingRule, createUpdateStatement(), Collections.emptyList());
private UpdateStatement createUpdateStatement() { }
UpdateStatement result = new UpdateStatement();
result.getAllSQLSegments().add(new TableSegment(0, 0, "user")); @Test(expected = ShardingException.class)
result.setSetAssignment(new SetAssignmentsSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, "id"), new LiteralExpressionSegment(0, 0, ""))))); public void assertValidateUpdateWithShardingKey() {
return result; when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
} new ShardingUpdateStatementValidator().validate(shardingRule, createUpdateStatement(), Collections.emptyList());
} }
@Test
public void assertValidateUpdateWithoutShardingKeyAndParameters() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(false);
List parameters = Arrays.asList(1, 1);
new ShardingUpdateStatementValidator().validate(shardingRule, createUpdateStatement(), parameters);
}
@Test
public void assertValidateUpdateWithShardingKeyAndShardingParameterEquals() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
List parameters = Arrays.asList(1, 1);
new ShardingUpdateStatementValidator().validate(shardingRule, createUpdateStatementAndParameters(1), parameters);
}
@Test(expected = ShardingException.class)
public void assertValidateUpdateWithShardingKeyAndShardingParameterNotEquals() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
List parameters = Arrays.asList(1, 1);
new ShardingUpdateStatementValidator().validate(shardingRule, createUpdateStatementAndParameters(2), parameters);
}
private UpdateStatement createUpdateStatement() {
UpdateStatement result = new UpdateStatement();
result.getAllSQLSegments().add(new TableSegment(0, 0, "user"));
result.setSetAssignment(new SetAssignmentsSegment(0, 0, Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, "id"), new LiteralExpressionSegment(0, 0, "")))));
return result;
}
private UpdateStatement createUpdateStatementAndParameters(final Object shardingColumnPatamater) {
UpdateStatement result = new UpdateStatement();
result.getAllSQLSegments().add(new TableSegment(0, 0, "user"));
Collection<AssignmentSegment> assignments = Collections.singletonList(new AssignmentSegment(0, 0, new ColumnSegment(0, 0, "id"), new LiteralExpressionSegment(0, 0, shardingColumnPatamater)));
SetAssignmentsSegment setAssignmentsSegment = new SetAssignmentsSegment(0, 0, assignments);
result.setSetAssignment(setAssignmentsSegment);
WhereSegment where = new WhereSegment(0, 0, 1);
where.setParameterStartIndex(0);
AndPredicate andPre = new AndPredicate();
andPre.getPredicates().add(new PredicateSegment(0, 1, new ColumnSegment(0, 0, "id"), new PredicateCompareRightValue("=", new ParameterMarkerExpressionSegment(0, 0, 0))));
where.getAndPredicates().add(andPre);
result.setWhere(where);
return result;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册