strongduanmu commented on issue #7703:
URL: https://github.com/apache/shardingsphere/issues/7703#issuecomment-736588951
> Thanks for your feedback. @heqiao @sabz90
> I read the conversation above, and gave my thinking here.
>
> First, the annotation `@Column(name = "xxx", updatable = false)` seems one
solution to avoid `SET shardingKey=xxx`and the exception `Can not update
sharding key` accordingly.
>
> Second, how to understand `But this will execute the update query in all
the shards :) Which is not optimal.`?
> Does it mean that if `@Column(name = "xxx", updatable = false)` is added,
the `where clause` will not contain `shardingKey=xx`, which will cause all
shards updating?
>
> Third, Do you think the following improvement is reasonable and in your
expectation?
>
> ```java
> if (no shardingKey in SET Clause)
> then PASS
> else if (the value of shardingKey in SET Clause == the value of
shardingKey in WHERE Clause)
> then PASS
> else
> then an exception
> ```
@tristaZero @heqiao I have checked the logic in
`ShardingUpdateStatementValidator`, whether it is version `4.1.1` or version
`5.0.0-alpha`, it has already supported the verification logic of whether the
sharding key is the same in the `SET` and `WHERE` clauses. The code is as
follows:
```java
/**
* Sharding update statement validator.
*/
public final class ShardingUpdateStatementValidator implements
ShardingStatementValidator<UpdateStatement> {
@Override
public void validate(final ShardingRule shardingRule, final
UpdateStatement sqlStatement, final List<Object> parameters) {
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> shardingValue = Optional.empty();
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 ShardingSphereException("Can not update sharding
key, logic table: [%s], column: [%s].", tableName, each);
}
}
}
}
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]