You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2020/10/03 16:09:19 UTC
[shardingsphere] branch master updated: Refactor
InsertClauseShardingConditionEngine (#7693)
This is an automated email from the ASF dual-hosted git repository.
menghaoran 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 732dab6 Refactor InsertClauseShardingConditionEngine (#7693)
732dab6 is described below
commit 732dab61bba3d3b186123b2c75a480a1cc41f0f4
Author: Liang Zhang <te...@163.com>
AuthorDate: Sun Oct 4 00:09:02 2020 +0800
Refactor InsertClauseShardingConditionEngine (#7693)
---
.../impl/InsertClauseShardingConditionEngine.java | 39 ++++++++++++++--------
1 file changed, 25 insertions(+), 14 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
index 8cc6bb1..9e44990 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
@@ -57,24 +57,19 @@ public final class InsertClauseShardingConditionEngine implements ShardingCondit
@Override
public List<ShardingCondition> createShardingConditions(final InsertStatementContext sqlStatementContext, final List<Object> parameters) {
+ List<ShardingCondition> result = null == sqlStatementContext.getInsertSelectContext()
+ ? createShardingConditionsWithInsertValues(sqlStatementContext, parameters) : createShardingConditionsWithInsertSelect(sqlStatementContext, parameters);
+ appendGeneratedKeyConditions(sqlStatementContext, result);
+ return result;
+ }
+
+ private List<ShardingCondition> createShardingConditionsWithInsertValues(final InsertStatementContext sqlStatementContext, final List<Object> parameters) {
List<ShardingCondition> result = new LinkedList<>();
String tableName = sqlStatementContext.getSqlStatement().getTable().getTableName().getIdentifier().getValue();
Collection<String> columnNames = getColumnNames(sqlStatementContext);
for (InsertValueContext each : sqlStatementContext.getInsertValueContexts()) {
result.add(createShardingCondition(tableName, columnNames.iterator(), each, parameters));
}
- if (null != sqlStatementContext.getInsertSelectContext()) {
- SelectStatementContext selectStatementContext = sqlStatementContext.getInsertSelectContext().getSelectStatementContext();
- List<ShardingCondition> shardingConditions = new WhereClauseShardingConditionEngine(shardingRule, schemaMetaData).createShardingConditions(selectStatementContext, parameters);
- result.addAll(shardingConditions);
- }
- Optional<GeneratedKeyContext> generatedKey = sqlStatementContext.getGeneratedKeyContext();
- if (generatedKey.isPresent() && generatedKey.get().isGenerated()) {
- generatedKey.get().getGeneratedValues().addAll(getGeneratedKeys(tableName, sqlStatementContext.getValueListCount()));
- if (shardingRule.isShardingColumn(generatedKey.get().getColumnName(), tableName)) {
- appendGeneratedKeyCondition(generatedKey.get(), tableName, result);
- }
- }
return result;
}
@@ -117,8 +112,24 @@ public final class InsertClauseShardingConditionEngine implements ShardingCondit
return (Comparable) result;
}
- private Collection<Comparable<?>> getGeneratedKeys(final String tableName, final int valueListCount) {
- return IntStream.range(0, valueListCount).mapToObj(i -> shardingRule.generateKey(tableName)).collect(Collectors.toCollection(LinkedList::new));
+ private List<ShardingCondition> createShardingConditionsWithInsertSelect(final InsertStatementContext sqlStatementContext, final List<Object> parameters) {
+ SelectStatementContext selectStatementContext = sqlStatementContext.getInsertSelectContext().getSelectStatementContext();
+ return new LinkedList<>(new WhereClauseShardingConditionEngine(shardingRule, schemaMetaData).createShardingConditions(selectStatementContext, parameters));
+ }
+
+ private void appendGeneratedKeyConditions(final InsertStatementContext sqlStatementContext, final List<ShardingCondition> shardingConditions) {
+ Optional<GeneratedKeyContext> generatedKey = sqlStatementContext.getGeneratedKeyContext();
+ String tableName = sqlStatementContext.getSqlStatement().getTable().getTableName().getIdentifier().getValue();
+ if (generatedKey.isPresent() && generatedKey.get().isGenerated()) {
+ generatedKey.get().getGeneratedValues().addAll(generateKeys(tableName, sqlStatementContext.getValueListCount()));
+ if (shardingRule.isShardingColumn(generatedKey.get().getColumnName(), tableName)) {
+ appendGeneratedKeyCondition(generatedKey.get(), tableName, shardingConditions);
+ }
+ }
+ }
+
+ private Collection<Comparable<?>> generateKeys(final String tableName, final int valueListCount) {
+ return IntStream.range(0, valueListCount).mapToObj(i -> shardingRule.generateKey(tableName)).collect(Collectors.toList());
}
private void appendGeneratedKeyCondition(final GeneratedKeyContext generatedKey, final String tableName, final List<ShardingCondition> shardingConditions) {